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