author | wenzelm |
Thu, 05 Jan 2012 14:15:37 +0100 | |
changeset 46121 | 30a69cd8a9a0 |
parent 46120 | f7ee2e5a83dd |
child 46122 | 1e9ec1a44dfc |
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 |
|
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
10 |
|
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
43443
diff
changeset
|
11 |
import java.lang.System |
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
12 |
import java.util.{Timer, TimerTask} |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
13 |
|
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
14 |
import scala.collection.mutable |
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
15 |
import scala.actors.TIMEOUT |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
16 |
import scala.actors.Actor._ |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
17 |
|
34815
6bae73cd8e33
unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents:
34813
diff
changeset
|
18 |
|
34791 | 19 |
object Session |
20 |
{ |
|
34813
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents:
34810
diff
changeset
|
21 |
/* events */ |
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents:
34810
diff
changeset
|
22 |
|
43716 | 23 |
//{{{ |
34791 | 24 |
case object Global_Settings |
44805 | 25 |
case object Caret_Focus |
44615 | 26 |
case class Commands_Changed(nodes: Set[Document.Node.Name], commands: Set[Command]) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
27 |
|
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
28 |
sealed abstract class Phase |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
29 |
case object Inactive extends Phase |
39701 | 30 |
case object Startup extends Phase // transient |
31 |
case object Failed extends Phase |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
32 |
case object Ready extends Phase |
39701 | 33 |
case object Shutdown extends Phase // transient |
43716 | 34 |
//}}} |
34791 | 35 |
} |
36 |
||
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
37 |
|
44953 | 38 |
class Session(thy_load: Thy_Load = new Thy_Load) |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
39 |
{ |
37849 | 40 |
/* real time parameters */ // FIXME properties or settings (!?) |
41 |
||
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
42 |
val message_delay = Time.seconds(0.01) // prover messages |
43716 | 43 |
val input_delay = Time.seconds(0.3) // user input (e.g. text edits, cursor movement) |
44 |
val output_delay = Time.seconds(0.1) // prover output (markup, common messages) |
|
45 |
val update_delay = Time.seconds(0.5) // GUI layout updates |
|
44573
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44484
diff
changeset
|
46 |
val load_delay = Time.seconds(0.5) // file load operations (new buffers etc.) |
44676 | 47 |
val prune_delay = Time.seconds(60.0) // prune history -- delete old versions |
48 |
val prune_size = 0 // size of retained history |
|
37849 | 49 |
|
50 |
||
34809 | 51 |
/* pervasive event buses */ |
52 |
||
53 |
val global_settings = new Event_Bus[Session.Global_Settings.type] |
|
44805 | 54 |
val caret_focus = new Event_Bus[Session.Caret_Focus.type] |
43716 | 55 |
val commands_changed = new Event_Bus[Session.Commands_Changed] |
56 |
val phase_changed = new Event_Bus[Session.Phase] |
|
44734
7313e2db3d39
more specific message channels to avoid potential bottle-neck of raw_messages;
wenzelm
parents:
44733
diff
changeset
|
57 |
val syslog_messages = new Event_Bus[Isabelle_Process.Result] |
7313e2db3d39
more specific message channels to avoid potential bottle-neck of raw_messages;
wenzelm
parents:
44733
diff
changeset
|
58 |
val raw_output_messages = new Event_Bus[Isabelle_Process.Result] |
46120 | 59 |
val protocol_messages = new Event_Bus[Isabelle_Process.Message] // potential bottle-neck |
43716 | 60 |
|
34809 | 61 |
|
62 |
||
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
63 |
/** 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
|
64 |
|
43716 | 65 |
//{{{ |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
66 |
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
|
67 |
|
44722
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
68 |
private val (_, commands_changed_buffer) = |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
69 |
Simple_Thread.actor("commands_changed_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
|
70 |
{ |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
71 |
var finished = false |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
72 |
while (!finished) { |
44722
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
73 |
receive { |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
74 |
case Stop => finished = true; reply(()) |
44722
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
75 |
case changed: Session.Commands_Changed => commands_changed.event(changed) |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
76 |
case bad => System.err.println("commands_changed_buffer: ignoring bad message " + bad) |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
77 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
78 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
79 |
} |
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 |
|
45635
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
83 |
/** pipelined change parsing **/ |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
84 |
|
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
85 |
//{{{ |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
86 |
private case class Text_Edits( |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
87 |
syntax: Outer_Syntax, |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
88 |
name: Document.Node.Name, |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
89 |
previous: Future[Document.Version], |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
90 |
text_edits: List[Document.Edit_Text], |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
91 |
version_result: Promise[Document.Version]) |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
92 |
|
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
93 |
private val (_, change_parser) = Simple_Thread.actor("change_parser", daemon = true) |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
94 |
{ |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
95 |
var finished = false |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
96 |
while (!finished) { |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
97 |
receive { |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
98 |
case Stop => finished = true; reply(()) |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
99 |
|
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
100 |
case Text_Edits(syntax, name, previous, text_edits, version_result) => |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
101 |
val prev = previous.get_finished |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
102 |
val (doc_edits, version) = Thy_Syntax.text_edits(syntax, prev, text_edits) |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
103 |
version_result.fulfill(version) |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
104 |
sender ! Change_Node(name, doc_edits, prev, version) |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
105 |
|
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
106 |
case bad => System.err.println("change_parser: ignoring bad message " + bad) |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
107 |
} |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
108 |
} |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
109 |
} |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
110 |
//}}} |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
111 |
|
38841
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 |
||
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43673
diff
changeset
|
119 |
@volatile private var syntax = new Outer_Syntax |
38569 | 120 |
def current_syntax(): Outer_Syntax = syntax |
34809 | 121 |
|
39626
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
wenzelm
parents:
39625
diff
changeset
|
122 |
@volatile private var reverse_syslog = List[XML.Elem]() |
39629 | 123 |
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
|
124 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
125 |
@volatile private var _phase: Session.Phase = Session.Inactive |
39633 | 126 |
private def phase_=(new_phase: Session.Phase) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
127 |
{ |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
128 |
_phase = new_phase |
39701 | 129 |
phase_changed.event(new_phase) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
130 |
} |
43716 | 131 |
def phase = _phase |
43553 | 132 |
def is_ready: Boolean = phase == Session.Ready |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
133 |
|
45248 | 134 |
private val global_state = Volatile(Document.State.init) |
43719 | 135 |
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
|
136 |
|
44960 | 137 |
def snapshot(name: Document.Node.Name = Document.Node.Name.empty, |
138 |
pending_edits: List[Text.Edit] = Nil): Document.Snapshot = |
|
43719 | 139 |
global_state().snapshot(name, pending_edits) |
43716 | 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 |
|
44615 | 144 |
def header_edit(name: Document.Node.Name, header: Document.Node_Header): Document.Edit_Text = |
44442
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
145 |
{ |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
146 |
def norm_import(s: String): String = |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
147 |
{ |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
148 |
val thy_name = Thy_Header.base_name(s) |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
149 |
if (thy_load.is_loaded(thy_name)) thy_name |
44616 | 150 |
else thy_load.append(name.dir, Thy_Header.thy_path(Path.explode(s))) |
44442
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
151 |
} |
44616 | 152 |
def norm_use(s: String): String = thy_load.append(name.dir, Path.explode(s)) |
44442
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
153 |
|
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
154 |
val header1: Document.Node_Header = |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
155 |
header match { |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
156 |
case Exn.Res(Thy_Header(thy_name, _, _)) |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
157 |
if (thy_load.is_loaded(thy_name)) => |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
158 |
Exn.Exn(ERROR("Attempt to update loaded theory " + quote(thy_name))) |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
159 |
case _ => Document.Node.norm_header(norm_import, norm_use, header) |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
160 |
} |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
161 |
(name, Document.Node.Header(header1)) |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
162 |
} |
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
wenzelm
parents:
44436
diff
changeset
|
163 |
|
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
164 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
165 |
/* actor messages */ |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
43649
diff
changeset
|
166 |
|
45158 | 167 |
private case class Start(timeout: Time, args: List[String]) |
44775
27930cf6f0f7
added "cancel" button based on cancel_execution, not interrupt (cf. 156be0e43336);
wenzelm
parents:
44734
diff
changeset
|
168 |
private case object Cancel_Execution |
44615 | 169 |
private case class Init_Node(name: Document.Node.Name, |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44384
diff
changeset
|
170 |
header: Document.Node_Header, perspective: Text.Perspective, text: String) |
44615 | 171 |
private case class Edit_Node(name: Document.Node.Name, |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44384
diff
changeset
|
172 |
header: Document.Node_Header, perspective: Text.Perspective, edits: List[Text.Edit]) |
43722 | 173 |
private case class Change_Node( |
44615 | 174 |
name: Document.Node.Name, |
43722 | 175 |
doc_edits: List[Document.Edit_Command], |
176 |
previous: Document.Version, |
|
177 |
version: Document.Version) |
|
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
178 |
private case class Messages(msgs: List[Isabelle_Process.Message]) |
41534 | 179 |
|
39572 | 180 |
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
|
181 |
{ |
43648 | 182 |
val this_actor = self |
34809 | 183 |
|
44676 | 184 |
var prune_next = System.currentTimeMillis() + prune_delay.ms |
185 |
||
34809 | 186 |
|
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
187 |
/* buffered prover messages */ |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
188 |
|
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
189 |
object receiver |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
190 |
{ |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
191 |
private var buffer = new mutable.ListBuffer[Isabelle_Process.Message] |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
192 |
|
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
193 |
def flush(): Unit = synchronized { |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
194 |
if (!buffer.isEmpty) { |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
195 |
val msgs = buffer.toList |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
196 |
this_actor ! Messages(msgs) |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
197 |
buffer = new mutable.ListBuffer[Isabelle_Process.Message] |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
198 |
} |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
199 |
} |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
200 |
def invoke(msg: Isabelle_Process.Message): Unit = synchronized { |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
201 |
buffer += msg |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
202 |
msg match { |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
203 |
case result: Isabelle_Process.Result if result.is_raw => flush() |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
204 |
case _ => |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
205 |
} |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
206 |
} |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
207 |
|
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
208 |
private val timer = new Timer("session_actor.receiver", true) |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
209 |
timer.schedule(new TimerTask { def run = flush }, message_delay.ms, message_delay.ms) |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
210 |
|
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
211 |
def cancel() { timer.cancel() } |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
212 |
} |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
213 |
|
45709
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
wenzelm
parents:
45672
diff
changeset
|
214 |
var prover: Option[Isabelle_Process with Protocol] = None |
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
215 |
|
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
216 |
|
44722
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
217 |
/* delayed command changes */ |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
218 |
|
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
219 |
object commands_changed_delay |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
220 |
{ |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
221 |
private var changed_nodes: Set[Document.Node.Name] = Set.empty |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
222 |
private var changed_commands: Set[Command] = Set.empty |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
223 |
private def changed: Boolean = !changed_nodes.isEmpty || !changed_commands.isEmpty |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
224 |
|
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
225 |
private var flush_time: Option[Long] = None |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
226 |
|
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
227 |
def flush_timeout: Long = |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
228 |
flush_time match { |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
229 |
case None => 5000L |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
230 |
case Some(time) => (time - System.currentTimeMillis()) max 0 |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
231 |
} |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
232 |
|
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
233 |
def flush() |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
234 |
{ |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
235 |
if (changed) |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
236 |
commands_changed_buffer ! Session.Commands_Changed(changed_nodes, changed_commands) |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
237 |
changed_nodes = Set.empty |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
238 |
changed_commands = Set.empty |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
239 |
flush_time = None |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
240 |
} |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
241 |
|
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
242 |
def invoke(command: Command) |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
243 |
{ |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
244 |
changed_nodes += command.node_name |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
245 |
changed_commands += command |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
246 |
val now = System.currentTimeMillis() |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
247 |
flush_time match { |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
248 |
case None => flush_time = Some(now + output_delay.ms) |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
249 |
case Some(time) => if (now >= time) flush() |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
250 |
} |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
251 |
} |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
252 |
} |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
253 |
|
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
254 |
|
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
255 |
/* perspective */ |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
256 |
|
44615 | 257 |
def update_perspective(name: Document.Node.Name, text_perspective: Text.Perspective) |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
258 |
{ |
44475 | 259 |
val previous = global_state().tip_version |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
260 |
val (perspective, version) = Thy_Syntax.edit_perspective(previous, name, text_perspective) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
261 |
|
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
262 |
val text_edits: List[Document.Edit_Text] = |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
263 |
List((name, Document.Node.Perspective(text_perspective))) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
264 |
val change = |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
265 |
global_state.change_yield( |
44446 | 266 |
_.continue_history(Future.value(previous), text_edits, Future.value(version))) |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
267 |
|
44477 | 268 |
val assignment = global_state().the_assignment(previous).check_finished |
44479
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44477
diff
changeset
|
269 |
global_state.change(_.define_version(version, assignment)) |
44476
e8a87398f35d
propagate information about last command with exec state assignment through document model;
wenzelm
parents:
44475
diff
changeset
|
270 |
global_state.change_yield(_.assign(version.id, Document.no_assign)) |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
271 |
|
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
272 |
prover.get.update_perspective(previous.id, version.id, name, perspective) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
273 |
} |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
274 |
|
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
275 |
|
43716 | 276 |
/* incoming edits */ |
277 |
||
44615 | 278 |
def handle_edits(name: Document.Node.Name, |
44384 | 279 |
header: Document.Node_Header, edits: List[Document.Node.Edit[Text.Edit, Text.Perspective]]) |
43716 | 280 |
//{{{ |
281 |
{ |
|
282 |
val syntax = current_syntax() |
|
43719 | 283 |
val previous = global_state().history.tip.version |
44185 | 284 |
|
44612
990ac978854c
explicit cancel_execution before queueing new edits -- potential performance improvement for machines with few cores;
wenzelm
parents:
44608
diff
changeset
|
285 |
prover.get.cancel_execution() |
990ac978854c
explicit cancel_execution before queueing new edits -- potential performance improvement for machines with few cores;
wenzelm
parents:
44608
diff
changeset
|
286 |
|
44615 | 287 |
val text_edits = header_edit(name, header) :: edits.map(edit => (name, edit)) |
45635
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
288 |
val version = Future.promise[Document.Version] |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
289 |
val change = global_state.change_yield(_.continue_history(previous, text_edits, version)) |
43716 | 290 |
|
45635
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
291 |
change_parser ! Text_Edits(syntax, name, previous, text_edits, version) |
43716 | 292 |
} |
293 |
//}}} |
|
294 |
||
295 |
||
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
296 |
/* exec state assignment */ |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
297 |
|
44476
e8a87398f35d
propagate information about last command with exec state assignment through document model;
wenzelm
parents:
44475
diff
changeset
|
298 |
def handle_assign(id: Document.Version_ID, assign: Document.Assign) |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
299 |
//{{{ |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
300 |
{ |
44476
e8a87398f35d
propagate information about last command with exec state assignment through document model;
wenzelm
parents:
44475
diff
changeset
|
301 |
val cmds = global_state.change_yield(_.assign(id, assign)) |
44722
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
302 |
for (cmd <- cmds) commands_changed_delay.invoke(cmd) |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
303 |
} |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
304 |
//}}} |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
305 |
|
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
306 |
|
44676 | 307 |
/* removed versions */ |
308 |
||
309 |
def handle_removed(removed: List[Document.Version_ID]) |
|
310 |
//{{{ |
|
311 |
{ |
|
312 |
global_state.change(_.removed_versions(removed)) |
|
313 |
} |
|
314 |
//}}} |
|
315 |
||
316 |
||
43716 | 317 |
/* resulting changes */ |
34809 | 318 |
|
43722 | 319 |
def handle_change(change: Change_Node) |
38221 | 320 |
//{{{ |
34809 | 321 |
{ |
43722 | 322 |
val previous = change.previous |
323 |
val version = change.version |
|
324 |
val name = change.name |
|
325 |
val doc_edits = change.doc_edits |
|
38366
fea82d1add74
simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents:
38365
diff
changeset
|
326 |
|
44383 | 327 |
def id_command(command: Command) |
43720 | 328 |
{ |
44582 | 329 |
if (!global_state().defined_command(command.id)) { |
43720 | 330 |
global_state.change(_.define_command(command)) |
44644
317e4962dd0f
clarified define_command: store name as structural information;
wenzelm
parents:
44616
diff
changeset
|
331 |
prover.get.define_command(command) |
43720 | 332 |
} |
333 |
} |
|
44383 | 334 |
doc_edits foreach { |
335 |
case (_, edit) => |
|
336 |
edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command } |
|
337 |
} |
|
43722 | 338 |
|
44479
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44477
diff
changeset
|
339 |
val assignment = global_state().the_assignment(previous).check_finished |
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44477
diff
changeset
|
340 |
global_state.change(_.define_version(version, assignment)) |
44481 | 341 |
prover.get.update(previous.id, version.id, doc_edits) |
34809 | 342 |
} |
38221 | 343 |
//}}} |
34809 | 344 |
|
345 |
||
346 |
/* prover results */ |
|
347 |
||
348 |
def handle_result(result: Isabelle_Process.Result) |
|
38221 | 349 |
//{{{ |
34809 | 350 |
{ |
43716 | 351 |
def bad_result(result: Isabelle_Process.Result) |
352 |
{ |
|
353 |
if (verbose) |
|
354 |
System.err.println("Ignoring prover result: " + result.message.toString) |
|
355 |
} |
|
356 |
||
38722
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38639
diff
changeset
|
357 |
result.properties match { |
43748 | 358 |
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
|
359 |
try { |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
360 |
val st = global_state.change_yield(_.accumulate(state_id, result.message)) |
44722
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
361 |
commands_changed_delay.invoke(st.command) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
362 |
} |
43748 | 363 |
catch { |
364 |
case _: Document.State.Fail => bad_result(result) |
|
365 |
} |
|
45666 | 366 |
case Isabelle_Markup.Assign_Execs if result.is_raw => |
44661
383c9d758a56
raw message function "assign_execs" avoids full overhead of decoding and caching message body;
wenzelm
parents:
44644
diff
changeset
|
367 |
XML.content(result.body).mkString match { |
45709
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
wenzelm
parents:
45672
diff
changeset
|
368 |
case Protocol.Assign(id, assign) => |
44661
383c9d758a56
raw message function "assign_execs" avoids full overhead of decoding and caching message body;
wenzelm
parents:
44644
diff
changeset
|
369 |
try { handle_assign(id, assign) } |
383c9d758a56
raw message function "assign_execs" avoids full overhead of decoding and caching message body;
wenzelm
parents:
44644
diff
changeset
|
370 |
catch { case _: Document.State.Fail => bad_result(result) } |
383c9d758a56
raw message function "assign_execs" avoids full overhead of decoding and caching message body;
wenzelm
parents:
44644
diff
changeset
|
371 |
case _ => bad_result(result) |
383c9d758a56
raw message function "assign_execs" avoids full overhead of decoding and caching message body;
wenzelm
parents:
44644
diff
changeset
|
372 |
} |
44676 | 373 |
// FIXME separate timeout event/message!? |
374 |
if (prover.isDefined && System.currentTimeMillis() > prune_next) { |
|
375 |
val old_versions = global_state.change_yield(_.prune_history(prune_size)) |
|
376 |
if (!old_versions.isEmpty) prover.get.remove_versions(old_versions) |
|
377 |
prune_next = System.currentTimeMillis() + prune_delay.ms |
|
378 |
} |
|
45666 | 379 |
case Isabelle_Markup.Removed_Versions if result.is_raw => |
44676 | 380 |
XML.content(result.body).mkString match { |
45709
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
wenzelm
parents:
45672
diff
changeset
|
381 |
case Protocol.Removed(removed) => |
44676 | 382 |
try { handle_removed(removed) } |
383 |
catch { case _: Document.State.Fail => bad_result(result) } |
|
384 |
case _ => bad_result(result) |
|
385 |
} |
|
45666 | 386 |
case Isabelle_Markup.Invoke_Scala(name, id) if result.is_raw => |
43748 | 387 |
Future.fork { |
388 |
val arg = XML.content(result.body).mkString |
|
389 |
val (tag, res) = Invoke_Scala.method(name, arg) |
|
390 |
prover.get.invoke_scala(id, tag, res) |
|
391 |
} |
|
46121 | 392 |
case Isabelle_Markup.Cancel_Scala(id) if result.is_raw => |
393 |
System.err.println("cancel_scala " + id) // FIXME actually cancel JVM task |
|
394 |
case Isabelle_Markup.Ready if result.is_raw => |
|
395 |
// FIXME move to ML side (!?) |
|
396 |
syntax += ("hence", Keyword.PRF_ASM_GOAL, "then have") |
|
397 |
syntax += ("thus", Keyword.PRF_ASM_GOAL, "then show") |
|
398 |
phase = Session.Ready |
|
38722
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38639
diff
changeset
|
399 |
case _ => |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
400 |
if (result.is_syslog) { |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
401 |
reverse_syslog ::= result.message |
46121 | 402 |
if (result.is_exit && phase == Session.Startup) phase = Session.Failed |
39696 | 403 |
else if (result.is_exit) phase = Session.Inactive |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
404 |
} |
39626
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
wenzelm
parents:
39625
diff
changeset
|
405 |
else if (result.is_stdout) { } |
39625 | 406 |
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
|
407 |
result.body match { |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
408 |
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
|
409 |
case List(Keyword.Keyword_Decl(name)) => syntax += name |
44574 | 410 |
case List(Thy_Info.Loaded_Theory(name)) => thy_load.register_thy(name) |
39625 | 411 |
case _ => bad_result(result) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
412 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
413 |
} |
39625 | 414 |
else bad_result(result) |
43748 | 415 |
} |
34809 | 416 |
} |
38221 | 417 |
//}}} |
34809 | 418 |
|
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
419 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
420 |
/* main loop */ |
34809 | 421 |
|
43716 | 422 |
//{{{ |
38639
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
423 |
var finished = false |
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
424 |
while (!finished) { |
44722
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
425 |
receiveWithin(commands_changed_delay.flush_timeout) { |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
426 |
case TIMEOUT => commands_changed_delay.flush() |
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
wenzelm
parents:
44721
diff
changeset
|
427 |
|
45158 | 428 |
case Start(timeout, args) if prover.isEmpty => |
39701 | 429 |
if (phase == Session.Inactive || phase == Session.Failed) { |
430 |
phase = Session.Startup |
|
45709
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
wenzelm
parents:
45672
diff
changeset
|
431 |
prover = Some(new Isabelle_Process(timeout, receiver.invoke _, args) with Protocol) |
39701 | 432 |
} |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
433 |
|
39701 | 434 |
case Stop => |
435 |
if (phase == Session.Ready) { |
|
436 |
global_state.change(_ => Document.State.init) // FIXME event bus!? |
|
437 |
phase = Session.Shutdown |
|
43649 | 438 |
prover.get.terminate |
43717 | 439 |
prover = None |
39701 | 440 |
phase = Session.Inactive |
441 |
} |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
442 |
finished = true |
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
443 |
receiver.cancel() |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
444 |
reply(()) |
38850
5c3e5c548f12
session_actor: ignore spurious TIMEOUT (again) -- probably stemming from earlier use of receiveWithin;
wenzelm
parents:
38849
diff
changeset
|
445 |
|
44775
27930cf6f0f7
added "cancel" button based on cancel_execution, not interrupt (cf. 156be0e43336);
wenzelm
parents:
44734
diff
changeset
|
446 |
case Cancel_Execution if prover.isDefined => |
27930cf6f0f7
added "cancel" button based on cancel_execution, not interrupt (cf. 156be0e43336);
wenzelm
parents:
44734
diff
changeset
|
447 |
prover.get.cancel_execution() |
43716 | 448 |
|
44615 | 449 |
case Init_Node(name, header, perspective, text) if prover.isDefined => |
43716 | 450 |
// FIXME compare with existing node |
44615 | 451 |
handle_edits(name, header, |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44384
diff
changeset
|
452 |
List(Document.Node.Clear(), |
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44384
diff
changeset
|
453 |
Document.Node.Edits(List(Text.Edit.insert(0, text))), |
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44384
diff
changeset
|
454 |
Document.Node.Perspective(perspective))) |
43716 | 455 |
reply(()) |
456 |
||
44615 | 457 |
case Edit_Node(name, header, perspective, text_edits) if prover.isDefined => |
44484
470f2ee5950e
tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents:
44481
diff
changeset
|
458 |
if (text_edits.isEmpty && global_state().tip_stable && |
470f2ee5950e
tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents:
44481
diff
changeset
|
459 |
perspective.range.stop <= global_state().last_exec_offset(name)) |
470f2ee5950e
tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents:
44481
diff
changeset
|
460 |
update_perspective(name, perspective) |
470f2ee5950e
tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents:
44481
diff
changeset
|
461 |
else |
44615 | 462 |
handle_edits(name, header, |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
463 |
List(Document.Node.Edits(text_edits), Document.Node.Perspective(perspective))) |
43718 | 464 |
reply(()) |
465 |
||
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
466 |
case Messages(msgs) => |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
467 |
msgs foreach { |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
468 |
case input: Isabelle_Process.Input => |
46120 | 469 |
protocol_messages.event(input) |
43721
fad8634cee62
echo prover input via raw_messages, for improved protocol tracing;
wenzelm
parents:
43720
diff
changeset
|
470 |
|
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
471 |
case result: Isabelle_Process.Result => |
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
472 |
handle_result(result) |
44734
7313e2db3d39
more specific message channels to avoid potential bottle-neck of raw_messages;
wenzelm
parents:
44733
diff
changeset
|
473 |
if (result.is_syslog) syslog_messages.event(result) |
45633
2cb7e34f6096
retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
wenzelm
parents:
45248
diff
changeset
|
474 |
if (result.is_stdout || result.is_stderr) raw_output_messages.event(result) |
46120 | 475 |
protocol_messages.event(result) |
44733
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
wenzelm
parents:
44732
diff
changeset
|
476 |
} |
43716 | 477 |
|
45635
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
478 |
case change: Change_Node |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
479 |
if prover.isDefined && global_state().is_assigned(change.previous) => |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
480 |
handle_change(change) |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
481 |
|
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
482 |
case bad if !bad.isInstanceOf[Change_Node] => |
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
483 |
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
|
484 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
485 |
} |
43716 | 486 |
//}}} |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
487 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
488 |
|
34809 | 489 |
|
43716 | 490 |
/* actions */ |
34809 | 491 |
|
45158 | 492 |
def start(timeout: Time, args: List[String]) |
493 |
{ session_actor ! Start(timeout, args) } |
|
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
494 |
|
45158 | 495 |
def start(args: List[String]) { start (Time.seconds(25), args) } |
45076 | 496 |
|
45635
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
wenzelm
parents:
45633
diff
changeset
|
497 |
def stop() { commands_changed_buffer !? Stop; change_parser !? 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
|
498 |
|
44775
27930cf6f0f7
added "cancel" button based on cancel_execution, not interrupt (cf. 156be0e43336);
wenzelm
parents:
44734
diff
changeset
|
499 |
def cancel_execution() { session_actor ! Cancel_Execution } |
43648 | 500 |
|
44615 | 501 |
def init_node(name: Document.Node.Name, |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44384
diff
changeset
|
502 |
header: Document.Node_Header, perspective: Text.Perspective, text: String) |
44615 | 503 |
{ session_actor !? Init_Node(name, header, perspective, text) } |
38221 | 504 |
|
44615 | 505 |
def edit_node(name: Document.Node.Name, |
506 |
header: Document.Node_Header, perspective: Text.Perspective, edits: List[Text.Edit]) |
|
507 |
{ session_actor !? Edit_Node(name, header, perspective, edits) } |
|
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
508 |
} |