src/Pure/System/session.scala
author wenzelm
Mon May 20 16:17:56 2013 +0200 (2013-05-20 ago)
changeset 52084 573e80625c78
parent 51818 517f232e867d
child 52111 1fd184eaa310
permissions -rw-r--r--
more explicit Session.update_options as source of Global_Options event;
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@50697
    25
  case class Statistics(props: Properties.T)
wenzelm@50117
    26
  case class Global_Options(options: Options)
wenzelm@44805
    27
  case object Caret_Focus
wenzelm@49196
    28
  case class Raw_Edits(edits: List[Document.Edit_Text])
wenzelm@50500
    29
  case class Dialog_Result(id: Document.ID, serial: Long, result: String)
wenzelm@47027
    30
  case class Commands_Changed(
wenzelm@47027
    31
    assignment: Boolean, nodes: Set[Document.Node.Name], commands: Set[Command])
wenzelm@39630
    32
wenzelm@39630
    33
  sealed abstract class Phase
wenzelm@39630
    34
  case object Inactive extends Phase
wenzelm@39701
    35
  case object Startup extends Phase  // transient
wenzelm@39701
    36
  case object Failed extends Phase
wenzelm@39630
    37
  case object Ready extends Phase
wenzelm@39701
    38
  case object Shutdown extends Phase  // transient
wenzelm@43716
    39
  //}}}
wenzelm@34791
    40
}
wenzelm@34791
    41
wenzelm@34777
    42
wenzelm@48870
    43
