src/Tools/jEdit/src/proofdocument/session.scala
author wenzelm
Fri Jan 01 21:37:37 2010 +0100 (2010-01-01)
changeset 34825 7f72547f9b12
parent 34824 ac35eee85f5c
child 34826 6b38fc0b3406
permissions -rw-r--r--
use isabelle.Future;
tuned;
wenzelm@34777
     1
/*
wenzelm@34777
     2
 * Isabelle session, potentially with running prover
wenzelm@34777
     3
 *
wenzelm@34777
     4
 * @author Makarius
wenzelm@34777
     5
 */
wenzelm@34777
     6
wenzelm@34777
     7
package isabelle.proofdocument
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@34813
    28
    def consume(session: Session, message: XML.Tree): Unit
wenzelm@34813
    29
  }
wenzelm@34791
    30
}
wenzelm@34791
    31
wenzelm@34777
    32
wenzelm@34777
    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@34809
    39
  val results = new Event_Bus[Command]
wenzelm@34809
    40
wenzelm@34809
    41
  val command_change = new Event_Bus[Command]
wenzelm@34823
    42
  val document_change = new Event_Bus[Document]
wenzelm@34809
    43
wenzelm@34809
    44
wenzelm@34778
    45
  /* unique ids */
wenzelm@34778
    46
wenzelm@34778
    47
  private var id_count: BigInt = 0
wenzelm@34813
    48
  def create_id(): Session.Entity_ID = synchronized { id_count += 1; "j" + id_count }
wenzelm@34778
    49
wenzelm@34778
    50
wenzelm@34810
    51
  /* document state information -- owned by session_actor */
wenzelm@34809
    52
wenzelm@34819
    53
  @volatile private var syntax = new Outer_Syntax(system.symbols)
wenzelm@34819
    54
  def current_syntax: Outer_Syntax = syntax
wenzelm@34809
    55
wenzelm@34816
    56
  @volatile private var entities = Map[Session.Entity_ID, Session.Entity]()
wenzelm@34816
    57
  def lookup_entity(id: Session.Entity_ID): Option[Session.Entity] = entities.get(id)
wenzelm@34816
    58
wenzelm@34818
    59
  // FIXME eliminate
wenzelm@34823
    60
  @volatile private var documents = Map[Isar_Document.Document_ID, Document]()
wenzelm@34823
    61
  def document(id: Isar_Document.Document_ID): Option[Document] = documents.get(id)
wenzelm@34809
    62
wenzelm@34809
    63
wenzelm@34818
    64
wenzelm@34809
    65
  /** main actor **/
wenzelm@34777
    66
wenzelm@34816
    67
  private case class Register(entity: Session.Entity)
wenzelm@34820
    68
  private case class Start(timeout: Int, args: List[String])
wenzelm@34777
    69
  private case object Stop
wenzelm@34808
    70
  private case class Begin_Document(path: String)
wenzelm@34777
    71
