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