author | wenzelm |
Sat, 09 Jul 2011 17:14:08 +0200 | |
changeset 43717 | c3ddb5537a2f |
parent 43716 | 1d64662c1bfd |
child 43718 | 4a4ca9e4a14b |
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 |
{ |
|
23 |
def read(path: Path): String |
|
24 |
} |
|
25 |
||
26 |
||
34813
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents:
34810
diff
changeset
|
27 |
/* events */ |
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents:
34810
diff
changeset
|
28 |
|
43716 | 29 |
//{{{ |
34791 | 30 |
case object Global_Settings |
37849 | 31 |
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
|
32 |
case object Assignment |
38360 | 33 |
case class Commands_Changed(set: Set[Command]) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
34 |
|
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
35 |
sealed abstract class Phase |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
36 |
case object Inactive extends Phase |
39701 | 37 |
case object Startup extends Phase // transient |
38 |
case object Failed extends Phase |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
39 |
case object Ready extends Phase |
39701 | 40 |
case object Shutdown extends Phase // transient |
43716 | 41 |
//}}} |
34791 | 42 |
} |
43 |
||
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
44 |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
45 |
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
|
46 |
{ |
37849 | 47 |
/* real time parameters */ // FIXME properties or settings (!?) |
48 |
||
43716 | 49 |
val input_delay = Time.seconds(0.3) // user input (e.g. text edits, cursor movement) |
50 |
val output_delay = Time.seconds(0.1) // prover output (markup, common messages) |
|
51 |
val update_delay = Time.seconds(0.5) // GUI layout updates |
|
37849 | 52 |
|
53 |
||
34809 | 54 |
/* pervasive event buses */ |
55 |
||
56 |
val global_settings = new Event_Bus[Session.Global_Settings.type] |
|
37849 | 57 |
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
|
58 |
val assignments = new Event_Bus[Session.Assignment.type] |
43716 | 59 |
val commands_changed = new Event_Bus[Session.Commands_Changed] |
60 |
val phase_changed = new Event_Bus[Session.Phase] |
|
61 |
val raw_messages = new Event_Bus[Isabelle_Process.Result] |
|
62 |
||
34809 | 63 |
|
64 |
||
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
65 |
/** 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
|
66 |
|
43716 | 67 |
//{{{ |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
68 |
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
|
69 |
|
39572 | 70 |
private val (_, command_change_buffer) = |
71 |
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
|
72 |
{ |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
73 |
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
|
74 |
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
|
75 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
76 |
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
|
77 |
flush_time match { |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
78 |
case None => 5000L |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
79 |
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
|
80 |
} |
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 |
def flush() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
83 |
{ |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
84 |
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
|
85 |
changed = Set() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
86 |
flush_time = None |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
87 |
} |
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 |
def invoke() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
90 |
{ |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
91 |
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
|
92 |
flush_time match { |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
93 |
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
|
94 |
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
|
95 |
} |
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 |
var finished = false |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
99 |
while (!finished) { |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
100 |
receiveWithin(flush_timeout) { |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
101 |
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
|
102 |
case TIMEOUT => flush() |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
103 |
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
|
104 |
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
|
105 |
} |
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 |
/** main protocol actor **/ |
34809 | 113 |
|
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
114 |
/* global state */ |
43644 | 115 |
|
43716 | 116 |
@volatile var verbose: Boolean = false |
117 |
||
118 |
@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
|
119 |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43673
diff
changeset
|
120 |
@volatile private var syntax = new Outer_Syntax |
38569 | 121 |
def current_syntax(): Outer_Syntax = syntax |
34809 | 122 |
|
39626
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
wenzelm
parents:
39625
diff
changeset
|
123 |
@volatile private var reverse_syslog = List[XML.Elem]() |
39629 | 124 |
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
|
125 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
126 |
@volatile private var _phase: Session.Phase = Session.Inactive |
39633 | 127 |
private def phase_=(new_phase: Session.Phase) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
128 |
{ |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
129 |
_phase = new_phase |
39701 | 130 |
phase_changed.event(new_phase) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
131 |
} |
43716 | 132 |
def phase = _phase |
43553 | 133 |
def is_ready: Boolean = phase == Session.Ready |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
134 |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
135 |
private val global_state = new Volatile(Document.State.init) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
136 |
def current_state(): Document.State = global_state.peek() |
34816
d33312514220
maintain generic session entities -- cover commands, states, etc. (but not yet documents);
wenzelm
parents:
34815
diff
changeset
|
137 |
|
43716 | 138 |
def snapshot(name: String, pending_edits: List[Text.Edit]): Document.Snapshot = |
139 |
current_state().snapshot(name, pending_edits) |
|
140 |
||
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
141 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
142 |
/* theory files */ |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
143 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
144 |
val thy_load = new Thy_Load |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
145 |
{ |
43673
29eb1cd29961
Thy_Info.dependencies: ignore already loaded theories, according to initial prover session status;
wenzelm
parents:
43662
diff
changeset
|
146 |
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
|
147 |
loaded_theories.contains(name) |
29eb1cd29961
Thy_Info.dependencies: ignore already loaded theories, according to initial prover session status;
wenzelm
parents:
43662
diff
changeset
|
148 |
|
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
149 |
override def check_thy(dir: Path, name: String): (String, Thy_Header.Header) = |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
150 |
{ |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
151 |
val file = Isabelle_System.platform_file(dir + Thy_Header.thy_path(name)) |
43652 | 152 |
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
|
153 |
val text = Standard_System.read_file(file) |
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
154 |
val header = Thy_Header.read(text) |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
155 |
(text, header) |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
156 |
} |
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 |
val thy_info = new Thy_Info(thy_load) |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
160 |
|
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 |
/* actor messages */ |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
163 |
|
43716 | 164 |
private case class Start(timeout: Time, args: List[String]) |
165 |
private case object Interrupt |
|
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43695
diff
changeset
|
166 |
private case class Init_Node(name: String, header: Document.Node.Header, text: String) |
43716 | 167 |
private case class Edit_Node(name: String, header: Document.Node.Header, edits: List[Text.Edit]) |
41534 | 168 |
|
39572 | 169 |
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
|
170 |
{ |
43648 | 171 |
val this_actor = self |
43649 | 172 |
var prover: Option[Isabelle_Process with Isar_Document] = None |
34809 | 173 |
|
174 |
||
43716 | 175 |
/* incoming edits */ |
176 |
||
177 |
def handle_edits(headers: List[Document.Node.Header], edits: List[Document.Edit_Text]) |
|
178 |
//{{{ |
|
179 |
{ |
|
180 |
val syntax = current_syntax() |
|
181 |
val previous = current_state().history.tip.version |
|
182 |
val result = Future.fork { Thy_Syntax.text_edits(syntax, previous.join, edits) } |
|
183 |
val change = global_state.change_yield(_.extend_history(previous, edits, result)) |
|
184 |
||
185 |
change.version.map(_ => { |
|
186 |
assignments.await { current_state().is_assigned(previous.get_finished) } |
|
187 |
this_actor ! change }) |
|
188 |
} |
|
189 |
//}}} |
|
190 |
||
191 |
||
192 |
/* resulting changes */ |
|
34809 | 193 |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38226
diff
changeset
|
194 |
def handle_change(change: Document.Change) |
38221 | 195 |
//{{{ |
34809 | 196 |
{ |
38848
9483bb678d96
use Future.get_finished where this is the intended meaning -- prefer immediate crash over deadlock due to promises that are never fulfilled;
wenzelm
parents:
38842
diff
changeset
|
197 |
val previous = change.previous.get_finished |
39698 | 198 |
val (node_edits, version) = change.result.get_finished |
38366
fea82d1add74
simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents:
38365
diff
changeset
|
199 |
|
43716 | 200 |
var former_assignment = current_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
|
201 |
for { |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
202 |
(name, Some(cmd_edits)) <- node_edits |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
203 |
(prev, None) <- cmd_edits |
38417 | 204 |
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
|
205 |
} former_assignment -= removed |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
206 |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
207 |
val id_edits = |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
208 |
node_edits map { |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
209 |
case (name, None) => (name, None) |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
210 |
case (name, Some(cmd_edits)) => |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
211 |
val ids = |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
212 |
cmd_edits map { |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
213 |
case (c1, c2) => |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
214 |
val id1 = c1.map(_.id) |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
215 |
val id2 = |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
216 |
c2 match { |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
217 |
case None => None |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
218 |
case Some(command) => |
43716 | 219 |
if (current_state().lookup_command(command.id).isEmpty) { |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
220 |
global_state.change(_.define_command(command)) |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43673
diff
changeset
|
221 |
prover.get.define_command(command.id, Symbol.encode(command.source)) |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
222 |
} |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
223 |
Some(command.id) |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
224 |
} |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
225 |
(id1, id2) |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
226 |
} |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
227 |
(name -> Some(ids)) |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
228 |
} |
39698 | 229 |
global_state.change(_.define_version(version, former_assignment)) |
43649 | 230 |
prover.get.edit_version(previous.id, version.id, id_edits) |
34809 | 231 |
} |
38221 | 232 |
//}}} |
34809 | 233 |
|
234 |
||
235 |
/* prover results */ |
|
236 |
||
237 |
def handle_result(result: Isabelle_Process.Result) |
|
38221 | 238 |
//{{{ |
34809 | 239 |
{ |
43716 | 240 |
def bad_result(result: Isabelle_Process.Result) |
241 |
{ |
|
242 |
if (verbose) |
|
243 |
System.err.println("Ignoring prover result: " + result.message.toString) |
|
244 |
} |
|
245 |
||
38722
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38639
diff
changeset
|
246 |
result.properties match { |
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38639
diff
changeset
|
247 |
case Position.Id(state_id) => |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
248 |
try { |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
249 |
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
|
250 |
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
|
251 |
} |
38414
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38413
diff
changeset
|
252 |
catch { case _: Document.State.Fail => bad_result(result) } |
38722
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38639
diff
changeset
|
253 |
case _ => |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
254 |
if (result.is_syslog) { |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
255 |
reverse_syslog ::= result.message |
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
256 |
if (result.is_ready) { |
43716 | 257 |
// FIXME move to ML side (!?) |
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
258 |
syntax += ("hence", Keyword.PRF_ASM_GOAL, "then have") |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
259 |
syntax += ("thus", Keyword.PRF_ASM_GOAL, "then show") |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
260 |
phase = Session.Ready |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
261 |
} |
39701 | 262 |
else if (result.is_exit && phase == Session.Startup) phase = Session.Failed |
39696 | 263 |
else if (result.is_exit) phase = Session.Inactive |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
264 |
} |
39626
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
wenzelm
parents:
39625
diff
changeset
|
265 |
else if (result.is_stdout) { } |
39625 | 266 |
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
|
267 |
result.body match { |
38417 | 268 |
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
|
269 |
try { |
38882
e1fb3bbc22ab
Document state assignment indicates command change;
wenzelm
parents:
38850
diff
changeset
|
270 |
val cmds: List[Command] = global_state.change_yield(_.assign(id, edits)) |
e1fb3bbc22ab
Document state assignment indicates command change;
wenzelm
parents:
38850
diff
changeset
|
271 |
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
|
272 |
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
|
273 |
} |
38414
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38413
diff
changeset
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
case List(Thy_Info.Loaded_Theory(name)) => loaded_theories += name |
39625 | 278 |
case _ => bad_result(result) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
279 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
280 |
} |
39625 | 281 |
else bad_result(result) |
34809 | 282 |
} |
40566
36d4f2757f4f
post raw messages last, to ensure that result has been handled by session actor already (e.g. to avoid race between Session.session_actor and Session_Dockable.main_actor);
wenzelm
parents:
40533
diff
changeset
|
283 |
|
36d4f2757f4f
post raw messages last, to ensure that result has been handled by session actor already (e.g. to avoid race between Session.session_actor and Session_Dockable.main_actor);
wenzelm
parents:
40533
diff
changeset
|
284 |
raw_messages.event(result) |
34809 | 285 |
} |
38221 | 286 |
//}}} |
34809 | 287 |
|
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
288 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
289 |
/* main loop */ |
34809 | 290 |
|
43716 | 291 |
//{{{ |
38639
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
292 |
var finished = false |
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
293 |
while (!finished) { |
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
294 |
receive { |
43649 | 295 |
case Start(timeout, args) if prover.isEmpty => |
39701 | 296 |
if (phase == Session.Inactive || phase == Session.Failed) { |
297 |
phase = Session.Startup |
|
43661
39fdbd814c7f
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
wenzelm
parents:
43660
diff
changeset
|
298 |
prover = Some(new Isabelle_Process(timeout, this_actor, args:_*) with Isar_Document) |
39701 | 299 |
} |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
300 |
|
39701 | 301 |
case Stop => |
302 |
if (phase == Session.Ready) { |
|
303 |
global_state.change(_ => Document.State.init) // FIXME event bus!? |
|
304 |
phase = Session.Shutdown |
|
43649 | 305 |
prover.get.terminate |
43717 | 306 |
prover = None |
39701 | 307 |
phase = Session.Inactive |
308 |
} |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
309 |
finished = true |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
310 |
reply(()) |
38850
5c3e5c548f12
session_actor: ignore spurious TIMEOUT (again) -- probably stemming from earlier use of receiveWithin;
wenzelm
parents:
38849
diff
changeset
|
311 |
|
43717 | 312 |
case Interrupt if prover.isDefined => |
313 |
prover.get.interrupt |
|
43716 | 314 |
|
315 |
case Edit_Node(name, header, text_edits) if prover.isDefined => |
|
316 |
val node_name = (header.master_dir + Path.basic(name)).implode // FIXME |
|
317 |
handle_edits(List(header), List((node_name, Some(text_edits)))) |
|
318 |
reply(()) |
|
319 |
||
320 |
case Init_Node(name, header, text) if prover.isDefined => |
|
321 |
// FIXME compare with existing node |
|
322 |
val node_name = (header.master_dir + Path.basic(name)).implode // FIXME |
|
323 |
handle_edits(List(header), |
|
324 |
List( |
|
325 |
(node_name, None), |
|
326 |
(node_name, Some(List(Text.Edit.insert(0, text)))))) |
|
327 |
reply(()) |
|
328 |
||
329 |
case change: Document.Change if prover.isDefined => |
|
330 |
handle_change(change) |
|
331 |
||
332 |
case result: Isabelle_Process.Result => |
|
333 |
handle_result(result) |
|
334 |
||
43717 | 335 |
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
|
336 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
337 |
} |
43716 | 338 |
//}}} |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
339 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
340 |
|
34809 | 341 |
|
43716 | 342 |
/* actions */ |
34809 | 343 |
|
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
344 |
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
|
345 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
346 |
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
|
347 |
|
43716 | 348 |
def interrupt() { session_actor ! Interrupt } |
43648 | 349 |
|
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43695
diff
changeset
|
350 |
def init_node(name: String, header: Document.Node.Header, text: String) |
43716 | 351 |
{ session_actor !? Init_Node(name, header, text) } |
38221 | 352 |
|
43716 | 353 |
def edit_node(name: String, header: Document.Node.Header, edits: List[Text.Edit]) |
354 |
{ 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
|
355 |
} |