author | wenzelm |
Tue, 29 Dec 2009 21:31:17 +0100 | |
changeset 34809 | 0fed930f2992 |
parent 34808 | e462572536e9 |
child 34810 | 9ad3431a34a5 |
permissions | -rw-r--r-- |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
1 |
/* |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
2 |
* Isabelle session, potentially with running prover |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
3 |
* |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
4 |
* @author Makarius |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
5 |
*/ |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
6 |
|
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
7 |
package isabelle.proofdocument |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
8 |
|
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
9 |
|
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
10 |
import scala.actors.Actor._ |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
11 |
|
34791 | 12 |
object Session |
13 |
{ |
|
14 |
case object Global_Settings |
|
15 |
} |
|
16 |
||
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
17 |
|
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
18 |
class Session(system: Isabelle_System) |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
19 |
{ |
34809 | 20 |
/* pervasive event buses */ |
21 |
||
22 |
val global_settings = new Event_Bus[Session.Global_Settings.type] |
|
23 |
val raw_results = new Event_Bus[Isabelle_Process.Result] |
|
24 |
val results = new Event_Bus[Command] |
|
25 |
||
26 |
val command_change = new Event_Bus[Command] |
|
27 |
val document_change = new Event_Bus[Proof_Document] |
|
28 |
||
29 |
||
34778 | 30 |
/* unique ids */ |
31 |
||
32 |
private var id_count: BigInt = 0 |
|
33 |
def create_id(): String = synchronized { id_count += 1; "j" + id_count } |
|
34 |
||
35 |
||
34809 | 36 |
/* document state information -- vars belong to session_actor */ |
37 |
||
38 |
@volatile private var outer_syntax = new Outer_Syntax(system.symbols) |
|
39 |
def syntax(): Outer_Syntax = outer_syntax |
|
40 |
||
41 |
@volatile private var states = Map[Isar_Document.State_ID, Command_State]() |
|
42 |
@volatile private var commands = Map[Isar_Document.Command_ID, Command]() |
|
43 |
@volatile private var documents = Map[Isar_Document.Document_ID, Proof_Document]() |
|
44 |
||
45 |
def state(id: Isar_Document.State_ID): Option[Command_State] = states.get(id) |
|
46 |
def command(id: Isar_Document.Command_ID): Option[Command] = commands.get(id) |
|
47 |
def document(id: Isar_Document.Document_ID): Option[Proof_Document] = documents.get(id) |
|
48 |
||
49 |
||
50 |
/** main actor **/ |
|
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
51 |
|
34780 | 52 |
private case class Start(args: List[String]) |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
53 |
private case object Stop |
34808
e462572536e9
eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents:
34807
diff
changeset
|
54 |
private case class Begin_Document(path: String) |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
55 |
|
34809 | 56 |
private val session_actor = actor { |
57 |
||
58 |
var prover: Isabelle_Process with Isar_Document = null |
|
59 |
var prover_ready = false |
|
60 |
||
61 |
||
62 |
/* document changes */ |
|
63 |
||
64 |
def handle_change(change: Change) |
|
65 |
{ |
|
66 |
val old = document(change.parent.get.id).get |
|
67 |
val (doc, changes) = old.text_changed(this, change) |
|
68 |
||
69 |
val id_changes = changes map { |
|
70 |
case (c1, c2) => |
|
71 |
(c1.map(_.id).getOrElse(""), |
|
72 |
c2 match { |
|
73 |
case None => None |
|
74 |
case Some(command) => // FIXME clarify -- may reuse existing commands!?? |
|
75 |
commands += (command.id -> command) |
|
76 |
prover.define_command(command.id, system.symbols.encode(command.content)) |
|
77 |
Some(command.id) |
|
78 |
}) |
|
79 |
} |
|
80 |
documents += (doc.id -> doc) |
|
81 |
prover.edit_document(old.id, doc.id, id_changes) |
|
82 |
||
83 |
document_change.event(doc) |
|
84 |
} |
|
85 |
||
86 |
||
87 |
/* prover results */ |
|
88 |
||
89 |
def handle_result(result: Isabelle_Process.Result) |
|
90 |
{ |
|
91 |
raw_results.event(result) |
|
34799
0330a4284a9b
just one variable for outer syntax keywords and completion;
wenzelm
parents:
34795
diff
changeset
|
92 |
|
34809 | 93 |
val state = |
94 |
Position.id_of(result.props) match { |
|
95 |
case None => None |
|
96 |
case Some(id) => commands.get(id) orElse states.get(id) orElse None |
|
97 |
} |
|
98 |
if (state.isDefined) state.get ! (this, result.message) |
|
99 |
else if (result.kind == Isabelle_Process.Kind.STATUS) { |
|
100 |
//{{{ global status message |
|
101 |
for (elem <- result.body) { |
|
102 |
elem match { |
|
103 |
// document edits |
|
104 |
case XML.Elem(Markup.EDITS, (Markup.ID, doc_id) :: _, edits) => |
|
105 |
document(doc_id) match { |
|
106 |
case None => // FIXME clarify |
|
107 |
case Some(doc) => |
|
108 |
for { |
|
109 |
XML.Elem(Markup.EDIT, (Markup.ID, cmd_id) :: (Markup.STATE, state_id) :: _, _) |
|
110 |
<- edits } |
|
111 |
{ |
|
112 |
commands.get(cmd_id) match { |
|
113 |
case Some(cmd) => |
|
114 |
val state = new Command_State(cmd) |
|
115 |
states += (state_id -> state) |
|
116 |
doc.states += (cmd -> state) |
|
117 |
command_change.event(cmd) // FIXME really!? |
|
118 |
case None => |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
122 |
|
34809 | 123 |
// command and keyword declarations |
124 |
case XML.Elem(Markup.COMMAND_DECL, (Markup.NAME, name) :: (Markup.KIND, kind) :: _, _) => |
|
125 |
outer_syntax += (name, kind) |
|
126 |
case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) => |
|
127 |
outer_syntax += name |
|
128 |
||
129 |
// process ready (after initialization) |
|
130 |
case XML.Elem(Markup.READY, _, _) => prover_ready = true |
|
131 |
||
132 |
case _ => |
|
133 |
} |
|
134 |
} |
|
135 |
//}}} |
|
136 |
} |
|
137 |
else if (result.kind == Isabelle_Process.Kind.EXIT) |
|
138 |
prover = null |
|
139 |
} |
|
140 |
||
34795 | 141 |
val xml_cache = new XML.Cache(131071) |
34809 | 142 |
|
143 |
||
144 |
/* main loop */ |
|
145 |
||
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
146 |
loop { |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
147 |
react { |
34780 | 148 |
case Start(args) => |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
149 |
if (prover == null) { |
34780 | 150 |
prover = new Isabelle_Process(system, self, args:_*) with Isar_Document |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
151 |
reply(()) |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
152 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
153 |
|
34807 | 154 |
case Stop => // FIXME clarify |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
155 |
if (prover != null) |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
156 |
prover.kill |
34778 | 157 |
prover_ready = false |
34808
e462572536e9
eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents:
34807
diff
changeset
|
158 |
|
e462572536e9
eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents:
34807
diff
changeset
|
159 |
case Begin_Document(path: String) if prover_ready => |
e462572536e9
eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents:
34807
diff
changeset
|
160 |
val id = create_id() |
e462572536e9
eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents:
34807
diff
changeset
|
161 |
val doc = Proof_Document.empty(id) |
e462572536e9
eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents:
34807
diff
changeset
|
162 |
documents += (id -> doc) |
e462572536e9
eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents:
34807
diff
changeset
|
163 |
prover.begin_document(id, path) |
e462572536e9
eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents:
34807
diff
changeset
|
164 |
reply(doc) |
34809 | 165 |
|
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
166 |
case change: Change if prover_ready => |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
167 |
handle_change(change) |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
168 |
|
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
169 |
case result: Isabelle_Process.Result => |
34795 | 170 |
handle_result(result.cache(xml_cache)) |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
171 |
|
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
172 |
case bad if prover_ready => |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
173 |
System.err.println("session_actor: ignoring bad message " + bad) |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
174 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
175 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
176 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
177 |
|
34809 | 178 |
|
179 |
/* main methods */ |
|
180 |
||
34780 | 181 |
def start(args: List[String]) { session_actor !? Start(args) } |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
182 |
def stop() { session_actor ! Stop } |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
183 |
def input(change: Change) { session_actor ! change } |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
184 |
|
34808
e462572536e9
eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents:
34807
diff
changeset
|
185 |
def begin_document(path: String): Proof_Document = |
e462572536e9
eliminated global Session.document_0 -- did not work due to hardwired id;
wenzelm
parents:
34807
diff
changeset
|
186 |
(session_actor !? Begin_Document(path)).asInstanceOf[Proof_Document] |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
187 |
|
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
188 |
|
34809 | 189 |
/* selected state */ // FIXME eliminate!?! |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
190 |
|
34809 | 191 |
private var _selected_state: Command = null |
192 |
def selected_state = _selected_state |
|
193 |
def selected_state_=(state: Command) { _selected_state = state; results.event(state) } |
|
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
194 |
} |