src/Pure/System/session.scala
author wenzelm
Tue Aug 31 17:35:41 2010 +0200 (2010-08-31)
changeset 38882 e1fb3bbc22ab
parent 38850 5c3e5c548f12
child 39524 59ebce09ce6e
permissions -rw-r--r--
Document state assignment indicates command change;
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@38423
    44
  val raw_protocol = new Event_Bus[Isabelle_Process.Result]
wenzelm@37065
    45
  val raw_output = new Event_Bus[Isabelle_Process.Result]
wenzelm@38360
    46
  val commands_changed = new Event_Bus[Session.Commands_Changed]
wenzelm@37849
    47
  val perspective = new Event_Bus[Session.Perspective.type]
wenzelm@38849
    48
  val assignments = new Event_Bus[Session.Assignment.type]
wenzelm@34809
    49
wenzelm@34809
    50
wenzelm@34778
    51
  /* unique ids */
wenzelm@34778
    52
wenzelm@38363
    53
  private var id_count: Document.ID = 0
wenzelm@38419
    54
  def new_id(): Document.ID = synchronized {
wenzelm@38355
    55
    require(id_count > java.lang.Long.MIN_VALUE)
wenzelm@38355
    56
    id_count -= 1
wenzelm@38355
    57
    id_count
wenzelm@38355
    58
  }
wenzelm@34778
    59
wenzelm@34778
    60
wenzelm@34826
    61
wenzelm@38841
    62
  /** buffered command changes (delay_first discipline) **/
wenzelm@38841
    63
wenzelm@38841
    64
  private case object Stop
wenzelm@38841
    65
wenzelm@38841
    66
  private val command_change_buffer = 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@38841
   115
  private val global_state = new Volatile(Document.State.init)
wenzelm@38841
   116
  def current_state(): Document.State = global_state.peek()
wenzelm@34816
   117
wenzelm@38841
   118
  private case class Edit_Version(edits: List[Document.Node_Text_Edit])
wenzelm@38221
   119
  private case class Started(timeout: Int, args: List[String])
wenzelm@34777
   120
wenzelm@38639
   121
  private val session_actor = Simple_Thread.actor("session_actor", daemon = true)
wenzelm@38639
   122
  {
wenzelm@34809
   123
    var prover: Isabelle_Process with Isar_Document = null
wenzelm@34809
   124
wenzelm@34809
   125
wenzelm@34809
   126
    /* document changes */
wenzelm@34809
   127
wenzelm@38227
   128
    def handle_change(change: Document.Change)
wenzelm@38221
   129
    //{{{
wenzelm@34809
   130
    {
wenzelm@38848
   131
      val previous = change.previous.get_finished
wenzelm@38848
   132
      val (node_edits, current) = change.result.get_finished
wenzelm@38366
   133
wenzelm@38848
   134
      var former_assignment = global_state.peek().the_assignment(previous).get_finished
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@38849
   192
                try {
wenzelm@38882
   193
                  val cmds: List[Command] = global_state.change_yield(_.assign(id, edits))
wenzelm@38882
   194
                  for (cmd <- cmds) command_change_buffer ! cmd
wenzelm@38849
   195
                  assignments.event(Session.Assignment)
wenzelm@38849
   196
                }
wenzelm@38414
   197
                catch { case _: Document.State.Fail => bad_result(result) }
wenzelm@38370
   198
              case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
wenzelm@38370
   199
              case List(Keyword.Keyword_Decl(name)) => syntax += name
wenzelm@38370
   200
              case _ => if (!result.is_ready) bad_result(result)
wenzelm@38370
   201
            }
wenzelm@38370
   202
          }
wenzelm@38370
   203
          else if (result.kind == Markup.EXIT) prover = null
wenzelm@38370
   204
          else if (result.is_raw) raw_output.event(result)
wenzelm@38370
   205
          else if (!result.is_system) bad_result(result)  // FIXME syslog for system messages (!?)
wenzelm@34809
   206
        }
wenzelm@34809
   207
    }
wenzelm@38221
   208
    //}}}
wenzelm@34809
   209
wenzelm@34820
   210
wenzelm@34820
   211
    /* prover startup */
wenzelm@34820
   212
wenzelm@34820
   213
    def startup_error(): String =
wenzelm@34820
   214
    {
wenzelm@34820
   215
      val buf = new StringBuilder
wenzelm@34820
   216
      while (
wenzelm@34820
   217
        receiveWithin(0) {
wenzelm@34820
   218
          case result: Isabelle_Process.Result =>
wenzelm@34820
   219
            if (result.is_raw) {
wenzelm@34820
   220
              for (text <- XML.content(result.message))
wenzelm@34820
   221
                buf.append(text)
wenzelm@34820
   222
            }
wenzelm@34820
   223
            true
wenzelm@34820
   224
          case TIMEOUT => false
wenzelm@34820
   225
        }) {}
wenzelm@34820
   226
      buf.toString
wenzelm@34820
   227
    }
