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