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