src/Pure/System/session.scala
author wenzelm
Mon Jul 19 22:19:18 2010 +0200 (2010-07-19)
changeset 37849 4f9de312cc23
parent 37689 628eabe2213a
child 38150 67fc24df3721
permissions -rw-r--r--
Session: predefined real time parameters;
Document_View: delayed caret handling, for improved reactivity;
selected_command: proper_command_at ignores ignored commands;
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@34808
    79
  private case class Begin_Document(path: String)
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@34835
    87
    var documents = Map[Isar_Document.Document_ID, Document]()
wenzelm@34835
    88
    def register_document(doc: Document) { documents += (doc.id -> doc) }
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@34853
    97
      val (changes, doc) = change.result.join
wenzelm@34809
    98
      val id_changes = changes map {
wenzelm@34809
    99
        case (c1, c2) =>
wenzelm@34809
   100
          (c1.map(_.id).getOrElse(""),
wenzelm@34809
   101
           c2 match {
wenzelm@34809
   102
              case None => None
wenzelm@34835
   103
              case Some(command) =>
wenzelm@34835
   104
                if (!lookup_command(command.id).isDefined) {
wenzelm@34835
   105
                  register(command)
wenzelm@34859
   106
                  prover.define_command(command.id, system.symbols.encode(command.source))
wenzelm@34835
   107
                }
wenzelm@34809
   108
                Some(command.id)
wenzelm@34809
   109
            })
wenzelm@34809
   110
      }
wenzelm@34835
   111
      register_document(doc)
wenzelm@34833
   112
      prover.edit_document(change.parent.get.id, doc.id, id_changes)
wenzelm@34809
   113
    }
wenzelm@34809
   114
wenzelm@34809
   115
wenzelm@34809
   116
    /* prover results */
wenzelm@34809
   117
wenzelm@34836
   118
    def bad_result(result: Isabelle_Process.Result)
wenzelm@34836
   119
    {
wenzelm@37041
   120
      System.err.println("Ignoring prover result: " + result.message.toString)
wenzelm@34836
   121
    }
wenzelm@34836
   122
wenzelm@34809
   123
    def handle_result(result: Isabelle_Process.Result)
wenzelm@34809
   124
    {
wenzelm@34809
   125
      raw_results.event(result)
wenzelm@34799
   126
wenzelm@37689
   127
      val target_id: Option[Session.Entity_ID] = Position.get_id(result.message.attributes)
wenzelm@34813
   128
      val target: Option[Session.Entity] =
wenzelm@34835
   129
        target_id match {
wenzelm@34809
   130
          case None => None
wenzelm@34835
   131
          case Some(id) => lookup_entity(id)
wenzelm@34809
   132
        }
wenzelm@37129
   133
      if (target.isDefined) target.get.consume(result.message, indicate_command_change)
wenzelm@37689
   134
      else if (result.is_status) {
wenzelm@34818
   135
        // global status message
wenzelm@34836
   136
        result.body match {
wenzelm@34835
   137
wenzelm@35013
   138
          // document state assignment
wenzelm@36682
   139
          case List(Isar_Document.Assign(edits)) if target_id.isDefined =>
wenzelm@34836
   140
            documents.get(target_id.get) match {
wenzelm@34836
   141
              case Some(doc) =>
wenzelm@34836
   142
                val states =
wenzelm@34836
   143
                  for {
wenzelm@36682
   144
                    Isar_Document.Edit(cmd_id, state_id) <- edits
wenzelm@34836
   145
                    cmd <- lookup_command(cmd_id)
wenzelm@34836
   146
                  } yield {
wenzelm@34836
   147
                    val st = cmd.assign_state(state_id)
wenzelm@34836
   148
                    register(st)
wenzelm@34836
   149
                    (cmd, st)
wenzelm@34836
   150
                  }
wenzelm@34836
   151
                doc.assign_states(states)
wenzelm@34836
   152
              case None => bad_result(result)
wenzelm@34836
   153
            }
wenzelm@34809
   154
wenzelm@36682
   155
          // keyword declarations
wenzelm@36947
   156
          case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
wenzelm@36947
   157
          case List(Keyword.Keyword_Decl(name)) => syntax += name
wenzelm@34836
   158
wenzelm@34839
   159
          case _ => if (!result.is_ready) bad_result(result)
wenzelm@34809
   160
        }
wenzelm@34809
   161
      }
wenzelm@37689
   162
      else if (result.kind == Markup.EXIT)
wenzelm@34809
   163
        prover = null
wenzelm@37065
   164
      else if (result.is_raw)
wenzelm@37065
   165
        raw_output.event(result)
wenzelm@37063
   166
      else if (!result.is_system)   // FIXME syslog (!?)
wenzelm@34837
   167
        bad_result(result)
wenzelm@34809
   168
    }