class Session(val thy_load: Thy_Load)
wenzelm@34777
    44
{
wenzelm@47653
    45
  /* global flags */
wenzelm@47653
    46
wenzelm@47653
    47
  @volatile var timing: Boolean = false
wenzelm@47653
    48
  @volatile var verbose: Boolean = false
wenzelm@47653
    49
wenzelm@47653
    50
wenzelm@49288
    51
  /* tuning parameters */
wenzelm@49288
    52
wenzelm@49288
    53
  def output_delay: Time = Time.seconds(0.1)  // prover output (markup, common messages)
wenzelm@49293
    54
  def message_delay: Time = Time.seconds(0.01)  // incoming prover messages
wenzelm@49293
    55
  def prune_delay: Time = Time.seconds(60.0)  // prune history -- delete old versions
wenzelm@49293
    56
  def prune_size: Int = 0  // size of retained history
wenzelm@49293
    57
  def syslog_limit: Int = 100
wenzelm@49524
    58
  def reparse_limit: Int = 0
wenzelm@37849
    59
wenzelm@37849
    60
wenzelm@34809
    61
  /* pervasive event buses */
wenzelm@34809
    62
wenzelm@50433
    63
  val statistics = new Event_Bus[Session.Statistics]
wenzelm@50117
    64
  val global_options = new Event_Bus[Session.Global_Options]
wenzelm@44805
    65
  val caret_focus = new Event_Bus[Session.Caret_Focus.type]
wenzelm@49196
    66
  val raw_edits = new Event_Bus[Session.Raw_Edits]
wenzelm@43716
    67
  val commands_changed = new Event_Bus[Session.Commands_Changed]
wenzelm@43716
    68
  val phase_changed = new Event_Bus[Session.Phase]
wenzelm@46772
    69
  val syslog_messages = new Event_Bus[Isabelle_Process.Output]
wenzelm@46772
    70
  val raw_output_messages = new Event_Bus[Isabelle_Process.Output]
wenzelm@46774
    71
  val all_messages = new Event_Bus[Isabelle_Process.Message]  // potential bottle-neck
wenzelm@43716
    72
wenzelm@34809
    73
wenzelm@34809
    74
wenzelm@38841
    75
  /** buffered command changes (delay_first discipline) **/
wenzelm@38841
    76
wenzelm@43716
    77
  //{{{
wenzelm@38841
    78
  private case object Stop
wenzelm@38841
    79
wenzelm@44722
    80
  private val (_, commands_changed_buffer) =
wenzelm@44722
    81
    Simple_Thread.actor("commands_changed_buffer", daemon = true)
wenzelm@38841
    82
  {
wenzelm@38841
    83
    var finished = false
wenzelm@38841
    84
    while (!finished) {
wenzelm@44722
    85
      receive {
wenzelm@39630
    86
        case Stop => finished = true; reply(())
wenzelm@44722
    87
        case changed: Session.Commands_Changed => commands_changed.event(changed)
wenzelm@44722
    88
        case bad => System.err.println("commands_changed_buffer: ignoring bad message " + bad)
wenzelm@38841
    89
      }
wenzelm@38841
    90
    }
wenzelm@38841
    91
  }
wenzelm@38841
    92
  //}}}
wenzelm@38841
    93
wenzelm@38841
    94
wenzelm@45635
    95
  /** pipelined change parsing **/
wenzelm@45635
    96
wenzelm@45635
    97
  //{{{
wenzelm@45635
    98
  private case class Text_Edits(
wenzelm@45635
    99
    previous: Future[Document.Version],
wenzelm@45635
   100
    text_edits: List[Document.Edit_Text],
wenzelm@45635
   101
    version_result: Promise[Document.Version])
wenzelm@45635
   102
wenzelm@45635
   103
  private val (_, change_parser) = Simple_Thread.actor("change_parser", daemon = true)
wenzelm@45635
   104
  {
wenzelm@45635
   105
    var finished = false
wenzelm@45635
   106
    while (!finished) {
wenzelm@45635
   107
      receive {
wenzelm@45635
   108
        case Stop => finished = true; reply(())
wenzelm@45635
   109
wenzelm@47629
   110
        case Text_Edits(previous, text_edits, version_result) =>
wenzelm@45635
   111
          val prev = previous.get_finished
wenzelm@47653
   112
          val (doc_edits, version) =
wenzelm@50566
   113
            Timing.timeit("Thy_Load.text_edits", timing) {
wenzelm@50566
   114
              thy_load.text_edits(reparse_limit, prev, text_edits)
wenzelm@47653
   115
            }
wenzelm@45635
   116
          version_result.fulfill(version)
wenzelm@47629
   117
          sender ! Change(doc_edits, prev, version)
wenzelm@45635
   118
wenzelm@45635
   119
        case bad => System.err.println("change_parser: ignoring bad message " + bad)
wenzelm@45635
   120
      }
wenzelm@45635
   121
    }
wenzelm@45635
   122
  }
wenzelm@45635
   123
  //}}}
wenzelm@45635
   124
wenzelm@38841
   125
wenzelm@47653
   126
wenzelm@38841
   127
  /** main protocol actor **/
wenzelm@34809
   128
wenzelm@43651
   129
  /* global state */
wenzelm@43644
   130
wenzelm@46771
   131
  private val syslog = Volatile(Queue.empty[XML.Elem])
wenzelm@49416
   132
  def current_syslog(): String = cat_lines(syslog().iterator.map(XML.content))
wenzelm@39626
   133
wenzelm@39630
   134
  @volatile private var _phase: Session.Phase = Session.Inactive
wenzelm@39633
   135
  private def phase_=(new_phase: Session.Phase)
wenzelm@39630
   136
  {
wenzelm@39630
   137
    _phase = new_phase
wenzelm@39701
   138
    phase_changed.event(new_phase)
wenzelm@39630
   139
  }
wenzelm@43716
   140
  def phase = _phase
wenzelm@43553
   141
  def is_ready: Boolean = phase == Session.Ready
wenzelm@39630
   142
wenzelm@45248
   143
  private val global_state = Volatile(Document.State.init)
wenzelm@43719
   144
  def current_state(): Document.State = global_state()
wenzelm@46944
   145
wenzelm@46944
   146
  def recent_syntax(): Outer_Syntax =
wenzelm@46944
   147
  {
wenzelm@46944
   148
    val version = current_state().recent_finished.version.get_finished
wenzelm@48870
   149
    if (version.is_init) thy_load.base_syntax
wenzelm@46944
   150
    else version.syntax
wenzelm@46944
   151
  }
wenzelm@34816
   152
wenzelm@44960
   153
  def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
wenzelm@44960
   154
      pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
wenzelm@43719
   155
    global_state().snapshot(name, pending_edits)
wenzelm@43716
   156
wenzelm@43651
   157
wenzelm@43651
   158
  /* theory files */
wenzelm@43651
   159
wenzelm@48707
   160
  def header_edit(name: Document.Node.Name, header: Document.Node.Header): Document.Edit_Text =
wenzelm@44442
   161
  {
wenzelm@48707
   162
    val header1 =
wenzelm@48870
   163
      if (thy_load.loaded_theories(name.theory))
wenzelm@48707
   164
        header.error("Attempt to update loaded theory " + quote(name.theory))
wenzelm@48707
   165
      else header
wenzelm@48707
   166
    (name, Document.Node.Deps(header1))
wenzelm@44442
   167
  }
wenzelm@44442
   168
wenzelm@43651
   169
wenzelm@43651
   170
  /* actor messages */
wenzelm@43651
   171
wenzelm@48870
   172
  private case class Start(args: List[String])
wenzelm@44775
   173
  private case object Cancel_Execution
wenzelm@47629
   174
  private case class Change(
wenzelm@43722
   175
    doc_edits: List[Document.Edit_Command],
wenzelm@43722
   176
    previous: Document.Version,
wenzelm@43722
   177
    version: Document.Version)
wenzelm@44733
   178
  private case class Messages(msgs: List[Isabelle_Process.Message])
wenzelm@49470
   179
  private case class Finished_Scala(id: String, tag: Invoke_Scala.Tag.Value, result: String)
wenzelm@52084
   180
  private case class Update_Options(options: Options)
wenzelm@41534
   181
wenzelm@39572
   182
  private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
wenzelm@38639
   183
  {
wenzelm@43648
   184
    val this_actor = self
wenzelm@34809
   185
wenzelm@44676
   186
    var prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   187
wenzelm@49470
   188
    var futures = Map.empty[String, java.util.concurrent.Future[Unit]]
wenzelm@49470
   189
wenzelm@34809
   190
wenzelm@44733
   191
    /* buffered prover messages */
wenzelm@44733
   192
wenzelm@44733
   193
    object receiver
wenzelm@44733
   194
    {
wenzelm@44733
   195
      private var buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   196
wenzelm@44733
   197
      def flush(): Unit = synchronized {
wenzelm@44733
   198
        if (!buffer.isEmpty) {
wenzelm@44733
   199
          val msgs = buffer.toList
wenzelm@44733
   200
          this_actor ! Messages(msgs)
wenzelm@44733
   201
          buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   202
        }
wenzelm@44733
   203
      }
wenzelm@44733
   204
      def invoke(msg: Isabelle_Process.Message): Unit = synchronized {
wenzelm@44733
   205
        buffer += msg
wenzelm@44733
   206
        msg match {
wenzelm@46772
   207
          case output: Isabelle_Process.Output =>
wenzelm@46772
   208
            if (output.is_syslog)
wenzelm@46771
   209
              syslog >> (queue =>
wenzelm@46771
   210
                {
wenzelm@46772
   211
                  val queue1 = queue.enqueue(output.message)
wenzelm@46771
   212
                  if (queue1.length > syslog_limit) queue1.dequeue._2 else queue1
wenzelm@46771
   213
                })
wenzelm@46774
   214
            if (output.is_protocol) flush()
wenzelm@44733
   215
          case _ =>
wenzelm@44733
   216
        }
wenzelm@44733
   217
      }
wenzelm@44733
   218
wenzelm@44733
   219
      private val timer = new Timer("session_actor.receiver", true)
wenzelm@44733
   220
      timer.schedule(new TimerTask { def run = flush }, message_delay.ms, message_delay.ms)
wenzelm@44733
   221
wenzelm@44733
   222
      def cancel() { timer.cancel() }
wenzelm@44733
   223
    }
wenzelm@44733
   224
wenzelm@45709
   225
    var prover: Option[Isabelle_Process with Protocol] = None
wenzelm@44733
   226
wenzelm@44733
   227
wenzelm@44722
   228
    /* delayed command changes */
wenzelm@44722
   229
wenzelm@46570
   230
    object delay_commands_changed
wenzelm@44722
   231
    {
wenzelm@47027
   232
      private var changed_assignment: Boolean = false
wenzelm@44722
   233
      private var changed_nodes: Set[Document.Node.Name] = Set.empty
wenzelm@44722
   234
      private var changed_commands: Set[Command] = Set.empty
wenzelm@44722
   235
wenzelm@44722
   236
      private var flush_time: Option[Long] = None
wenzelm@44722
   237
wenzelm@44722
   238
      def flush_timeout: Long =
wenzelm@44722
   239
        flush_time match {
wenzelm@44722
   240
          case None => 5000L
wenzelm@44722
   241
          case Some(time) => (time - System.currentTimeMillis()) max 0
wenzelm@44722
   242
        }
wenzelm@44722
   243
wenzelm@44722
   244
      def flush()
wenzelm@44722
   245
      {
wenzelm@47027
   246
        if (changed_assignment || !changed_nodes.isEmpty || !changed_commands.isEmpty)
wenzelm@47027
   247
          commands_changed_buffer !
wenzelm@47027
   248
            Session.Commands_Changed(changed_assignment, changed_nodes, changed_commands)
wenzelm@47027
   249
        changed_assignment = false
wenzelm@44722
   250
        changed_nodes = Set.empty
wenzelm@44722
   251
        changed_commands = Set.empty
wenzelm@44722
   252
        flush_time = None
wenzelm@44722
   253
      }
wenzelm@44722
   254
wenzelm@47027
   255
      def invoke(assign: Boolean, commands: List[Command])
wenzelm@44722
   256
      {
wenzelm@47027
   257
        changed_assignment |= assign
wenzelm@47027
   258
        for (command <- commands) {
wenzelm@47027
   259
          changed_nodes += command.node_name
wenzelm@47027
   260
          changed_commands += command
wenzelm@47027
   261
        }
wenzelm@44722
   262
        val now = System.currentTimeMillis()
wenzelm@44722
   263
        flush_time match {
wenzelm@44722
   264
          case None => flush_time = Some(now + output_delay.ms)
wenzelm@44722
   265
          case Some(time) => if (now >= time) flush()
wenzelm@44722
   266
        }
wenzelm@44722
   267
      }
wenzelm@44722
   268
    }
wenzelm@44722
   269
wenzelm@44722
   270
wenzelm@43716
   271
    /* resulting changes */
wenzelm@34809
   272
wenzelm@47629
   273
    def handle_change(change: Change)
wenzelm@38221
   274
    //{{{
wenzelm@34809
   275
    {
wenzelm@43722
   276
      val previous = change.previous
wenzelm@43722
   277
      val version = change.version
wenzelm@43722
   278
      val doc_edits = change.doc_edits
wenzelm@38366
   279
wenzelm@44383
   280
      def id_command(command: Command)
wenzelm@43720
   281
      {
wenzelm@44582
   282
        if (!global_state().defined_command(command.id)) {
wenzelm@46687
   283
          global_state >> (_.define_command(command))
wenzelm@44644
   284
          prover.get.define_command(command)
wenzelm@43720
   285
        }
wenzelm@43720
   286
      }
wenzelm@44383
   287
      doc_edits foreach {
wenzelm@44383
   288
        case (_, edit) =>
wenzelm@44383
   289
          edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
wenzelm@44383
   290
      }
wenzelm@43722
   291
wenzelm@44479
   292
      val assignment = global_state().the_assignment(previous).check_finished
wenzelm@46687
   293
      global_state >> (_.define_version(version, assignment))
wenzelm@44481
   294
      prover.get.update(previous.id, version.id, doc_edits)
wenzelm@34809
   295
    }
wenzelm@38221
   296
    //}}}
wenzelm@34809
   297
wenzelm@34809
   298
wenzelm@46772
   299
    /* prover output */
wenzelm@34809
   300
wenzelm@46772
   301
    def handle_output(output: Isabelle_Process.Output)
wenzelm@38221
   302
    //{{{
wenzelm@34809
   303
    {
wenzelm@51662
   304
      def bad_output()
wenzelm@43716
   305
      {
wenzelm@43716
   306
        if (verbose)
wenzelm@46772
   307
          System.err.println("Ignoring prover output: " + output.message.toString)
wenzelm@43716
   308
      }
wenzelm@43716
   309
wenzelm@51662
   310
      def accumulate(state_id: Document.ID, message: XML.Elem)
wenzelm@51662
   311
      {
wenzelm@51662
   312
        try {
wenzelm@51662
   313
          val st = global_state >>> (_.accumulate(state_id, message))
wenzelm@51662
   314
          delay_commands_changed.invoke(false, List(st.command))
wenzelm@51662
   315
        }
wenzelm@51662
   316
        catch {
wenzelm@51662
   317
          case _: Document.State.Fail => bad_output()
wenzelm@51662
   318
        }
wenzelm@51662
   319
      }
wenzelm@51662
   320
wenzelm@46772
   321
      output.properties match {
wenzelm@46122
   322
wenzelm@46774
   323
        case Position.Id(state_id) if !output.is_protocol =>
wenzelm@51662
   324
          accumulate(state_id, output.message)
wenzelm@51662
   325
wenzelm@51818
   326
        case Protocol.Command_Timing(state_id, timing) if output.is_protocol =>
wenzelm@51664
   327
          val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil)))
wenzelm@51664
   328
          accumulate(state_id, prover.get.xml_cache.elem(message))
wenzelm@46122
   329
wenzelm@50201
   330
        case Markup.Assign_Execs if output.is_protocol =>
wenzelm@49416
   331
          XML.content(output.body) match {
wenzelm@45709
   332
            case Protocol.Assign(id, assign) =>
wenzelm@47629
   333
              try {
wenzelm@47629
   334
                val cmds = global_state >>> (_.assign(id, assign))
wenzelm@47629
   335
                delay_commands_changed.invoke(true, cmds)
wenzelm@47629
   336
              }
wenzelm@51662
   337
              catch { case _: Document.State.Fail => bad_output() }
wenzelm@51662
   338
            case _ => bad_output()
wenzelm@44661
   339
          }
wenzelm@44676
   340
          // FIXME separate timeout event/message!?
wenzelm@44676
   341
          if (prover.isDefined && System.currentTimeMillis() > prune_next) {
wenzelm@46687
   342
            val old_versions = global_state >>> (_.prune_history(prune_size))
wenzelm@44676
   343
            if (!old_versions.isEmpty) prover.get.remove_versions(old_versions)
wenzelm@44676
   344
            prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   345
          }
wenzelm@46122
   346
wenzelm@50201
   347
        case Markup.Removed_Versions if output.is_protocol =>
wenzelm@49416
   348
          XML.content(output.body) match {
wenzelm@45709
   349
            case Protocol.Removed(removed) =>
wenzelm@47629
   350
              try {
wenzelm@47629
   351
                global_state >> (_.removed_versions(removed))
wenzelm@47629
   352
              }
wenzelm@51662
   353
              catch { case _: Document.State.Fail => bad_output() }
wenzelm@51662
   354
            case _ => bad_output()
wenzelm@44676
   355
          }
wenzelm@46122
   356
wenzelm@50201
   357
        case Markup.Invoke_Scala(name, id) if output.is_protocol =>
wenzelm@49470
   358
          futures += (id ->
wenzelm@49470
   359
            default_thread_pool.submit(() =>
wenzelm@49470
   360
              {
wenzelm@49470
   361
                val arg = XML.content(output.body)
wenzelm@49470
   362
                val (tag, result) = Invoke_Scala.method(name, arg)
wenzelm@49470
   363
                this_actor ! Finished_Scala(id, tag, result)
wenzelm@49470
   364
              }))
wenzelm@46122
   365
wenzelm@50201
   366
        case Markup.Cancel_Scala(id) if output.is_protocol =>
wenzelm@49470
   367
          futures.get(id) match {
wenzelm@49470
   368
            case Some(future) =>
wenzelm@49470
   369
              future.cancel(true)
wenzelm@49470
   370
              this_actor ! Finished_Scala(id, Invoke_Scala.Tag.INTERRUPT, "")
wenzelm@49470
   371
            case None =>
wenzelm@49470
   372
          }
wenzelm@46122
   373
wenzelm@50697
   374
        case Markup.ML_Statistics(props) if output.is_protocol =>
wenzelm@50697
   375
          statistics.event(Session.Statistics(props))
wenzelm@50255
   376
wenzelm@50975
   377
        case Markup.Task_Statistics(props) if output.is_protocol =>
wenzelm@50975
   378
          // FIXME
wenzelm@50975
   379
wenzelm@48712
   380
        case _ if output.is_init =>
wenzelm@50433
   381
          phase = Session.Ready
wenzelm@46122
   382
wenzelm@50201
   383
        case Markup.Return_Code(rc) if output.is_exit =>
wenzelm@48016
   384
          if (rc == 0) phase = Session.Inactive
wenzelm@48016
   385
          else phase = Session.Failed
wenzelm@48016
   386
wenzelm@51662
   387
        case _ => bad_output()
wenzelm@43748
   388
      }
