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