src/Pure/System/session.scala
author wenzelm
Thu May 27 00:47:15 2010 +0200 (2010-05-27)
changeset 37129 4c83696b340e
parent 37065 2a73253b5898
child 37132 10ef4da1c314
permissions -rw-r--r--
Command.toString: include id for debugging;
Command.consume: explicit forward, avoid dependency on Session and side-effect on event bus;
State.+ without side-effect on event bus;
Session.commands_changed: delayed command changes (outside of Swing thread), also subsumes former Session.results;
Document_View: tuned commands_changed handling and caret listening;
Document_View.selected_command: proper function, not event handler state;
Output_Dockable: directly act upon commands_changed, not caret events (via former Session.results);
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@34813
    19
wenzelm@34813
    20
wenzelm@34813
    21
  /* managed entities */
wenzelm@34813
    22
wenzelm@34813
    23
  type Entity_ID = String
wenzelm@34813
    24
wenzelm@34813
    25
  trait Entity
wenzelm@34813
    26
  {
wenzelm@34813
    27
    val id: Entity_ID
wenzelm@37129
    28
    def consume(message: XML.Tree, forward: Command => Unit): Unit
wenzelm@34813
    29
  }
wenzelm@34791
    30
}
wenzelm@34791
    31
wenzelm@34777
    32
wenzelm@34851
    33