wenzelm@34809
   169
wenzelm@34820
   170
wenzelm@34820
   171
    /* prover startup */
wenzelm@34820
   172
wenzelm@34820
   173
    def startup_error(): String =
wenzelm@34820
   174
    {
wenzelm@34820
   175
      val buf = new StringBuilder
wenzelm@34820
   176
      while (
wenzelm@34820
   177
        receiveWithin(0) {
wenzelm@34820
   178
          case result: Isabelle_Process.Result =>
wenzelm@34820
   179
            if (result.is_raw) {
wenzelm@34820
   180
              for (text <- XML.content(result.message))
wenzelm@34820
   181
                buf.append(text)
wenzelm@34820
   182
            }
wenzelm@34820
   183
            true
wenzelm@34820
   184
          case TIMEOUT => false
wenzelm@34820
   185
        }) {}
wenzelm@34820
   186
      buf.toString
wenzelm@34820
   187
    }
wenzelm@34820
   188
wenzelm@34820
   189
    def prover_startup(timeout: Int): Option[String] =
wenzelm@34820
   190
    {
wenzelm@34820
   191
      receiveWithin(timeout) {
wenzelm@34820
   192
        case result: Isabelle_Process.Result
wenzelm@37689
   193
          if result.kind == Markup.INIT =>
wenzelm@34820
   194
          while (receive {
wenzelm@34820
   195
            case result: Isabelle_Process.Result =>
wenzelm@34820
   196
              handle_result(result); !result.is_ready
wenzelm@34820
   197
            }) {}
wenzelm@34820
   198
          None
wenzelm@34820
   199
wenzelm@34820
   200
        case result: Isabelle_Process.Result
wenzelm@37689
   201
          if result.kind == Markup.EXIT =>
wenzelm@34820
   202
          Some(startup_error())
wenzelm@34820
   203
wenzelm@34820
   204
        case TIMEOUT =>  // FIXME clarify
wenzelm@34820
   205
          prover.kill; Some(startup_error())
wenzelm@34820
   206
      }
wenzelm@34820
   207
    }
wenzelm@34809
   208
wenzelm@34809
   209
wenzelm@34809
   210
    /* main loop */
wenzelm@34809
   211
wenzelm@34820
   212
    val xml_cache = new XML.Cache(131071)
wenzelm@34820
   213
wenzelm@34777
   214
    loop {
wenzelm@34777
   215
      react {
wenzelm@34820
   216
        case Start(timeout, args) =>
wenzelm@34777
   217
          if (prover == null) {
wenzelm@34851
   218
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34820
   219
            val origin = sender
wenzelm@34820
   220
            val opt_err = prover_startup(timeout)
wenzelm@34820
   221
            if (opt_err.isDefined) prover = null
wenzelm@34820
   222
            origin ! opt_err
wenzelm@34820
   223
          }
wenzelm@34820
   224
          else reply(None)
wenzelm@34820
   225
wenzelm@34820
   226
        case Stop =>  // FIXME clarify; synchronous
wenzelm@34820
   227
          if (prover != null) {
wenzelm@34820
   228
            prover.kill
wenzelm@34820
   229
            prover = null
wenzelm@34777
   230
          }
wenzelm@34777
   231
wenzelm@34820
   232
        case Begin_Document(path: String) if prover != null =>
wenzelm@34808
   233
          val id = create_id()
wenzelm@34823
   234
          val doc = Document.empty(id)
wenzelm@34835
   235
          register_document(doc)
wenzelm@34808
   236
          prover.begin_document(id, path)
wenzelm@34808
   237
          reply(doc)
wenzelm@34809
   238
wenzelm@34820
   239
        case change: Change if prover != null =>
wenzelm@34777
   240
          handle_change(change)
wenzelm@34777
   241
wenzelm@34777
   242
        case result: Isabelle_Process.Result =>
wenzelm@34795
   243
          handle_result(result.cache(xml_cache))
wenzelm@34777
   244
wenzelm@36782
   245
        case TIMEOUT =>  // FIXME clarify!
wenzelm@36782
   246
wenzelm@34820
   247
        case bad if prover != null =>
wenzelm@34777
   248
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   249
      }
wenzelm@34777
   250
    }
