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