src/Pure/System/session.scala
author wenzelm
Sat Mar 03 17:30:14 2012 +0100 (2012-03-03 ago)
changeset 46772 be21f050eda4
parent 46771 06a9b24c4a36
child 46774 38f113b052b1
permissions -rw-r--r--
tuned signature -- emphasize Isabelle_Process Input vs. Output;
wenzelm@36676
     1
/*  Title:      Pure/System/session.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@38221
     3
    Options:    :folding=explicit:collapseFolds=1:
wenzelm@36676
     4
wenzelm@43651
     5
Main Isabelle/Scala session, potentially with running prover process.
wenzelm@36676
     6
*/
wenzelm@34777
     7
wenzelm@34871
     8
package isabelle
wenzelm@34777
     9
wenzelm@44733
    10
wenzelm@43520
    11
import java.lang.System
wenzelm@44733
    12
import java.util.{Timer, TimerTask}
wenzelm@34777
    13
wenzelm@44733
    14
import scala.collection.mutable
wenzelm@46771
    15
import scala.collection.immutable.Queue
wenzelm@34820
    16
import scala.actors.TIMEOUT
wenzelm@34777
    17
import scala.actors.Actor._
wenzelm@34777
    18
wenzelm@34815
    19
wenzelm@34791
    20
object Session
wenzelm@34791
    21
{
wenzelm@34813
    22
  /* events */
wenzelm@34813
    23
wenzelm@43716
    24
  //{{{
wenzelm@34791
    25
  case object Global_Settings
wenzelm@44805
    26
  case object Caret_Focus
wenzelm@44615
    27
  case class Commands_Changed(nodes: Set[Document.Node.Name], commands: Set[Command])
wenzelm@39630
    28
wenzelm@39630
    29
  sealed abstract class Phase
wenzelm@39630
    30
  case object Inactive extends Phase
wenzelm@39701
    31
  case object Startup extends Phase  // transient
wenzelm@39701
    32
  case object Failed extends Phase
wenzelm@39630
    33
  case object Ready extends Phase
wenzelm@39701
    34
  case object Shutdown extends Phase  // transient
wenzelm@43716
    35
  //}}}
wenzelm@34791
    36
}
wenzelm@34791
    37
wenzelm@34777
    38
wenzelm@46739
    39
