src/Tools/jEdit/src/proofdocument/session.scala
author wenzelm
Fri Dec 18 21:46:29 2009 +0100 (2009-12-18 ago)
changeset 34795 c97335b7e8c3
parent 34792 65130daf2883
child 34799 0330a4284a9b
permissions -rw-r--r--
cache results;
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@34777
    30
wenzelm@34777
    31
  private var prover: Isabelle_Process with Isar_Document = null
wenzelm@34777
    32
  private var prover_ready = false
wenzelm@34777
    33
wenzelm@34777
    34
  private val session_actor = actor {
wenzelm@34795
    35
    val xml_cache = new XML.Cache(131071)
wenzelm@34777
    36
    loop {
wenzelm@34777
    37
      react {
wenzelm@34780
    38
        case Start(args) =>
wenzelm@34777
    39
          if (prover == null) {
wenzelm@34780
    40
            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
wenzelm@34777
    41
            reply(())
wenzelm@34777
    42
          }
wenzelm@34777
    43
wenzelm@34777
    44
        case Stop =>
wenzelm@34777
    45
          if (prover != null)
wenzelm@34777
    46
            prover.kill
wenzelm@34778
    47
          prover_ready = false
wenzelm@34777
    48
          
wenzelm@34777
    49
        case change: Change if prover_ready =>
wenzelm@34777
    50
          handle_change(change)
wenzelm@34777
    51
wenzelm@34777
    52
        case result: Isabelle_Process.Result =>
wenzelm@34795
    53
          handle_result(result.cache(xml_cache))
wenzelm@34777
    54
wenzelm@34777
    55
        case bad if prover_ready =>
wenzelm@34777
    56
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
    57
      }
wenzelm@34777
    58
    }
wenzelm@34777
    59
  }
wenzelm@34777
    60
wenzelm@34780
    61
  def start(args: List[String]) { session_actor !? Start(args) }
wenzelm@34777
    62
  def stop() { session_actor ! Stop }
wenzelm@34777
    63
  def input(change: Change) { session_actor ! change }
wenzelm@34777
    64
wenzelm@34777
    65
wenzelm@34777
    66
  /* pervasive event buses */
wenzelm@34777
    67
wenzelm@34791
    68
  val global_settings = new Event_Bus[Session.Global_Settings.type]
wenzelm@34777
    69
  val raw_results = new Event_Bus[Isabelle_Process.Result]
wenzelm@34777
    70
  val results = new Event_Bus[Command]
wenzelm@34777
    71
wenzelm@34777
    72
  val command_change = new Event_Bus[Command]
wenzelm@34777
    73
  val document_change = new Event_Bus[Proof_Document]
wenzelm@34777
    74
wenzelm@34777
    75
wenzelm@34777
    76
  /* selected state */  // FIXME!? races!?
wenzelm@34777
    77
wenzelm@34777
    78
  private var _selected_state: Command = null
wenzelm@34777
    79
  def selected_state = _selected_state
wenzelm@34777
    80
  def selected_state_=(state: Command) { _selected_state = state; results.event(state) }
wenzelm@34777
    81
wenzelm@34777
    82
wenzelm@34777
    83
  /* outer syntax keywords and completion */
wenzelm@34777
    84
wenzelm@34777
    85
  @volatile private var _command_decls = Map[String, String]()
wenzelm@34777
    86
  def command_decls() = _command_decls
wenzelm@34777
    87
wenzelm@34777
    88
  @volatile private var _keyword_decls = Set[String]()
wenzelm@34777
    89
  def keyword_decls() = _keyword_decls
wenzelm@34777
    90
wenzelm@34777
    91
  @volatile private var _completion = new Completion + system.symbols
wenzelm@34777
    92
  def completion() = _completion
wenzelm@34777
    93
wenzelm@34778
    94
  def is_command_keyword(s: String): Boolean = command_decls().contains(s)
wenzelm@34778
    95
wenzelm@34777
    96
wenzelm@34777
    97
  /* document state information */
wenzelm@34777
    98
wenzelm@34777
    99
  @volatile private var states = Map[Isar_Document.State_ID, Command_State]()
wenzelm@34777
   100
  @volatile private var commands = Map[Isar_Document.Command_ID, Command]()
wenzelm@34778
   101
  val document_0 = Proof_Document.empty(create_id())  // FIXME fresh id (!??)
wenzelm@34777
   102
  @volatile private var document_versions = List(document_0)
wenzelm@34777
   103
wenzelm@34777
   104
  def command(id: Isar_Document.Command_ID): Option[Command] = commands.get(id)
wenzelm@34777
   105
  def document(id: Isar_Document.Document_ID): Option[Proof_Document] =
wenzelm@34777
   106
    document_versions.find(_.id == id)
wenzelm@34777
   107
wenzelm@34777
   108
wenzelm@34777
   109
  /* document changes */
wenzelm@34777
   110
