src/Pure/System/session.scala
author wenzelm
Thu Sep 23 18:44:26 2010 +0200 (2010-09-23)
changeset 39630 44181423183a
parent 39629 08eb2730a8a1
child 39633 26a28110ece5
permissions -rw-r--r--
explicit Session.Phase indication with associated event bus;
asynchronous Session.start();
synchronous Session.stop();
added Plugin.session_manager on top of basic Session;
eliminated separate isabelle.activate action;
misc tuning;
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@36676
     5
Isabelle session, potentially with running prover.
wenzelm@36676
     6
*/
wenzelm@34777
     7
wenzelm@34871
     8
package isabelle
wenzelm@34777
     9
wenzelm@34777
    10
wenzelm@34820
    11
import scala.actors.TIMEOUT
wenzelm@38226
    12
import scala.actors.Actor
wenzelm@34777
    13
import scala.actors.Actor._
wenzelm@34777
    14
wenzelm@34815
    15
wenzelm@34791
    16
object Session
wenzelm@34791
    17
{
wenzelm@34813
    18
  /* events */
wenzelm@34813
    19
wenzelm@34791
    20
  case object Global_Settings
wenzelm@37849
    21
  case object Perspective
wenzelm@38849
    22
  case object Assignment
wenzelm@38360
    23
  case class Commands_Changed(set: Set[Command])
wenzelm@39630
    24
wenzelm@39630
    25
  sealed abstract class Phase
wenzelm@39630
    26
  case object Inactive extends Phase
wenzelm@39630
    27
  case object Startup extends Phase
wenzelm@39630
    28
  case object Exit extends Phase
wenzelm@39630
    29
  case object Ready extends Phase
wenzelm@39630
    30
  case object Shutdown extends Phase
wenzelm@39630
    31
  case object Terminated extends Phase
wenzelm@34791
    32
}
wenzelm@34791
    33
wenzelm@34777
    34
wenzelm@34851
    35