class Session(thy_load: Thy_Load = new Thy_Load)
wenzelm@34777
    40
{
wenzelm@46771
    41
  /* tuning parameters */  // FIXME properties or settings (!?)
wenzelm@37849
    42
wenzelm@44733
    43
  val message_delay = Time.seconds(0.01)  // prover messages
wenzelm@43716
    44
  val input_delay = Time.seconds(0.3)  // user input (e.g. text edits, cursor movement)
wenzelm@43716
    45
  val output_delay = Time.seconds(0.1)  // prover output (markup, common messages)
wenzelm@43716
    46
  val update_delay = Time.seconds(0.5)  // GUI layout updates
wenzelm@44573
    47
  val load_delay = Time.seconds(0.5)  // file load operations (new buffers etc.)
wenzelm@44676
    48
  val prune_delay = Time.seconds(60.0)  // prune history -- delete old versions
wenzelm@44676
    49
  val prune_size = 0  // size of retained history
wenzelm@46771
    50
  val syslog_limit = 100
wenzelm@37849
    51
wenzelm@37849
    52
wenzelm@34809
    53
  /* pervasive event buses */
wenzelm@34809
    54
wenzelm@34809
    55
  val global_settings = new Event_Bus[Session.Global_Settings.type]
wenzelm@44805
    56
  val caret_focus = new Event_Bus[Session.Caret_Focus.type]
wenzelm@43716
    57
  val commands_changed = new Event_Bus[Session.Commands_Changed]
wenzelm@43716
    58
  val phase_changed = new Event_Bus[Session.Phase]
wenzelm@46772
    59
  val syslog_messages = new Event_Bus[Isabelle_Process.Output]
wenzelm@46772
    60
  val raw_output_messages = new Event_Bus[Isabelle_Process.Output]
wenzelm@46120
    61
  val protocol_messages = new Event_Bus[Isabelle_Process.Message]  // potential bottle-neck
wenzelm@43716
    62
wenzelm@34809
    63
wenzelm@34809
    64
wenzelm@38841
    65
  /** buffered command changes (delay_first discipline) **/
wenzelm@38841
    66
wenzelm@43716
    67
  //{{{
wenzelm@38841
    68
  private case object Stop
wenzelm@38841
    69
wenzelm@44722
    70
  private val (_, commands_changed_buffer) =
wenzelm@44722
    71
    Simple_Thread.actor("commands_changed_buffer", daemon = true)
wenzelm@38841
    72
  {
wenzelm@38841
    73
    var finished = false
wenzelm@38841
    74
    while (!finished) {
wenzelm@44722
    75
      receive {
wenzelm@39630
    76
        case Stop => finished = true; reply(())
wenzelm@44722
    77
        case changed: Session.Commands_Changed => commands_changed.event(changed)
wenzelm@44722
    78
        case bad => System.err.println("commands_changed_buffer: ignoring bad message " + bad)
wenzelm@38841
    79
      }
wenzelm@38841
    80
    }
wenzelm@38841
    81
  }
wenzelm@38841
    82
  //}}}
wenzelm@38841
    83
wenzelm@38841
    84
wenzelm@45635
    85
  /** pipelined change parsing **/
wenzelm@45635
    86
wenzelm@45635
    87
  //{{{
wenzelm@45635
    88
  private case class Text_Edits(
wenzelm@45635
    89
    syntax: Outer_Syntax,
wenzelm@45635
    90
    name: Document.Node.Name,
wenzelm@45635
    91
    previous: Future[Document.Version],
wenzelm@45635
    92
    text_edits: List[Document.Edit_Text],
wenzelm@45635
    93
    version_result: Promise[Document.Version])
wenzelm@45635
    94
wenzelm@45635
    95
  private val (_, change_parser) = Simple_Thread.actor("change_parser", daemon = true)
wenzelm@45635
    96
  {
wenzelm@45635
    97
    var finished = false
wenzelm@45635
    98
    while (!finished) {
wenzelm@45635
    99
      receive {
wenzelm@45635
   100
        case Stop => finished = true; reply(())
wenzelm@45635
   101
wenzelm@45635
   102
        case Text_Edits(syntax, name, previous, text_edits, version_result) =>
wenzelm@45635
   103
          val prev = previous.get_finished
wenzelm@45635
   104
          val (doc_edits, version) = Thy_Syntax.text_edits(syntax, prev, text_edits)
wenzelm@45635
   105
          version_result.fulfill(version)
wenzelm@45635
   106
          sender ! Change_Node(name, doc_edits, prev, version)
wenzelm@45635
   107
wenzelm@45635
   108
        case bad => System.err.println("change_parser: ignoring bad message " + bad)
wenzelm@45635
   109
      }
wenzelm@45635
   110
    }
wenzelm@45635
   111
  }
wenzelm@45635
   112
  //}}}
wenzelm@45635
   113
wenzelm@38841
   114
wenzelm@38841
   115
  /** main protocol actor **/
wenzelm@34809
   116
wenzelm@43651
   117
  /* global state */
wenzelm@43644
   118
wenzelm@43716
   119
  @volatile var verbose: Boolean = false
wenzelm@43716
   120
wenzelm@46626
   121
  @volatile private var syntax = Outer_Syntax.init()
wenzelm@38569
   122
  def current_syntax(): Outer_Syntax = syntax
wenzelm@34809
   123
wenzelm@46771
   124
  private val syslog = Volatile(Queue.empty[XML.Elem])
wenzelm@46771
   125
  def current_syslog(): String = cat_lines(syslog().iterator.map(msg => XML.content(msg).mkString))
wenzelm@39626
   126
wenzelm@39630
   127
  @volatile private var _phase: Session.Phase = Session.Inactive
wenzelm@39633
   128
  private def phase_=(new_phase: Session.Phase)
wenzelm@39630
   129
  {
wenzelm@39630
   130
    _phase = new_phase
wenzelm@39701
   131
    phase_changed.event(new_phase)
wenzelm@39630
   132
  }
wenzelm@43716
   133
  def phase = _phase
wenzelm@43553
   134
  def is_ready: Boolean = phase == Session.Ready
wenzelm@39630
   135
wenzelm@45248
   136
  private val global_state = Volatile(Document.State.init)
wenzelm@43719
   137
  def current_state(): Document.State = global_state()
wenzelm@34816
   138
wenzelm@44960
   139
  def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
wenzelm@44960
   140
      pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
wenzelm@43719
   141
    global_state().snapshot(name, pending_edits)
wenzelm@43716
   142
wenzelm@43651
   143
wenzelm@43651
   144
  /* theory files */
wenzelm@43651
   145
wenzelm@44615
   146
  def header_edit(name: Document.Node.Name, header: Document.Node_Header): Document.Edit_Text =
wenzelm@44442
   147
  {
wenzelm@44442
   148
    val header1: Document.Node_Header =
wenzelm@44442
   149
      header match {
wenzelm@46737
   150
        case Exn.Res(_) if (thy_load.is_loaded(name.theory)) =>
wenzelm@46737
   151
          Exn.Exn(ERROR("Attempt to update loaded theory " + quote(name.theory)))
wenzelm@46737
   152
        case _ => header
wenzelm@44442
   153
      }
wenzelm@44442
   154
    (name, Document.Node.Header(header1))
wenzelm@44442
   155
  }
wenzelm@44442
   156
wenzelm@43651
   157
wenzelm@43651
   158
  /* actor messages */
wenzelm@43651
   159
wenzelm@45158
   160
  private case class Start(timeout: Time, args: List[String])
wenzelm@44775
   161
  private case object Cancel_Execution
wenzelm@44615
   162
  private case class Init_Node(name: Document.Node.Name,
wenzelm@44385
   163
    header: Document.Node_Header, perspective: Text.Perspective, text: String)
wenzelm@44615
   164
  private case class Edit_Node(name: Document.Node.Name,
wenzelm@44385
   165
    header: Document.Node_Header, perspective: Text.Perspective, edits: List[Text.Edit])
wenzelm@43722
   166
  private case class Change_Node(
wenzelm@44615
   167
    name: Document.Node.Name,
wenzelm@43722
   168
    doc_edits: List[Document.Edit_Command],
wenzelm@43722
   169
    previous: Document.Version,
wenzelm@43722
   170
    version: Document.Version)
wenzelm@44733
   171
  private case class Messages(msgs: List[Isabelle_Process.Message])
wenzelm@41534
   172
wenzelm@39572
   173
  private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
wenzelm@38639
   174
  {
wenzelm@43648
   175
    val this_actor = self
wenzelm@34809
   176
wenzelm@44676
   177
    var prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   178
wenzelm@34809
   179
wenzelm@44733
   180
    /* buffered prover messages */
wenzelm@44733
   181
wenzelm@44733
   182
    object receiver
wenzelm@44733
   183
    {
wenzelm@44733
   184
      private var buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   185
wenzelm@44733
   186
      def flush(): Unit = synchronized {
wenzelm@44733
   187
        if (!buffer.isEmpty) {
wenzelm@44733
   188
          val msgs = buffer.toList
wenzelm@44733
   189
          this_actor ! Messages(msgs)
wenzelm@44733
   190
          buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   191
        }
wenzelm@44733
   192
      }
wenzelm@44733
   193
      def invoke(msg: Isabelle_Process.Message): Unit = synchronized {
wenzelm@44733
   194
        buffer += msg
wenzelm@44733
   195
        msg match {
wenzelm@46772
   196
          case output: Isabelle_Process.Output =>
wenzelm@46772
   197
            if (output.is_syslog)
wenzelm@46771
   198
              syslog >> (queue =>
wenzelm@46771
   199
                {
wenzelm@46772
   200
                  val queue1 = queue.enqueue(output.message)
wenzelm@46771
   201
                  if (queue1.length > syslog_limit) queue1.dequeue._2 else queue1
wenzelm@46771
   202
                })
wenzelm@46772
   203
            if (output.is_raw) flush()
wenzelm@44733
   204
          case _ =>
wenzelm@44733
   205
        }
wenzelm@44733
   206
      }
wenzelm@44733
   207
wenzelm@44733
   208
      private val timer = new Timer("session_actor.receiver", true)
wenzelm@44733
   209
      timer.schedule(new TimerTask { def run = flush }, message_delay.ms, message_delay.ms)
wenzelm@44733
   210
wenzelm@44733
   211
      def cancel() { timer.cancel() }
wenzelm@44733
   212
    }
wenzelm@44733
   213
wenzelm@45709
   214
    var prover: Option[Isabelle_Process with Protocol] = None
wenzelm@44733
   215
wenzelm@44733
   216
wenzelm@44722
   217
    /* delayed command changes */
wenzelm@44722
   218
wenzelm@46570
   219
    object delay_commands_changed
wenzelm@44722
   220
    {
wenzelm@44722
   221
      private var changed_nodes: Set[Document.Node.Name] = Set.empty
wenzelm@44722
   222
      private var changed_commands: Set[Command] = Set.empty
wenzelm@44722
   223
      private def changed: Boolean = !changed_nodes.isEmpty || !changed_commands.isEmpty
wenzelm@44722
   224
wenzelm@44722
   225
      private var flush_time: Option[Long] = None
wenzelm@44722
   226
wenzelm@44722
   227
      def flush_timeout: Long =
wenzelm@44722
   228
        flush_time match {
wenzelm@44722
   229
          case None => 5000L
wenzelm@44722
   230
          case Some(time) => (time - System.currentTimeMillis()) max 0
wenzelm@44722
   231
        }
wenzelm@44722
   232
wenzelm@44722
   233
      def flush()
wenzelm@44722
   234
      {
wenzelm@44722
   235
        if (changed)
wenzelm@44722
   236
          commands_changed_buffer ! Session.Commands_Changed(changed_nodes, changed_commands)
wenzelm@44722
   237
        changed_nodes = Set.empty
wenzelm@44722
   238
        changed_commands = Set.empty
wenzelm@44722
   239
        flush_time = None
wenzelm@44722
   240
      }
wenzelm@44722
   241
wenzelm@44722
   242
      def invoke(command: Command)
wenzelm@44722
   243
      {
wenzelm@44722
   244
        changed_nodes += command.node_name
wenzelm@44722
   245
        changed_commands += command
wenzelm@44722
   246
        val now = System.currentTimeMillis()
wenzelm@44722
   247
        flush_time match {
wenzelm@44722
   248
          case None => flush_time = Some(now + output_delay.ms)
wenzelm@44722
   249
          case Some(time) => if (now >= time) flush()
wenzelm@44722
   250
        }
wenzelm@44722
   251
      }
wenzelm@44722
   252
    }
wenzelm@44722
   253
wenzelm@44722
   254
wenzelm@44436
   255
    /* perspective */
wenzelm@44436
   256
wenzelm@44615
   257
    def update_perspective(name: Document.Node.Name, text_perspective: Text.Perspective)
wenzelm@44436
   258
    {
wenzelm@44475
   259
      val previous = global_state().tip_version
wenzelm@44436
   260
      val (perspective, version) = Thy_Syntax.edit_perspective(previous, name, text_perspective)
wenzelm@44436
   261
wenzelm@44436
   262
      val text_edits: List[Document.Edit_Text] =
wenzelm@44436
   263
        List((name, Document.Node.Perspective(text_perspective)))
wenzelm@44436
   264
      val change =
wenzelm@46687
   265
        global_state >>>
wenzelm@46687
   266
          (_.continue_history(Future.value(previous), text_edits, Future.value(version)))
wenzelm@44436
   267
wenzelm@44477
   268
      val assignment = global_state().the_assignment(previous).check_finished
wenzelm@46687
   269
      global_state >> (_.define_version(version, assignment))
wenzelm@46687
   270
      global_state >>> (_.assign(version.id))
wenzelm@44436
   271
wenzelm@44436
   272
      prover.get.update_perspective(previous.id, version.id, name, perspective)
wenzelm@44436
   273
    }
wenzelm@44436
   274
wenzelm@44436
   275
wenzelm@43716
   276
    /* incoming edits */
wenzelm@43716
   277
wenzelm@44615
   278
    def handle_edits(name: Document.Node.Name,
wenzelm@44384
   279
        header: Document.Node_Header, edits: List[Document.Node.Edit[Text.Edit, Text.Perspective]])
wenzelm@43716
   280
    //{{{
wenzelm@43716
   281
    {
wenzelm@43716
   282
      val syntax = current_syntax()
wenzelm@43719
   283
      val previous = global_state().history.tip.version
wenzelm@44185
   284
wenzelm@44612
   285
      prover.get.cancel_execution()
wenzelm@44612
   286
wenzelm@44615
   287
      val text_edits = header_edit(name, header) :: edits.map(edit => (name, edit))
wenzelm@45635
   288
      val version = Future.promise[Document.Version]
wenzelm@46687
   289
      val change = global_state >>> (_.continue_history(previous, text_edits, version))
wenzelm@43716
   290
wenzelm@45635
   291
      change_parser ! Text_Edits(syntax, name, previous, text_edits, version)
wenzelm@43716
   292
    }
wenzelm@43716
   293
    //}}}
wenzelm@43716
   294
wenzelm@43716
   295
wenzelm@44436
   296
    /* exec state assignment */
wenzelm@44436
   297
wenzelm@44476
   298
    def handle_assign(id: Document.Version_ID, assign: Document.Assign)
wenzelm@44436
   299
    //{{{
wenzelm@44436
   300
    {
wenzelm@46687
   301
      val cmds = global_state >>> (_.assign(id, assign))
wenzelm@46570
   302
      for (cmd <- cmds) delay_commands_changed.invoke(cmd)
wenzelm@44436
   303
    }
wenzelm@44436
   304
    //}}}
wenzelm@44436
   305
wenzelm@44436
   306
wenzelm@44676
   307
    /* removed versions */
wenzelm@44676
   308
wenzelm@44676
   309
    def handle_removed(removed: List[Document.Version_ID])
wenzelm@44676
   310
    //{{{
wenzelm@44676
   311
    {
wenzelm@46687
   312
      global_state >> (_.removed_versions(removed))
wenzelm@44676
   313
    }
wenzelm@44676
   314
    //}}}
wenzelm@44676
   315
wenzelm@44676
   316
wenzelm@43716
   317
    /* resulting changes */
wenzelm@34809
   318
wenzelm@43722
   319
    def handle_change(change: Change_Node)
wenzelm@38221
   320
    //{{{
wenzelm@34809
   321
    {
wenzelm@43722
   322
      val previous = change.previous
wenzelm@43722
   323
      val version = change.version
wenzelm@43722
   324
      val name = change.name
wenzelm@43722
   325
      val doc_edits = change.doc_edits
wenzelm@38366
   326
wenzelm@44383
   327
      def id_command(command: Command)
wenzelm@43720
   328
      {
wenzelm@44582
   329
        if (!global_state().defined_command(command.id)) {
wenzelm@46687
   330
          global_state >> (_.define_command(command))
wenzelm@44644
   331
          prover.get.define_command(command)
wenzelm@43720
   332
        }
wenzelm@43720
   333
      }
wenzelm@44383
   334
      doc_edits foreach {
wenzelm@44383
   335
        case (_, edit) =>
wenzelm@44383
   336
          edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
wenzelm@44383
   337
      }
wenzelm@43722
   338
wenzelm@44479
   339
      val assignment = global_state().the_assignment(previous).check_finished
wenzelm@46687
   340
      global_state >> (_.define_version(version, assignment))
wenzelm@44481
   341
      prover.get.update(previous.id, version.id, doc_edits)
wenzelm@34809
   342
    }
wenzelm@38221
   343
    //}}}
wenzelm@34809
   344
wenzelm@34809
   345
wenzelm@46772
   346
    /* prover output */
wenzelm@34809
   347
wenzelm@46772
   348
    def handle_output(output: Isabelle_Process.Output)
wenzelm@38221
   349
    //{{{
wenzelm@34809
   350
    {
wenzelm@46772
   351
      def bad_output(output: Isabelle_Process.Output)
wenzelm@43716
   352
      {
wenzelm@43716
   353
        if (verbose)
wenzelm@46772
   354
          System.err.println("Ignoring prover output: " + output.message.toString)
wenzelm@43716
   355
      }
wenzelm@43716
   356
wenzelm@46772
   357
      output.properties match {
wenzelm@46122
   358
wenzelm@46772
   359
        case Position.Id(state_id) if !output.is_raw =>
wenzelm@38370
   360
          try {
wenzelm@46772
   361
            val st = global_state >>> (_.accumulate(state_id, output.message))
wenzelm@46570
   362
            delay_commands_changed.invoke(st.command)
wenzelm@38370
   363
          }
wenzelm@43748
   364
          catch {
wenzelm@46772
   365
            case _: Document.State.Fail => bad_output(output)
wenzelm@43748
   366
          }
wenzelm@46122
   367
wenzelm@46772
   368
        case Isabelle_Markup.Assign_Execs if output.is_raw =>
wenzelm@46772
   369
          XML.content(output.body).mkString match {
wenzelm@45709
   370
            case Protocol.Assign(id, assign) =>
wenzelm@44661
   371
              try { handle_assign(id, assign) }
wenzelm@46772
   372
              catch { case _: Document.State.Fail => bad_output(output) }
wenzelm@46772
   373
            case _ => bad_output(output)
wenzelm@44661
   374
          }
wenzelm@44676
   375
          // FIXME separate timeout event/message!?
wenzelm@44676
   376
          if (prover.isDefined && System.currentTimeMillis() > prune_next) {
wenzelm@46687
   377
            val old_versions = global_state >>> (_.prune_history(prune_size))
wenzelm@44676
   378
            if (!old_versions.isEmpty) prover.get.remove_versions(old_versions)
wenzelm@44676
   379
            prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   380
          }
wenzelm@46122
   381
wenzelm@46772
   382
        case Isabelle_Markup.Removed_Versions if output.is_raw =>
wenzelm@46772
   383
          XML.content(output.body).mkString match {
wenzelm@45709
   384
            case Protocol.Removed(removed) =>
wenzelm@44676
   385
              try { handle_removed(removed) }
wenzelm@46772
   386
              catch { case _: Document.State.Fail => bad_output(output) }
wenzelm@46772
   387
            case _ => bad_output(output)
wenzelm@44676
   388
          }
wenzelm@46122
   389
wenzelm@46772
   390
        case Isabelle_Markup.Invoke_Scala(name, id) if output.is_raw =>
wenzelm@43748
   391
          Future.fork {
wenzelm@46772
   392
            val arg = XML.content(output.body).mkString
wenzelm@43748
   393
            val (tag, res) = Invoke_Scala.method(name, arg)
wenzelm@43748
   394
            prover.get.invoke_scala(id, tag, res)
wenzelm@43748
   395
          }
wenzelm@46122
   396
wenzelm@46772
   397
        case Isabelle_Markup.Cancel_Scala(id) if output.is_raw =>
wenzelm@46121
   398
          System.err.println("cancel_scala " + id)  // FIXME actually cancel JVM task
wenzelm@46122
   399
wenzelm@46772
   400
        case Isabelle_Markup.Ready if output.is_raw =>
wenzelm@46121
   401
            // FIXME move to ML side (!?)
wenzelm@46121
   402
            syntax += ("hence", Keyword.PRF_ASM_GOAL, "then have")
wenzelm@46121
   403
            syntax += ("thus", Keyword.PRF_ASM_GOAL, "then show")
wenzelm@46121
   404
            phase = Session.Ready
wenzelm@46122
   405
wenzelm@46772
   406
        case Isabelle_Markup.Loaded_Theory(name) if output.is_raw =>
wenzelm@46122
   407
          thy_load.register_thy(name)
wenzelm@46122
   408
wenzelm@46772
   409
        case Isabelle_Markup.Command_Decl(name, kind) if output.is_raw =>
wenzelm@46123
   410
          syntax += (name, kind)
wenzelm@46123
   411
wenzelm@46772
   412
        case Isabelle_Markup.Keyword_Decl(name) if output.is_raw =>
wenzelm@46123
   413
          syntax += name
wenzelm@46123
   414
wenzelm@38722
   415
        case _ =>
wenzelm@46772
   416
          if (output.is_exit && phase == Session.Startup) phase = Session.Failed
wenzelm@46772
   417
          else if (output.is_exit) phase = Session.Inactive
wenzelm@46772
   418
          else if (output.is_stdout) { }
wenzelm@46772
   419
          else bad_output(output)
wenzelm@43748
   420
      }
wenzelm@34809
   421
    }
wenzelm@38221
   422
    //}}}
wenzelm@34809
   423
wenzelm@34820
   424
wenzelm@39630
   425
    /* main loop */
wenzelm@34809
   426
wenzelm@43716
   427
    //{{{
wenzelm@38639
   428
    var finished = false
wenzelm@38639
   429
    while (!finished) {
wenzelm@46570
   430
      receiveWithin(delay_commands_changed.flush_timeout) {
wenzelm@46570
   431
        case TIMEOUT => delay_commands_changed.flush()
wenzelm@44722
   432
wenzelm@45158
   433
        case Start(timeout, args) if prover.isEmpty =>
wenzelm@39701
   434
          if (phase == Session.Inactive || phase == Session.Failed) {
wenzelm@39701
   435
            phase = Session.Startup
wenzelm@45709
   436
            prover = Some(new Isabelle_Process(timeout, receiver.invoke _, args) with Protocol)
wenzelm@39701
   437
          }
wenzelm@38841
   438
wenzelm@39701
   439
        case Stop =>
wenzelm@39701
   440
          if (phase == Session.Ready) {
wenzelm@46687
   441
            global_state >> (_ => Document.State.init)  // FIXME event bus!?
wenzelm@39701
   442
            phase = Session.Shutdown
wenzelm@43649
   443
            prover.get.terminate
wenzelm@43717
   444
            prover = None
wenzelm@39701
   445
            phase = Session.Inactive
wenzelm@39701
   446
          }
wenzelm@39630
   447
          finished = true
wenzelm@44733
   448
          receiver.cancel()
wenzelm@39630
   449
          reply(())
wenzelm@38850
   450
wenzelm@44775
   451
        case Cancel_Execution if prover.isDefined =>
wenzelm@44775
   452
          prover.get.cancel_execution()
wenzelm@43716
   453
wenzelm@44615
   454
        case Init_Node(name, header, perspective, text) if prover.isDefined =>
wenzelm@43716
   455
          // FIXME compare with existing node
wenzelm@44615
   456
          handle_edits(name, header,
wenzelm@44385
   457
            List(Document.Node.Clear(),
wenzelm@44385
   458
              Document.Node.Edits(List(Text.Edit.insert(0, text))),
wenzelm@44385
   459
              Document.Node.Perspective(perspective)))
wenzelm@43716
   460
          reply(())
wenzelm@43716
   461
wenzelm@44615
   462
        case Edit_Node(name, header, perspective, text_edits) if prover.isDefined =>
wenzelm@44484
   463
          if (text_edits.isEmpty && global_state().tip_stable &&
wenzelm@44484
   464
              perspective.range.stop <= global_state().last_exec_offset(name))
wenzelm@44484
   465
            update_perspective(name, perspective)
wenzelm@44484
   466
          else
wenzelm@44615
   467
            handle_edits(name, header,
wenzelm@44436
   468
              List(Document.Node.Edits(text_edits), Document.Node.Perspective(perspective)))
wenzelm@43718
   469
          reply(())
wenzelm@43718
   470
wenzelm@44733
   471
        case Messages(msgs) =>
wenzelm@44733
   472
          msgs foreach {
wenzelm@44733
   473
            case input: Isabelle_Process.Input =>
wenzelm@46120
   474
              protocol_messages.event(input)
wenzelm@43721
   475
wenzelm@46772
   476
            case output: Isabelle_Process.Output =>
wenzelm@46772
   477
              handle_output(output)
wenzelm@46772
   478
              if (output.is_syslog) syslog_messages.event(output)
wenzelm@46772
   479
              if (output.is_stdout || output.is_stderr) raw_output_messages.event(output)
wenzelm@46772
   480
              protocol_messages.event(output)
wenzelm@44733
   481
          }
wenzelm@43716
   482
wenzelm@45635
   483
        case change: Change_Node
wenzelm@45635
   484
        if prover.isDefined && global_state().is_assigned(change.previous) =>
wenzelm@45635
   485
          handle_change(change)
wenzelm@45635
   486
wenzelm@45635
   487
        case bad if !bad.isInstanceOf[Change_Node] =>
wenzelm@45635
   488
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   489
      }
wenzelm@34777
   490
    }
wenzelm@43716
   491
    //}}}
wenzelm@34777
   492
  }
wenzelm@34777
   493
wenzelm@34809
   494
wenzelm@43716
   495
  /* actions */
wenzelm@34809
   496
wenzelm@45158
   497
  def start(timeout: Time, args: List[String])
wenzelm@45158
   498
  { session_actor ! Start(timeout, args) }
wenzelm@34820
   499
wenzelm@45158
   500
  def start(args: List[String]) { start (Time.seconds(25), args) }
wenzelm@45076
   501
wenzelm@45635
   502
  def stop() { commands_changed_buffer !? Stop; change_parser !? Stop; session_actor !? Stop }
wenzelm@38841
   503
wenzelm@44775
   504
  def cancel_execution() { session_actor ! Cancel_Execution }
wenzelm@43648
   505
wenzelm@44615
   506
  def init_node(name: Document.Node.Name,
wenzelm@44385
   507
    header: Document.Node_Header, perspective: Text.Perspective, text: String)
wenzelm@44615
   508
  { session_actor !? Init_Node(name, header, perspective, text) }
wenzelm@38221
   509
wenzelm@44615
   510
  def edit_node(name: Document.Node.Name,
wenzelm@44615
   511
    header: Document.Node_Header, perspective: Text.Perspective, edits: List[Text.Edit])
wenzelm@44615
   512
  { session_actor !? Edit_Node(name, header, perspective, edits) }
wenzelm@34777
   513
}