wenzelm@34777
   111
  def begin_document(path: String)
wenzelm@34777
   112
  {
wenzelm@34778
   113
    prover.begin_document(document_0.id, path)   // FIXME fresh document!?!
wenzelm@34777
   114
  }
wenzelm@34777
   115
wenzelm@34777
   116
  def handle_change(change: Change)
wenzelm@34777
   117
  {
wenzelm@34777
   118
    val old = document(change.parent.get.id).get
wenzelm@34778
   119
    val (doc, changes) = old.text_changed(this, change)
wenzelm@34777
   120
    document_versions ::= doc
wenzelm@34777
   121
wenzelm@34777
   122
    val id_changes = changes map {
wenzelm@34777
   123
      case (c1, c2) =>
wenzelm@34777
   124
        (c1.map(_.id).getOrElse(document_0.id),
wenzelm@34777
   125
         c2 match {
wenzelm@34777
   126
            case None => None
wenzelm@34777
   127
            case Some(command) =>
wenzelm@34777
   128
              commands += (command.id -> command)
wenzelm@34777
   129
              prover.define_command(command.id, system.symbols.encode(command.content))
wenzelm@34777
   130
              Some(command.id)
wenzelm@34777
   131
          })
wenzelm@34777
   132
    }
wenzelm@34777
   133
    prover.edit_document(old.id, doc.id, id_changes)
wenzelm@34777
   134
wenzelm@34777
   135
    document_change.event(doc)
wenzelm@34777
   136
  }
wenzelm@34777
   137
wenzelm@34777
   138
wenzelm@34777
   139
  /* prover results */
wenzelm@34777
   140
wenzelm@34777
   141
  private def handle_result(result: Isabelle_Process.Result)
wenzelm@34777
   142
  {
wenzelm@34777
   143
    raw_results.event(result)
wenzelm@34777
   144
wenzelm@34777
   145
    val state =
wenzelm@34777
   146
      Position.id_of(result.props) match {
wenzelm@34777
   147
        case None => None
wenzelm@34777
   148
        case Some(id) => commands.get(id) orElse states.get(id) orElse None
wenzelm@34777
   149
      }
wenzelm@34792
   150
    if (state.isDefined) state.get ! (this, result.message)
wenzelm@34777
   151
    else if (result.kind == Isabelle_Process.Kind.STATUS) {
wenzelm@34777
   152
      //{{{ global status message
wenzelm@34792
   153
      for (elem <- result.body) {
wenzelm@34792
   154
        elem match {
wenzelm@34792
   155
          // document edits
wenzelm@34792
   156
          case XML.Elem(Markup.EDITS, (Markup.ID, doc_id) :: _, edits) =>
wenzelm@34792
   157
            document_versions.find(_.id == doc_id) match {
wenzelm@34792
   158
              case Some(doc) =>
wenzelm@34792
   159
                for {
wenzelm@34792
   160
                  XML.Elem(Markup.EDIT, (Markup.ID, cmd_id) :: (Markup.STATE, state_id) :: _, _)
wenzelm@34792
   161
                  <- edits }
wenzelm@34792
   162
                {
wenzelm@34792
   163
                  commands.get(cmd_id) match {
wenzelm@34792
   164
                    case Some(cmd) =>
wenzelm@34792
   165
                      val state = new Command_State(cmd)
wenzelm@34792
   166
                      states += (state_id -> state)
wenzelm@34792
   167
                      doc.states += (cmd -> state)
wenzelm@34792
   168
                      command_change.event(cmd)   // FIXME really!?
wenzelm@34792
   169
                    case None =>
wenzelm@34792
   170
                  }
wenzelm@34777
   171
                }
wenzelm@34792
   172
              case None =>
wenzelm@34792
   173
            }
wenzelm@34777
   174
wenzelm@34792
   175
          // command and keyword declarations
wenzelm@34792
   176
          case XML.Elem(Markup.COMMAND_DECL, (Markup.NAME, name) :: (Markup.KIND, kind) :: _, _) =>
wenzelm@34792
   177
            _command_decls += (name -> kind)
wenzelm@34792
   178
            _completion += name
wenzelm@34792
   179
          case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) =>
wenzelm@34792
   180
            _keyword_decls += name
wenzelm@34792
   181
            _completion += name
wenzelm@34777
   182
wenzelm@34792
   183
          // process ready (after initialization)
wenzelm@34792
   184
          case XML.Elem(Markup.READY, _, _) => prover_ready = true
wenzelm@34777
   185
wenzelm@34777
   186
        case _ =>
wenzelm@34792
   187
        }
wenzelm@34777
   188
      }
wenzelm@34777
   189
      //}}}
wenzelm@34777
   190
    }
wenzelm@34778
   191
    else if (result.kind == Isabelle_Process.Kind.EXIT)
wenzelm@34778
   192
      prover = null
wenzelm@34777
   193
  }
wenzelm@34777
   194
}