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