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