class Session(system: Isabelle_System)
wenzelm@34777
    36
{
wenzelm@37849
    37
  /* real time parameters */  // FIXME properties or settings (!?)
wenzelm@37849
    38
wenzelm@37849
    39
  // user input (e.g. text edits, cursor movement)
wenzelm@37849
    40
  val input_delay = 300
wenzelm@37849
    41
wenzelm@37849
    42
  // prover output (markup, common messages)
wenzelm@37849
    43
  val output_delay = 100
wenzelm@37849
    44
wenzelm@37849
    45
  // GUI layout updates
wenzelm@37849
    46
  val update_delay = 500
wenzelm@37849
    47
wenzelm@37849
    48
wenzelm@34809
    49
  /* pervasive event buses */
wenzelm@34809
    50
wenzelm@39630
    51
  val phase_changed = new Event_Bus[(Session.Phase, Session.Phase)]
wenzelm@34809
    52
  val global_settings = new Event_Bus[Session.Global_Settings.type]
wenzelm@39588
    53
  val raw_messages = new Event_Bus[Isabelle_Process.Result]
wenzelm@38360
    54
  val commands_changed = new Event_Bus[Session.Commands_Changed]
wenzelm@37849
    55
  val perspective = new Event_Bus[Session.Perspective.type]
wenzelm@38849
    56
  val assignments = new Event_Bus[Session.Assignment.type]
wenzelm@34809
    57
wenzelm@34809
    58
wenzelm@34778
    59
  /* unique ids */
wenzelm@34778
    60
wenzelm@38363
    61
  private var id_count: Document.ID = 0
wenzelm@38419
    62
  def new_id(): Document.ID = synchronized {
wenzelm@38355
    63
    require(id_count > java.lang.Long.MIN_VALUE)
wenzelm@38355
    64
    id_count -= 1
wenzelm@38355
    65
    id_count
wenzelm@38355
    66
  }
wenzelm@34778
    67
wenzelm@34778
    68
wenzelm@34826
    69
wenzelm@38841
    70
  /** buffered command changes (delay_first discipline) **/
wenzelm@38841
    71
wenzelm@38841
    72
  private case object Stop
wenzelm@38841
    73
wenzelm@39572
    74
  private val (_, command_change_buffer) =
wenzelm@39572
    75
    Simple_Thread.actor("command_change_buffer", daemon = true)
wenzelm@38841
    76
  //{{{
wenzelm@38841
    77
  {
wenzelm@38841
    78
    import scala.compat.Platform.currentTime
wenzelm@38841
    79
wenzelm@38841
    80
    var changed: Set[Command] = Set()
wenzelm@38841
    81
    var flush_time: Option[Long] = None
wenzelm@38841
    82
wenzelm@38841
    83
    def flush_timeout: Long =
wenzelm@38841
    84
      flush_time match {
wenzelm@38841
    85
        case None => 5000L
wenzelm@38841
    86
        case Some(time) => (time - currentTime) max 0
wenzelm@38841
    87
      }
wenzelm@38841
    88
wenzelm@38841
    89
    def flush()
wenzelm@38841
    90
    {
wenzelm@38841
    91
      if (!changed.isEmpty) commands_changed.event(Session.Commands_Changed(changed))
wenzelm@38841
    92
      changed = Set()
wenzelm@38841
    93
      flush_time = None
wenzelm@38841
    94
    }
wenzelm@38841
    95
wenzelm@38841
    96
    def invoke()
wenzelm@38841
    97
    {
wenzelm@38841
    98
      val now = currentTime
wenzelm@38841
    99
      flush_time match {
wenzelm@38841
   100
        case None => flush_time = Some(now + output_delay)
wenzelm@38841
   101
        case Some(time) => if (now >= time) flush()
wenzelm@38841
   102
      }
wenzelm@38841
   103
    }
wenzelm@38841
   104
wenzelm@38841
   105
    var finished = false
wenzelm@38841
   106
    while (!finished) {
wenzelm@38841
   107
      receiveWithin(flush_timeout) {
wenzelm@38841
   108
        case command: Command => changed += command; invoke()
wenzelm@38841
   109
        case TIMEOUT => flush()
wenzelm@39630
   110
        case Stop => finished = true; reply(())
wenzelm@38841
   111
        case bad => System.err.println("command_change_buffer: ignoring bad message " + bad)
wenzelm@38841
   112
      }
wenzelm@38841
   113
    }
wenzelm@38841
   114
  }
wenzelm@38841
   115
  //}}}
wenzelm@38841
   116
wenzelm@38841
   117
wenzelm@38841
   118
wenzelm@38841
   119
  /** main protocol actor **/
wenzelm@34809
   120
wenzelm@34851
   121
  @volatile private var syntax = new Outer_Syntax(system.symbols)
wenzelm@38569
   122
  def current_syntax(): Outer_Syntax = syntax
wenzelm@34809
   123
wenzelm@39626
   124
  @volatile private var reverse_syslog = List[XML.Elem]()
wenzelm@39629
   125
  def syslog(): String = reverse_syslog.reverse.map(msg => XML.content(msg).mkString).mkString("\n")
wenzelm@39626
   126
wenzelm@39630
   127
  @volatile private var _phase: Session.Phase = Session.Inactive
wenzelm@39630
   128
  def phase = _phase
wenzelm@39630
   129
  def phase_=(new_phase: Session.Phase)
wenzelm@39630
   130
  {
wenzelm@39630
   131
    val old_phase = _phase
wenzelm@39630
   132
    _phase = new_phase
wenzelm@39630
   133
    phase_changed.event(old_phase, new_phase)
wenzelm@39630
   134
  }
wenzelm@39630
   135
wenzelm@38841
   136
  private val global_state = new Volatile(Document.State.init)
wenzelm@38841
   137
  def current_state(): Document.State = global_state.peek()
wenzelm@34816
   138
wenzelm@39593
   139
  private case object Interrupt_Prover
wenzelm@38841
   140
  private case class Edit_Version(edits: List[Document.Node_Text_Edit])
wenzelm@39630
   141
  private case class Start(timeout: Int, args: List[String])
wenzelm@34777
   142
wenzelm@39572
   143
  private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
wenzelm@38639
   144
  {
wenzelm@34809
   145
    var prover: Isabelle_Process with Isar_Document = null
wenzelm@34809
   146
wenzelm@34809
   147
wenzelm@34809
   148
    /* document changes */
wenzelm@34809
   149
wenzelm@38227
   150
    def handle_change(change: Document.Change)
wenzelm@38221
   151
    //{{{
wenzelm@34809
   152
    {
wenzelm@38848
   153
      val previous = change.previous.get_finished
wenzelm@38848
   154
      val (node_edits, current) = change.result.get_finished
wenzelm@38366
   155
wenzelm@38848
   156
      var former_assignment = global_state.peek().the_assignment(previous).get_finished
wenzelm@38370
   157
      for {
wenzelm@38370
   158
        (name, Some(cmd_edits)) <- node_edits
wenzelm@38370
   159
        (prev, None) <- cmd_edits
wenzelm@38417
   160
        removed <- previous.nodes(name).commands.get_after(prev)
wenzelm@38370
   161
      } former_assignment -= removed
wenzelm@38370
   162
wenzelm@38150
   163
      val id_edits =
wenzelm@38150
   164
        node_edits map {
wenzelm@38150
   165
          case (name, None) => (name, None)
wenzelm@38150
   166
          case (name, Some(cmd_edits)) =>
wenzelm@38370
   167
            val ids =
wenzelm@38150
   168
              cmd_edits map {
wenzelm@38150
   169
                case (c1, c2) =>
wenzelm@38150
   170
                  val id1 = c1.map(_.id)
wenzelm@38150
   171
                  val id2 =
wenzelm@38150
   172
                    c2 match {
wenzelm@38150
   173
                      case None => None
wenzelm@38150
   174
                      case Some(command) =>
wenzelm@38841
   175
                        if (global_state.peek().lookup_command(command.id).isEmpty) {
wenzelm@38841
   176
                          global_state.change(_.define_command(command))
wenzelm@38150
   177
                          prover.define_command(command.id, system.symbols.encode(command.source))
wenzelm@38150
   178
                        }
wenzelm@38150
   179
                        Some(command.id)
wenzelm@38150
   180
                    }
wenzelm@38150
   181
                  (id1, id2)
wenzelm@38150
   182
              }
wenzelm@38370
   183
            (name -> Some(ids))
wenzelm@38150
   184
        }
wenzelm@38841
   185
      global_state.change(_.define_version(current, former_assignment))
wenzelm@38417
   186
      prover.edit_version(previous.id, current.id, id_edits)
wenzelm@34809
   187
    }
wenzelm@38221
   188
    //}}}
wenzelm@34809
   189
wenzelm@34809
   190
wenzelm@34809
   191
    /* prover results */
wenzelm@34809
   192
wenzelm@34836
   193
    def bad_result(result: Isabelle_Process.Result)
wenzelm@34836
   194
    {
wenzelm@37041
   195
      System.err.println("Ignoring prover result: " + result.message.toString)
wenzelm@34836
   196
    }
wenzelm@34836
   197
wenzelm@34809
   198
    def handle_result(result: Isabelle_Process.Result)
wenzelm@38221
   199
    //{{{
wenzelm@34809
   200
    {
wenzelm@39588
   201
      raw_messages.event(result)
wenzelm@34799
   202
wenzelm@38722
   203
      result.properties match {
wenzelm@38722
   204
        case Position.Id(state_id) =>
wenzelm@38370
   205
          try {
wenzelm@38841
   206
            val st = global_state.change_yield(_.accumulate(state_id, result.message))
wenzelm@38841
   207
            command_change_buffer ! st.command
wenzelm@38370
   208
          }
wenzelm@38414
   209
          catch { case _: Document.State.Fail => bad_result(result) }
wenzelm@38722
   210
        case _ =>
wenzelm@39630
   211
          if (result.is_syslog) {
wenzelm@39630
   212
            reverse_syslog ::= result.message
wenzelm@39630
   213
            if (result.is_ready) phase = Session.Ready
wenzelm@39630
   214
            else if (result.is_exit) phase = Session.Exit
wenzelm@39630
   215
          }
wenzelm@39626
   216
          else if (result.is_stdout) { }
wenzelm@39625
   217
          else if (result.is_status) {
wenzelm@38370
   218
            result.body match {
wenzelm@38417
   219
              case List(Isar_Document.Assign(id, edits)) =>
wenzelm@38849
   220
                try {
wenzelm@38882
   221
                  val cmds: List[Command] = global_state.change_yield(_.assign(id, edits))
wenzelm@38882
   222
                  for (cmd <- cmds) command_change_buffer ! cmd
wenzelm@38849
   223
                  assignments.event(Session.Assignment)
wenzelm@38849
   224
                }
wenzelm@38414
   225
                catch { case _: Document.State.Fail => bad_result(result) }
wenzelm@38370
   226
              case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
wenzelm@38370
   227
              case List(Keyword.Keyword_Decl(name)) => syntax += name
wenzelm@39625
   228
              case _ => bad_result(result)
wenzelm@38370
   229
            }
wenzelm@38370
   230
          }
wenzelm@39625
   231
          else bad_result(result)
wenzelm@34809
   232
        }
wenzelm@34809
   233
    }
wenzelm@38221
   234
    //}}}
wenzelm@34809
   235
wenzelm@34820
   236
wenzelm@39630
   237
    /* main loop */
wenzelm@34809
   238
wenzelm@38639
   239
    var finished = false
wenzelm@38639
   240
    while (!finished) {
wenzelm@38639
   241
      receive {
wenzelm@39593
   242
        case Interrupt_Prover =>
wenzelm@39593
   243
          if (prover != null) prover.interrupt
wenzelm@39593
   244
wenzelm@39630
   245
        case Edit_Version(edits) if prover != null =>
wenzelm@38841
   246
          val previous = global_state.peek().history.tip.current
wenzelm@38841
   247
          val result = Future.fork { Thy_Syntax.text_edits(Session.this, previous.join, edits) }
wenzelm@38841
   248
          val change = global_state.change_yield(_.extend_history(previous, edits, result))
wenzelm@38849
   249
wenzelm@38849
   250
          val this_actor = self
wenzelm@38849
   251
          change.current.map(_ => {
wenzelm@38849
   252
            assignments.await { global_state.peek().is_assigned(previous.get_finished) }
wenzelm@38849
   253
            this_actor ! change })
wenzelm@38849
   254
wenzelm@38841
   255
          reply(())
wenzelm@38841
   256
wenzelm@39630
   257
        case change: Document.Change if prover != null => handle_change(change)
wenzelm@39630
   258
wenzelm@39630
   259
        case result: Isabelle_Process.Result => handle_result(result)
wenzelm@38841
   260
wenzelm@39630
   261
        case Start(timeout, args) if prover == null =>
wenzelm@39630
   262
          phase = Session.Startup
wenzelm@39630
   263
          prover = new Isabelle_Process(system, timeout, self, args:_*) with Isar_Document
wenzelm@38841
   264
wenzelm@39630
   265
        case Stop if phase == Session.Ready =>
wenzelm@39630
   266
          global_state.change(_ => Document.State.init)  // FIXME event bus!?
wenzelm@39630
   267
          phase = Session.Shutdown
wenzelm@39630
   268
          prover.terminate
wenzelm@39630
   269
          phase = Session.Terminated
wenzelm@39630
   270
          finished = true
wenzelm@39630
   271
          reply(())
wenzelm@38850
   272
wenzelm@34820
   273
        case bad if prover != null =>
wenzelm@34777
   274
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   275
      }
wenzelm@34777
   276
    }
wenzelm@34777
   277
  }
wenzelm@34777
   278
wenzelm@34809
   279
wenzelm@37129
   280
wenzelm@38226
   281
  /** main methods **/
wenzelm@34809
   282
wenzelm@39630
   283
  def start(timeout: Int, args: List[String]) { session_actor ! Start(timeout, args) }
wenzelm@34820
   284
wenzelm@39630
   285
  def stop() { command_change_buffer !? Stop; session_actor !? Stop }
wenzelm@38841
   286
wenzelm@39593
   287
  def interrupt() { session_actor ! Interrupt_Prover }
wenzelm@39593
   288
wenzelm@38841
   289
  def edit_version(edits: List[Document.Node_Text_Edit]) { session_actor !? Edit_Version(edits) }
wenzelm@38221
   290
wenzelm@38841
   291
  def snapshot(name: String, pending_edits: List[Text.Edit]): Document.Snapshot =
wenzelm@38841
   292
    global_state.peek().snapshot(name, pending_edits)
wenzelm@34777
   293
}