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