wenzelm@34820
   228
wenzelm@34820
   229
    def prover_startup(timeout: Int): Option[String] =
wenzelm@34820
   230
    {
wenzelm@34820
   231
      receiveWithin(timeout) {
wenzelm@34820
   232
        case result: Isabelle_Process.Result
wenzelm@37689
   233
          if result.kind == Markup.INIT =>
wenzelm@34820
   234
          while (receive {
wenzelm@34820
   235
            case result: Isabelle_Process.Result =>
wenzelm@34820
   236
              handle_result(result); !result.is_ready
wenzelm@34820
   237
            }) {}
wenzelm@34820
   238
          None
wenzelm@34820
   239
wenzelm@34820
   240
        case result: Isabelle_Process.Result
wenzelm@37689
   241
          if result.kind == Markup.EXIT =>
wenzelm@34820
   242
          Some(startup_error())
wenzelm@34820
   243
wenzelm@34820
   244
        case TIMEOUT =>  // FIXME clarify
wenzelm@34820
   245
          prover.kill; Some(startup_error())
wenzelm@34820
   246
      }
wenzelm@34820
   247
    }
wenzelm@34809
   248
wenzelm@34809
   249
wenzelm@34809
   250
    /* main loop */
wenzelm@34809
   251
wenzelm@38639
   252
    var finished = false
wenzelm@38639
   253
    while (!finished) {
wenzelm@38639
   254
      receive {
wenzelm@38841
   255
        case Edit_Version(edits) =>
wenzelm@38841
   256
          val previous = global_state.peek().history.tip.current
wenzelm@38841
   257
          val result = Future.fork { Thy_Syntax.text_edits(Session.this, previous.join, edits) }
wenzelm@38841
   258
          val change = global_state.change_yield(_.extend_history(previous, edits, result))
wenzelm@38849
   259
wenzelm@38849
   260
          val this_actor = self
wenzelm@38849
   261
          change.current.map(_ => {
wenzelm@38849
   262
            assignments.await { global_state.peek().is_assigned(previous.get_finished) }
wenzelm@38849
   263
            this_actor ! change })
wenzelm@38849
   264
wenzelm@38841
   265
          reply(())
wenzelm@38841
   266
wenzelm@38841
   267
        case change: Document.Change if prover != null =>
wenzelm@38841
   268
          handle_change(change)
wenzelm@38841
   269
wenzelm@38841
   270
        case result: Isabelle_Process.Result =>
wenzelm@38841
   271
          handle_result(result)
wenzelm@38841
   272
wenzelm@38221
   273
        case Started(timeout, args) =>
wenzelm@34777
   274
          if (prover == null) {
wenzelm@34851
   275
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34820
   276
            val origin = sender
wenzelm@34820
   277
            val opt_err = prover_startup(timeout)
wenzelm@34820
   278
            if (opt_err.isDefined) prover = null
wenzelm@34820
   279
            origin ! opt_err
wenzelm@34820
   280
          }
wenzelm@34820
   281
          else reply(None)
wenzelm@34820
   282
wenzelm@38639
   283
        case Stop => // FIXME synchronous!?
wenzelm@34820
   284
          if (prover != null) {
wenzelm@34820
   285
            prover.kill
wenzelm@34820
   286
            prover = null
wenzelm@38639
   287
            finished = true
wenzelm@34777
   288
          }
wenzelm@34777
   289
wenzelm@38850
   290
        case TIMEOUT =>  // FIXME clarify
wenzelm@38850
   291
wenzelm@34820
   292
        case bad if prover != null =>
wenzelm@34777
   293
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   294
      }
wenzelm@34777
   295
    }
wenzelm@34777
   296
  }
wenzelm@34777
   297
wenzelm@34809
   298
wenzelm@37129
   299
wenzelm@38226
   300
  /** main methods **/
wenzelm@34809
   301
wenzelm@38221
   302
  def started(timeout: Int, args: List[String]): Option[String] =
wenzelm@38221
   303
    (session_actor !? Started(timeout, args)).asInstanceOf[Option[String]]
wenzelm@34820
   304
wenzelm@38841
   305
  def stop() { command_change_buffer ! Stop; session_actor ! Stop }
wenzelm@38841
   306
wenzelm@38841
   307
  def edit_version(edits: List[Document.Node_Text_Edit]) { session_actor !? Edit_Version(edits) }
wenzelm@38221
   308
wenzelm@38841
   309
  def snapshot(name: String, pending_edits: List[Text.Edit]): Document.Snapshot =
wenzelm@38841
   310
    global_state.peek().snapshot(name, pending_edits)
wenzelm@34777
   311
}