| author | blanchet | 
| Thu, 21 Feb 2013 12:22:26 +0100 | |
| changeset 51214 | 4fb12e2598dc | 
| parent 51083 | 10062c40ddaa | 
| child 51662 | 3391a493f39a | 
| 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: 
43649diff
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: 
34859diff
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: 
44732diff
changeset | 10 | |
| 43520 
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
 wenzelm parents: 
43443diff
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: 
44732diff
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: 
44732diff
changeset | 14 | import scala.collection.mutable | 
| 46771 
06a9b24c4a36
explicit syslog_limit reduces danger of low-level message flooding;
 wenzelm parents: 
46739diff
changeset | 15 | import scala.collection.immutable.Queue | 
| 34820 
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
 wenzelm parents: 
34819diff
changeset | 16 | import scala.actors.TIMEOUT | 
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 17 | import scala.actors.Actor._ | 
| 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 18 | |
| 34815 
6bae73cd8e33
unified Command and Command_State, eliminated separate Accumulator;
 wenzelm parents: 
34813diff
changeset | 19 | |
| 34791 | 20 | object Session | 
| 21 | {
 | |
| 34813 
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
 wenzelm parents: 
34810diff
changeset | 22 | /* events */ | 
| 
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
 wenzelm parents: 
34810diff
changeset | 23 | |
| 43716 | 24 |   //{{{
 | 
| 50697 
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
 wenzelm parents: 
50566diff
changeset | 25 | case class Statistics(props: Properties.T) | 
| 50117 | 26 | case class Global_Options(options: Options) | 
| 44805 | 27 | case object Caret_Focus | 
| 49196 
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
 wenzelm parents: 
48999diff
changeset | 28 | case class Raw_Edits(edits: List[Document.Edit_Text]) | 
| 50500 
c94bba7906d2
identify dialogs via official serial and maintain as result message;
 wenzelm parents: 
50499diff
changeset | 29 | case class Dialog_Result(id: Document.ID, serial: Long, result: String) | 
| 47027 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 30 | case class Commands_Changed( | 
| 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 31 | assignment: Boolean, nodes: Set[Document.Node.Name], commands: Set[Command]) | 
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 32 | |
| 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 33 | sealed abstract class Phase | 
| 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 34 | case object Inactive extends Phase | 
| 39701 | 35 | case object Startup extends Phase // transient | 
| 36 | case object Failed extends Phase | |
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 37 | case object Ready extends Phase | 
| 39701 | 38 | case object Shutdown extends Phase // transient | 
| 43716 | 39 | //}}} | 
| 34791 | 40 | } | 
| 41 | ||
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 42 | |
| 48870 
4accee106f0f
clarified initialization of Thy_Load, Thy_Info, Session;
 wenzelm parents: 
