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