wenzelm@34809
   389
    }
wenzelm@38221
   390
    //}}}
wenzelm@34809
   391
wenzelm@34820
   392
wenzelm@39630
   393
    /* main loop */
wenzelm@34809
   394
wenzelm@43716
   395
    //{{{
wenzelm@38639
   396
    var finished = false
wenzelm@38639
   397
    while (!finished) {
wenzelm@46570
   398
      receiveWithin(delay_commands_changed.flush_timeout) {
wenzelm@46570
   399
        case TIMEOUT => delay_commands_changed.flush()
wenzelm@44722
   400
wenzelm@48870
   401
        case Start(args) if prover.isEmpty =>
wenzelm@39701
   402
          if (phase == Session.Inactive || phase == Session.Failed) {
wenzelm@39701
   403
            phase = Session.Startup
wenzelm@48020
   404
            prover = Some(new Isabelle_Process(receiver.invoke _, args) with Protocol)
wenzelm@39701
   405
          }
wenzelm@38841
   406
wenzelm@39701
   407
        case Stop =>
wenzelm@39701
   408
          if (phase == Session.Ready) {
wenzelm@46687
   409
            global_state >> (_ => Document.State.init)  // FIXME event bus!?
wenzelm@39701
   410
            phase = Session.Shutdown
wenzelm@43649
   411
            prover.get.terminate
wenzelm@43717
   412
            prover = None
wenzelm@39701
   413
            phase = Session.Inactive
wenzelm@39701
   414
          }
wenzelm@39630
   415
          finished = true
wenzelm@44733
   416
          receiver.cancel()
wenzelm@39630
   417
          reply(())
wenzelm@38850
   418
wenzelm@52084
   419
        case Update_Options(options) if prover.isDefined =>
wenzelm@51083
   420
          if (is_ready) prover.get.options(options)
wenzelm@52084
   421
          global_options.event(Session.Global_Options(options))
wenzelm@52084
   422
          reply(())
wenzelm@50117
   423
wenzelm@44775
   424
        case Cancel_Execution if prover.isDefined =>
wenzelm@44775
   425
          prover.get.cancel_execution()
wenzelm@43716
   426
wenzelm@49196
   427
        case raw @ Session.Raw_Edits(edits) if prover.isDefined =>
wenzelm@47629
   428
          prover.get.discontinue_execution()
wenzelm@43716
   429
wenzelm@47629
   430
          val previous = global_state().history.tip.version
wenzelm@47629
   431
          val version = Future.promise[Document.Version]
wenzelm@47629
   432
          val change = global_state >>> (_.continue_history(previous, edits, version))
wenzelm@49196
   433
          raw_edits.event(raw)
wenzelm@47629
   434
          change_parser ! Text_Edits(previous, edits, version)
wenzelm@47629
   435
wenzelm@43718
   436
          reply(())
wenzelm@43718
   437
wenzelm@50500
   438
        case Session.Dialog_Result(id, serial, result) if prover.isDefined =>
wenzelm@50500
   439
          prover.get.dialog_result(serial, result)
wenzelm@50501
   440
          handle_output(new Isabelle_Process.Output(Protocol.Dialog_Result(id, serial, result)))
wenzelm@50498
   441
wenzelm@44733
   442
        case Messages(msgs) =>
wenzelm@44733
   443
          msgs foreach {
wenzelm@44733
   444
            case input: Isabelle_Process.Input =>
wenzelm@46774
   445
              all_messages.event(input)
wenzelm@43721
   446
wenzelm@46772
   447
            case output: Isabelle_Process.Output =>
wenzelm@48999
   448
              if (output.is_stdout || output.is_stderr) raw_output_messages.event(output)
wenzelm@48999
   449
              else handle_output(output)
wenzelm@46772
   450
              if (output.is_syslog) syslog_messages.event(output)
wenzelm@46774
   451
              all_messages.event(output)
wenzelm@44733
   452
          }
wenzelm@43716
   453
wenzelm@47629
   454
        case change: Change
wenzelm@45635
   455
        if prover.isDefined && global_state().is_assigned(change.previous) =>
wenzelm@45635
   456
          handle_change(change)
wenzelm@45635
   457
wenzelm@49470
   458
        case Finished_Scala(id, tag, result) if prover.isDefined =>
wenzelm@49470
   459
          if (futures.isDefinedAt(id)) {
wenzelm@49470
   460
            prover.get.invoke_scala(id, tag, result)
wenzelm@49470
   461
            futures -= id
wenzelm@49470
   462
          }
wenzelm@49470
   463
wenzelm@47629
   464
        case bad if !bad.isInstanceOf[Change] =>
wenzelm@45635
   465
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   466
      }
wenzelm@34777
   467
    }
