src/Tools/jEdit/src/proofdocument/session.scala
author wenzelm
Wed Dec 30 20:18:50 2009 +0100 (2009-12-30)
changeset 34816 d33312514220
parent 34815 6bae73cd8e33
child 34817 b4efd0ef2f3e
permissions -rw-r--r--
maintain generic session entities -- cover commands, states, etc. (but not yet documents);
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@34809
    58
  @volatile private var documents = Map[Isar_Document.Document_ID, Proof_Document]()
wenzelm@34809
    59
  def document(id: Isar_Document.Document_ID): Option[Proof_Document] = documents.get(id)
wenzelm@34809
    60
wenzelm@34809
    61
wenzelm@34809
    62
  /** main actor **/
wenzelm@34777
    63
wenzelm@34816
    64
  private case class Register(entity: Session.Entity)
wenzelm@34780
    65
  private case class Start(args: List[String])
wenzelm@34777
    66
  private case object Stop
wenzelm@34808
    67
  private case class Begin_Document(path: String)
wenzelm@34777
    68
wenzelm@34809
    69
  private val session_actor = actor {
wenzelm@34809
    70
wenzelm@34809
    71
    var prover: Isabelle_Process with Isar_Document = null
wenzelm@34809
    72
    var prover_ready = false
wenzelm@34809
    73
wenzelm@34816
    74
    def register(entity: Session.Entity) { entities += (entity.id -> entity) }
wenzelm@34816
    75
wenzelm@34809
    76
wenzelm@34809
    77
    /* document changes */
wenzelm@34809
    78
wenzelm@34809
    79
    def handle_change(change: Change)
wenzelm@34809
    80
    {
wenzelm@34809
    81
      val old = document(change.parent.get.id).get
wenzelm@34809
    82
      val (doc, changes) = old.text_changed(this, change)
wenzelm@34809
    83
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@34809
    89
              case Some(command) =>  // FIXME clarify -- may reuse existing commands!??
wenzelm@34816
    90
                register(command)
wenzelm@34809
    91
                prover.define_command(command.id, system.symbols.encode(command.content))
wenzelm@34809
    92
                Some(command.id)
wenzelm@34809
    93
            })
wenzelm@34809
    94
      }
wenzelm@34809
    95
      documents += (doc.id -> doc)
wenzelm@34809
    96
      prover.edit_document(old.id, doc.id, id_changes)
wenzelm@34809
    97
wenzelm@34809
    98
      document_change.event(doc)
wenzelm@34809
    99
    }
wenzelm@34809
   100
wenzelm@34809
   101
wenzelm@34809
   102
    /* prover results */
wenzelm@34809
   103
wenzelm@34809
   104
    def handle_result(result: Isabelle_Process.Result)
wenzelm@34809
   105
    {
wenzelm@34809
   106
      raw_results.event(result)
wenzelm@34799
   107
wenzelm@34813
   108
      val target: Option[Session.Entity] =
wenzelm@34809
   109
        Position.id_of(result.props) match {
wenzelm@34809
   110
          case None => None
wenzelm@34816
   111
          case Some(id) => entities.get(id)
wenzelm@34809
   112
        }
wenzelm@34813
   113
      if (target.isDefined) target.get.consume(this, result.message)
wenzelm@34809
   114
      else if (result.kind == Isabelle_Process.Kind.STATUS) {
wenzelm@34809
   115
        //{{{ global status message
wenzelm@34809
   116
        for (elem <- result.body) {
wenzelm@34809
   117
          elem match {
wenzelm@34809
   118
            // document edits
wenzelm@34809
   119
            case XML.Elem(Markup.EDITS, (Markup.ID, doc_id) :: _, edits) =>
wenzelm@34809
   120
              document(doc_id) match {
wenzelm@34809
   121
                case None =>  // FIXME clarify
wenzelm@34809
   122
                case Some(doc) =>
wenzelm@34809
   123
                  for {
wenzelm@34809
   124
                    XML.Elem(Markup.EDIT, (Markup.ID, cmd_id) :: (Markup.STATE, state_id) :: _, _)
wenzelm@34809
   125
                    <- edits }
wenzelm@34809
   126
                  {
wenzelm@34816
   127
                    entities.get(cmd_id) match {
wenzelm@34816
   128
                      case Some(cmd: Command) =>
wenzelm@34815
   129
                        val state = cmd.finish_static(state_id)
wenzelm@34816
   130
                        register(state)
wenzelm@34816
   131
                        doc.states += (cmd -> state)  // FIXME !?
wenzelm@34809
   132
                        command_change.event(cmd)   // FIXME really!?
wenzelm@34816
   133
                      case _ =>
wenzelm@34809
   134
                    }
wenzelm@34809
   135
                  }
wenzelm@34809
   136
              }
wenzelm@34777
   137
wenzelm@34809
   138
            // command and keyword declarations
wenzelm@34809
   139
            case XML.Elem(Markup.COMMAND_DECL, (Markup.NAME, name) :: (Markup.KIND, kind) :: _, _) =>
wenzelm@34809
   140
              outer_syntax += (name, kind)
wenzelm@34809
   141
            case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) =>
wenzelm@34809
   142
              outer_syntax += name
wenzelm@34809
   143
wenzelm@34809
   144
            // process ready (after initialization)
wenzelm@34809
   145
            case XML.Elem(Markup.READY, _, _) => prover_ready = true
wenzelm@34809
   146
wenzelm@34809
   147
          case _ =>
wenzelm@34809
   148
          }
wenzelm@34809
   149
        }
wenzelm@34809
   150
        //}}}
wenzelm@34809
   151
      }
