src/Tools/jEdit/src/proofdocument/session.scala
author wenzelm
Wed Dec 30 17:48:58 2009 +0100 (2009-12-30 ago)
changeset 34813 f0107bc96961
parent 34810 9ad3431a34a5
child 34815 6bae73cd8e33
permissions -rw-r--r--
more explicit modeling of Command and Command_State as Session.Entity;
misc tuning;
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@34791
    12
object Session
wenzelm@34791
    13
{
wenzelm@34813
    14
  /* events */
wenzelm@34813
    15
wenzelm@34791
    16
  case object Global_Settings
wenzelm@34813
    17
wenzelm@34813
    18
wenzelm@34813
    19
  /* managed entities */
wenzelm@34813
    20
wenzelm@34813
    21
  type Entity_ID = String
wenzelm@34813
    22
wenzelm@34813
    23
  trait Entity
wenzelm@34813
    24
  {
wenzelm@34813
    25
    val id: Entity_ID
wenzelm@34813
    26
    def consume(session: Session, message: XML.Tree): Unit
wenzelm@34813
    27
  }
wenzelm@34791
    28
}
wenzelm@34791
    29
wenzelm@34777
    30
wenzelm@34777
    31
class Session(system: Isabelle_System)
wenzelm@34777
    32
{
wenzelm@34809
    33
  /* pervasive event buses */
wenzelm@34809
    34
wenzelm@34809
    35
  val global_settings = new Event_Bus[Session.Global_Settings.type]
wenzelm@34809
    36
  val raw_results = new Event_Bus[Isabelle_Process.Result]
wenzelm@34809
    37
  val results = new Event_Bus[Command]
wenzelm@34809
    38
wenzelm@34809
    39
  val command_change = new Event_Bus[Command]
wenzelm@34809
    40
  val document_change = new Event_Bus[Proof_Document]
wenzelm@34809
    41
wenzelm@34809
    42
wenzelm@34778
    43
  /* unique ids */
wenzelm@34778
    44
wenzelm@34778
    45
  private var id_count: BigInt = 0
wenzelm@34813
    46
  def create_id(): Session.Entity_ID = synchronized { id_count += 1; "j" + id_count }
wenzelm@34778
    47
wenzelm@34778
    48
wenzelm@34810
    49
  /* document state information -- owned by session_actor */
wenzelm@34809
    50
wenzelm@34809
    51
  @volatile private var outer_syntax = new Outer_Syntax(system.symbols)
wenzelm@34809
    52
  def syntax(): Outer_Syntax = outer_syntax
wenzelm@34809
    53
wenzelm@34809
    54
  @volatile private var states = Map[Isar_Document.State_ID, Command_State]()
wenzelm@34809
    55
  @volatile private var commands = Map[Isar_Document.Command_ID, Command]()
wenzelm@34809
    56
  @volatile private var documents = Map[Isar_Document.Document_ID, Proof_Document]()
wenzelm@34809
    57
wenzelm@34809
    58
  def state(id: Isar_Document.State_ID): Option[Command_State] = states.get(id)
wenzelm@34809
    59
  def command(id: Isar_Document.Command_ID): Option[Command] = commands.get(id)
wenzelm@34809
    60
  def document(id: Isar_Document.Document_ID): Option[Proof_Document] = documents.get(id)
wenzelm@34809
    61
wenzelm@34809
    62
wenzelm@34809
    63
  /** main actor **/
wenzelm@34777
    64
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@34809
    74
wenzelm@34809
    75
    /* document changes */
wenzelm@34809
    76
wenzelm@34809
    77
    def handle_change(change: Change)
wenzelm@34809
    78
    {
wenzelm@34809
    79
      val old = document(change.parent.get.id).get
wenzelm@34809
    80
      val (doc, changes) = old.text_changed(this, change)
wenzelm@34809
    81
wenzelm@34809
    82
      val id_changes = changes map {
wenzelm@34809
    83
        case (c1, c2) =>
wenzelm@34809
    84
          (c1.map(_.id).getOrElse(""),
wenzelm@34809
    85
           c2 match {
wenzelm@34809
    86
              case None => None
wenzelm@34809
    87
              case Some(command) =>  // FIXME clarify -- may reuse existing commands!??
wenzelm@34809
    88
                commands += (command.id -> command)
wenzelm@34809
    89
                prover.define_command(command.id, system.symbols.encode(command.content))
wenzelm@34809
    90
                Some(command.id)
wenzelm@34809
    91
            })
wenzelm@34809
    92
      }
wenzelm@34809
    93
      documents += (doc.id -> doc)
wenzelm@34809
    94
      prover.edit_document(old.id, doc.id, id_changes)
wenzelm@34809
    95
wenzelm@34809
    96
      document_change.event(doc)
wenzelm@34809
    97
    }
wenzelm@34809
    98
wenzelm@34809
    99
wenzelm@34809
   100
    /* prover results */
wenzelm@34809
   101
wenzelm@34809
   102
    def handle_result(result: Isabelle_Process.Result)
wenzelm@34809
   103
    {
wenzelm@34809
   104
      raw_results.event(result)
wenzelm@34799
   105
wenzelm@34813
   106
      val target: Option[Session.Entity] =
wenzelm@34809
   107
        Position.id_of(result.props) match {
wenzelm@34809
   108
          case None => None
wenzelm@34809
   109
          case Some(id) => commands.get(id) orElse states.get(id) orElse None
wenzelm@34809
   110
        }
wenzelm@34813
   111
      if (target.isDefined) target.get.consume(this, result.message)
wenzelm@34809
   112
      else if (result.kind == Isabelle_Process.Kind.STATUS) {
wenzelm@34809
   113
        //{{{ global status message
wenzelm@34809
   114
        for (elem <- result.body) {
wenzelm@34809
   115
          elem match {
wenzelm@34809
   116
            // document edits
wenzelm@34809
   117
            case XML.Elem(Markup.EDITS, (Markup.ID, doc_id) :: _, edits) =>
wenzelm@34809
   118
              document(doc_id) match {
wenzelm@34809
   119
                case None =>  // FIXME clarify
wenzelm@34809
   120
                case Some(doc) =>
wenzelm@34809
   121
                  for {
wenzelm@34809
   122
                    XML.Elem(Markup.EDIT, (Markup.ID, cmd_id) :: (Markup.STATE, state_id) :: _, _)
wenzelm@34809
   123
                    <- edits }
wenzelm@34809
   124
                  {
wenzelm@34809
   125
                    commands.get(cmd_id) match {
wenzelm@34809
   126
                      case Some(cmd) =>
wenzelm@34813
   127
                        val state = new Command_State(state_id, cmd)
wenzelm@34809
   128
                        states += (state_id -> state)
wenzelm@34809
   129
                        doc.states += (cmd -> state)
wenzelm@34809
   130
                        command_change.event(cmd)   // FIXME really!?
wenzelm@34809
   131
                      case None =>
wenzelm@34809
   132
                    }
wenzelm@34809
   133
                  }
wenzelm@34809
   134
              }
wenzelm@34777
   135
wenzelm@34809
   136
            // command and keyword declarations
wenzelm@34809
   137
            case XML.Elem(Markup.COMMAND_DECL, (Markup.NAME, name) :: (Markup.KIND, kind) :: _, _) =>
wenzelm@34809
   138
              outer_syntax += (name, kind)
wenzelm@34809
   139
            case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) =>
wenzelm@34809
   140
              outer_syntax += name
wenzelm@34809
   141
wenzelm@34809
   142
            // process ready (after initialization)
wenzelm@34809
   143
            case XML.Elem(Markup.READY, _, _) => prover_ready = true
wenzelm@34809
   144
wenzelm@34809
   145
          case _ =>
wenzelm@34809
   146
          }
wenzelm@34809
   147
        }
wenzelm@34809
   148
        //}}}
wenzelm@34809
   149
      }
wenzelm@34809
   150
      else if (result.kind == Isabelle_Process.Kind.EXIT)
wenzelm@34809
   151
        prover = null
wenzelm@34809
   152
    }