48794diff
changeset | 43 | class Session(val thy_load: Thy_Load) | 
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 44 | {
 | 
| 47653 | 45 | /* global flags */ | 
| 46 | ||
| 47 | @volatile var timing: Boolean = false | |
| 48 | @volatile var verbose: Boolean = false | |
| 49 | ||
| 50 | ||
| 49288 | 51 | /* tuning parameters */ | 
| 52 | ||
| 53 | def output_delay: Time = Time.seconds(0.1) // prover output (markup, common messages) | |
| 49293 
afcccb9bfa3b
prefer tuning parameters as public methods (again) -- to allow overriding in applications;
 wenzelm parents: 
49288diff
changeset | 54 | def message_delay: Time = Time.seconds(0.01) // incoming prover messages | 
| 
afcccb9bfa3b
prefer tuning parameters as public methods (again) -- to allow overriding in applications;
 wenzelm parents: 
49288diff
changeset | 55 | def prune_delay: Time = Time.seconds(60.0) // prune history -- delete old versions | 
| 
afcccb9bfa3b
prefer tuning parameters as public methods (again) -- to allow overriding in applications;
 wenzelm parents: 
49288diff
changeset | 56 | def prune_size: Int = 0 // size of retained history | 
| 
afcccb9bfa3b
prefer tuning parameters as public methods (again) -- to allow overriding in applications;
 wenzelm parents: 
49288diff
changeset | 57 | def syslog_limit: Int = 100 | 
| 49524 
68796a77c42b
Thy_Syntax.consolidate_spans is subject to editor_reparse_limit, for improved experience of unbalanced comments etc.;
 wenzelm parents: 
49523diff
changeset | 58 | def reparse_limit: Int = 0 | 
| 37849 | 59 | |
| 60 | ||
| 34809 | 61 | /* pervasive event buses */ | 
| 62 | ||
| 50433 
9131dadb2bf7
basic monitor panel, using the powerful jfreechart library;
 wenzelm parents: 
50363diff
changeset | 63 | val statistics = new Event_Bus[Session.Statistics] | 
| 50117 | 64 | val global_options = new Event_Bus[Session.Global_Options] | 
| 44805 | 65 | val caret_focus = new Event_Bus[Session.Caret_Focus.type] | 
| 49196 
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
 wenzelm parents: 
48999diff
changeset | 66 | val raw_edits = new Event_Bus[Session.Raw_Edits] | 
| 43716 | 67 | val commands_changed = new Event_Bus[Session.Commands_Changed] | 
| 68 | val phase_changed = new Event_Bus[Session.Phase] | |
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 69 | val syslog_messages = new Event_Bus[Isabelle_Process.Output] | 
| 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 70 | val raw_output_messages = new Event_Bus[Isabelle_Process.Output] | 
| 46774 | 71 | val all_messages = new Event_Bus[Isabelle_Process.Message] // potential bottle-neck | 
| 43716 | 72 | |
| 34809 | 73 | |
| 74 | ||
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 75 | /** buffered command changes (delay_first discipline) **/ | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 76 | |
| 43716 | 77 |   //{{{
 | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 78 | private case object Stop | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 79 | |
| 44722 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 80 | private val (_, commands_changed_buffer) = | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 81 |     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: 
38722diff
changeset | 82 |   {
 | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 83 | var finished = false | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 84 |     while (!finished) {
 | 
| 44722 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 85 |       receive {
 | 
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 86 | case Stop => finished = true; reply(()) | 
| 44722 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 87 | 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: 
44721diff
changeset | 88 |         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: 
38722diff
changeset | 89 | } | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 90 | } | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 91 | } | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 92 | //}}} | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 93 | |
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 94 | |
| 45635 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 95 | /** pipelined change parsing **/ | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 96 | |
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 97 |   //{{{
 | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 98 | 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: 
45633diff
changeset | 99 | previous: Future[Document.Version], | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 100 | 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: 
45633diff
changeset | 101 | 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: 
45633diff
changeset | 102 | |
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 103 |   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: 
45633diff
changeset | 104 |   {
 | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 105 | var finished = false | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 106 |     while (!finished) {
 | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 107 |       receive {
 | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 108 | 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: 
45633diff
changeset | 109 | |
| 47629 | 110 | case Text_Edits(previous, text_edits, version_result) => | 
| 45635 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 111 | val prev = previous.get_finished | 
| 47653 | 112 | val (doc_edits, version) = | 
| 50566 
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
 wenzelm parents: 
50501diff
changeset | 113 |             Timing.timeit("Thy_Load.text_edits", timing) {
 | 
| 
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
 wenzelm parents: 
50501diff
changeset | 114 | thy_load.text_edits(reparse_limit, prev, text_edits) | 
| 47653 | 115 | } | 
| 45635 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 116 | version_result.fulfill(version) | 
| 47629 | 117 | sender ! Change(doc_edits, prev, version) | 
| 45635 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 118 | |
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 119 |         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: 
45633diff
changeset | 120 | } | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 121 | } | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 122 | } | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 123 | //}}} | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 124 | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 125 | |
| 47653 | 126 | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 127 | /** main protocol actor **/ | 
| 34809 | 128 | |
| 43651 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 129 | /* global state */ | 
| 43644 | 130 | |
| 46771 
06a9b24c4a36
explicit syslog_limit reduces danger of low-level message flooding;
 wenzelm parents: 
46739diff
changeset | 131 | private val syslog = Volatile(Queue.empty[XML.Elem]) | 
| 49416 
1053a564dd25
some actual rich text markup via XML.content_markup;
 wenzelm parents: 
49293diff
changeset | 132 | def current_syslog(): String = cat_lines(syslog().iterator.map(XML.content)) | 
| 39626 
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
 wenzelm parents: 
39625diff
changeset | 133 | |
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 134 | @volatile private var _phase: Session.Phase = Session.Inactive | 
| 39633 | 135 | private def phase_=(new_phase: Session.Phase) | 
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 136 |   {
 | 
| 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 137 | _phase = new_phase | 
| 39701 | 138 | phase_changed.event(new_phase) | 
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 139 | } | 
| 43716 | 140 | def phase = _phase | 
| 43553 | 141 | def is_ready: Boolean = phase == Session.Ready | 
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 142 | |
| 45248 | 143 | private val global_state = Volatile(Document.State.init) | 
| 43719 | 144 | def current_state(): Document.State = global_state() | 
| 46944 
9fc22eb6408c
more recent recent_syntax, e.g. relevant for document rendering during startup;
 wenzelm parents: 
46942diff
changeset | 145 | |
| 
9fc22eb6408c
more recent recent_syntax, e.g. relevant for document rendering during startup;
 wenzelm parents: 
46942diff
changeset | 146 | def recent_syntax(): Outer_Syntax = | 
| 
9fc22eb6408c
more recent recent_syntax, e.g. relevant for document rendering during startup;
 wenzelm parents: 
46942diff
changeset | 147 |   {
 | 
| 
9fc22eb6408c
more recent recent_syntax, e.g. relevant for document rendering during startup;
 wenzelm parents: 
46942diff
changeset | 148 | val version = current_state().recent_finished.version.get_finished | 
| 48870 
4accee106f0f
clarified initialization of Thy_Load, Thy_Info, Session;
 wenzelm parents: 
48794diff
changeset | 149 | if (version.is_init) thy_load.base_syntax | 
| 46944 
9fc22eb6408c
more recent recent_syntax, e.g. relevant for document rendering during startup;
 wenzelm parents: 
46942diff
changeset | 150 | else version.syntax | 
| 
9fc22eb6408c
more recent recent_syntax, e.g. relevant for document rendering during startup;
 wenzelm parents: 
46942diff
changeset | 151 | } | 
| 34816 
d33312514220
maintain generic session entities -- cover commands, states, etc. (but not yet documents);
 wenzelm parents: 
34815diff
changeset | 152 | |
| 44960 | 153 | def snapshot(name: Document.Node.Name = Document.Node.Name.empty, | 
| 154 | pending_edits: List[Text.Edit] = Nil): Document.Snapshot = | |
| 43719 | 155 | global_state().snapshot(name, pending_edits) | 
| 43716 | 156 | |
| 43651 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 157 | |
| 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 158 | /* theory files */ | 
| 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 159 | |
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48422diff
changeset | 160 | 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: 
44436diff
changeset | 161 |   {
 | 
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48422diff
changeset | 162 | val header1 = | 
| 48870 
4accee106f0f
clarified initialization of Thy_Load, Thy_Info, Session;
 wenzelm parents: 
48794diff
changeset | 163 | if (thy_load.loaded_theories(name.theory)) | 
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48422diff
changeset | 164 |         header.error("Attempt to update loaded theory " + quote(name.theory))
 | 
| 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48422diff
changeset | 165 | else header | 
| 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48422diff
changeset | 166 | (name, Document.Node.Deps(header1)) | 
| 44442 
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
 wenzelm parents: 
44436diff
changeset | 167 | } | 
| 
cb18e4f09053
clarified norm_header/header_edit -- disallow update of loaded theories;
 wenzelm parents: 
44436diff
changeset | 168 | |
| 43651 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 169 | |
| 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 170 | /* actor messages */ | 
| 
511df47bcadc
some support for theory files within Isabelle/Scala session;
 wenzelm parents: 
43649diff
changeset | 171 | |
| 48870 
4accee106f0f
clarified initialization of Thy_Load, Thy_Info, Session;
 wenzelm parents: 
48794diff
changeset | 172 | private case class Start(args: List[String]) | 
| 44775 
27930cf6f0f7
added "cancel" button based on cancel_execution, not interrupt (cf. 156be0e43336);
 wenzelm parents: 
44734diff
changeset | 173 | private case object Cancel_Execution | 
| 47629 | 174 | private case class Change( | 
| 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: 
44732diff
changeset | 178 | private case class Messages(msgs: List[Isabelle_Process.Message]) | 
| 49470 | 179 | private case class Finished_Scala(id: String, tag: Invoke_Scala.Tag.Value, result: String) | 
| 41534 | 180 | |
| 39572 | 181 |   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: 
38569diff
changeset | 182 |   {
 | 
| 43648 | 183 | val this_actor = self | 
| 34809 | 184 | |
| 44676 | 185 | var prune_next = System.currentTimeMillis() + prune_delay.ms | 
| 186 | ||
| 49470 | 187 | var futures = Map.empty[String, java.util.concurrent.Future[Unit]] | 
| 188 | ||
| 34809 | 189 | |
| 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: 
44732diff
changeset | 190 | /* 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: 
44732diff
changeset | 191 | |
| 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 192 | 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: 
44732diff
changeset | 193 |     {
 | 
| 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 194 | 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: 
44732diff
changeset | 195 | |
| 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 196 |       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: 
44732diff
changeset | 197 |         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: 
44732diff
changeset | 198 | 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: 
44732diff
changeset | 199 | 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: 
44732diff
changeset | 200 | 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: 
44732diff
changeset | 201 | } | 
| 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 202 | } | 
| 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 203 |       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: 
44732diff
changeset | 204 | 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: 
44732diff
changeset | 205 |         msg match {
 | 
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 206 | case output: Isabelle_Process.Output => | 
| 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 207 | if (output.is_syslog) | 
| 46771 
06a9b24c4a36
explicit syslog_limit reduces danger of low-level message flooding;
 wenzelm parents: 
46739diff
changeset | 208 | syslog >> (queue => | 
| 
06a9b24c4a36
explicit syslog_limit reduces danger of low-level message flooding;
 wenzelm parents: 
46739diff
changeset | 209 |                 {
 | 
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 210 | val queue1 = queue.enqueue(output.message) | 
| 46771 
06a9b24c4a36
explicit syslog_limit reduces danger of low-level message flooding;
 wenzelm parents: 
46739diff
changeset | 211 | if (queue1.length > syslog_limit) queue1.dequeue._2 else queue1 | 
| 
06a9b24c4a36
explicit syslog_limit reduces danger of low-level message flooding;
 wenzelm parents: 
46739diff
changeset | 212 | }) | 
| 46774 | 213 | if (output.is_protocol) flush() | 
| 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: 
44732diff
changeset | 214 | 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: 
44732diff
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: 
44732diff
changeset | 216 | } | 
| 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 217 | |
| 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 218 |       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: 
44732diff
changeset | 219 |       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: 
44732diff
changeset | 220 | |
| 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 221 |       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: 
44732diff
changeset | 222 | } | 
| 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 223 | |
| 45709 
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
 wenzelm parents: 
45672diff
changeset | 224 | 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: 
44732diff
changeset | 225 | |
| 
329320fc88df
buffer prover messages to prevent overloading of session_actor input channel -- which is critical due to synchronous messages wrt. GUI thread;
 wenzelm parents: 
44732diff
changeset | 226 | |
| 44722 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 227 | /* delayed command changes */ | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 228 | |
| 46570 | 229 | object delay_commands_changed | 
| 44722 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 230 |     {
 | 
| 47027 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 231 | private var changed_assignment: Boolean = false | 
| 44722 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 232 | 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: 
44721diff
changeset | 233 | 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: 
44721diff
changeset | 234 | |
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 235 | private var flush_time: Option[Long] = None | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 236 | |
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 237 | def flush_timeout: Long = | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 238 |         flush_time match {
 | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 239 | case None => 5000L | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 240 | 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: 
44721diff
changeset | 241 | } | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 242 | |
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 243 | def flush() | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 244 |       {
 | 
| 47027 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 245 | if (changed_assignment || !changed_nodes.isEmpty || !changed_commands.isEmpty) | 
| 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 246 | commands_changed_buffer ! | 
| 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 247 | Session.Commands_Changed(changed_assignment, changed_nodes, changed_commands) | 
| 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 248 | changed_assignment = false | 
| 44722 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 249 | changed_nodes = Set.empty | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 250 | changed_commands = Set.empty | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 251 | flush_time = None | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 252 | } | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 253 | |
| 47027 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 254 | def invoke(assign: Boolean, commands: List[Command]) | 
| 44722 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 255 |       {
 | 
| 47027 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 256 | changed_assignment |= assign | 
| 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 257 |         for (command <- commands) {
 | 
| 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 258 | changed_nodes += command.node_name | 
| 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 259 | changed_commands += command | 
| 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 260 | } | 
| 44722 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 261 | val now = System.currentTimeMillis() | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 262 |         flush_time match {
 | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 263 | 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: 
44721diff
changeset | 264 | case Some(time) => if (now >= time) flush() | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 265 | } | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 266 | } | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 267 | } | 
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 268 | |
| 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 269 | |
| 43716 | 270 | /* resulting changes */ | 
| 34809 | 271 | |
| 47629 | 272 | def handle_change(change: Change) | 
| 38221 | 273 |     //{{{
 | 
| 34809 | 274 |     {
 | 
| 43722 | 275 | val previous = change.previous | 
| 276 | val version = change.version | |
| 277 | val doc_edits = change.doc_edits | |
| 38366 
fea82d1add74
simplified/clarified Change: transition prev --edits--> result, based on futures;
 wenzelm parents: 
38365diff
changeset | 278 | |
| 44383 | 279 | def id_command(command: Command) | 
| 43720 | 280 |       {
 | 
| 44582 | 281 |         if (!global_state().defined_command(command.id)) {
 | 
| 46687 | 282 | global_state >> (_.define_command(command)) | 
| 44644 
317e4962dd0f
clarified define_command: store name as structural information;
 wenzelm parents: 
44616diff
changeset | 283 | prover.get.define_command(command) | 
| 43720 | 284 | } | 
| 285 | } | |
| 44383 | 286 |       doc_edits foreach {
 | 
| 287 | case (_, edit) => | |
| 288 |           edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
 | |
| 289 | } | |
| 43722 | 290 | |
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 291 | val assignment = global_state().the_assignment(previous).check_finished | 
| 46687 | 292 | global_state >> (_.define_version(version, assignment)) | 
| 44481 | 293 | prover.get.update(previous.id, version.id, doc_edits) | 
| 34809 | 294 | } | 
| 38221 | 295 | //}}} | 
| 34809 | 296 | |
| 297 | ||
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 298 | /* prover output */ | 
| 34809 | 299 | |
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 300 | def handle_output(output: Isabelle_Process.Output) | 
| 38221 | 301 |     //{{{
 | 
| 34809 | 302 |     {
 | 
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 303 | def bad_output(output: Isabelle_Process.Output) | 
| 43716 | 304 |       {
 | 
| 305 | if (verbose) | |
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 306 |           System.err.println("Ignoring prover output: " + output.message.toString)
 | 
| 43716 | 307 | } | 
| 308 | ||
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 309 |       output.properties match {
 | 
| 46122 | 310 | |
| 46774 | 311 | case Position.Id(state_id) if !output.is_protocol => | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38366diff
changeset | 312 |           try {
 | 
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 313 | val st = global_state >>> (_.accumulate(state_id, output.message)) | 
| 47027 
fc3bb6c02a3c
explicit propagation of assignment event, even if changed command set is empty;
 wenzelm parents: 
46944diff
changeset | 314 | delay_commands_changed.invoke(false, List(st.command)) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38366diff
changeset | 315 | } | 
| 43748 | 316 |           catch {
 | 
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 317 | case _: Document.State.Fail => bad_output(output) | 
| 43748 | 318 | } | 
| 46122 | 319 | |
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
50117diff
changeset | 320 | case Markup.Assign_Execs if output.is_protocol => | 
| 49416 
1053a564dd25
some actual rich text markup via XML.content_markup;
 wenzelm parents: 
49293diff
changeset | 321 |           XML.content(output.body) match {
 | 
| 45709 
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
 wenzelm parents: 
45672diff
changeset | 322 | case Protocol.Assign(id, assign) => | 
| 47629 | 323 |               try {
 | 
| 324 | val cmds = global_state >>> (_.assign(id, assign)) | |
| 325 | delay_commands_changed.invoke(true, cmds) | |
| 326 | } | |
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 327 |               catch { case _: Document.State.Fail => bad_output(output) }
 | 
| 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 328 | case _ => bad_output(output) | 
| 44661 
383c9d758a56
raw message function "assign_execs" avoids full overhead of decoding and caching message body;
 wenzelm parents: 
44644diff
changeset | 329 | } | 
| 44676 | 330 | // FIXME separate timeout event/message!? | 
| 331 |           if (prover.isDefined && System.currentTimeMillis() > prune_next) {
 | |
| 46687 | 332 | val old_versions = global_state >>> (_.prune_history(prune_size)) | 
| 44676 | 333 | if (!old_versions.isEmpty) prover.get.remove_versions(old_versions) | 
| 334 | prune_next = System.currentTimeMillis() + prune_delay.ms | |
| 335 | } | |
| 46122 | 336 | |
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
50117diff
changeset | 337 | case Markup.Removed_Versions if output.is_protocol => | 
| 49416 
1053a564dd25
some actual rich text markup via XML.content_markup;
 wenzelm parents: 
49293diff
changeset | 338 |           XML.content(output.body) match {
 | 
| 45709 
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
 wenzelm parents: 
45672diff
changeset | 339 | case Protocol.Removed(removed) => | 
| 47629 | 340 |               try {
 | 
| 341 | global_state >> (_.removed_versions(removed)) | |
| 342 | } | |
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 343 |               catch { case _: Document.State.Fail => bad_output(output) }
 | 
| 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 344 | case _ => bad_output(output) | 
| 44676 | 345 | } | 
| 46122 | 346 | |
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
50117diff
changeset | 347 | case Markup.Invoke_Scala(name, id) if output.is_protocol => | 
| 49470 | 348 | futures += (id -> | 
| 349 | default_thread_pool.submit(() => | |
| 350 |               {
 | |
| 351 | val arg = XML.content(output.body) | |
| 352 | val (tag, result) = Invoke_Scala.method(name, arg) | |
| 353 | this_actor ! Finished_Scala(id, tag, result) | |
| 354 | })) | |
| 46122 | 355 | |
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
50117diff
changeset | 356 | case Markup.Cancel_Scala(id) if output.is_protocol => | 
| 49470 | 357 |           futures.get(id) match {
 | 
| 358 | case Some(future) => | |
| 359 | future.cancel(true) | |
| 360 | this_actor ! Finished_Scala(id, Invoke_Scala.Tag.INTERRUPT, "") | |
| 361 | case None => | |
| 362 | } | |
| 46122 | 363 | |
| 50697 
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
 wenzelm parents: 
50566diff
changeset | 364 | case Markup.ML_Statistics(props) if output.is_protocol => | 
| 
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
 wenzelm parents: 
50566diff
changeset | 365 | statistics.event(Session.Statistics(props)) | 
| 50255 | 366 | |
| 50975 | 367 | case Markup.Task_Statistics(props) if output.is_protocol => | 
| 368 | // FIXME | |
| 369 | ||
| 48712 
6b7a9bcc0bae
simplified process startup phases: INIT suffices for is_ready;
 wenzelm parents: 
48710diff
changeset | 370 | case _ if output.is_init => | 
| 50433 
9131dadb2bf7
basic monitor panel, using the powerful jfreechart library;
 wenzelm parents: 
50363diff
changeset | 371 | phase = Session.Ready | 
| 46122 | 372 | |
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
50117diff
changeset | 373 | case Markup.Return_Code(rc) if output.is_exit => | 
| 48016 
edbc8e8accd9
more explicit treatment of return code vs. session phase;
 wenzelm parents: 
47653diff
changeset | 374 | if (rc == 0) phase = Session.Inactive | 
| 
edbc8e8accd9
more explicit treatment of return code vs. session phase;
 wenzelm parents: 
47653diff
changeset | 375 | else phase = Session.Failed | 
| 
edbc8e8accd9
more explicit treatment of return code vs. session phase;
 wenzelm parents: 
47653diff
changeset | 376 | |
| 48712 
6b7a9bcc0bae
simplified process startup phases: INIT suffices for is_ready;
 wenzelm parents: 
48710diff
changeset | 377 | case _ => bad_output(output) | 
| 43748 | 378 | } | 
| 34809 | 379 | } | 
| 38221 | 380 | //}}} | 
| 34809 | 381 | |
| 34820 
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
 wenzelm parents: 
34819diff
changeset | 382 | |
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 383 | /* main loop */ | 
| 34809 | 384 | |
| 43716 | 385 |     //{{{
 | 
| 38639 
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
 wenzelm parents: 
38569diff
changeset | 386 | var finished = false | 
| 
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
 wenzelm parents: 
38569diff
changeset | 387 |     while (!finished) {
 | 
| 46570 | 388 |       receiveWithin(delay_commands_changed.flush_timeout) {
 | 
| 389 | case TIMEOUT => delay_commands_changed.flush() | |
| 44722 
a8331fb5c959
commands_change_delay within main actor -- prevents overloading of commands_change_buffer input channel;
 wenzelm parents: 
44721diff
changeset | 390 | |
| 48870 
4accee106f0f
clarified initialization of Thy_Load, Thy_Info, Session;
 wenzelm parents: 
48794diff
changeset | 391 | case Start(args) if prover.isEmpty => | 
| 39701 | 392 |           if (phase == Session.Inactive || phase == Session.Failed) {
 | 
| 393 | phase = Session.Startup | |
| 48020 
a4f9957878ab
clarified prover startup: no timeout, read stderr more carefully;
 wenzelm parents: 
48016diff
changeset | 394 | prover = Some(new Isabelle_Process(receiver.invoke _, args) with Protocol) | 
| 39701 | 395 | } | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 396 | |
| 39701 | 397 | case Stop => | 
| 398 |           if (phase == Session.Ready) {
 | |
| 46687 | 399 | global_state >> (_ => Document.State.init) // FIXME event bus!? | 
| 39701 | 400 | phase = Session.Shutdown | 
| 43649 | 401 | prover.get.terminate | 
| 43717 | 402 | prover = None | 
| 39701 | 403 | phase = Session.Inactive | 
| 404 | } | |
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 405 | 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: 
44732diff
changeset | 406 | receiver.cancel() | 
| 39630 
44181423183a
explicit Session.Phase indication with associated event bus;
 wenzelm parents: 
39629diff
changeset | 407 | reply(()) | 
| 38850 
5c3e5c548f12
session_actor: ignore spurious TIMEOUT (again) -- probably stemming from earlier use of receiveWithin;
 wenzelm parents: 
38849diff
changeset | 408 | |
| 50117 | 409 | case Session.Global_Options(options) if prover.isDefined => | 
| 51083 
10062c40ddaa
avoid crash (NPE) when properties are changed during prover startup (e.g. by font scaling);
 wenzelm parents: 
50975diff
changeset | 410 | if (is_ready) prover.get.options(options) | 
| 50117 | 411 | |
| 44775 
27930cf6f0f7
added "cancel" button based on cancel_execution, not interrupt (cf. 156be0e43336);
 wenzelm parents: 
44734diff
changeset | 412 | case Cancel_Execution if prover.isDefined => | 
| 
27930cf6f0f7
added "cancel" button based on cancel_execution, not interrupt (cf. 156be0e43336);
 wenzelm parents: 
44734diff
changeset | 413 | prover.get.cancel_execution() | 
| 43716 | 414 | |
| 49196 
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
 wenzelm parents: 
48999diff
changeset | 415 | case raw @ Session.Raw_Edits(edits) if prover.isDefined => | 
| 47629 | 416 | prover.get.discontinue_execution() | 
| 43716 | 417 | |
| 47629 | 418 | val previous = global_state().history.tip.version | 
| 419 | val version = Future.promise[Document.Version] | |
| 420 | val change = global_state >>> (_.continue_history(previous, edits, version)) | |
| 49196 
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
 wenzelm parents: 
48999diff
changeset | 421 | raw_edits.event(raw) | 
| 47629 | 422 | change_parser ! Text_Edits(previous, edits, version) | 
| 423 | ||
| 43718 | 424 | reply(()) | 
| 425 | ||
| 50500 
c94bba7906d2
identify dialogs via official serial and maintain as result message;
 wenzelm parents: 
50499diff
changeset | 426 | case Session.Dialog_Result(id, serial, result) if prover.isDefined => | 
| 
c94bba7906d2
identify dialogs via official serial and maintain as result message;
 wenzelm parents: 
50499diff
changeset | 427 | prover.get.dialog_result(serial, result) | 
| 50501 
6f41f1646617
more careful handling of Dialog_Result, with active area and color feedback;
 wenzelm parents: 
50500diff
changeset | 428 | handle_output(new Isabelle_Process.Output(Protocol.Dialog_Result(id, serial, result))) | 
| 50498 | 429 | |
| 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: 
44732diff
changeset | 430 | 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: 
44732diff
changeset | 431 |           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: 
44732diff
changeset | 432 | case input: Isabelle_Process.Input => | 
| 46774 | 433 | all_messages.event(input) | 
| 43721 
fad8634cee62
echo prover input via raw_messages, for improved protocol tracing;
 wenzelm parents: 
43720diff
changeset | 434 | |
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 435 | case output: Isabelle_Process.Output => | 
| 48999 | 436 | if (output.is_stdout || output.is_stderr) raw_output_messages.event(output) | 
| 437 | else handle_output(output) | |
| 46772 
be21f050eda4
tuned signature -- emphasize Isabelle_Process Input vs. Output;
 wenzelm parents: 
46771diff
changeset | 438 | if (output.is_syslog) syslog_messages.event(output) | 
| 46774 | 439 | all_messages.event(output) | 
| 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: 
44732diff
changeset | 440 | } | 
| 43716 | 441 | |
| 47629 | 442 | case change: Change | 
| 45635 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 443 | 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: 
45633diff
changeset | 444 | handle_change(change) | 
| 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 445 | |
| 49470 | 446 | case Finished_Scala(id, tag, result) if prover.isDefined => | 
| 447 |           if (futures.isDefinedAt(id)) {
 | |
| 448 | prover.get.invoke_scala(id, tag, result) | |
| 449 | futures -= id | |
| 450 | } | |
| 451 | ||
| 47629 | 452 | case bad if !bad.isInstanceOf[Change] => | 
| 45635 
d9cf3520083c
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;
 wenzelm parents: 
45633diff
changeset | 453 |           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 | 454 | } | 
| 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 455 | } | 
| 43716 | 456 | //}}} | 
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 457 | } | 
| 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 458 | |
| 34809 | 459 | |
| 43716 | 460 | /* actions */ | 
| 34809 | 461 | |
| 48870 
4accee106f0f
clarified initialization of Thy_Load, Thy_Info, Session;
 wenzelm parents: 
48794diff
changeset | 462 | def start(args: List[String]) | 
| 50117 | 463 |   {
 | 
| 464 | global_options += session_actor | |
| 465 | session_actor ! Start(args) | |
| 466 | } | |
| 45076 | 467 | |
| 50117 | 468 | def stop() | 
| 469 |   {
 | |
| 470 | global_options -= session_actor | |
| 471 | commands_changed_buffer !? Stop | |
| 472 | change_parser !? Stop | |
| 473 | session_actor !? Stop | |
| 474 | } | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38722diff
changeset | 475 | |
| 44775 
27930cf6f0f7
added "cancel" button based on cancel_execution, not interrupt (cf. 156be0e43336);
 wenzelm parents: 
44734diff
changeset | 476 |   def cancel_execution() { session_actor ! Cancel_Execution }
 | 
| 43648 | 477 | |
| 50363 
2f8dc9e65401
tuned signature in accordance to document operations;
 wenzelm parents: 
50344diff
changeset | 478 | def update(edits: List[Document.Edit_Text]) | 
| 50745 
6e7e8e310797
ignore vacuous edits (e.g. stemming from Plugin.init_models) to avoid pointless protocol round-trip, which could lead to painting of outdated snapshot in the meantime (notably on Windows);
 wenzelm parents: 
50697diff
changeset | 479 |   { if (!edits.isEmpty) session_actor !? Session.Raw_Edits(edits) }
 | 
| 50498 | 480 | |
| 50500 
c94bba7906d2
identify dialogs via official serial and maintain as result message;
 wenzelm parents: 
50499diff
changeset | 481 | def dialog_result(id: Document.ID, serial: Long, result: String) | 
| 
c94bba7906d2
identify dialogs via official serial and maintain as result message;
 wenzelm parents: 
50499diff
changeset | 482 |   { session_actor ! Session.Dialog_Result(id, serial, result) }
 | 
| 34777 
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
 wenzelm parents: diff
changeset | 483 | } |