src/Tools/jEdit/src/proofdocument/session.scala
author wenzelm
Wed Dec 30 21:34:33 2009 +0100 (2009-12-30 ago)
changeset 34818 7df68a8f0e3e
parent 34817 b4efd0ef2f3e
child 34819 86cb7f8e5a0d
permissions -rw-r--r--
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
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@34777
    10
import scala.actors.Actor._
wenzelm@34777
    11
wenzelm@34815
    12
wenzelm@34791
    13
object Session
wenzelm@34791
    14
{
wenzelm@34813
    15
  /* events */
wenzelm@34813
    16
wenzelm@34791
    17
  case object Global_Settings
wenzelm@34813
    18
wenzelm@34813
    19
wenzelm@34813
    20
  /* managed entities */
wenzelm@34813
    21
wenzelm@34813
    22
  type Entity_ID = String
wenzelm@34813
    23
wenzelm@34813
    24
  trait Entity
wenzelm@34813
    25
  {
wenzelm@34813
    26
    val id: Entity_ID
wenzelm@34813
    27
    def consume(session: Session, message: XML.Tree): Unit
wenzelm@34813
    28
  }
wenzelm@34791
    29
}
wenzelm@34791
    30
wenzelm@34777
    31
wenzelm@34777
    32
class Session(system: Isabelle_System)
wenzelm@34777
    33
{
wenzelm@34809
    34
  /* pervasive event buses */
wenzelm@34809
    35
wenzelm@34809
    36
  val global_settings = new Event_Bus[Session.Global_Settings.type]
wenzelm@34809
    37
  val raw_results = new Event_Bus[Isabelle_Process.Result]
wenzelm@34809
    38
  val results = new Event_Bus[Command]
wenzelm@34809
    39
wenzelm@34809
    40
  val command_change = new Event_Bus[Command]
wenzelm@34809
    41
  val document_change = new Event_Bus[Proof_Document]
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@34810
    50
  /* document state information -- owned by session_actor */
wenzelm@34809
    51
wenzelm@34809
    52
  @volatile private var outer_syntax = new Outer_Syntax(system.symbols)
wenzelm@34809
    53
  def syntax(): Outer_Syntax = outer_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@34816
    57
wenzelm@34818
    58
  // FIXME eliminate
wenzelm@34809
    59
  @volatile private var documents = Map[Isar_Document.Document_ID, Proof_Document]()
wenzelm@34809
    60
  def document(id: Isar_Document.Document_ID): Option[Proof_Document] = documents.get(id)
wenzelm@34809
    61
wenzelm@34809
    62
wenzelm@34818
    63
wenzelm@34809
    64
  /** main actor **/
wenzelm@34777
    65
wenzelm@34816
    66
  private case class Register(entity: Session.Entity)
wenzelm@34780
    67
  private case class Start(args: List[String])
wenzelm@34777
    68
  private case object Stop
wenzelm@34808
    69
  private case class Begin_Document(path: String)
wenzelm@34777
    70
wenzelm@34809
    71
  private val session_actor = actor {
wenzelm@34809
    72
wenzelm@34809
    73
    var prover: Isabelle_Process with Isar_Document = null
wenzelm@34809
    74
    var prover_ready = false
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@34809
    83
      val old = document(change.parent.get.id).get
wenzelm@34809
    84
      val (doc, changes) = old.text_changed(this, change)
wenzelm@34809
    85
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@34809
    98
      documents += (doc.id -> doc)
wenzelm@34809
    99
      prover.edit_document(old.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@34809
   123
              outer_syntax += (name, kind)
wenzelm@34809
   124
            case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) =>
wenzelm@34809
   125
              outer_syntax += name
wenzelm@34809
   126
wenzelm@34809
   127
            // process ready (after initialization)
wenzelm@34809
   128
            case XML.Elem(Markup.READY, _, _) => prover_ready = true
wenzelm@34809
   129
wenzelm@34809
   130
          case _ =>
wenzelm@34809
   131
          }
wenzelm@34809
   132
        }
wenzelm@34809
   133
      }
wenzelm@34809
   134
      else if (result.kind == Isabelle_Process.Kind.EXIT)
wenzelm@34809
   135
        prover = null
wenzelm@34809
   136
    }
wenzelm@34809
   137
wenzelm@34795
   138
    val xml_cache = new XML.Cache(131071)
wenzelm@34809
   139
wenzelm@34809
   140
wenzelm@34809
   141
    /* main loop */
wenzelm@34809
   142
wenzelm@34777
   143
    loop {
wenzelm@34777
   144
      react {
wenzelm@34816
   145
        case Register(entity: Session.Entity) =>
wenzelm@34816
   146
          register(entity)
wenzelm@34816
   147
          reply(())
wenzelm@34816
   148
wenzelm@34780
   149
        case Start(args) =>
wenzelm@34777
   150
          if (prover == null) {
wenzelm@34780
   151
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34777
   152
            reply(())
wenzelm@34777
   153
          }
wenzelm@34777
   154
wenzelm@34807
   155
        case Stop =>  // FIXME clarify
wenzelm@34777
   156
          if (prover != null)
wenzelm@34777
   157
            prover.kill
wenzelm@34778
   158
          prover_ready = false
wenzelm@34808
   159
wenzelm@34808
   160
        case Begin_Document(path: String) if prover_ready =>
wenzelm@34808
   161
          val id = create_id()
wenzelm@34808
   162
          val doc = Proof_Document.empty(id)
wenzelm@34818
   163
          register(doc)
wenzelm@34808
   164
          documents += (id -> doc)
wenzelm@34808
   165
          prover.begin_document(id, path)
wenzelm@34808
   166
          reply(doc)
wenzelm@34809
   167
wenzelm@34777
   168
        case change: Change if prover_ready =>
wenzelm@34777
   169
          handle_change(change)
wenzelm@34777
   170
wenzelm@34777
   171
        case result: Isabelle_Process.Result =>
wenzelm@34795
   172
          handle_result(result.cache(xml_cache))
wenzelm@34777
   173
wenzelm@34777
   174
        case bad if prover_ready =>
wenzelm@34777
   175
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   176
      }
wenzelm@34777
   177
    }
wenzelm@34777
   178
  }
wenzelm@34777
   179
wenzelm@34809
   180
wenzelm@34809
   181
  /* main methods */
wenzelm@34809
   182
wenzelm@34816
   183
  def register_entity(entity: Session.Entity) { session_actor !? Register(entity) }
wenzelm@34816
   184
wenzelm@34780
   185
  def start(args: List[String]) { session_actor !? Start(args) }
wenzelm@34777
   186
  def stop() { session_actor ! Stop }
wenzelm@34777
   187
  def input(change: Change) { session_actor ! change }
wenzelm@34777
   188
wenzelm@34808
   189
  def begin_document(path: String): Proof_Document =
wenzelm@34808
   190
    (session_actor !? Begin_Document(path)).asInstanceOf[Proof_Document]
wenzelm@34777
   191
}