src/Pure/System/session.scala
author wenzelm
Sat Aug 07 17:24:46 2010 +0200 (2010-08-07)
changeset 38226 9d8848d70b0a
parent 38222 dac5fa0ac971
child 38227 6bbb42843b6e
permissions -rw-r--r--
maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
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@34813
    22
wenzelm@34813
    23
wenzelm@34813
    24
  /* managed entities */
wenzelm@34813
    25
wenzelm@34813
    26
  type Entity_ID = String
wenzelm@34813
    27
wenzelm@34813
    28
  trait Entity
wenzelm@34813
    29
  {
wenzelm@34813
    30
    val id: Entity_ID
wenzelm@37129
    31
    def consume(message: XML.Tree, forward: Command => Unit): Unit
wenzelm@34813
    32
  }
wenzelm@34791
    33
}
wenzelm@34791
    34
wenzelm@34777
    35
wenzelm@34851
    36
class Session(system: Isabelle_System)
wenzelm@34777
    37
{
wenzelm@37849
    38
  /* real time parameters */  // FIXME properties or settings (!?)
wenzelm@37849
    39
wenzelm@37849
    40
  // user input (e.g. text edits, cursor movement)
wenzelm@37849
    41
  val input_delay = 300
wenzelm@37849
    42
wenzelm@37849
    43
  // prover output (markup, common messages)
wenzelm@37849
    44
  val output_delay = 100
wenzelm@37849
    45
wenzelm@37849
    46
  // GUI layout updates
wenzelm@37849
    47
  val update_delay = 500
wenzelm@37849
    48
wenzelm@37849
    49
wenzelm@34809
    50
  /* pervasive event buses */
wenzelm@34809
    51
wenzelm@34809
    52
  val global_settings = new Event_Bus[Session.Global_Settings.type]
wenzelm@34809
    53
  val raw_results = new Event_Bus[Isabelle_Process.Result]
wenzelm@37065
    54
  val raw_output = new Event_Bus[Isabelle_Process.Result]
wenzelm@37129
    55
  val commands_changed = new Event_Bus[Command_Set]
wenzelm@37849
    56
  val perspective = new Event_Bus[Session.Perspective.type]
wenzelm@34809
    57
wenzelm@34809
    58
wenzelm@34778
    59
  /* unique ids */
wenzelm@34778
    60
wenzelm@34778
    61
  private var id_count: BigInt = 0
wenzelm@34813
    62
  def create_id(): Session.Entity_ID = synchronized { id_count += 1; "j" + id_count }
wenzelm@34778
    63
wenzelm@34778
    64
wenzelm@34826
    65
wenzelm@34826
    66
  /** main actor **/
wenzelm@34809
    67
wenzelm@34851
    68
  @volatile private var syntax = new Outer_Syntax(system.symbols)
wenzelm@34819
    69
  def current_syntax: Outer_Syntax = syntax
wenzelm@34809
    70
wenzelm@34816
    71
  @volatile private var entities = Map[Session.Entity_ID, Session.Entity]()
wenzelm@34816
    72
  def lookup_entity(id: Session.Entity_ID): Option[Session.Entity] = entities.get(id)
wenzelm@34835
    73
  def lookup_command(id: Session.Entity_ID): Option[Command] =
wenzelm@34835
    74
    lookup_entity(id) match {
wenzelm@34835
    75
      case Some(cmd: Command) => Some(cmd)
wenzelm@34835
    76
      case _ => None
wenzelm@34835
    77
    }
wenzelm@34816
    78
wenzelm@38221
    79
  private case class Started(timeout: Int, args: List[String])
wenzelm@34777
    80
  private case object Stop
wenzelm@34777
    81
wenzelm@37129
    82
  private lazy val session_actor = actor {
wenzelm@34809
    83
wenzelm@34809
    84
    var prover: Isabelle_Process with Isar_Document = null
wenzelm@34809
    85
wenzelm@34816
    86
    def register(entity: Session.Entity) { entities += (entity.id -> entity) }
wenzelm@34816
    87
wenzelm@38150
    88
    var documents = Map[Document.Version_ID, Document]()
wenzelm@34835
    89
    def register_document(doc: Document) { documents += (doc.id -> doc) }
wenzelm@38150
    90
    register_document(Document.init)
wenzelm@34835
    91
wenzelm@34809
    92
wenzelm@34809
    93
    /* document changes */
wenzelm@34809
    94
wenzelm@34809
    95
    def handle_change(change: Change)
wenzelm@38221
    96
    //{{{
wenzelm@34809
    97
    {
wenzelm@34824
    98
      require(change.parent.isDefined)
wenzelm@34809
    99
wenzelm@38150
   100
      val (node_edits, doc) = change.result.join
wenzelm@38150
   101
      val id_edits =
wenzelm@38150
   102
        node_edits map {
wenzelm@38150
   103
          case (name, None) => (name, None)
wenzelm@38150
   104
          case (name, Some(cmd_edits)) =>
wenzelm@38150
   105
            val chs =
wenzelm@38150
   106
              cmd_edits map {
wenzelm@38150
   107
                case (c1, c2) =>
wenzelm@38150
   108
                  val id1 = c1.map(_.id)
wenzelm@38150
   109
                  val id2 =
wenzelm@38150
   110
                    c2 match {
wenzelm@38150
   111
                      case None => None
wenzelm@38150
   112
                      case Some(command) =>
wenzelm@38150
   113
                        if (!lookup_command(command.id).isDefined) {
wenzelm@38150
   114
                          register(command)
wenzelm@38150
   115
                          prover.define_command(command.id, system.symbols.encode(command.source))
wenzelm@38150
   116
                        }
wenzelm@38150
   117
                        Some(command.id)
wenzelm@38150
   118
                    }
wenzelm@38150
   119
                  (id1, id2)
wenzelm@38150
   120
              }
wenzelm@38150
   121
            (name -> Some(chs))
wenzelm@38150
   122
        }
wenzelm@34835
   123
      register_document(doc)
wenzelm@38150
   124
      prover.edit_document(change.parent.get.id, doc.id, id_edits)
wenzelm@34809
   125
    }
wenzelm@38221
   126
    //}}}
wenzelm@34809
   127
wenzelm@34809
   128
wenzelm@34809
   129
    /* prover results */
wenzelm@34809
   130
wenzelm@34836
   131
    def bad_result(result: Isabelle_Process.Result)
wenzelm@34836
   132
    {
wenzelm@37041
   133
      System.err.println("Ignoring prover result: " + result.message.toString)
wenzelm@34836
   134
    }
wenzelm@34836
   135
wenzelm@34809
   136
    def handle_result(result: Isabelle_Process.Result)
wenzelm@38221
   137
    //{{{
wenzelm@34809
   138
    {
wenzelm@34809
   139
      raw_results.event(result)
wenzelm@34799
   140
wenzelm@37689
   141
      val target_id: Option[Session.Entity_ID] = Position.get_id(result.message.attributes)
wenzelm@34813
   142
      val target: Option[Session.Entity] =
wenzelm@34835
   143
        target_id match {
wenzelm@34809
   144
          case None => None
wenzelm@34835
   145
          case Some(id) => lookup_entity(id)
wenzelm@34809
   146
        }
wenzelm@37129
   147
      if (target.isDefined) target.get.consume(result.message, indicate_command_change)
wenzelm@37689
   148
      else if (result.is_status) {
wenzelm@34818
   149
        // global status message
wenzelm@34836
   150
        result.body match {
wenzelm@34835
   151
wenzelm@35013
   152
          // document state assignment
wenzelm@36682
   153
          case List(Isar_Document.Assign(edits)) if target_id.isDefined =>
wenzelm@34836
   154
            documents.get(target_id.get) match {
wenzelm@34836
   155
              case Some(doc) =>
wenzelm@34836
   156
                val states =
wenzelm@34836
   157
                  for {
wenzelm@36682
   158
                    Isar_Document.Edit(cmd_id, state_id) <- edits
wenzelm@34836
   159
                    cmd <- lookup_command(cmd_id)
wenzelm@34836
   160
                  } yield {
wenzelm@34836
   161
                    val st = cmd.assign_state(state_id)
wenzelm@34836
   162
                    register(st)
wenzelm@34836
   163
                    (cmd, st)
wenzelm@34836
   164
                  }
wenzelm@34836
   165
                doc.assign_states(states)
wenzelm@34836
   166
              case None => bad_result(result)
wenzelm@34836
   167
            }
wenzelm@34809
   168
wenzelm@36682
   169
          // keyword declarations
wenzelm@36947
   170
          case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
wenzelm@36947
   171
          case List(Keyword.Keyword_Decl(name)) => syntax += name
wenzelm@34836
   172
wenzelm@34839
   173
          case _ => if (!result.is_ready) bad_result(result)
wenzelm@34809
   174
        }
wenzelm@34809
   175
      }
wenzelm@37689
   176
      else if (result.kind == Markup.EXIT)
wenzelm@34809
   177
        prover = null
wenzelm@37065
   178
      else if (result.is_raw)
wenzelm@37065
   179
        raw_output.event(result)
wenzelm@37063
   180
      else if (!result.is_system)   // FIXME syslog (!?)
wenzelm@34837
   181
        bad_result(result)
wenzelm@34809
   182
    }
wenzelm@38221
   183
    //}}}
wenzelm@34809
   184
wenzelm@34820
   185
wenzelm@34820
   186
    /* prover startup */
wenzelm@34820
   187
wenzelm@34820
   188
    def startup_error(): String =
wenzelm@34820
   189
    {
wenzelm@34820
   190
      val buf = new StringBuilder
wenzelm@34820
   191
      while (
wenzelm@34820
   192
        receiveWithin(0) {
wenzelm@34820
   193
          case result: Isabelle_Process.Result =>
wenzelm@34820
   194
            if (result.is_raw) {
wenzelm@34820
   195
              for (text <- XML.content(result.message))
wenzelm@34820
   196
                buf.append(text)
wenzelm@34820
   197
            }
wenzelm@34820
   198
            true
wenzelm@34820
   199
          case TIMEOUT => false
wenzelm@34820
   200
        }) {}
wenzelm@34820
   201
      buf.toString
wenzelm@34820
   202
    }
wenzelm@34820
   203
wenzelm@34820
   204
    def prover_startup(timeout: Int): Option[String] =
wenzelm@34820
   205
    {
wenzelm@34820
   206
      receiveWithin(timeout) {
wenzelm@34820
   207
        case result: Isabelle_Process.Result
wenzelm@37689
   208
          if result.kind == Markup.INIT =>
wenzelm@34820
   209
          while (receive {
wenzelm@34820
   210
            case result: Isabelle_Process.Result =>
wenzelm@34820
   211
              handle_result(result); !result.is_ready
wenzelm@34820
   212
            }) {}
wenzelm@34820
   213
          None
wenzelm@34820
   214
wenzelm@34820
   215
        case result: Isabelle_Process.Result
wenzelm@37689
   216
          if result.kind == Markup.EXIT =>
wenzelm@34820
   217
          Some(startup_error())
wenzelm@34820
   218
wenzelm@34820
   219
        case TIMEOUT =>  // FIXME clarify
wenzelm@34820
   220
          prover.kill; Some(startup_error())
wenzelm@34820
   221
      }
wenzelm@34820
   222
    }
wenzelm@34809
   223
wenzelm@34809
   224
wenzelm@34809
   225
    /* main loop */
wenzelm@34809
   226
wenzelm@34820
   227
    val xml_cache = new XML.Cache(131071)
wenzelm@34820
   228
wenzelm@34777
   229
    loop {
wenzelm@34777
   230
      react {
wenzelm@38221
   231
        case Started(timeout, args) =>
wenzelm@34777
   232
          if (prover == null) {
wenzelm@34851
   233
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34820
   234
            val origin = sender
wenzelm@34820
   235
            val opt_err = prover_startup(timeout)
wenzelm@34820
   236
            if (opt_err.isDefined) prover = null
wenzelm@34820
   237
            origin ! opt_err
wenzelm@34820
   238
          }
wenzelm@34820
   239
          else reply(None)
wenzelm@34820
   240
wenzelm@34820
   241
        case Stop =>  // FIXME clarify; synchronous
wenzelm@34820
   242
          if (prover != null) {
wenzelm@34820
   243
            prover.kill
wenzelm@34820
   244
            prover = null
wenzelm@34777
   245
          }
wenzelm@34777
   246
wenzelm@34820
   247
        case change: Change if prover != null =>
wenzelm@34777
   248
          handle_change(change)
wenzelm@34777
   249
wenzelm@34777
   250
        case result: Isabelle_Process.Result =>
wenzelm@34795
   251
          handle_result(result.cache(xml_cache))
wenzelm@34777
   252
wenzelm@36782
   253
        case TIMEOUT =>  // FIXME clarify!
wenzelm@36782
   254
wenzelm@34820
   255
        case bad if prover != null =>
wenzelm@34777
   256
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   257
      }
wenzelm@34777
   258
    }
wenzelm@34777
   259
  }
wenzelm@34777
   260
wenzelm@34809
   261
wenzelm@37129
   262
wenzelm@38226
   263
  /** buffered command changes (delay_first discipline) **/
wenzelm@37129
   264
wenzelm@38221
   265
  private lazy val command_change_buffer = actor
wenzelm@38221
   266
  //{{{
wenzelm@38221
   267
  {
wenzelm@37129
   268
    import scala.compat.Platform.currentTime
wenzelm@37129
   269
wenzelm@37129
   270
    var changed: Set[Command] = Set()
wenzelm@37129
   271
    var flush_time: Option[Long] = None
wenzelm@37129
   272
wenzelm@37129
   273
    def flush_timeout: Long =
wenzelm@37129
   274
      flush_time match {
wenzelm@37129
   275
        case None => 5000L
wenzelm@37129
   276
        case Some(time) => (time - currentTime) max 0
wenzelm@37129
   277
      }
wenzelm@37129
   278
wenzelm@37129
   279
    def flush()
wenzelm@37129
   280
    {
wenzelm@37129
   281
      if (!changed.isEmpty) commands_changed.event(Command_Set(changed))
wenzelm@37129
   282
      changed = Set()
wenzelm@37129
   283
      flush_time = None
wenzelm@37129
   284
    }
wenzelm@37129
   285
wenzelm@37129
   286
    def invoke()
wenzelm@37129
   287
    {
wenzelm@37129
   288
      val now = currentTime
wenzelm@37129
   289
      flush_time match {
wenzelm@37849
   290
        case None => flush_time = Some(now + output_delay)
wenzelm@37129
   291
        case Some(time) => if (now >= time) flush()
wenzelm@37129
   292
      }
wenzelm@37129
   293
    }
wenzelm@37129
   294
wenzelm@37129
   295
    loop {
wenzelm@37129
   296
      reactWithin(flush_timeout) {
wenzelm@37129
   297
        case command: Command => changed += command; invoke()
wenzelm@37129
   298
        case TIMEOUT => flush()
wenzelm@37129
   299
        case bad => System.err.println("command_change_buffer: ignoring bad message " + bad)
wenzelm@37129
   300
      }
wenzelm@37129
   301
    }
wenzelm@37129
   302
  }
wenzelm@38221
   303
  //}}}
wenzelm@37129
   304
wenzelm@37129
   305
  def indicate_command_change(command: Command)
wenzelm@37129
   306
  {
wenzelm@37129
   307
    command_change_buffer ! command
wenzelm@37129
   308
  }
wenzelm@37129
   309
wenzelm@37129
   310
wenzelm@38226
   311
wenzelm@38226
   312
  /** editor history **/
wenzelm@38226
   313
wenzelm@38226
   314
  private case class Edit_Document(edits: List[Document.Node_Text_Edit])
wenzelm@38226
   315
wenzelm@38226
   316
  private val editor_history = new Actor
wenzelm@38226
   317
  {
wenzelm@38226
   318
    @volatile private var history = Change.init
wenzelm@38226
   319
    def current_change(): Change = history
wenzelm@38226
   320
wenzelm@38226
   321
    def act
wenzelm@38226
   322
    {
wenzelm@38226
   323
      loop {
wenzelm@38226
   324
        react {
wenzelm@38226
   325
          case Edit_Document(edits) =>
wenzelm@38226
   326
            val old_change = history
wenzelm@38226
   327
            val new_id = create_id()
wenzelm@38226
   328
            val result: isabelle.Future[(List[Document.Edit[Command]], Document)] =
wenzelm@38226
   329
              isabelle.Future.fork {
wenzelm@38226
   330
                val old_doc = old_change.join_document
wenzelm@38226
   331
                old_doc.await_assignment
wenzelm@38226
   332
                Document.text_edits(Session.this, old_doc, new_id, edits)
wenzelm@38226
   333
              }
wenzelm@38226
   334
            val new_change = new Change(new_id, Some(old_change), edits, result)
wenzelm@38226
   335
            history = new_change
wenzelm@38226
   336
            new_change.result.map(_ => session_actor ! new_change)
wenzelm@38226
   337
wenzelm@38226
   338
          case bad => System.err.println("editor_model: ignoring bad message " + bad)
wenzelm@38226
   339
        }
wenzelm@38226
   340
      }
wenzelm@38226
   341
    }
wenzelm@38226
   342
  }
wenzelm@38226
   343
  editor_history.start
wenzelm@38226
   344
wenzelm@38226
   345
wenzelm@38226
   346
wenzelm@38226
   347
  /** main methods **/
wenzelm@34809
   348
wenzelm@38221
   349
  def started(timeout: Int, args: List[String]): Option[String] =
wenzelm@38221
   350
    (session_actor !? Started(timeout, args)).asInstanceOf[Option[String]]
wenzelm@34820
   351
wenzelm@34777
   352
  def stop() { session_actor ! Stop }
wenzelm@38221
   353
wenzelm@38226
   354
  def current_change(): Change = editor_history.current_change()
wenzelm@38226
   355
  def edit_document(edits: List[Document.Node_Text_Edit]) { editor_history ! Edit_Document(edits) }
wenzelm@34777
   356
}