author | wenzelm |
Fri, 12 Aug 2011 22:10:49 +0200 | |
changeset 44163 | 32e0c150c010 |
parent 44159 | 9a35e88d9dc9 |
child 44182 | ecb51b457064 |
permissions | -rw-r--r-- |
36676 | 1 |
/* Title: Pure/System/session.scala |
2 |
Author: Makarius |
|
38221 | 3 |
Options: :folding=explicit:collapseFolds=1: |
36676 | 4 |
|
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
5 |
Main Isabelle/Scala session, potentially with running prover process. |
36676 | 6 |
*/ |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
7 |
|
34871
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents:
34859
diff
changeset
|
8 |
package isabelle |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
9 |
|
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
43443
diff
changeset
|
10 |
import java.lang.System |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
11 |
|
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
12 |
import scala.actors.TIMEOUT |
38226
9d8848d70b0a
maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents:
38222
diff
changeset
|
13 |
import scala.actors.Actor |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
14 |
import scala.actors.Actor._ |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
15 |
|
34815
6bae73cd8e33
unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents:
34813
diff
changeset
|
16 |
|
34791 | 17 |
object Session |
18 |
{ |
|
43716 | 19 |
/* file store */ |
43645 | 20 |
|
21 |
abstract class File_Store |
|
22 |
{ |
|
44163 | 23 |
def append(master_dir: String, path: Path): String |
24 |
def require(file: String): Unit |
|
43645 | 25 |
} |
26 |
||
27 |
||
34813
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents:
34810
diff
changeset
|
28 |
/* events */ |
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents:
34810
diff
changeset
|
29 |
|
43716 | 30 |
//{{{ |
34791 | 31 |
case object Global_Settings |
37849 | 32 |
case object Perspective |
38849
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
33 |
case object Assignment |
38360 | 34 |
case class Commands_Changed(set: Set[Command]) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
35 |
|
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
36 |
sealed abstract class Phase |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
37 |
case object Inactive extends Phase |
39701 | 38 |
case object Startup extends Phase // transient |
39 |
case object Failed extends Phase |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
40 |
case object Ready extends Phase |
39701 | 41 |
case object Shutdown extends Phase // transient |
43716 | 42 |
//}}} |
34791 | 43 |
} |
44 |
||
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
45 |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
46 |
class Session(val file_store: Session.File_Store) |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
47 |
{ |
37849 | 48 |
/* real time parameters */ // FIXME properties or settings (!?) |
49 |
||
43716 | 50 |
val input_delay = Time.seconds(0.3) // user input (e.g. text edits, cursor movement) |
51 |
val output_delay = Time.seconds(0.1) // prover output (markup, common messages) |
|
52 |
val update_delay = Time.seconds(0.5) // GUI layout updates |
|
37849 | 53 |
|
54 |
||
34809 | 55 |
/* pervasive event buses */ |
56 |
||
57 |
val global_settings = new Event_Bus[Session.Global_Settings.type] |
|
37849 | 58 |
val perspective = new Event_Bus[Session.Perspective.type] |
38849
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
59 |
val assignments = new Event_Bus[Session.Assignment.type] |
43716 | 60 |
val commands_changed = new Event_Bus[Session.Commands_Changed] |
61 |
val phase_changed = new Event_Bus[Session.Phase] |
|
43721
fad8634cee62
echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents:
43720
diff
changeset
|
62 |
val raw_messages = new Event_Bus[Isabelle_Process.Message] |
43716 | 63 |
|
34809 | 64 |
|
65 |
||
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
66 |
/** buffered command changes (delay_first discipline) **/ |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
67 |
|
43716 | 68 |
//{{{ |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
69 |
private case object Stop |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
70 |
|
39572 | 71 |
private val (_, command_change_buffer) = |
72 |
Simple_Thread.actor("command_change_buffer", daemon = true) |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
73 |
{ |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
74 |
var changed: Set[Command] = Set() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
75 |
var flush_time: Option[Long] = None |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
76 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
77 |
def flush_timeout: Long = |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
78 |
flush_time match { |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
79 |
case None => 5000L |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
80 |
case Some(time) => (time - System.currentTimeMillis()) max 0 |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
81 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
82 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
83 |
def flush() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
84 |
{ |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
85 |
if (!changed.isEmpty) commands_changed.event(Session.Commands_Changed(changed)) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
86 |
changed = Set() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
87 |
flush_time = None |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
88 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
89 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
90 |
def invoke() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
91 |
{ |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
92 |
val now = System.currentTimeMillis() |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
93 |
flush_time match { |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
94 |
case None => flush_time = Some(now + output_delay.ms) |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
95 |
case Some(time) => if (now >= time) flush() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
96 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
97 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
98 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
99 |
var finished = false |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
100 |
while (!finished) { |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
101 |
receiveWithin(flush_timeout) { |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
102 |
case command: Command => changed += command; invoke() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
103 |
case TIMEOUT => flush() |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
104 |
case Stop => finished = true; reply(()) |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
105 |
case bad => System.err.println("command_change_buffer: ignoring bad message " + bad) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
106 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
107 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
108 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
109 |
//}}} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
110 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
111 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
112 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
113 |
/** main protocol actor **/ |
34809 | 114 |
|
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
115 |
/* global state */ |
43644 | 116 |
|
43716 | 117 |
@volatile var verbose: Boolean = false |
118 |
||
119 |
@volatile private var loaded_theories: Set[String] = Set() |
|
43673
29eb1cd29961
Thy_Info.dependencies: ignore already loaded theories, according to initial prover session status;
wenzelm
parents:
43662
diff
changeset
|
120 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43673
diff
changeset
|
121 |
@volatile private var syntax = new Outer_Syntax |
38569 | 122 |
def current_syntax(): Outer_Syntax = syntax |
34809 | 123 |
|
39626
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
wenzelm
parents:
39625
diff
changeset
|
124 |
@volatile private var reverse_syslog = List[XML.Elem]() |
39629 | 125 |
def syslog(): String = reverse_syslog.reverse.map(msg => XML.content(msg).mkString).mkString("\n") |
39626
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
wenzelm
parents:
39625
diff
changeset
|
126 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
127 |
@volatile private var _phase: Session.Phase = Session.Inactive |
39633 | 128 |
private def phase_=(new_phase: Session.Phase) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
129 |
{ |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
130 |
_phase = new_phase |
39701 | 131 |
phase_changed.event(new_phase) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
132 |
} |
43716 | 133 |
def phase = _phase |
43553 | 134 |
def is_ready: Boolean = phase == Session.Ready |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
135 |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
136 |
private val global_state = new Volatile(Document.State.init) |
43719 | 137 |
def current_state(): Document.State = global_state() |
34816
d33312514220
maintain generic session entities -- cover commands, states, etc. (but not yet documents);
wenzelm
parents:
34815
diff
changeset
|
138 |
|
43716 | 139 |
def snapshot(name: String, pending_edits: List[Text.Edit]): Document.Snapshot = |
43719 | 140 |
global_state().snapshot(name, pending_edits) |
43716 | 141 |
|
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
142 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
143 |
/* theory files */ |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
144 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
145 |
val thy_load = new Thy_Load |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
146 |
{ |
43673
29eb1cd29961
Thy_Info.dependencies: ignore already loaded theories, according to initial prover session status;
wenzelm
parents:
43662
diff
changeset
|
147 |
override def is_loaded(name: String): Boolean = |
29eb1cd29961
Thy_Info.dependencies: ignore already loaded theories, according to initial prover session status;
wenzelm
parents:
43662
diff
changeset
|
148 |
loaded_theories.contains(name) |
29eb1cd29961
Thy_Info.dependencies: ignore already loaded theories, according to initial prover session status;
wenzelm
parents:
43662
diff
changeset
|
149 |
|
44159 | 150 |
override def check_thy(dir: Path, name: String): (String, Thy_Header) = |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
151 |
{ |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
152 |
val file = Isabelle_System.platform_file(dir + Thy_Header.thy_path(name)) |
43652 | 153 |
if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString)) |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
154 |
val text = Standard_System.read_file(file) |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
155 |
val header = Thy_Header.read(text) |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
156 |
(text, header) |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
157 |
} |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
158 |
} |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
159 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
160 |
val thy_info = new Thy_Info(thy_load) |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
161 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
162 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
163 |
/* actor messages */ |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
164 |
|
43716 | 165 |
private case class Start(timeout: Time, args: List[String]) |
166 |
private case object Interrupt |
|
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43695
diff
changeset
|
167 |
private case class Init_Node(name: String, header: Document.Node.Header, text: String) |
43716 | 168 |
private case class Edit_Node(name: String, header: Document.Node.Header, edits: List[Text.Edit]) |
43722 | 169 |
private case class Change_Node( |
170 |
name: String, |
|
171 |
doc_edits: List[Document.Edit_Command], |
|
172 |
previous: Document.Version, |
|
173 |
version: Document.Version) |
|
41534 | 174 |
|
39572 | 175 |
private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true) |
38639
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
176 |
{ |
43648 | 177 |
val this_actor = self |
43649 | 178 |
var prover: Option[Isabelle_Process with Isar_Document] = None |
34809 | 179 |
|
180 |
||
43716 | 181 |
/* incoming edits */ |
182 |
||
43718 | 183 |
def handle_edits(name: String, |
44156 | 184 |
header: Document.Node.Header, edits: List[Document.Node.Edit[Text.Edit]]) |
43716 | 185 |
//{{{ |
186 |
{ |
|
187 |
val syntax = current_syntax() |
|
43719 | 188 |
val previous = global_state().history.tip.version |
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
189 |
val doc_edits = |
44163 | 190 |
(name, Document.Node.Update_Header[Text.Edit](header.norm_deps(file_store.append))) :: |
191 |
edits.map(edit => (name, edit)) |
|
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
192 |
val result = Future.fork { Thy_Syntax.text_edits(syntax, previous.join, doc_edits) } |
43722 | 193 |
val change = |
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
194 |
global_state.change_yield(_.extend_history(previous, doc_edits, result.map(_._2))) |
43716 | 195 |
|
43722 | 196 |
result.map { |
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
197 |
case (doc_edits, _) => |
43722 | 198 |
assignments.await { global_state().is_assigned(previous.get_finished) } |
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
199 |
this_actor ! Change_Node(name, doc_edits, previous.join, change.version.join) |
43722 | 200 |
} |
43716 | 201 |
} |
202 |
//}}} |
|
203 |
||
204 |
||
205 |
/* resulting changes */ |
|
34809 | 206 |
|
43722 | 207 |
def handle_change(change: Change_Node) |
38221 | 208 |
//{{{ |
34809 | 209 |
{ |
43722 | 210 |
val previous = change.previous |
211 |
val version = change.version |
|
212 |
val name = change.name |
|
213 |
val doc_edits = change.doc_edits |
|
38366
fea82d1add74
simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents:
38365
diff
changeset
|
214 |
|
43719 | 215 |
var former_assignment = global_state().the_assignment(previous).get_finished |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
216 |
for { |
44156 | 217 |
(name, Document.Node.Edits(cmd_edits)) <- doc_edits // FIXME proper coverage!? |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
218 |
(prev, None) <- cmd_edits |
38417 | 219 |
removed <- previous.nodes(name).commands.get_after(prev) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
220 |
} former_assignment -= removed |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
221 |
|
43720 | 222 |
def id_command(command: Command): Document.Command_ID = |
223 |
{ |
|
224 |
if (global_state().lookup_command(command.id).isEmpty) { |
|
225 |
global_state.change(_.define_command(command)) |
|
226 |
prover.get.define_command(command.id, Symbol.encode(command.source)) |
|
227 |
} |
|
228 |
command.id |
|
229 |
} |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
230 |
val id_edits = |
43722 | 231 |
doc_edits map { |
44156 | 232 |
case (name, edit) => |
233 |
(name, edit.map({ case (c1, c2) => (c1.map(id_command), c2.map(id_command)) })) |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
234 |
} |
43722 | 235 |
|
39698 | 236 |
global_state.change(_.define_version(version, former_assignment)) |
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
237 |
prover.get.edit_version(previous.id, version.id, id_edits) |
34809 | 238 |
} |
38221 | 239 |
//}}} |
34809 | 240 |
|
241 |
||
242 |
/* prover results */ |
|
243 |
||
244 |
def handle_result(result: Isabelle_Process.Result) |
|
38221 | 245 |
//{{{ |
34809 | 246 |
{ |
43716 | 247 |
def bad_result(result: Isabelle_Process.Result) |
248 |
{ |
|
249 |
if (verbose) |
|
250 |
System.err.println("Ignoring prover result: " + result.message.toString) |
|
251 |
} |
|
252 |
||
38722
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38639
diff
changeset
|
253 |
result.properties match { |
43748 | 254 |
case Position.Id(state_id) if !result.is_raw => |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
255 |
try { |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
256 |
val st = global_state.change_yield(_.accumulate(state_id, result.message)) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
257 |
command_change_buffer ! st.command |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
258 |
} |
43748 | 259 |
catch { |
260 |
case _: Document.State.Fail => bad_result(result) |
|
261 |
} |
|
262 |
case Markup.Invoke_Scala(name, id) if result.is_raw => |
|
263 |
Future.fork { |
|
264 |
val arg = XML.content(result.body).mkString |
|
265 |
val (tag, res) = Invoke_Scala.method(name, arg) |
|
266 |
prover.get.invoke_scala(id, tag, res) |
|
267 |
} |
|
38722
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38639
diff
changeset
|
268 |
case _ => |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
269 |
if (result.is_syslog) { |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
270 |
reverse_syslog ::= result.message |
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
271 |
if (result.is_ready) { |
43716 | 272 |
// FIXME move to ML side (!?) |
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
273 |
syntax += ("hence", Keyword.PRF_ASM_GOAL, "then have") |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
274 |
syntax += ("thus", Keyword.PRF_ASM_GOAL, "then show") |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
275 |
phase = Session.Ready |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
276 |
} |
39701 | 277 |
else if (result.is_exit && phase == Session.Startup) phase = Session.Failed |
39696 | 278 |
else if (result.is_exit) phase = Session.Inactive |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
279 |
} |
39626
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
wenzelm
parents:
39625
diff
changeset
|
280 |
else if (result.is_stdout) { } |
39625 | 281 |
else if (result.is_status) { |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
282 |
result.body match { |
38417 | 283 |
case List(Isar_Document.Assign(id, edits)) => |
38849
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
284 |
try { |
38882
e1fb3bbc22ab
Document state assignment indicates command change;
wenzelm
parents:
38850
diff
changeset
|
285 |
val cmds: List[Command] = global_state.change_yield(_.assign(id, edits)) |
e1fb3bbc22ab
Document state assignment indicates command change;
wenzelm
parents:
38850
diff
changeset
|
286 |
for (cmd <- cmds) command_change_buffer ! cmd |
38849
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
287 |
assignments.event(Session.Assignment) |
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
288 |
} |
38414
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38413
diff
changeset
|
289 |
catch { case _: Document.State.Fail => bad_result(result) } |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
290 |
case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
291 |
case List(Keyword.Keyword_Decl(name)) => syntax += name |
43673
29eb1cd29961
Thy_Info.dependencies: ignore already loaded theories, according to initial prover session status;
wenzelm
parents:
43662
diff
changeset
|
292 |
case List(Thy_Info.Loaded_Theory(name)) => loaded_theories += name |
39625 | 293 |
case _ => bad_result(result) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
294 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
295 |
} |
39625 | 296 |
else bad_result(result) |
43748 | 297 |
} |
34809 | 298 |
} |
38221 | 299 |
//}}} |
34809 | 300 |
|
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
301 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
302 |
/* main loop */ |
34809 | 303 |
|
43716 | 304 |
//{{{ |
38639
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
305 |
var finished = false |
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
306 |
while (!finished) { |
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
307 |
receive { |
43649 | 308 |
case Start(timeout, args) if prover.isEmpty => |
39701 | 309 |
if (phase == Session.Inactive || phase == Session.Failed) { |
310 |
phase = Session.Startup |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
311 |
prover = Some(new Isabelle_Process(timeout, this_actor, args:_*) with Isar_Document) |
39701 | 312 |
} |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
313 |
|
39701 | 314 |
case Stop => |
315 |
if (phase == Session.Ready) { |
|
316 |
global_state.change(_ => Document.State.init) // FIXME event bus!? |
|
317 |
phase = Session.Shutdown |
|
43649 | 318 |
prover.get.terminate |
43717 | 319 |
prover = None |
39701 | 320 |
phase = Session.Inactive |
321 |
} |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
322 |
finished = true |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
323 |
reply(()) |
38850
5c3e5c548f12
session_actor: ignore spurious TIMEOUT (again) -- probably stemming from earlier use of receiveWithin;
wenzelm
parents:
38849
diff
changeset
|
324 |
|
43717 | 325 |
case Interrupt if prover.isDefined => |
326 |
prover.get.interrupt |
|
43716 | 327 |
|
328 |
case Init_Node(name, header, text) if prover.isDefined => |
|
329 |
// FIXME compare with existing node |
|
44156 | 330 |
handle_edits(name, header, |
331 |
List(Document.Node.Remove(), Document.Node.Edits(List(Text.Edit.insert(0, text))))) |
|
43716 | 332 |
reply(()) |
333 |
||
43718 | 334 |
case Edit_Node(name, header, text_edits) if prover.isDefined => |
44156 | 335 |
handle_edits(name, header, List(Document.Node.Edits(text_edits))) |
43718 | 336 |
reply(()) |
337 |
||
43722 | 338 |
case change: Change_Node if prover.isDefined => |
339 |
handle_change(change) |
|
43716 | 340 |
|
43721
fad8634cee62
echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents:
43720
diff
changeset
|
341 |
case input: Isabelle_Process.Input => |
fad8634cee62
echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents:
43720
diff
changeset
|
342 |
raw_messages.event(input) |
fad8634cee62
echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents:
43720
diff
changeset
|
343 |
|
43716 | 344 |
case result: Isabelle_Process.Result => |
345 |
handle_result(result) |
|
43721
fad8634cee62
echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents:
43720
diff
changeset
|
346 |
raw_messages.event(result) |
43716 | 347 |
|
43717 | 348 |
case bad => System.err.println("session_actor: ignoring bad message " + bad) |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
349 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
350 |
} |
43716 | 351 |
//}}} |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
352 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
353 |
|
34809 | 354 |
|
43716 | 355 |
/* actions */ |
34809 | 356 |
|
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
357 |
def start(timeout: Time, args: List[String]) { session_actor ! Start(timeout, args) } |
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
358 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
359 |
def stop() { command_change_buffer !? Stop; session_actor !? Stop } |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
360 |
|
43716 | 361 |
def interrupt() { session_actor ! Interrupt } |
43648 | 362 |
|
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43695
diff
changeset
|
363 |
def init_node(name: String, header: Document.Node.Header, text: String) |
43716 | 364 |
{ session_actor !? Init_Node(name, header, text) } |
38221 | 365 |
|
43716 | 366 |
def edit_node(name: String, header: Document.Node.Header, edits: List[Text.Edit]) |
367 |
{ session_actor !? Edit_Node(name, header, edits) } |
|
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
368 |
} |