src/Pure/System/session.scala
author wenzelm
Sun Dec 16 18:44:27 2012 +0100 (2012-12-16 ago)
changeset 50566 b43c4f660320
parent 50501 6f41f1646617
child 50697 82e9178e6a98
permissions -rw-r--r--
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
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@50433
    25
  case class Statistics(stats: 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@41534
   180
wenzelm@39572
   181
  private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
wenzelm@38639
   182
  {
wenzelm@43648
   183
    val this_actor = self
wenzelm@34809
   184
wenzelm@44676
   185
    var prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   186
wenzelm@49470
   187
    var futures = Map.empty[String, java.util.concurrent.Future[Unit]]
wenzelm@49470
   188
wenzelm@34809
   189
wenzelm@44733
   190
    /* buffered prover messages */
wenzelm@44733
   191
wenzelm@44733
   192
    object receiver
wenzelm@44733
   193
    {
wenzelm@44733
   194
      private var buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   195
wenzelm@44733
   196
      def flush(): Unit = synchronized {
wenzelm@44733
   197
        if (!buffer.isEmpty) {
wenzelm@44733
   198
          val msgs = buffer.toList
wenzelm@44733
   199
          this_actor ! Messages(msgs)
wenzelm@44733
   200
          buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   201
        }
wenzelm@44733
   202
      }
wenzelm@44733
   203
      def invoke(msg: Isabelle_Process.Message): Unit = synchronized {
wenzelm@44733
   204
        buffer += msg
wenzelm@44733
   205
        msg match {
wenzelm@46772
   206
          case output: Isabelle_Process.Output =>
wenzelm@46772
   207
            if (output.is_syslog)
wenzelm@46771
   208
              syslog >> (queue =>
wenzelm@46771
   209
                {
wenzelm@46772
   210
                  val queue1 = queue.enqueue(output.message)
wenzelm@46771
   211
                  if (queue1.length > syslog_limit) queue1.dequeue._2 else queue1
wenzelm@46771
   212
                })
wenzelm@46774
   213
            if (output.is_protocol) flush()
wenzelm@44733
   214
          case _ =>
wenzelm@44733
   215
        }
wenzelm@44733
   216
      }
wenzelm@44733
   217
wenzelm@44733
   218
      private val timer = new Timer("session_actor.receiver", true)
wenzelm@44733
   219
      timer.schedule(new TimerTask { def run = flush }, message_delay.ms, message_delay.ms)
wenzelm@44733
   220
wenzelm@44733
   221
      def cancel() { timer.cancel() }
wenzelm@44733
   222
    }
wenzelm@44733
   223
wenzelm@45709
   224
    var prover: Option[Isabelle_Process with Protocol] = None
wenzelm@44733
   225
wenzelm@44733
   226
wenzelm@44722
   227
    /* delayed command changes */
wenzelm@44722
   228
wenzelm@46570
   229
    object delay_commands_changed
wenzelm@44722
   230
    {
wenzelm@47027
   231
      private var changed_assignment: Boolean = false
wenzelm@44722
   232
      private var changed_nodes: Set[Document.Node.Name] = Set.empty
wenzelm@44722
   233
      private var changed_commands: Set[Command] = Set.empty
wenzelm@44722
   234
wenzelm@44722
   235
      private var flush_time: Option[Long] = None
wenzelm@44722
   236
wenzelm@44722
   237
      def flush_timeout: Long =
wenzelm@44722
   238
        flush_time match {
wenzelm@44722
   239
          case None => 5000L
wenzelm@44722
   240
          case Some(time) => (time - System.currentTimeMillis()) max 0
wenzelm@44722
   241
        }
wenzelm@44722
   242
wenzelm@44722
   243
      def flush()
wenzelm@44722
   244
      {
wenzelm@47027
   245
        if (changed_assignment || !changed_nodes.isEmpty || !changed_commands.isEmpty)
wenzelm@47027
   246
          commands_changed_buffer !
wenzelm@47027
   247
            Session.Commands_Changed(changed_assignment, changed_nodes, changed_commands)
wenzelm@47027
   248
        changed_assignment = false
wenzelm@44722
   249
        changed_nodes = Set.empty
wenzelm@44722
   250
        changed_commands = Set.empty
wenzelm@44722
   251
        flush_time = None
wenzelm@44722
   252
      }
wenzelm@44722
   253
wenzelm@47027
   254
      def invoke(assign: Boolean, commands: List[Command])
wenzelm@44722
   255
      {
wenzelm@47027
   256
        changed_assignment |= assign
wenzelm@47027
   257
        for (command <- commands) {
wenzelm@47027
   258
          changed_nodes += command.node_name
wenzelm@47027
   259
          changed_commands += command
wenzelm@47027
   260
        }
wenzelm@44722
   261
        val now = System.currentTimeMillis()
wenzelm@44722
   262
        flush_time match {
wenzelm@44722
   263
          case None => flush_time = Some(now + output_delay.ms)
wenzelm@44722
   264
          case Some(time) => if (now >= time) flush()
wenzelm@44722
   265
        }
wenzelm@44722
   266
      }
wenzelm@44722
   267
    }
wenzelm@44722
   268
wenzelm@44722
   269
wenzelm@43716
   270
    /* resulting changes */
wenzelm@34809
   271
wenzelm@47629
   272
    def handle_change(change: Change)
wenzelm@38221
   273
    //{{{
wenzelm@34809
   274
    {
wenzelm@43722
   275
      val previous = change.previous
wenzelm@43722
   276
      val version = change.version
wenzelm@43722
   277
      val doc_edits = change.doc_edits
wenzelm@38366
   278
wenzelm@44383
   279
      def id_command(command: Command)
wenzelm@43720
   280
      {
wenzelm@44582
   281
        if (!global_state().defined_command(command.id)) {
wenzelm@46687
   282
          global_state >> (_.define_command(command))
wenzelm@44644
   283
          prover.get.define_command(command)
wenzelm@43720
   284
        }
wenzelm@43720
   285
      }
wenzelm@44383
   286
      doc_edits foreach {
wenzelm@44383
   287
        case (_, edit) =>
wenzelm@44383
   288
          edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
wenzelm@44383
   289
      }
wenzelm@43722
   290
wenzelm@44479
   291
      val assignment = global_state().the_assignment(previous).check_finished
wenzelm@46687
   292
      global_state >> (_.define_version(version, assignment))
wenzelm@44481
   293
      prover.get.update(previous.id, version.id, doc_edits)
wenzelm@34809
   294
    }
wenzelm@38221
   295
    //}}}
wenzelm@34809
   296
wenzelm@34809
   297
wenzelm@46772
   298
    /* prover output */
wenzelm@34809
   299
wenzelm@46772
   300
    def handle_output(output: Isabelle_Process.Output)
wenzelm@38221
   301
    //{{{
wenzelm@34809
   302
    {
wenzelm@46772
   303
      def bad_output(output: Isabelle_Process.Output)
wenzelm@43716
   304
      {
wenzelm@43716
   305
        if (verbose)
wenzelm@46772
   306
          System.err.println("Ignoring prover output: " + output.message.toString)
wenzelm@43716
   307
      }
wenzelm@43716
   308
wenzelm@46772
   309
      output.properties match {
wenzelm@46122
   310
wenzelm@46774
   311
        case Position.Id(state_id) if !output.is_protocol =>
wenzelm@38370
   312
          try {
wenzelm@46772
   313
            val st = global_state >>> (_.accumulate(state_id, output.message))
wenzelm@47027
   314
            delay_commands_changed.invoke(false, List(st.command))
wenzelm@38370
   315
          }
wenzelm@43748
   316
          catch {
wenzelm@46772
   317
            case _: Document.State.Fail => bad_output(output)
wenzelm@43748
   318
          }
wenzelm@46122
   319
wenzelm@50201
   320
        case Markup.Assign_Execs if output.is_protocol =>
wenzelm@49416
   321
          XML.content(output.body) match {
wenzelm@45709
   322
            case Protocol.Assign(id, assign) =>
wenzelm@47629
   323
              try {
wenzelm@47629
   324
                val cmds = global_state >>> (_.assign(id, assign))
wenzelm@47629
   325
                delay_commands_changed.invoke(true, cmds)
wenzelm@47629
   326
              }
wenzelm@46772
   327
              catch { case _: Document.State.Fail => bad_output(output) }
wenzelm@46772
   328
            case _ => bad_output(output)
wenzelm@44661
   329
          }
wenzelm@44676
   330
          // FIXME separate timeout event/message!?
wenzelm@44676
   331
          if (prover.isDefined && System.currentTimeMillis() > prune_next) {
wenzelm@46687
   332
            val old_versions = global_state >>> (_.prune_history(prune_size))
wenzelm@44676
   333
            if (!old_versions.isEmpty) prover.get.remove_versions(old_versions)
wenzelm@44676
   334
            prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   335
          }
wenzelm@46122
   336
wenzelm@50201
   337
        case Markup.Removed_Versions if output.is_protocol =>
wenzelm@49416
   338
          XML.content(output.body) match {
wenzelm@45709
   339
            case Protocol.Removed(removed) =>
wenzelm@47629
   340
              try {
wenzelm@47629
   341
                global_state >> (_.removed_versions(removed))
wenzelm@47629
   342
              }
wenzelm@46772
   343
              catch { case _: Document.State.Fail => bad_output(output) }
wenzelm@46772
   344
            case _ => bad_output(output)
wenzelm@44676
   345
          }
wenzelm@46122
   346
wenzelm@50201
   347
        case Markup.Invoke_Scala(name, id) if output.is_protocol =>
wenzelm@49470
   348
          futures += (id ->
wenzelm@49470
   349
            default_thread_pool.submit(() =>
wenzelm@49470
   350
              {
wenzelm@49470
   351
                val arg = XML.content(output.body)
wenzelm@49470
   352
                val (tag, result) = Invoke_Scala.method(name, arg)
wenzelm@49470
   353
                this_actor ! Finished_Scala(id, tag, result)
wenzelm@49470
   354
              }))
wenzelm@46122
   355
wenzelm@50201
   356
        case Markup.Cancel_Scala(id) if output.is_protocol =>
wenzelm@49470
   357
          futures.get(id) match {
wenzelm@49470
   358
            case Some(future) =>
wenzelm@49470
   359
              future.cancel(true)
wenzelm@49470
   360
              this_actor ! Finished_Scala(id, Invoke_Scala.Tag.INTERRUPT, "")
wenzelm@49470
   361
            case None =>
wenzelm@49470
   362
          }
wenzelm@46122
   363
wenzelm@50255
   364
        case Markup.ML_Statistics(stats) if output.is_protocol =>
wenzelm@50433
   365
          statistics.event(Session.Statistics(stats))
wenzelm@50255
   366
wenzelm@48712
   367
        case _ if output.is_init =>
wenzelm@50433
   368
          phase = Session.Ready
wenzelm@46122
   369
wenzelm@50201
   370
        case Markup.Return_Code(rc) if output.is_exit =>
wenzelm@48016
   371
          if (rc == 0) phase = Session.Inactive
wenzelm@48016
   372
          else phase = Session.Failed
wenzelm@48016
   373
wenzelm@48712
   374
        case _ => bad_output(output)
wenzelm@43748
   375
      }
wenzelm@34809
   376
    }
wenzelm@38221
   377
    //}}}
wenzelm@34809
   378
wenzelm@34820
   379
wenzelm@39630
   380
    /* main loop */
wenzelm@34809
   381
wenzelm@43716
   382
    //{{{
wenzelm@38639
   383
    var finished = false
wenzelm@38639
   384
    while (!finished) {
wenzelm@46570
   385
      receiveWithin(delay_commands_changed.flush_timeout) {
wenzelm@46570
   386
        case TIMEOUT => delay_commands_changed.flush()
wenzelm@44722
   387
wenzelm@48870
   388
        case Start(args) if prover.isEmpty =>
wenzelm@39701
   389
          if (phase == Session.Inactive || phase == Session.Failed) {
wenzelm@39701
   390
            phase = Session.Startup
wenzelm@48020
   391
            prover = Some(new Isabelle_Process(receiver.invoke _, args) with Protocol)
wenzelm@39701
   392
          }
wenzelm@38841
   393
wenzelm@39701
   394
        case Stop =>
wenzelm@39701
   395
          if (phase == Session.Ready) {
wenzelm@46687
   396
            global_state >> (_ => Document.State.init)  // FIXME event bus!?
wenzelm@39701
   397
            phase = Session.Shutdown
wenzelm@43649
   398
            prover.get.terminate
wenzelm@43717
   399
            prover = None
wenzelm@39701
   400
            phase = Session.Inactive
wenzelm@39701
   401
          }
wenzelm@39630
   402
          finished = true
wenzelm@44733
   403
          receiver.cancel()
wenzelm@39630
   404
          reply(())
wenzelm@38850
   405
wenzelm@50117
   406
        case Session.Global_Options(options) if prover.isDefined =>
wenzelm@50117
   407
          prover.get.options(options)
wenzelm@50117
   408
wenzelm@44775
   409
        case Cancel_Execution if prover.isDefined =>
wenzelm@44775
   410
          prover.get.cancel_execution()
wenzelm@43716
   411
wenzelm@49196
   412
        case raw @ Session.Raw_Edits(edits) if prover.isDefined =>
wenzelm@47629
   413
          prover.get.discontinue_execution()
wenzelm@43716
   414
wenzelm@47629
   415
          val previous = global_state().history.tip.version
wenzelm@47629
   416
          val version = Future.promise[Document.Version]
wenzelm@47629
   417
          val change = global_state >>> (_.continue_history(previous, edits, version))
wenzelm@49196
   418
          raw_edits.event(raw)
wenzelm@47629
   419
          change_parser ! Text_Edits(previous, edits, version)
wenzelm@47629
   420
wenzelm@43718
   421
          reply(())
wenzelm@43718
   422
wenzelm@50500
   423
        case Session.Dialog_Result(id, serial, result) if prover.isDefined =>
wenzelm@50500
   424
          prover.get.dialog_result(serial, result)
wenzelm@50501
   425
          handle_output(new Isabelle_Process.Output(Protocol.Dialog_Result(id, serial, result)))
wenzelm@50498
   426
wenzelm@44733
   427
        case Messages(msgs) =>
wenzelm@44733
   428
          msgs foreach {
wenzelm@44733
   429
            case input: Isabelle_Process.Input =>
wenzelm@46774
   430
              all_messages.event(input)
wenzelm@43721
   431
wenzelm@46772
   432
            case output: Isabelle_Process.Output =>
wenzelm@48999
   433
              if (output.is_stdout || output.is_stderr) raw_output_messages.event(output)
wenzelm@48999
   434
              else handle_output(output)
wenzelm@46772
   435
              if (output.is_syslog) syslog_messages.event(output)
wenzelm@46774
   436
              all_messages.event(output)
wenzelm@44733
   437
          }
wenzelm@43716
   438
wenzelm@47629
   439
        case change: Change
wenzelm@45635
   440
        if prover.isDefined && global_state().is_assigned(change.previous) =>
wenzelm@45635
   441
          handle_change(change)
wenzelm@45635
   442
wenzelm@49470
   443
        case Finished_Scala(id, tag, result) if prover.isDefined =>
wenzelm@49470
   444
          if (futures.isDefinedAt(id)) {
wenzelm@49470
   445
            prover.get.invoke_scala(id, tag, result)
wenzelm@49470
   446
            futures -= id
wenzelm@49470
   447
          }
wenzelm@49470
   448
wenzelm@47629
   449
        case bad if !bad.isInstanceOf[Change] =>
wenzelm@45635
   450
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   451
      }
wenzelm@34777
   452
    }
wenzelm@43716
   453
    //}}}
wenzelm@34777
   454
  }
wenzelm@34777
   455
wenzelm@34809
   456
wenzelm@43716
   457
  /* actions */
wenzelm@34809
   458
wenzelm@48870
   459
  def start(args: List[String])
wenzelm@50117
   460
  {
wenzelm@50117
   461
    global_options += session_actor
wenzelm@50117
   462
    session_actor ! Start(args)
wenzelm@50117
   463
  }
wenzelm@45076
   464
wenzelm@50117
   465
  def stop()
wenzelm@50117
   466
  {
wenzelm@50117
   467
    global_options -= session_actor
wenzelm@50117
   468
    commands_changed_buffer !? Stop
wenzelm@50117
   469
    change_parser !? Stop
wenzelm@50117
   470
    session_actor !? Stop
wenzelm@50117
   471
  }
wenzelm@38841
   472
wenzelm@44775
   473
  def cancel_execution() { session_actor ! Cancel_Execution }
wenzelm@43648
   474
wenzelm@50363
   475
  def update(edits: List[Document.Edit_Text])
wenzelm@49196
   476
  { session_actor !? Session.Raw_Edits(edits) }
wenzelm@50498
   477
wenzelm@50500
   478
  def dialog_result(id: Document.ID, serial: Long, result: String)
wenzelm@50500
   479
  { session_actor ! Session.Dialog_Result(id, serial, result) }
wenzelm@34777
   480
}