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