class Session(system: Isabelle_System)
wenzelm@34777
    34
{
wenzelm@34809
    35
  /* pervasive event buses */
wenzelm@34809
    36
wenzelm@34809
    37
  val global_settings = new Event_Bus[Session.Global_Settings.type]
wenzelm@34809
    38
  val raw_results = new Event_Bus[Isabelle_Process.Result]
wenzelm@37065
    39
  val raw_output = new Event_Bus[Isabelle_Process.Result]
wenzelm@37129
    40
  val commands_changed = new Event_Bus[Command_Set]
wenzelm@34809
    41
wenzelm@34809
    42
wenzelm@34778
    43
  /* unique ids */
wenzelm@34778
    44
wenzelm@34778
    45
  private var id_count: BigInt = 0
wenzelm@34813
    46
  def create_id(): Session.Entity_ID = synchronized { id_count += 1; "j" + id_count }
wenzelm@34778
    47
wenzelm@34778
    48
wenzelm@34826
    49
wenzelm@34826
    50
  /** main actor **/
wenzelm@34809
    51
wenzelm@34851
    52
  @volatile private var syntax = new Outer_Syntax(system.symbols)
wenzelm@34819
    53
  def current_syntax: Outer_Syntax = syntax
wenzelm@34809
    54
wenzelm@34816
    55
  @volatile private var entities = Map[Session.Entity_ID, Session.Entity]()
wenzelm@34816
    56
  def lookup_entity(id: Session.Entity_ID): Option[Session.Entity] = entities.get(id)
wenzelm@34835
    57
  def lookup_command(id: Session.Entity_ID): Option[Command] =
wenzelm@34835
    58
    lookup_entity(id) match {
wenzelm@34835
    59
      case Some(cmd: Command) => Some(cmd)
wenzelm@34835
    60
      case _ => None
wenzelm@34835
    61
    }
wenzelm@34816
    62
wenzelm@34820
    63
  private case class Start(timeout: Int, args: List[String])
wenzelm@34777
    64
  private case object Stop
wenzelm@34808
    65
  private case class Begin_Document(path: String)
wenzelm@34777
    66
wenzelm@37129
    67
  private lazy val session_actor = actor {
wenzelm@34809
    68
wenzelm@34809
    69
    var prover: Isabelle_Process with Isar_Document = null
wenzelm@34809
    70
wenzelm@34816
    71
    def register(entity: Session.Entity) { entities += (entity.id -> entity) }
wenzelm@34816
    72
wenzelm@34835
    73
    var documents = Map[Isar_Document.Document_ID, Document]()
wenzelm@34835
    74
    def register_document(doc: Document) { documents += (doc.id -> doc) }
wenzelm@34835
    75
wenzelm@34809
    76
wenzelm@34809
    77
    /* document changes */
wenzelm@34809
    78
wenzelm@34809
    79
    def handle_change(change: Change)
wenzelm@34809
    80
    {
wenzelm@34824
    81
      require(change.parent.isDefined)
wenzelm@34809
    82
wenzelm@34853
    83
      val (changes, doc) = change.result.join
wenzelm@34809
    84
      val id_changes = changes map {
wenzelm@34809
    85
        case (c1, c2) =>
wenzelm@34809
    86
          (c1.map(_.id).getOrElse(""),
wenzelm@34809
    87
           c2 match {
wenzelm@34809
    88
              case None => None
wenzelm@34835
    89
              case Some(command) =>
wenzelm@34835
    90
                if (!lookup_command(command.id).isDefined) {
wenzelm@34835
    91
                  register(command)
wenzelm@34859
    92
                  prover.define_command(command.id, system.symbols.encode(command.source))
wenzelm@34835
    93
                }
wenzelm@34809
    94
                Some(command.id)
wenzelm@34809
    95
            })
wenzelm@34809
    96
      }
wenzelm@34835
    97
      register_document(doc)
wenzelm@34833
    98
      prover.edit_document(change.parent.get.id, doc.id, id_changes)
wenzelm@34809
    99
    }
wenzelm@34809
   100
wenzelm@34809
   101
wenzelm@34809
   102
    /* prover results */
wenzelm@34809
   103
wenzelm@34836
   104
    def bad_result(result: Isabelle_Process.Result)
wenzelm@34836
   105
    {
wenzelm@37041
   106
      System.err.println("Ignoring prover result: " + result.message.toString)
wenzelm@34836
   107
    }
wenzelm@34836
   108
wenzelm@34809
   109
    def handle_result(result: Isabelle_Process.Result)
wenzelm@34809
   110
    {
wenzelm@34809
   111
      raw_results.event(result)
wenzelm@34799
   112
wenzelm@34835
   113
      val target_id: Option[Session.Entity_ID] = Position.get_id(result.props)
wenzelm@34813
   114
      val target: Option[Session.Entity] =
wenzelm@34835
   115
        target_id match {
wenzelm@34809
   116
          case None => None
wenzelm@34835
   117
          case Some(id) => lookup_entity(id)
wenzelm@34809
   118
        }
wenzelm@37129
   119
      if (target.isDefined) target.get.consume(result.message, indicate_command_change)
wenzelm@34809
   120
      else if (result.kind == Isabelle_Process.Kind.STATUS) {
wenzelm@34818
   121
        // global status message
wenzelm@34836
   122
        result.body match {
wenzelm@34835
   123
wenzelm@35013
   124
          // document state assignment
wenzelm@36682
   125
          case List(Isar_Document.Assign(edits)) if target_id.isDefined =>
wenzelm@34836
   126
            documents.get(target_id.get) match {
wenzelm@34836
   127
              case Some(doc) =>
wenzelm@34836
   128
                val states =
wenzelm@34836
   129
                  for {
wenzelm@36682
   130
                    Isar_Document.Edit(cmd_id, state_id) <- edits
wenzelm@34836
   131
                    cmd <- lookup_command(cmd_id)
wenzelm@34836
   132
                  } yield {
wenzelm@34836
   133
                    val st = cmd.assign_state(state_id)
wenzelm@34836
   134
                    register(st)
wenzelm@34836
   135
                    (cmd, st)
wenzelm@34836
   136
                  }
wenzelm@34836
   137
                doc.assign_states(states)
wenzelm@34836
   138
              case None => bad_result(result)
wenzelm@34836
   139
            }
wenzelm@34809
   140
wenzelm@36682
   141
          // keyword declarations
wenzelm@36947
   142
          case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
wenzelm@36947
   143
          case List(Keyword.Keyword_Decl(name)) => syntax += name
wenzelm@34836
   144
wenzelm@34839
   145
          case _ => if (!result.is_ready) bad_result(result)
wenzelm@34809
   146
        }
wenzelm@34809
   147
      }
wenzelm@34809
   148
      else if (result.kind == Isabelle_Process.Kind.EXIT)
wenzelm@34809
   149
        prover = null
wenzelm@37065
   150
      else if (result.is_raw)
wenzelm@37065
   151
        raw_output.event(result)
wenzelm@37063
   152
      else if (!result.is_system)   // FIXME syslog (!?)
wenzelm@34837
   153
        bad_result(result)
wenzelm@34809
   154
    }
wenzelm@34809
   155
wenzelm@34820
   156
wenzelm@34820
   157
    /* prover startup */
wenzelm@34820
   158
wenzelm@34820
   159
    def startup_error(): String =
wenzelm@34820
   160
    {
wenzelm@34820
   161
      val buf = new StringBuilder
wenzelm@34820
   162
      while (
wenzelm@34820
   163
        receiveWithin(0) {
wenzelm@34820
   164
          case result: Isabelle_Process.Result =>
wenzelm@34820
   165
            if (result.is_raw) {
wenzelm@34820
   166
              for (text <- XML.content(result.message))
wenzelm@34820
   167
                buf.append(text)
wenzelm@34820
   168
            }
wenzelm@34820
   169
            true
wenzelm@34820
   170
          case TIMEOUT => false
wenzelm@34820
   171
        }) {}
wenzelm@34820
   172
      buf.toString
wenzelm@34820
   173
    }
wenzelm@34820
   174
wenzelm@34820
   175
    def prover_startup(timeout: Int): Option[String] =
wenzelm@34820
   176
    {
wenzelm@34820
   177
      receiveWithin(timeout) {
wenzelm@34820
   178
        case result: Isabelle_Process.Result
wenzelm@34820
   179
          if result.kind == Isabelle_Process.Kind.INIT =>
wenzelm@34820
   180
          while (receive {
wenzelm@34820
   181
            case result: Isabelle_Process.Result =>
wenzelm@34820
   182
              handle_result(result); !result.is_ready
wenzelm@34820
   183
            }) {}
wenzelm@34820
   184
          None
wenzelm@34820
   185
wenzelm@34820
   186
        case result: Isabelle_Process.Result
wenzelm@34820
   187
          if result.kind == Isabelle_Process.Kind.EXIT =>
wenzelm@34820
   188
          Some(startup_error())
wenzelm@34820
   189
wenzelm@34820
   190
        case TIMEOUT =>  // FIXME clarify
wenzelm@34820
   191
          prover.kill; Some(startup_error())
wenzelm@34820
   192
      }
wenzelm@34820
   193
    }
wenzelm@34809
   194
wenzelm@34809
   195
wenzelm@34809
   196
    /* main loop */
wenzelm@34809
   197
wenzelm@34820
   198
    val xml_cache = new XML.Cache(131071)
wenzelm@34820
   199
wenzelm@34777
   200
    loop {
wenzelm@34777
   201
      react {
wenzelm@34820
   202
        case Start(timeout, args) =>
wenzelm@34777
   203
          if (prover == null) {
wenzelm@34851
   204
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34820
   205
            val origin = sender
wenzelm@34820
   206
            val opt_err = prover_startup(timeout)
wenzelm@34820
   207
            if (opt_err.isDefined) prover = null
wenzelm@34820
   208
            origin ! opt_err
wenzelm@34820
   209
          }
wenzelm@34820
   210
          else reply(None)
wenzelm@34820
   211
wenzelm@34820
   212
        case Stop =>  // FIXME clarify; synchronous
wenzelm@34820
   213
          if (prover != null) {
wenzelm@34820
   214
            prover.kill
wenzelm@34820
   215
            prover = null
wenzelm@34777
   216
          }
wenzelm@34777
   217
wenzelm@34820
   218
        case Begin_Document(path: String) if prover != null =>
wenzelm@34808
   219
          val id = create_id()
wenzelm@34823
   220
          val doc = Document.empty(id)
wenzelm@34835
   221
          register_document(doc)
wenzelm@34808
   222
          prover.begin_document(id, path)
wenzelm@34808
   223
          reply(doc)
wenzelm@34809
   224
wenzelm@34820
   225
        case change: Change if prover != null =>
wenzelm@34777
   226
          handle_change(change)
wenzelm@34777
   227
wenzelm@34777
   228
        case result: Isabelle_Process.Result =>
wenzelm@34795
   229
          handle_result(result.cache(xml_cache))
wenzelm@34777
   230
wenzelm@36782
   231
        case TIMEOUT =>  // FIXME clarify!
wenzelm@36782
   232
wenzelm@34820
   233
        case bad if prover != null =>
wenzelm@34777
   234
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   235
      }
wenzelm@34777
   236
    }
wenzelm@34777
   237
  }
wenzelm@34777
   238
wenzelm@34809
   239
wenzelm@37129
   240
wenzelm@37129
   241
  /** buffered command changes -- delay_first discipline **/
wenzelm@37129
   242
wenzelm@37129
   243
  private lazy val command_change_buffer = actor {
wenzelm@37129
   244
    import scala.compat.Platform.currentTime
wenzelm@37129
   245
wenzelm@37129
   246
    var changed: Set[Command] = Set()
wenzelm@37129
   247
    var flush_time: Option[Long] = None
wenzelm@37129
   248
wenzelm@37129
   249
    def flush_timeout: Long =
wenzelm@37129
   250
      flush_time match {
wenzelm@37129
   251
        case None => 5000L
wenzelm@37129
   252
        case Some(time) => (time - currentTime) max 0
wenzelm@37129
   253
      }
wenzelm@37129
   254
wenzelm@37129
   255
    def flush()
wenzelm@37129
   256
    {
wenzelm@37129
   257
      if (!changed.isEmpty) commands_changed.event(Command_Set(changed))
wenzelm@37129
   258
      changed = Set()
wenzelm@37129
   259
      flush_time = None
wenzelm@37129
   260
    }
wenzelm@37129
   261
wenzelm@37129
   262
    def invoke()
wenzelm@37129
   263
    {
wenzelm@37129
   264
      val now = currentTime
wenzelm@37129
   265
      flush_time match {
wenzelm@37129
   266
        case None => flush_time = Some(now + 100)
wenzelm@37129
   267
        case Some(time) => if (now >= time) flush()
wenzelm@37129
   268
      }
wenzelm@37129
   269
    }
wenzelm@37129
   270
wenzelm@37129
   271
    loop {
wenzelm@37129
   272
      reactWithin(flush_timeout) {
wenzelm@37129
   273
        case command: Command => changed += command; invoke()
wenzelm@37129
   274
        case TIMEOUT => flush()
wenzelm@37129
   275
        case bad => System.err.println("command_change_buffer: ignoring bad message " + bad)
wenzelm@37129
   276
      }
wenzelm@37129
   277
    }
wenzelm@37129
   278
  }
wenzelm@37129
   279
wenzelm@37129
   280
  def indicate_command_change(command: Command)
wenzelm@37129
   281
  {
wenzelm@37129
   282
    command_change_buffer ! command
wenzelm@37129
   283
  }
wenzelm@37129
   284
wenzelm@37129
   285
wenzelm@34809
   286
  /* main methods */
wenzelm@34809
   287
wenzelm@34820
   288
  def start(timeout: Int, args: List[String]): Option[String] =
wenzelm@34820
   289
    (session_actor !? Start(timeout, args)).asInstanceOf[Option[String]]
wenzelm@34820
   290
wenzelm@34777
   291
  def stop() { session_actor ! Stop }
wenzelm@34777
   292
  def input(change: Change) { session_actor ! change }
wenzelm@34777
   293
wenzelm@34823
   294
  def begin_document(path: String): Document =
wenzelm@34823
   295
    (session_actor !? Begin_Document(path)).asInstanceOf[Document]
wenzelm@34777
   296
}