wenzelm@34809
    72
  private val session_actor = actor {
wenzelm@34809
    73
wenzelm@34809
    74
    var prover: Isabelle_Process with Isar_Document = null
wenzelm@34809
    75
wenzelm@34816
    76
    def register(entity: Session.Entity) { entities += (entity.id -> entity) }
wenzelm@34816
    77
wenzelm@34809
    78
wenzelm@34809
    79
    /* document changes */
wenzelm@34809
    80
wenzelm@34809
    81
    def handle_change(change: Change)
wenzelm@34809
    82
    {
wenzelm@34824
    83
      require(change.parent.isDefined)
wenzelm@34809
    84
wenzelm@34825
    85
      val (doc, changes) = change.result.join
wenzelm@34809
    86
      val id_changes = changes map {
wenzelm@34809
    87
        case (c1, c2) =>
wenzelm@34809
    88
          (c1.map(_.id).getOrElse(""),
wenzelm@34809
    89
           c2 match {
wenzelm@34809
    90
              case None => None
wenzelm@34809
    91
              case Some(command) =>  // FIXME clarify -- may reuse existing commands!??
wenzelm@34816
    92
                register(command)
wenzelm@34809
    93
                prover.define_command(command.id, system.symbols.encode(command.content))
wenzelm@34809
    94
                Some(command.id)
wenzelm@34809
    95
            })
wenzelm@34809
    96
      }
wenzelm@34818
    97
      register(doc)
wenzelm@34824
    98
      documents += (doc.id -> doc)  // FIXME remove
wenzelm@34824
    99
      prover.edit_document(change.parent.get.document.id, doc.id, id_changes)
wenzelm@34809
   100
wenzelm@34809
   101
      document_change.event(doc)
wenzelm@34809
   102
    }
wenzelm@34809
   103
wenzelm@34809
   104
wenzelm@34809
   105
    /* prover results */
wenzelm@34809
   106
wenzelm@34809
   107
    def handle_result(result: Isabelle_Process.Result)
wenzelm@34809
   108
    {
wenzelm@34809
   109
      raw_results.event(result)
wenzelm@34799
   110
wenzelm@34813
   111
      val target: Option[Session.Entity] =
wenzelm@34817
   112
        Position.get_id(result.props) match {
wenzelm@34809
   113
          case None => None
wenzelm@34816
   114
          case Some(id) => entities.get(id)
wenzelm@34809
   115
        }
wenzelm@34813
   116
      if (target.isDefined) target.get.consume(this, result.message)
wenzelm@34809
   117
      else if (result.kind == Isabelle_Process.Kind.STATUS) {
wenzelm@34818
   118
        // global status message
wenzelm@34809
   119
        for (elem <- result.body) {
wenzelm@34809
   120
          elem match {
wenzelm@34809
   121
            // command and keyword declarations
wenzelm@34809
   122
            case XML.Elem(Markup.COMMAND_DECL, (Markup.NAME, name) :: (Markup.KIND, kind) :: _, _) =>
wenzelm@34819
   123
              syntax += (name, kind)
wenzelm@34809
   124
            case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) =>
wenzelm@34819
   125
              syntax += name
wenzelm@34809
   126
wenzelm@34820
   127
            case _ =>
wenzelm@34809
   128
          }
wenzelm@34809
   129
        }
wenzelm@34809
   130
      }
wenzelm@34809
   131
      else if (result.kind == Isabelle_Process.Kind.EXIT)
wenzelm@34809
   132
        prover = null
wenzelm@34809
   133
    }
wenzelm@34809
   134
wenzelm@34820
   135
wenzelm@34820
   136
    /* prover startup */
wenzelm@34820
   137
wenzelm@34820
   138
    def startup_error(): String =
wenzelm@34820
   139
    {
wenzelm@34820
   140
      val buf = new StringBuilder
wenzelm@34820
   141
      while (
wenzelm@34820
   142
        receiveWithin(0) {
wenzelm@34820
   143
          case result: Isabelle_Process.Result =>
wenzelm@34820
   144
            if (result.is_raw) {
wenzelm@34820
   145
              for (text <- XML.content(result.message))
wenzelm@34820
   146
                buf.append(text)
wenzelm@34820
   147
            }
wenzelm@34820
   148
            true
wenzelm@34820
   149
          case TIMEOUT => false
wenzelm@34820
   150
        }) {}
wenzelm@34820
   151
      buf.toString
wenzelm@34820
   152
    }
wenzelm@34820
   153
wenzelm@34820
   154
    def prover_startup(timeout: Int): Option[String] =
