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