wenzelm@43716
   468
    //}}}
wenzelm@34777
   469
  }
wenzelm@34777
   470
wenzelm@34809
   471
wenzelm@43716
   472
  /* actions */
wenzelm@34809
   473
wenzelm@48870
   474
  def start(args: List[String])
wenzelm@50117
   475
  {
wenzelm@50117
   476
    session_actor ! Start(args)
wenzelm@50117
   477
  }
wenzelm@45076
   478
wenzelm@50117
   479
  def stop()
wenzelm@50117
   480
  {
wenzelm@50117
   481
    commands_changed_buffer !? Stop
wenzelm@50117
   482
    change_parser !? Stop
wenzelm@50117
   483
    session_actor !? Stop
wenzelm@50117
   484
  }
wenzelm@38841
   485
wenzelm@44775
   486
  def cancel_execution() { session_actor ! Cancel_Execution }
wenzelm@43648
   487
wenzelm@50363
   488
  def update(edits: List[Document.Edit_Text])
wenzelm@50745
   489
  { if (!edits.isEmpty) session_actor !? Session.Raw_Edits(edits) }
wenzelm@50498
   490
wenzelm@52084
   491
  def update_options(options: Options)
wenzelm@52084
   492
  { session_actor !? Update_Options(options) }
wenzelm@52084
   493
wenzelm@50500
   494
  def dialog_result(id: Document.ID, serial: Long, result: String)
wenzelm@50500
   495
  { session_actor ! Session.Dialog_Result(id, serial, result) }
wenzelm@34777
   496
}