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