wenzelm@34809
   153
wenzelm@34795
   154
    val xml_cache = new XML.Cache(131071)
wenzelm@34809
   155
wenzelm@34809
   156
wenzelm@34809
   157
    /* main loop */
wenzelm@34809
   158
wenzelm@34777
   159
    loop {
wenzelm@34777
   160
      react {
wenzelm@34780
   161
        case Start(args) =>
wenzelm@34777
   162
          if (prover == null) {
wenzelm@34780
   163
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34777
   164
            reply(())
wenzelm@34777
   165
          }
wenzelm@34777
   166
wenzelm@34807
   167
        case Stop =>  // FIXME clarify
wenzelm@34777
   168
          if (prover != null)
wenzelm@34777
   169
            prover.kill
wenzelm@34778
   170
          prover_ready = false
wenzelm@34808
   171
wenzelm@34808
   172
        case Begin_Document(path: String) if prover_ready =>
wenzelm@34808
   173
          val id = create_id()
wenzelm@34808
   174
          val doc = Proof_Document.empty(id)
wenzelm@34808
   175
          documents += (id -> doc)
wenzelm@34808
   176
          prover.begin_document(id, path)
wenzelm@34808
   177
          reply(doc)
wenzelm@34809
   178
wenzelm@34777
   179
        case change: Change if prover_ready =>
wenzelm@34777
   180
          handle_change(change)
wenzelm@34777
   181
wenzelm@34777
   182
        case result: Isabelle_Process.Result =>
wenzelm@34795
   183
          handle_result(result.cache(xml_cache))
wenzelm@34777
   184
wenzelm@34777
   185
        case bad if prover_ready =>
wenzelm@34777
   186
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
   187
      }
wenzelm@34777
   188
    }
wenzelm@34777
   189
  }
wenzelm@34777
   190
wenzelm@34809
   191
wenzelm@34809
   192
  /* main methods */
wenzelm@34809
   193
wenzelm@34780
   194
  def start(args: List[String]) { session_actor !? Start(args) }
wenzelm@34777
   195
  def stop() { session_actor ! Stop }
wenzelm@34777
   196
  def input(change: Change) { session_actor ! change }
wenzelm@34777
   197
wenzelm@34808
   198
  def begin_document(path: String): Proof_Document =
wenzelm@34808
   199
    (session_actor !? Begin_Document(path)).asInstanceOf[Proof_Document]
wenzelm@34777
   200
}