src/Tools/jEdit/src/proofdocument/session.scala
author wenzelm
Thu Dec 10 22:15:19 2009 +0100 (2009-12-10)
changeset 34777 91d6089cef88
child 34778 8eccd35e975e
permissions -rw-r--r--
class Session models full session, with or without prover process (cf. heaps, browser_info);
replaced Prover by Session, with a singleton instance in Isabelle plugin (shared by all active buffers);
misc cleanup of Session vs. Plugin instance;
eliminated Prover_Setup -- maintain mapping of JEditBuffer <-> Theory_View directly;
misc tuning and simplification;
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@34777
    12
wenzelm@34777
    13
class Session(system: Isabelle_System)
wenzelm@34777
    14
{
wenzelm@34777
    15
  /* main actor */
wenzelm@34777
    16
wenzelm@34777
    17
  private case class Start(logic: String)
wenzelm@34777
    18
  private case object Stop
wenzelm@34777
    19
wenzelm@34777
    20
  private var prover: Isabelle_Process with Isar_Document = null
wenzelm@34777
    21
  private var prover_logic = ""
wenzelm@34777
    22
  private var prover_ready = false
wenzelm@34777
    23
wenzelm@34777
    24
  private val session_actor = actor {
wenzelm@34777
    25
    loop {
wenzelm@34777
    26
      react {
wenzelm@34777
    27
        case Start(logic) =>
wenzelm@34777
    28
          if (prover == null) {
wenzelm@34777
    29
            // FIXME only once
wenzelm@34777
    30
            prover =  // FIXME rebust error handling (via results)
wenzelm@34777
    31
              new Isabelle_Process(system, self,   // FIXME improve options
wenzelm@34777
    32
                  "-m", "xsymbols", "-m", "no_brackets", "-m", "no_type_brackets", logic)
wenzelm@34777
    33
                with Isar_Document
wenzelm@34777
    34
            prover_logic = logic
wenzelm@34777
    35
            reply(())
wenzelm@34777
    36
          }
wenzelm@34777
    37
wenzelm@34777
    38
        case Stop =>
wenzelm@34777
    39
          if (prover != null)
wenzelm@34777
    40
            prover.kill
wenzelm@34777
    41
          prover = null   // FIXME later (via results)!?
wenzelm@34777
    42
          prover_ready = false // FIXME !??
wenzelm@34777
    43
          
wenzelm@34777
    44
        case change: Change if prover_ready =>
wenzelm@34777
    45
          handle_change(change)
wenzelm@34777
    46
wenzelm@34777
    47
        case result: Isabelle_Process.Result =>
wenzelm@34777
    48
          handle_result(result)
wenzelm@34777
    49
wenzelm@34777
    50
        case bad if prover_ready =>
wenzelm@34777
    51
          System.err.println("session_actor: ignoring bad message " + bad)
wenzelm@34777
    52
      }
wenzelm@34777
    53
    }
wenzelm@34777
    54
  }
wenzelm@34777
    55
wenzelm@34777
    56
  def start(logic: String) { session_actor !? Start(logic) }
wenzelm@34777
    57
  def stop() { session_actor ! Stop }
wenzelm@34777
    58
  def input(change: Change) { session_actor ! change }
wenzelm@34777
    59
wenzelm@34777
    60
wenzelm@34777
    61
  /* pervasive event buses */
wenzelm@34777
    62
wenzelm@34777
    63
  val global_settings = new Event_Bus[Unit]
wenzelm@34777
    64
  val raw_results = new Event_Bus[Isabelle_Process.Result]
wenzelm@34777
    65
  val results = new Event_Bus[Command]
wenzelm@34777
    66
wenzelm@34777
    67
  val command_change = new Event_Bus[Command]
wenzelm@34777
    68
  val document_change = new Event_Bus[Proof_Document]
wenzelm@34777
    69
wenzelm@34777
    70
wenzelm@34777
    71
  /* selected state */  // FIXME!? races!?
wenzelm@34777
    72
wenzelm@34777
    73
  private var _selected_state: Command = null
wenzelm@34777
    74
  def selected_state = _selected_state
wenzelm@34777
    75
  def selected_state_=(state: Command) { _selected_state = state; results.event(state) }
wenzelm@34777
    76
wenzelm@34777
    77
wenzelm@34777
    78
  /* outer syntax keywords and completion */
wenzelm@34777
    79
wenzelm@34777
    80
  @volatile private var _command_decls = Map[String, String]()
wenzelm@34777
    81
  def command_decls() = _command_decls
wenzelm@34777
    82
wenzelm@34777
    83
  @volatile private var _keyword_decls = Set[String]()
wenzelm@34777
    84
  def keyword_decls() = _keyword_decls
wenzelm@34777
    85
wenzelm@34777
    86
  @volatile private var _completion = new Completion + system.symbols
wenzelm@34777
    87
  def completion() = _completion
wenzelm@34777
    88
wenzelm@34777
    89
wenzelm@34777
    90
  /* document state information */
wenzelm@34777
    91
wenzelm@34777
    92
  @volatile private var states = Map[Isar_Document.State_ID, Command_State]()
wenzelm@34777
    93
  @volatile private var commands = Map[Isar_Document.Command_ID, Command]()
wenzelm@34777
    94
  val document_0 =
wenzelm@34777
    95
    Proof_Document.empty.
wenzelm@34777
    96
    set_command_keyword((s: String) => command_decls().contains(s))  // FIXME !?
wenzelm@34777
    97
  @volatile private var document_versions = List(document_0)
wenzelm@34777
    98
wenzelm@34777
    99
  def command(id: Isar_Document.Command_ID): Option[Command] = commands.get(id)
wenzelm@34777
   100
  def document(id: Isar_Document.Document_ID): Option[Proof_Document] =
wenzelm@34777
   101
    document_versions.find(_.id == id)
wenzelm@34777
   102
wenzelm@34777
   103
wenzelm@34777
   104
  /* document changes */
wenzelm@34777
   105
wenzelm@34777
   106
  def begin_document(path: String)
wenzelm@34777
   107
  {
wenzelm@34777
   108
    prover.begin_document(document_0.id, path)   // FIXME fresh id!?!
wenzelm@34777
   109
  }
wenzelm@34777
   110
wenzelm@34777
   111
  def handle_change(change: Change)
wenzelm@34777
   112
  {
wenzelm@34777
   113
    val old = document(change.parent.get.id).get
wenzelm@34777
   114
    val (doc, changes) = old.text_changed(change)
wenzelm@34777
   115
    document_versions ::= doc
wenzelm@34777
   116
wenzelm@34777
   117
    val id_changes = changes map {
wenzelm@34777
   118
      case (c1, c2) =>
wenzelm@34777
   119
        (c1.map(_.id).getOrElse(document_0.id),
wenzelm@34777
   120
         c2 match {
wenzelm@34777
   121
            case None => None
wenzelm@34777
   122
            case Some(command) =>
wenzelm@34777
   123
              commands += (command.id -> command)
wenzelm@34777
   124
              prover.define_command(command.id, system.symbols.encode(command.content))
wenzelm@34777
   125
              Some(command.id)
wenzelm@34777
   126
          })
wenzelm@34777
   127
    }
wenzelm@34777
   128
    prover.edit_document(old.id, doc.id, id_changes)
wenzelm@34777
   129
wenzelm@34777
   130
    document_change.event(doc)
wenzelm@34777
   131
  }
wenzelm@34777
   132
wenzelm@34777
   133
wenzelm@34777
   134
  /* prover results */
wenzelm@34777
   135
wenzelm@34777
   136
  private def handle_result(result: Isabelle_Process.Result)
wenzelm@34777
   137
  {
wenzelm@34777
   138
    raw_results.event(result)
wenzelm@34777
   139
    val message = Isabelle_Process.parse_message(system, result)
wenzelm@34777
   140
wenzelm@34777
   141
    val state =
wenzelm@34777
   142
      Position.id_of(result.props) match {
wenzelm@34777
   143
        case None => None
wenzelm@34777
   144
        case Some(id) => commands.get(id) orElse states.get(id) orElse None
wenzelm@34777
   145
      }
wenzelm@34777
   146
    if (state.isDefined) state.get ! (this, message)
wenzelm@34777
   147
    else if (result.kind == Isabelle_Process.Kind.STATUS) {
wenzelm@34777
   148
      //{{{ global status message
wenzelm@34777
   149
      message match {
wenzelm@34777
   150
        case XML.Elem(Markup.MESSAGE, _, elems) =>
wenzelm@34777
   151
          for (elem <- elems) {
wenzelm@34777
   152
            elem match {
wenzelm@34777
   153
              // document edits
wenzelm@34777
   154
              case XML.Elem(Markup.EDITS, (Markup.ID, doc_id) :: _, edits) =>
wenzelm@34777
   155
                document_versions.find(_.id == doc_id) match {
wenzelm@34777
   156
                  case Some(doc) =>
wenzelm@34777
   157
                    for {
wenzelm@34777
   158
                      XML.Elem(Markup.EDIT, (Markup.ID, cmd_id) :: (Markup.STATE, state_id) :: _, _)
wenzelm@34777
   159
                      <- edits }
wenzelm@34777
   160
                    {
wenzelm@34777
   161
                      commands.get(cmd_id) match {
wenzelm@34777
   162
                        case Some(cmd) =>
wenzelm@34777
   163
                          val state = new Command_State(cmd)
wenzelm@34777
   164
                          states += (state_id -> state)
wenzelm@34777
   165
                          doc.states += (cmd -> state)
wenzelm@34777
   166
                          command_change.event(cmd)   // FIXME really!?
wenzelm@34777
   167
                        case None =>
wenzelm@34777
   168
                      }
wenzelm@34777
   169
                    }
wenzelm@34777
   170
                  case None =>
wenzelm@34777
   171
                }
wenzelm@34777
   172
wenzelm@34777
   173
              // command and keyword declarations
wenzelm@34777
   174
              case XML.Elem(Markup.COMMAND_DECL, (Markup.NAME, name) :: (Markup.KIND, kind) :: _, _) =>
wenzelm@34777
   175
                _command_decls += (name -> kind)
wenzelm@34777
   176
                _completion += name
wenzelm@34777
   177
              case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) =>
wenzelm@34777
   178
                _keyword_decls += name
wenzelm@34777
   179
                _completion += name
wenzelm@34777
   180
wenzelm@34777
   181
              // process ready (after initialization)
wenzelm@34777
   182
              case XML.Elem(Markup.READY, _, _) => prover_ready = true
wenzelm@34777
   183
wenzelm@34777
   184
              case _ =>
wenzelm@34777
   185
            }
wenzelm@34777
   186
          }
wenzelm@34777
   187
        case _ =>
wenzelm@34777
   188
      }
wenzelm@34777
   189
      //}}}
wenzelm@34777
   190
    }
wenzelm@34777
   191
  }
wenzelm@34777
   192
}