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