src/Pure/System/session.scala
author wenzelm
Sun Sep 25 17:25:34 2011 +0200 (2011-09-25 ago)
changeset 45076 dd803d319d5b
parent 45075 6c66e268f8eb
child 45158 db4bf4fb5492
permissions -rw-r--r--
tuned signature;
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@44953
    39
class Session(thy_load: Thy_Load = new 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@44960
   110
  def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
wenzelm@44960
   111
      pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
wenzelm@43719
   112
    global_state().snapshot(name, pending_edits)
wenzelm@43716
   113
wenzelm@43651
   114
wenzelm@43651
   115
  /* theory files */
wenzelm@43651
   116
wenzelm@44615
   117
  def header_edit(name: Document.Node.Name, header: Document.Node_Header): Document.Edit_Text =
wenzelm@44442
   118
  {
wenzelm@44442
   119
    def norm_import(s: String): String =
wenzelm@44442
   120
    {
wenzelm@44442
   121
      val thy_name = Thy_Header.base_name(s)
wenzelm@44442
   122
      if (thy_load.is_loaded(thy_name)) thy_name
wenzelm@44616
   123
      else thy_load.append(name.dir, Thy_Header.thy_path(Path.explode(s)))
wenzelm@44442
   124
    }
wenzelm@44616
   125
    def norm_use(s: String): String = thy_load.append(name.dir, Path.explode(s))
wenzelm@44442
   126
wenzelm@44442
   127
    val header1: Document.Node_Header =
wenzelm@44442
   128
      header match {
wenzelm@44442
   129
        case Exn.Res(Thy_Header(thy_name, _, _))
wenzelm@44442
   130
        if (thy_load.is_loaded(thy_name)) =>
wenzelm@44442
   131
          Exn.Exn(ERROR("Attempt to update loaded theory " + quote(thy_name)))
wenzelm@44442
   132
        case _ => Document.Node.norm_header(norm_import, norm_use, header)
wenzelm@44442
   133
      }
wenzelm@44442
   134
    (name, Document.Node.Header(header1))
wenzelm@44442
   135
  }
wenzelm@44442
   136
wenzelm@43651
   137
wenzelm@43651
   138
  /* actor messages */
wenzelm@43651
   139
wenzelm@45055
   140
  private case class Start(timeout: Time, use_socket: Boolean, args: List[String])
wenzelm@44775
   141
  private case object Cancel_Execution
wenzelm@44615
   142
  private case class Init_Node(name: Document.Node.Name,
wenzelm@44385
   143
    header: Document.Node_Header, perspective: Text.Perspective, text: String)
wenzelm@44615
   144
  private case class Edit_Node(name: Document.Node.Name,
wenzelm@44385
   145
    header: Document.Node_Header, perspective: Text.Perspective, edits: List[Text.Edit])
wenzelm@43722
   146
  private case class Change_Node(
wenzelm@44615
   147
    name: Document.Node.Name,
wenzelm@43722
   148
    doc_edits: List[Document.Edit_Command],
wenzelm@43722
   149
    previous: Document.Version,
wenzelm@43722
   150
    version: Document.Version)
wenzelm@44733
   151
  private case class Messages(msgs: List[Isabelle_Process.Message])
wenzelm@41534
   152
wenzelm@39572
   153
  private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
wenzelm@38639
   154
  {
wenzelm@43648
   155
    val this_actor = self
wenzelm@34809
   156
wenzelm@44676
   157
    var prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   158
wenzelm@34809
   159
wenzelm@44733
   160
    /* buffered prover messages */
wenzelm@44733
   161
wenzelm@44733
   162
    object receiver
wenzelm@44733
   163
    {
wenzelm@44733
   164
      private var buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   165
wenzelm@44733
   166
      def flush(): Unit = synchronized {
wenzelm@44733
   167
        if (!buffer.isEmpty) {
wenzelm@44733
   168
          val msgs = buffer.toList
wenzelm@44733
   169
          this_actor ! Messages(msgs)
wenzelm@44733
   170
          buffer = new mutable.ListBuffer[Isabelle_Process.Message]
wenzelm@44733
   171
        }
wenzelm@44733
   172
      }
wenzelm@44733
   173
      def invoke(msg: Isabelle_Process.Message): Unit = synchronized {
wenzelm@44733
   174
        buffer += msg
wenzelm@44733
   175
        msg match {
wenzelm@44733
   176
          case result: Isabelle_Process.Result if result.is_raw => flush()
wenzelm@44733
   177
          case _ =>
wenzelm@44733
   178
        }
wenzelm@44733
   179
      }
wenzelm@44733
   180
wenzelm@44733
   181
      private val timer = new Timer("session_actor.receiver", true)
wenzelm@44733
   182
      timer.schedule(new TimerTask { def run = flush }, message_delay.ms, message_delay.ms)
wenzelm@44733
   183
wenzelm@44733
   184
      def cancel() { timer.cancel() }
wenzelm@44733
   185
    }
wenzelm@44733
   186
wenzelm@44733
   187
    var prover: Option[Isabelle_Process with Isar_Document] = None
wenzelm@44733
   188
wenzelm@44733
   189
wenzelm@44722
   190
    /* delayed command changes */
wenzelm@44722
   191
wenzelm@44722
   192
    object commands_changed_delay
wenzelm@44722
   193
    {
wenzelm@44722
   194
      private var changed_nodes: Set[Document.Node.Name] = Set.empty
wenzelm@44722
   195
      private var changed_commands: Set[Command] = Set.empty
wenzelm@44722
   196
      private def changed: Boolean = !changed_nodes.isEmpty || !changed_commands.isEmpty
wenzelm@44722
   197
wenzelm@44722
   198
      private var flush_time: Option[Long] = None
wenzelm@44722
   199
wenzelm@44722
   200
      def flush_timeout: Long =
wenzelm@44722
   201
        flush_time match {
wenzelm@44722
   202
          case None => 5000L
wenzelm@44722
   203
          case Some(time) => (time - System.currentTimeMillis()) max 0
wenzelm@44722
   204
        }
wenzelm@44722
   205
wenzelm@44722
   206
      def flush()
wenzelm@44722
   207
      {
wenzelm@44722
   208
        if (changed)
wenzelm@44722
   209
          commands_changed_buffer ! Session.Commands_Changed(changed_nodes, changed_commands)
wenzelm@44722
   210
        changed_nodes = Set.empty
wenzelm@44722
   211
        changed_commands = Set.empty
wenzelm@44722
   212
        flush_time = None
wenzelm@44722
   213
      }
wenzelm@44722
   214
wenzelm@44722
   215
      def invoke(command: Command)
wenzelm@44722
   216
      {
wenzelm@44722
   217
        changed_nodes += command.node_name
wenzelm@44722
   218
        changed_commands += command
wenzelm@44722
   219
        val now = System.currentTimeMillis()
wenzelm@44722
   220
        flush_time match {
wenzelm@44722
   221
          case None => flush_time = Some(now + output_delay.ms)
wenzelm@44722
   222
          case Some(time) => if (now >= time) flush()
wenzelm@44722
   223
        }
wenzelm@44722
   224
      }
wenzelm@44722
   225
    }
wenzelm@44722
   226
wenzelm@44722
   227
wenzelm@44436
   228
    /* perspective */
wenzelm@44436
   229
wenzelm@44615
   230
    def update_perspective(name: Document.Node.Name, text_perspective: Text.Perspective)
wenzelm@44436
   231
    {
wenzelm@44475
   232
      val previous = global_state().tip_version
wenzelm@44436
   233
      val (perspective, version) = Thy_Syntax.edit_perspective(previous, name, text_perspective)
wenzelm@44436
   234
wenzelm@44436
   235
      val text_edits: List[Document.Edit_Text] =
wenzelm@44436
   236
        List((name, Document.Node.Perspective(text_perspective)))
wenzelm@44436
   237
      val change =
wenzelm@44436
   238
        global_state.change_yield(
wenzelm@44446
   239
          _.continue_history(Future.value(previous), text_edits, Future.value(version)))
wenzelm@44436
   240
wenzelm@44477
   241
      val assignment = global_state().the_assignment(previous).check_finished
wenzelm@44479
   242
      global_state.change(_.define_version(version, assignment))
wenzelm@44476
   243
      global_state.change_yield(_.assign(version.id, Document.no_assign))
wenzelm@44436
   244
wenzelm@44436
   245
      prover.get.update_perspective(previous.id, version.id, name, perspective)
wenzelm@44436
   246
    }
wenzelm@44436
   247
wenzelm@44436
   248
wenzelm@43716
   249
    /* incoming edits */
wenzelm@43716
   250
wenzelm@44615
   251
    def handle_edits(name: Document.Node.Name,
wenzelm@44384
   252
        header: Document.Node_Header, edits: List[Document.Node.Edit[Text.Edit, Text.Perspective]])
wenzelm@43716
   253
    //{{{
wenzelm@43716
   254
    {
wenzelm@43716
   255
      val syntax = current_syntax()
wenzelm@43719
   256
      val previous = global_state().history.tip.version
wenzelm@44185
   257
wenzelm@44612
   258
      prover.get.cancel_execution()
wenzelm@44612
   259
wenzelm@44615
   260
      val text_edits = header_edit(name, header) :: edits.map(edit => (name, edit))
wenzelm@44185
   261
      val result = Future.fork { Thy_Syntax.text_edits(syntax, previous.join, text_edits) }
wenzelm@43722
   262
      val change =
wenzelm@44446
   263
        global_state.change_yield(_.continue_history(previous, text_edits, result.map(_._2)))
wenzelm@43716
   264
wenzelm@43722
   265
      result.map {
wenzelm@44157
   266
        case (doc_edits, _) =>
wenzelm@43722
   267
          assignments.await { global_state().is_assigned(previous.get_finished) }
wenzelm@44157
   268
          this_actor ! Change_Node(name, doc_edits, previous.join, change.version.join)
wenzelm@43722
   269
      }
wenzelm@43716
   270
    }
wenzelm@43716
   271
    //}}}
wenzelm@43716
   272
wenzelm@43716
   273
wenzelm@44436
   274
    /* exec state assignment */
wenzelm@44436
   275
wenzelm@44476
   276
    def handle_assign(id: Document.Version_ID, assign: Document.Assign)
wenzelm@44436
   277
    //{{{
wenzelm@44436
   278
    {
wenzelm@44476
   279
      val cmds = global_state.change_yield(_.assign(id, assign))
wenzelm@44722
   280
      for (cmd <- cmds) commands_changed_delay.invoke(cmd)
wenzelm@44436
   281
      assignments.event(Session.Assignment)
wenzelm@44436
   282
    }
wenzelm@44436
   283
    //}}}
wenzelm@44436
   284
wenzelm@44436
   285
wenzelm@44676
   286
    /* removed versions */
wenzelm@44676
   287
wenzelm@44676
   288
    def handle_removed(removed: List[Document.Version_ID])
wenzelm@44676
   289
    //{{{
wenzelm@44676
   290
    {
wenzelm@44676
   291
      global_state.change(_.removed_versions(removed))
wenzelm@44676
   292
    }
wenzelm@44676
   293
    //}}}
wenzelm@44676
   294
wenzelm@44676
   295
wenzelm@43716
   296
    /* resulting changes */
wenzelm@34809
   297
wenzelm@43722
   298
    def handle_change(change: Change_Node)
wenzelm@38221
   299
    //{{{
wenzelm@34809
   300
    {
wenzelm@43722
   301
      val previous = change.previous
wenzelm@43722
   302
      val version = change.version
wenzelm@43722
   303
      val name = change.name
wenzelm@43722
   304
      val doc_edits = change.doc_edits
wenzelm@38366
   305
wenzelm@44383
   306
      def id_command(command: Command)
wenzelm@43720
   307
      {
wenzelm@44582
   308
        if (!global_state().defined_command(command.id)) {
wenzelm@43720
   309
          global_state.change(_.define_command(command))
wenzelm@44644
   310
          prover.get.define_command(command)
wenzelm@43720
   311
        }
wenzelm@43720
   312
      }
wenzelm@44383
   313
      doc_edits foreach {
wenzelm@44383
   314
        case (_, edit) =>
wenzelm@44383
   315
          edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
wenzelm@44383
   316
      }
wenzelm@43722
   317
wenzelm@44479
   318
      val assignment = global_state().the_assignment(previous).check_finished
wenzelm@44479
   319
      global_state.change(_.define_version(version, assignment))
wenzelm@44481
   320
      prover.get.update(previous.id, version.id, doc_edits)
wenzelm@34809
   321
    }
wenzelm@38221
   322
    //}}}
wenzelm@34809
   323
wenzelm@34809
   324
wenzelm@34809
   325
    /* prover results */
wenzelm@34809
   326
wenzelm@34809
   327
    def handle_result(result: Isabelle_Process.Result)
wenzelm@38221
   328
    //{{{
wenzelm@34809
   329
    {
wenzelm@43716
   330
      def bad_result(result: Isabelle_Process.Result)
wenzelm@43716
   331
      {
wenzelm@43716
   332
        if (verbose)
wenzelm@43716
   333
          System.err.println("Ignoring prover result: " + result.message.toString)
wenzelm@43716
   334
      }
wenzelm@43716
   335
wenzelm@38722
   336
      result.properties match {
wenzelm@43748
   337
        case Position.Id(state_id) if !result.is_raw =>
wenzelm@38370
   338
          try {
wenzelm@38841
   339
            val st = global_state.change_yield(_.accumulate(state_id, result.message))
wenzelm@44722
   340
            commands_changed_delay.invoke(st.command)
wenzelm@38370
   341
          }
wenzelm@43748
   342
          catch {
wenzelm@43748
   343
            case _: Document.State.Fail => bad_result(result)
wenzelm@43748
   344
          }
wenzelm@44661
   345
        case Markup.Assign_Execs if result.is_raw =>
wenzelm@44661
   346
          XML.content(result.body).mkString match {
wenzelm@44661
   347
            case Isar_Document.Assign(id, assign) =>
wenzelm@44661
   348
              try { handle_assign(id, assign) }
wenzelm@44661
   349
              catch { case _: Document.State.Fail => bad_result(result) }
wenzelm@44661
   350
            case _ => bad_result(result)
wenzelm@44661
   351
          }
wenzelm@44676
   352
          // FIXME separate timeout event/message!?
wenzelm@44676
   353
          if (prover.isDefined && System.currentTimeMillis() > prune_next) {
wenzelm@44676
   354
            val old_versions = global_state.change_yield(_.prune_history(prune_size))
wenzelm@44676
   355
            if (!old_versions.isEmpty) prover.get.remove_versions(old_versions)
wenzelm@44676
   356
            prune_next = System.currentTimeMillis() + prune_delay.ms
wenzelm@44676
   357
          }
wenzelm@44676
   358
        case Markup.Removed_Versions if result.is_raw =>
wenzelm@44676
   359
          XML.content(result.body).mkString match {
wenzelm@44676
   360
            case Isar_Document.Removed(removed) =>
wenzelm@44676
   361
              try { handle_removed(removed) }
wenzelm@44676
   362
              catch { case _: Document.State.Fail => bad_result(result) }
wenzelm@44676
   363
            case _ => bad_result(result)
wenzelm@44676
   364
          }
wenzelm@43748
   365
        case Markup.Invoke_Scala(name, id) if result.is_raw =>
wenzelm@43748
   366
          Future.fork {
wenzelm@43748
   367
            val arg = XML.content(result.body).mkString
wenzelm@43748
   368
            val (tag, res) = Invoke_Scala.method(name, arg)
wenzelm@43748
   369
            prover.get.invoke_scala(id, tag, res)
wenzelm@43748
   370
          }
wenzelm@44298
   371
        case Markup.Cancel_Scala(id) =>
wenzelm@44298
   372
          System.err.println("cancel_scala " + id)  // FIXME cancel JVM task
wenzelm@38722
   373
        case _ =>
wenzelm@39630
   374
          if (result.is_syslog) {
wenzelm@39630
   375
            reverse_syslog ::= result.message
wenzelm@40533
   376
            if (result.is_ready) {
wenzelm@43716
   377
              // FIXME move to ML side (!?)
wenzelm@40533
   378
              syntax += ("hence", Keyword.PRF_ASM_GOAL, "then have")
wenzelm@40533
   379
              syntax += ("thus", Keyword.PRF_ASM_GOAL, "then show")
wenzelm@40533
   380
              phase = Session.Ready
wenzelm@40533
   381
            }
wenzelm@39701
   382
            else if (result.is_exit && phase == Session.Startup) phase = Session.Failed
wenzelm@39696
   383
            else if (result.is_exit) phase = Session.Inactive
wenzelm@39630
   384
          }
wenzelm@39626
   385
          else if (result.is_stdout) { }
wenzelm@39625
   386
          else if (result.is_status) {
wenzelm@38370
   387
            result.body match {
wenzelm@38370
   388
              case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
wenzelm@38370
   389
              case List(Keyword.Keyword_Decl(name)) => syntax += name
wenzelm@44574
   390
              case List(Thy_Info.Loaded_Theory(name)) => thy_load.register_thy(name)
wenzelm@39625
   391
              case _ => bad_result(result)
wenzelm@38370
   392
            }
wenzelm@38370
   393
          }
wenzelm@39625
   394
          else bad_result(result)
wenzelm@43748
   395
      }
wenzelm@34809
   396
    }
wenzelm@38221
   397
    //}}}
wenzelm@34809
   398
wenzelm@34820
   399
wenzelm@39630
   400
    /* main loop */
wenzelm@34809
   401
wenzelm@43716
   402
    //{{{
wenzelm@38639
   403
    var finished = false
wenzelm@38639
   404
    while (!finished) {
wenzelm@44722
   405
      receiveWithin(commands_changed_delay.flush_timeout) {
wenzelm@44722
   406
        case TIMEOUT => commands_changed_delay.flush()
wenzelm@44722
   407
wenzelm@45055
   408
        case Start(timeout, use_socket, args) if prover.isEmpty =>
wenzelm@39701
   409
          if (phase == Session.Inactive || phase == Session.Failed) {
wenzelm@39701
   410
            phase = Session.Startup
wenzelm@44733
   411
            prover =
wenzelm@45055
   412
              Some(new Isabelle_Process(timeout, use_socket, receiver.invoke _, args)
wenzelm@45055
   413
                with Isar_Document)
wenzelm@39701
   414
          }
wenzelm@38841
   415
wenzelm@39701
   416
        case Stop =>
wenzelm@39701
   417
          if (phase == Session.Ready) {
wenzelm@39701
   418
            global_state.change(_ => Document.State.init)  // FIXME event bus!?
wenzelm@39701
   419
            phase = Session.Shutdown
wenzelm@43649
   420
            prover.get.terminate
wenzelm@43717
   421
            prover = None
wenzelm@39701
   422
            phase = Session.Inactive
wenzelm@39701
   423
          }
wenzelm@39630
   424
          finished = true
wenzelm@44733
   425
          receiver.cancel()
wenzelm@39630
   426
          reply(())
wenzelm@38850
   427
wenzelm@44775
   428
        case Cancel_Execution if prover.isDefined =>
wenzelm@44775
   429
          prover.get.cancel_execution()
wenzelm@43716
   430
wenzelm@44615
   431
        case Init_Node(name, header, perspective, text) if prover.isDefined =>
wenzelm@43716
   432
          // FIXME compare with existing node
wenzelm@44615
   433
          handle_edits(name, header,
wenzelm@44385
   434
            List(Document.Node.Clear(),
wenzelm@44385
   435
              Document.Node.Edits(List(Text.Edit.insert(0, text))),
wenzelm@44385
   436
              Document.Node.Perspective(perspective)))
wenzelm@43716
   437
          reply(())
wenzelm@43716
   438
wenzelm@44615
   439
        case Edit_Node(name, header, perspective, text_edits) if prover.isDefined =>
wenzelm@44484
   440
          if (text_edits.isEmpty && global_state().tip_stable &&
wenzelm@44484
   441
              perspective.range.stop <= global_state().last_exec_offset(name))
wenzelm@44484
   442
            update_perspective(name, perspective)
wenzelm@44484
   443
          else
wenzelm@44615
   444
            handle_edits(name, header,
wenzelm@44436
   445
              List(Document.Node.Edits(text_edits), Document.Node.Perspective(perspective)))
wenzelm@43718
   446
          reply(())
wenzelm@43718
   447
wenzelm@43722
   448
        case change: Change_Node if prover.isDefined =>
wenzelm@43722
   449
          handle_change(change)
wenzelm@43716
   450
wenzelm@44733
   451
        case Messages(msgs) =>
wenzelm@44733
   452
          msgs foreach {
wenzelm@44733
   453
            case input: Isabelle_Process.Input =>
wenzelm@44733
   454
              raw_messages.event(input)
wenzelm@43721
   455
wenzelm@44733
   456
            case result: Isabelle_Process.Result =>
wenzelm@44733
   457
              handle_result(result)
wenzelm@44734
   458
              if (result.is_syslog) syslog_messages.event(result)
wenzelm@44734
   459
              if (result.is_stdout) raw_output_messages.event(result)
wenzelm@44733
   460
              raw_messages.event(result)
wenzelm@44733
   461
          }
wenzelm@43716
   462
wenzelm@43717
   463
        case bad => System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   464
      }
wenzelm@34777
   465
    }
wenzelm@43716
   466
    //}}}
wenzelm@34777
   467
  }
wenzelm@34777
   468
wenzelm@34809
   469
wenzelm@43716
   470
  /* actions */
wenzelm@34809
   471
wenzelm@45076
   472
  def start(timeout: Time, use_socket: Boolean, args: List[String])
wenzelm@45055
   473
  { session_actor ! Start(timeout, use_socket, args) }
wenzelm@34820
   474
wenzelm@45076
   475
  def start(args: List[String]) { start (Time.seconds(25), false, args) }
wenzelm@45076
   476
wenzelm@44722
   477
  def stop() { commands_changed_buffer !? Stop; session_actor !? Stop }
wenzelm@38841
   478
wenzelm@44775
   479
  def cancel_execution() { session_actor ! Cancel_Execution }
wenzelm@43648
   480
wenzelm@44615
   481
  def init_node(name: Document.Node.Name,
wenzelm@44385
   482
    header: Document.Node_Header, perspective: Text.Perspective, text: String)
wenzelm@44615
   483
  { session_actor !? Init_Node(name, header, perspective, text) }
wenzelm@38221
   484
wenzelm@44615
   485
  def edit_node(name: Document.Node.Name,
wenzelm@44615
   486
    header: Document.Node_Header, perspective: Text.Perspective, edits: List[Text.Edit])
wenzelm@44615
   487
  { session_actor !? Edit_Node(name, header, perspective, edits) }
wenzelm@34777
   488
}