src/Tools/jEdit/src/proofdocument/session.scala
author wenzelm
Mon Jan 04 19:08:10 2010 +0100 (2010-01-04 ago)
changeset 34835 67733fd0e3fa
parent 34833 683ddf358698
child 34836 b83c7a738eb8
permissions -rw-r--r--
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
recent_document: require finished state assignment;
explicitly typed Session.lookup_command;
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@34809
    42
wenzelm@34809
    43
wenzelm@34778
    44
  /* unique ids */
wenzelm@34778
    45
wenzelm@34778
    46
  private var id_count: BigInt = 0
wenzelm@34813
    47
  def create_id(): Session.Entity_ID = synchronized { id_count += 1; "j" + id_count }
wenzelm@34778
    48
wenzelm@34778
    49
wenzelm@34826
    50
wenzelm@34826
    51
  /** main 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@34835
    58
  def lookup_command(id: Session.Entity_ID): Option[Command] =
wenzelm@34835
    59
    lookup_entity(id) match {
wenzelm@34835
    60
      case Some(cmd: Command) => Some(cmd)
wenzelm@34835
    61
      case _ => None
wenzelm@34835
    62
    }
wenzelm@34816
    63
wenzelm@34820
    64
  private case class Start(timeout: Int, args: List[String])
wenzelm@34777
    65
  private case object Stop
wenzelm@34808
    66
  private case class Begin_Document(path: String)
wenzelm@34777
    67
wenzelm@34809
    68
  private val session_actor = actor {
wenzelm@34809
    69
wenzelm@34809
    70
    var prover: Isabelle_Process with Isar_Document = null
wenzelm@34809
    71
wenzelm@34816
    72
    def register(entity: Session.Entity) { entities += (entity.id -> entity) }
wenzelm@34816
    73
wenzelm@34835
    74
    var documents = Map[Isar_Document.Document_ID, Document]()
wenzelm@34835
    75
    def register_document(doc: Document) { documents += (doc.id -> doc) }
wenzelm@34835
    76
wenzelm@34809
    77
wenzelm@34809
    78
    /* document changes */
wenzelm@34809
    79
wenzelm@34809
    80
    def handle_change(change: Change)
wenzelm@34809
    81
    {
wenzelm@34824
    82
      require(change.parent.isDefined)
wenzelm@34809
    83
wenzelm@34825
    84
      val (doc, changes) = change.result.join
wenzelm@34809
    85
      val id_changes = changes map {
wenzelm@34809
    86
        case (c1, c2) =>
wenzelm@34809
    87
          (c1.map(_.id).getOrElse(""),
wenzelm@34809
    88
           c2 match {
wenzelm@34809
    89
              case None => None
wenzelm@34835
    90
              case Some(command) =>
wenzelm@34835
    91
                if (!lookup_command(command.id).isDefined) {
wenzelm@34835
    92
                  register(command)
wenzelm@34835
    93
                  prover.define_command(command.id, system.symbols.encode(command.content))
wenzelm@34835
    94
                }
wenzelm@34809
    95
                Some(command.id)
wenzelm@34809
    96
            })
wenzelm@34809
    97
      }
wenzelm@34835
    98
      register_document(doc)
wenzelm@34833
    99
      prover.edit_document(change.parent.get.id, doc.id, id_changes)
wenzelm@34809
   100
    }
wenzelm@34809
   101
wenzelm@34809
   102
wenzelm@34809
   103
    /* prover results */
wenzelm@34809
   104
wenzelm@34809
   105
    def handle_result(result: Isabelle_Process.Result)
wenzelm@34809
   106
    {
wenzelm@34809
   107
      raw_results.event(result)
wenzelm@34799
   108
wenzelm@34835
   109
      val target_id: Option[Session.Entity_ID] = Position.get_id(result.props)
wenzelm@34813
   110
      val target: Option[Session.Entity] =
wenzelm@34835
   111
        target_id match {
wenzelm@34809
   112
          case None => None
wenzelm@34835
   113
          case Some(id) => lookup_entity(id)
wenzelm@34809
   114
        }
wenzelm@34813
   115
      if (target.isDefined) target.get.consume(this, result.message)
wenzelm@34809
   116
      else if (result.kind == Isabelle_Process.Kind.STATUS) {
wenzelm@34818
   117
        // global status message
wenzelm@34809
   118
        for (elem <- result.body) {
wenzelm@34809
   119
          elem match {
wenzelm@34835
   120
            // document state assigment
wenzelm@34835
   121
            case XML.Elem(Markup.ASSIGN, _, edits) if target_id.isDefined =>
wenzelm@34835
   122
              documents.get(target_id.get) match {
wenzelm@34835
   123
                case Some(doc) =>
wenzelm@34835
   124
                  val states =
wenzelm@34835
   125
                    for {
wenzelm@34835
   126
                      XML.Elem(Markup.EDIT, (Markup.ID, cmd_id) :: (Markup.STATE, state_id) :: _, _)
wenzelm@34835
   127
                        <- edits
wenzelm@34835
   128
                      cmd <- lookup_command(cmd_id)
wenzelm@34835
   129
                    } yield {
wenzelm@34835
   130
                      val st = cmd.assign_state(state_id)
wenzelm@34835
   131
                      register(st)
wenzelm@34835
   132
                      (cmd, st)
wenzelm@34835
   133
                    }
wenzelm@34835
   134
                  doc.assign_states(states)
wenzelm@34835
   135
                case None =>
wenzelm@34835
   136
              }
wenzelm@34835
   137
wenzelm@34809
   138
            // command and keyword declarations
wenzelm@34809
   139
            case XML.Elem(Markup.COMMAND_DECL, (Markup.NAME, name) :: (Markup.KIND, kind) :: _, _) =>
wenzelm@34819
   140
              syntax += (name, kind)
wenzelm@34809
   141
            case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) =>
wenzelm@34819
   142
              syntax += name
wenzelm@34809
   143
wenzelm@34820
   144
            case _ =>
wenzelm@34809
   145
          }
wenzelm@34809
   146
        }
wenzelm@34809
   147
      }