wenzelm@34777
   251
  }
wenzelm@34777
   252
wenzelm@34809
   253
wenzelm@37129
   254
wenzelm@37129
   255
  /** buffered command changes -- delay_first discipline **/
wenzelm@37129
   256
wenzelm@37129
   257
  private lazy val command_change_buffer = actor {
wenzelm@37129
   258
    import scala.compat.Platform.currentTime
wenzelm@37129
   259
wenzelm@37129
   260
    var changed: Set[Command] = Set()
wenzelm@37129
   261
    var flush_time: Option[Long] = None
wenzelm@37129
   262
wenzelm@37129
   263
    def flush_timeout: Long =
wenzelm@37129
   264
      flush_time match {
wenzelm@37129
   265
        case None => 5000L
wenzelm@37129
   266
        case Some(time) => (time - currentTime) max 0
wenzelm@37129
   267
      }
wenzelm@37129
   268
wenzelm@37129
   269
    def flush()
wenzelm@37129
   270
    {
wenzelm@37129
   271
      if (!changed.isEmpty) commands_changed.event(Command_Set(changed))
wenzelm@37129
   272
      changed = Set()
wenzelm@37129
   273
      flush_time = None
wenzelm@37129
   274
    }
wenzelm@37129
   275
wenzelm@37129
   276
    def invoke()
wenzelm@37129
   277
    {
wenzelm@37129
   278
      val now = currentTime
wenzelm@37129
   279
      flush_time match {
wenzelm@37849
   280
        case None => flush_time = Some(now + output_delay)
wenzelm@37129
   281
        case Some(time) => if (now >= time) flush()
wenzelm@37129
   282
      }
wenzelm@37129
   283
    }
wenzelm@37129
   284
wenzelm@37129
   285
    loop {
wenzelm@37129
   286
      reactWithin(flush_timeout) {
wenzelm@37129
   287
        case command: Command => changed += command; invoke()
wenzelm@37129
   288
        case TIMEOUT => flush()
wenzelm@37129
   289
        case bad => System.err.println("command_change_buffer: ignoring bad message " + bad)
wenzelm@37129
   290
      }
wenzelm@37129
   291
    }
wenzelm@37129
   292
  }
wenzelm@37129
   293
wenzelm@37129
   294
  def indicate_command_change(command: Command)
wenzelm@37129
   295
  {
wenzelm@37129
   296
    command_change_buffer ! command
wenzelm@37129
   297
  }
wenzelm@37129
   298
wenzelm@37129
   299
wenzelm@34809
   300
  /* main methods */
wenzelm@34809
   301
wenzelm@34820
   302
  def start(timeout: Int, args: List[String]): Option[String] =
wenzelm@34820
   303
    (session_actor !? Start(timeout, args)).asInstanceOf[Option[String]]
wenzelm@34820
   304
wenzelm@34777
   305
  def stop() { session_actor ! Stop }
wenzelm@34777
   306
  def input(change: Change) { session_actor ! change }
wenzelm@34777
   307
wenzelm@34823
   308
  def begin_document(path: String): Document =
wenzelm@34823
   309
    (session_actor !? Begin_Document(path)).asInstanceOf[Document]
wenzelm@34777
   310
}