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