wenzelm@34809
   148
      else if (result.kind == Isabelle_Process.Kind.EXIT)
wenzelm@34809
   149
        prover = null
wenzelm@34809
   150
    }
wenzelm@34809
   151
wenzelm@34820
   152
wenzelm@34820
   153
    /* prover startup */
wenzelm@34820
   154
wenzelm@34820
   155
    def startup_error(): String =
wenzelm@34820
   156
    {
wenzelm@34820
   157
      val buf = new StringBuilder
wenzelm@34820
   158
      while (
wenzelm@34820
   159
        receiveWithin(0) {
wenzelm@34820
   160
          case result: Isabelle_Process.Result =>
wenzelm@34820
   161
            if (result.is_raw) {
wenzelm@34820
   162
              for (text <- XML.content(result.message))
wenzelm@34820
   163
                buf.append(text)
wenzelm@34820
   164
            }
wenzelm@34820
   165
            true
wenzelm@34820
   166
          case TIMEOUT => false
wenzelm@34820
   167
        }) {}
wenzelm@34820
   168
      buf.toString
wenzelm@34820
   169
    }
wenzelm@34820
   170
wenzelm@34820
   171
    def prover_startup(timeout: Int): Option[String] =
wenzelm@34820
   172
    {
wenzelm@34820
   173
      receiveWithin(timeout) {
wenzelm@34820
   174
        case result: Isabelle_Process.Result
wenzelm@34820
   175
          if result.kind == Isabelle_Process.Kind.INIT =>
wenzelm@34820
   176
          while (receive {
wenzelm@34820
   177
            case result: Isabelle_Process.Result =>
wenzelm@34820
   178
              handle_result(result); !result.is_ready
wenzelm@34820
   179
            }) {}
wenzelm@34820
   180
          None
wenzelm@34820
   181
wenzelm@34820
   182
        case result: Isabelle_Process.Result
wenzelm@34820
   183
          if result.kind == Isabelle_Process.Kind.EXIT =>
wenzelm@34820
   184
          Some(startup_error())
wenzelm@34820
   185
wenzelm@34820
   186
        case TIMEOUT =>  // FIXME clarify
wenzelm@34820
   187
          prover.kill; Some(startup_error())
wenzelm@34820
   188
      }
wenzelm@34820
   189
    }
wenzelm@34809
   190
wenzelm@34809
   191
wenzelm@34809
   192
    /* main loop */
wenzelm@34809
   193
wenzelm@34820
   194
    val xml_cache = new XML.Cache(131071)
wenzelm@34820
   195
wenzelm@34777
   196
    loop {
wenzelm@34777
   197
      react {
wenzelm@34820
   198
        case Start(timeout, args) =>
wenzelm@34777
   199
          if (prover == null) {
wenzelm@34780
   200
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34820
   201
            val origin = sender
wenzelm@34820
   202
            val opt_err = prover_startup(timeout)
wenzelm@34820
   203
            if (opt_err.isDefined) prover = null
wenzelm@34820
   204
            origin ! opt_err
wenzelm@34820
   205
          }
wenzelm@34820
   206
          else reply(None)
wenzelm@34820
   207
wenzelm@34820
   208
        case Stop =>  // FIXME clarify; synchronous
wenzelm@34820
   209
          if (prover != null) {
wenzelm@34820
   210
            prover.kill
wenzelm@34820
   211
            prover = null
wenzelm@34777
   212
          }
wenzelm@34777
   213
wenzelm@34820
   214
        case Begin_Document(path: String) if prover != null =>
wenzelm@34808
   215
          val id = create_id()
wenzelm@34823
   216
          val doc = Document.empty(id)
wenzelm@34835
   217
          register_document(doc)
wenzelm@34808
   218
          prover.begin_document(id, path)
wenzelm@34808
   219
          reply(doc)
wenzelm@34809
   220
wenzelm@34820
   221
        case change: Change if prover != null =>
wenzelm@34777
   222
          handle_change(change)
wenzelm@34777
   223
wenzelm@34777
   224
        case result: Isabelle_Process.Result =>
wenzelm@34795
   225
          handle_result(result.cache(xml_cache))
wenzelm@34777
   226
wenzelm@34820
   227
        case bad if prover != null =>
wenzelm@34777
   228
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   229
      }
wenzelm@34777
   230
    }
wenzelm@34777
   231
  }
wenzelm@34777
   232
wenzelm@34809
   233
wenzelm@34809
   234
  /* main methods */
wenzelm@34809
   235
wenzelm@34820
   236
  def start(timeout: Int, args: List[String]): Option[String] =
wenzelm@34820
   237
    (session_actor !? Start(timeout, args)).asInstanceOf[Option[String]]
wenzelm@34820
   238
wenzelm@34777
   239
  def stop() { session_actor ! Stop }
wenzelm@34777
   240
  def input(change: Change) { session_actor ! change }
wenzelm@34777
   241
wenzelm@34823
   242
  def begin_document(path: String): Document =
wenzelm@34823
   243
    (session_actor !? Begin_Document(path)).asInstanceOf[Document]
wenzelm@34777
   244
}