wenzelm@34820
   155
    {
wenzelm@34820
   156
      receiveWithin(timeout) {
wenzelm@34820
   157
        case result: Isabelle_Process.Result
wenzelm@34820
   158
          if result.kind == Isabelle_Process.Kind.INIT =>
wenzelm@34820
   159
          while (receive {
wenzelm@34820
   160
            case result: Isabelle_Process.Result =>
wenzelm@34820
   161
              handle_result(result); !result.is_ready
wenzelm@34820
   162
            }) {}
wenzelm@34820
   163
          None
wenzelm@34820
   164
wenzelm@34820
   165
        case result: Isabelle_Process.Result
wenzelm@34820
   166
          if result.kind == Isabelle_Process.Kind.EXIT =>
wenzelm@34820
   167
          Some(startup_error())
wenzelm@34820
   168
wenzelm@34820
   169
        case TIMEOUT =>  // FIXME clarify
wenzelm@34820
   170
          prover.kill; Some(startup_error())
wenzelm@34820
   171
      }
wenzelm@34820
   172
    }
wenzelm@34809
   173
wenzelm@34809
   174
wenzelm@34809
   175
    /* main loop */
wenzelm@34809
   176
wenzelm@34820
   177
    val xml_cache = new XML.Cache(131071)
wenzelm@34820
   178
wenzelm@34777
   179
    loop {
wenzelm@34777
   180
      react {
wenzelm@34816
   181
        case Register(entity: Session.Entity) =>
wenzelm@34816
   182
          register(entity)
wenzelm@34816
   183
          reply(())
wenzelm@34816
   184
wenzelm@34820
   185
        case Start(timeout, args) =>
wenzelm@34777
   186
          if (prover == null) {
wenzelm@34780
   187
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34820
   188
            val origin = sender
wenzelm@34820
   189
            val opt_err = prover_startup(timeout)
wenzelm@34820
   190
            if (opt_err.isDefined) prover = null
wenzelm@34820
   191
            origin ! opt_err
wenzelm@34820
   192
          }
wenzelm@34820
   193
          else reply(None)
wenzelm@34820
   194
wenzelm@34820
   195
        case Stop =>  // FIXME clarify; synchronous
wenzelm@34820
   196
          if (prover != null) {
wenzelm@34820
   197
            prover.kill
wenzelm@34820
   198
            prover = null
wenzelm@34777
   199
          }
wenzelm@34777
   200
wenzelm@34820
   201
        case Begin_Document(path: String) if prover != null =>
wenzelm@34808
   202
          val id = create_id()
wenzelm@34823
   203
          val doc = Document.empty(id)
wenzelm@34818
   204
          register(doc)
wenzelm@34808
   205
          documents += (id -> doc)
wenzelm@34808
   206
          prover.begin_document(id, path)
wenzelm@34808
   207
          reply(doc)
wenzelm@34809
   208
wenzelm@34820
   209
        case change: Change if prover != null =>
wenzelm@34777
   210
          handle_change(change)
wenzelm@34777
   211
wenzelm@34777
   212
        case result: Isabelle_Process.Result =>
wenzelm@34795
   213
          handle_result(result.cache(xml_cache))
wenzelm@34777
   214
wenzelm@34820
   215
        case bad if prover != null =>
wenzelm@34777
   216
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   217
      }
wenzelm@34777
   218
    }
wenzelm@34777
   219
  }
wenzelm@34777
   220
wenzelm@34809
   221
wenzelm@34809
   222
  /* main methods */
wenzelm@34809
   223
wenzelm@34824
   224
  // FIXME private?
wenzelm@34816
   225
  def register_entity(entity: Session.Entity) { session_actor !? Register(entity) }
wenzelm@34816
   226
wenzelm@34820
   227
  def start(timeout: Int, args: List[String]): Option[String] =
wenzelm@34820
   228
    (session_actor !? Start(timeout, args)).asInstanceOf[Option[String]]
wenzelm@34820
   229
wenzelm@34777
   230
  def stop() { session_actor ! Stop }
wenzelm@34777
   231
  def input(change: Change) { session_actor ! change }
wenzelm@34777
   232
wenzelm@34823
   233
  def begin_document(path: String): Document =
wenzelm@34823
   234
    (session_actor !? Begin_Document(path)).asInstanceOf[Document]
wenzelm@34777
   235
}