wenzelm@34809
   152
      else if (result.kind == Isabelle_Process.Kind.EXIT)
wenzelm@34809
   153
        prover = null
wenzelm@34809
   154
    }
wenzelm@34809
   155
wenzelm@34795
   156
    val xml_cache = new XML.Cache(131071)
wenzelm@34809
   157
wenzelm@34809
   158
wenzelm@34809
   159
    /* main loop */
wenzelm@34809
   160
wenzelm@34777
   161
    loop {
wenzelm@34777
   162
      react {
wenzelm@34816
   163
        case Register(entity: Session.Entity) =>
wenzelm@34816
   164
          register(entity)
wenzelm@34816
   165
          reply(())
wenzelm@34816
   166
wenzelm@34780
   167
        case Start(args) =>
wenzelm@34777
   168
          if (prover == null) {
wenzelm@34780
   169
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34777
   170
            reply(())
wenzelm@34777
   171
          }
wenzelm@34777
   172
wenzelm@34807
   173
        case Stop =>  // FIXME clarify
wenzelm@34777
   174
          if (prover != null)
wenzelm@34777
   175
            prover.kill
wenzelm@34778
   176
          prover_ready = false
wenzelm@34808
   177
wenzelm@34808
   178
        case Begin_Document(path: String) if prover_ready =>
wenzelm@34808
   179
          val id = create_id()
wenzelm@34808
   180
          val doc = Proof_Document.empty(id)
wenzelm@34808
   181
          documents += (id -> doc)
wenzelm@34808
   182
          prover.begin_document(id, path)
wenzelm@34808
   183
          reply(doc)
wenzelm@34809
   184
wenzelm@34777
   185
        case change: Change if prover_ready =>
wenzelm@34777
   186
          handle_change(change)
wenzelm@34777
   187
wenzelm@34777
   188
        case result: Isabelle_Process.Result =>
wenzelm@34795
   189
          handle_result(result.cache(xml_cache))
wenzelm@34777
   190
wenzelm@34777
   191
        case bad if prover_ready =>
wenzelm@34777
   192
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   193
      }
wenzelm@34777
   194
    }
wenzelm@34777
   195
  }
wenzelm@34777
   196
wenzelm@34809
   197
wenzelm@34809
   198
  /* main methods */
wenzelm@34809
   199
wenzelm@34816
   200
  def register_entity(entity: Session.Entity) { session_actor !? Register(entity) }
wenzelm@34816
   201
wenzelm@34780
   202
  def start(args: List[String]) { session_actor !? Start(args) }
wenzelm@34777
   203
  def stop() { session_actor ! Stop }
wenzelm@34777
   204
  def input(change: Change) { session_actor ! change }
wenzelm@34777
   205
wenzelm@34808
   206
  def begin_document(path: String): Proof_Document =
wenzelm@34808
   207
    (session_actor !? Begin_Document(path)).asInstanceOf[Proof_Document]
wenzelm@34777
   208
}