author | blanchet |
Fri, 01 Jul 2011 15:53:38 +0200 | |
changeset 43627 | ecd4bb7a8bc0 |
parent 43553 | df80747342cb |
child 43644 | ea08ce1c314b |
permissions | -rw-r--r-- |
36676 | 1 |
/* Title: Pure/System/session.scala |
2 |
Author: Makarius |
|
38221 | 3 |
Options: :folding=explicit:collapseFolds=1: |
36676 | 4 |
|
5 |
Isabelle session, potentially with running prover. |
|
6 |
*/ |
|
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
7 |
|
34871
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents:
34859
diff
changeset
|
8 |
package isabelle |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
9 |
|
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
43443
diff
changeset
|
10 |
import java.lang.System |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
11 |
|
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
12 |
import scala.actors.TIMEOUT |
38226
9d8848d70b0a
maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents:
38222
diff
changeset
|
13 |
import scala.actors.Actor |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
14 |
import scala.actors.Actor._ |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
15 |
|
34815
6bae73cd8e33
unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents:
34813
diff
changeset
|
16 |
|
34791 | 17 |
object Session |
18 |
{ |
|
34813
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents:
34810
diff
changeset
|
19 |
/* events */ |
f0107bc96961
more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents:
34810
diff
changeset
|
20 |
|
34791 | 21 |
case object Global_Settings |
37849 | 22 |
case object Perspective |
38849
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
23 |
case object Assignment |
38360 | 24 |
case class Commands_Changed(set: Set[Command]) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
25 |
|
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
26 |
sealed abstract class Phase |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
27 |
case object Inactive extends Phase |
39701 | 28 |
case object Startup extends Phase // transient |
29 |
case object Failed extends Phase |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
30 |
case object Ready extends Phase |
39701 | 31 |
case object Shutdown extends Phase // transient |
34791 | 32 |
} |
33 |
||
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
34 |
|
43443
5d9693c2337e
basic support for extended syntax styles: sub/superscript;
wenzelm
parents:
41534
diff
changeset
|
35 |
class Session(val system: Isabelle_System) |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
36 |
{ |
37849 | 37 |
/* real time parameters */ // FIXME properties or settings (!?) |
38 |
||
39 |
// user input (e.g. text edits, cursor movement) |
|
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
40 |
val input_delay = Time.seconds(0.3) |
37849 | 41 |
|
42 |
// prover output (markup, common messages) |
|
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
43 |
val output_delay = Time.seconds(0.1) |
37849 | 44 |
|
45 |
// GUI layout updates |
|
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
46 |
val update_delay = Time.seconds(0.5) |
37849 | 47 |
|
48 |
||
34809 | 49 |
/* pervasive event buses */ |
50 |
||
39701 | 51 |
val phase_changed = new Event_Bus[Session.Phase] |
34809 | 52 |
val global_settings = new Event_Bus[Session.Global_Settings.type] |
39588 | 53 |
val raw_messages = new Event_Bus[Isabelle_Process.Result] |
38360 | 54 |
val commands_changed = new Event_Bus[Session.Commands_Changed] |
37849 | 55 |
val perspective = new Event_Bus[Session.Perspective.type] |
38849
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
56 |
val assignments = new Event_Bus[Session.Assignment.type] |
34809 | 57 |
|
58 |
||
34778 | 59 |
/* unique ids */ |
60 |
||
38363
af7f41a8a0a8
clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents:
38360
diff
changeset
|
61 |
private var id_count: Document.ID = 0 |
38419 | 62 |
def new_id(): Document.ID = synchronized { |
38355
8cb265fb12fe
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents:
38260
diff
changeset
|
63 |
require(id_count > java.lang.Long.MIN_VALUE) |
8cb265fb12fe
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents:
38260
diff
changeset
|
64 |
id_count -= 1 |
8cb265fb12fe
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents:
38260
diff
changeset
|
65 |
id_count |
8cb265fb12fe
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents:
38260
diff
changeset
|
66 |
} |
34778 | 67 |
|
68 |
||
34826 | 69 |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
70 |
/** 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
|
71 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
72 |
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
|
73 |
|
39572 | 74 |
private val (_, command_change_buffer) = |
75 |
Simple_Thread.actor("command_change_buffer", daemon = true) |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
76 |
//{{{ |
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 |
var changed: Set[Command] = Set() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
79 |
var flush_time: Option[Long] = None |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
80 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
81 |
def flush_timeout: Long = |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
82 |
flush_time match { |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
83 |
case None => 5000L |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
84 |
case Some(time) => (time - System.currentTimeMillis()) max 0 |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
85 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
86 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
87 |
def flush() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
88 |
{ |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
89 |
if (!changed.isEmpty) commands_changed.event(Session.Commands_Changed(changed)) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
90 |
changed = Set() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
91 |
flush_time = None |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
92 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
93 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
94 |
def invoke() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
95 |
{ |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
96 |
val now = System.currentTimeMillis() |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
97 |
flush_time match { |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
98 |
case None => flush_time = Some(now + output_delay.ms) |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
99 |
case Some(time) => if (now >= time) flush() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
100 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
101 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
102 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
103 |
var finished = false |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
104 |
while (!finished) { |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
105 |
receiveWithin(flush_timeout) { |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
106 |
case command: Command => changed += command; invoke() |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
107 |
case TIMEOUT => flush() |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
108 |
case Stop => finished = true; reply(()) |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
109 |
case bad => System.err.println("command_change_buffer: ignoring bad message " + bad) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
110 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
111 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
112 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
113 |
//}}} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
114 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
115 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
116 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
117 |
/** main protocol actor **/ |
34809 | 118 |
|
34851
304a86164dd2
provide global "Isabelle" within interpreter loop -- using import instead of val avoids pontential conflicts with later import isabelle.jedit._;
wenzelm
parents:
34848
diff
changeset
|
119 |
@volatile private var syntax = new Outer_Syntax(system.symbols) |
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 |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
126 |
def phase = _phase |
39633 | 127 |
private def phase_=(new_phase: Session.Phase) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
128 |
{ |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
129 |
_phase = new_phase |
39701 | 130 |
phase_changed.event(new_phase) |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
131 |
} |
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 |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
134 |
private val global_state = new Volatile(Document.State.init) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
135 |
def current_state(): Document.State = global_state.peek() |
34816
d33312514220
maintain generic session entities -- cover commands, states, etc. (but not yet documents);
wenzelm
parents:
34815
diff
changeset
|
136 |
|
39593 | 137 |
private case object Interrupt_Prover |
40479 | 138 |
private case class Edit_Version(edits: List[Document.Edit_Text]) |
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
139 |
private case class Start(timeout: Time, args: List[String]) |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
140 |
|
41534 | 141 |
var verbose: Boolean = false |
142 |
||
39572 | 143 |
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
|
144 |
{ |
34809 | 145 |
var prover: Isabelle_Process with Isar_Document = null |
146 |
||
147 |
||
148 |
/* document changes */ |
|
149 |
||
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38226
diff
changeset
|
150 |
def handle_change(change: Document.Change) |
38221 | 151 |
//{{{ |
34809 | 152 |
{ |
38848
9483bb678d96
use Future.get_finished where this is the intended meaning -- prefer immediate crash over deadlock due to promises that are never fulfilled;
wenzelm
parents:
38842
diff
changeset
|
153 |
val previous = change.previous.get_finished |
39698 | 154 |
val (node_edits, version) = change.result.get_finished |
38366
fea82d1add74
simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents:
38365
diff
changeset
|
155 |
|
38848
9483bb678d96
use Future.get_finished where this is the intended meaning -- prefer immediate crash over deadlock due to promises that are never fulfilled;
wenzelm
parents:
38842
diff
changeset
|
156 |
var former_assignment = global_state.peek().the_assignment(previous).get_finished |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
157 |
for { |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
158 |
(name, Some(cmd_edits)) <- node_edits |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
159 |
(prev, None) <- cmd_edits |
38417 | 160 |
removed <- previous.nodes(name).commands.get_after(prev) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
161 |
} former_assignment -= removed |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
162 |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
163 |
val id_edits = |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
164 |
node_edits map { |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
165 |
case (name, None) => (name, None) |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
166 |
case (name, Some(cmd_edits)) => |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
167 |
val ids = |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
168 |
cmd_edits map { |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
169 |
case (c1, c2) => |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
170 |
val id1 = c1.map(_.id) |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
171 |
val id2 = |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
172 |
c2 match { |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
173 |
case None => None |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
174 |
case Some(command) => |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
175 |
if (global_state.peek().lookup_command(command.id).isEmpty) { |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
176 |
global_state.change(_.define_command(command)) |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
177 |
prover.define_command(command.id, system.symbols.encode(command.source)) |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
178 |
} |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
179 |
Some(command.id) |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
180 |
} |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
181 |
(id1, id2) |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
182 |
} |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
183 |
(name -> Some(ids)) |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
184 |
} |
39698 | 185 |
global_state.change(_.define_version(version, former_assignment)) |
186 |
prover.edit_version(previous.id, version.id, id_edits) |
|
34809 | 187 |
} |
38221 | 188 |
//}}} |
34809 | 189 |
|
190 |
||
191 |
/* prover results */ |
|
192 |
||
34836
b83c7a738eb8
singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents:
34835
diff
changeset
|
193 |
def bad_result(result: Isabelle_Process.Result) |
b83c7a738eb8
singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents:
34835
diff
changeset
|
194 |
{ |
41534 | 195 |
if (verbose) |
196 |
System.err.println("Ignoring prover result: " + result.message.toString) |
|
34836
b83c7a738eb8
singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents:
34835
diff
changeset
|
197 |
} |
b83c7a738eb8
singleton status messages, with more precise patterns -- report bad messages;
wenzelm
parents:
34835
diff
changeset
|
198 |
|
34809 | 199 |
def handle_result(result: Isabelle_Process.Result) |
38221 | 200 |
//{{{ |
34809 | 201 |
{ |
38722
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38639
diff
changeset
|
202 |
result.properties match { |
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38639
diff
changeset
|
203 |
case Position.Id(state_id) => |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
204 |
try { |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
205 |
val st = global_state.change_yield(_.accumulate(state_id, result.message)) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
206 |
command_change_buffer ! st.command |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
207 |
} |
38414
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38413
diff
changeset
|
208 |
catch { case _: Document.State.Fail => bad_result(result) } |
38722
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38639
diff
changeset
|
209 |
case _ => |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
210 |
if (result.is_syslog) { |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
211 |
reverse_syslog ::= result.message |
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
212 |
if (result.is_ready) { |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
213 |
// FIXME move to ML side |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
214 |
syntax += ("hence", Keyword.PRF_ASM_GOAL, "then have") |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
215 |
syntax += ("thus", Keyword.PRF_ASM_GOAL, "then show") |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
216 |
phase = Session.Ready |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
40479
diff
changeset
|
217 |
} |
39701 | 218 |
else if (result.is_exit && phase == Session.Startup) phase = Session.Failed |
39696 | 219 |
else if (result.is_exit) phase = Session.Inactive |
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
220 |
} |
39626
a5d0bcfb95a3
manage persistent syslog via Session, not Isabelle_Process;
wenzelm
parents:
39625
diff
changeset
|
221 |
else if (result.is_stdout) { } |
39625 | 222 |
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
|
223 |
result.body match { |
38417 | 224 |
case List(Isar_Document.Assign(id, edits)) => |
38849
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
225 |
try { |
38882
e1fb3bbc22ab
Document state assignment indicates command change;
wenzelm
parents:
38850
diff
changeset
|
226 |
val cmds: List[Command] = global_state.change_yield(_.assign(id, edits)) |
e1fb3bbc22ab
Document state assignment indicates command change;
wenzelm
parents:
38850
diff
changeset
|
227 |
for (cmd <- cmds) command_change_buffer ! cmd |
38849
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
228 |
assignments.event(Session.Assignment) |
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
229 |
} |
38414
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38413
diff
changeset
|
230 |
catch { case _: Document.State.Fail => bad_result(result) } |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
231 |
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
|
232 |
case List(Keyword.Keyword_Decl(name)) => syntax += name |
39625 | 233 |
case _ => bad_result(result) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
234 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38366
diff
changeset
|
235 |
} |
39625 | 236 |
else bad_result(result) |
34809 | 237 |
} |
40566
36d4f2757f4f
post raw messages last, to ensure that result has been handled by session actor already (e.g. to avoid race between Session.session_actor and Session_Dockable.main_actor);
wenzelm
parents:
40533
diff
changeset
|
238 |
|
36d4f2757f4f
post raw messages last, to ensure that result has been handled by session actor already (e.g. to avoid race between Session.session_actor and Session_Dockable.main_actor);
wenzelm
parents:
40533
diff
changeset
|
239 |
raw_messages.event(result) |
34809 | 240 |
} |
38221 | 241 |
//}}} |
34809 | 242 |
|
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
243 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
244 |
/* main loop */ |
34809 | 245 |
|
38639
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
246 |
var finished = false |
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
247 |
while (!finished) { |
f642faca303e
main session actor as independent thread, to avoid starvation via regular worker pool;
wenzelm
parents:
38569
diff
changeset
|
248 |
receive { |
39593 | 249 |
case Interrupt_Prover => |
250 |
if (prover != null) prover.interrupt |
|
251 |
||
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
252 |
case Edit_Version(edits) if prover != null => |
39698 | 253 |
val previous = global_state.peek().history.tip.version |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
254 |
val result = Future.fork { Thy_Syntax.text_edits(Session.this, previous.join, edits) } |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
255 |
val change = global_state.change_yield(_.extend_history(previous, edits, result)) |
38849
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
256 |
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
257 |
val this_actor = self |
39698 | 258 |
change.version.map(_ => { |
38849
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
259 |
assignments.await { global_state.peek().is_assigned(previous.get_finished) } |
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
260 |
this_actor ! change }) |
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38848
diff
changeset
|
261 |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
262 |
reply(()) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
263 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
264 |
case change: Document.Change if prover != null => handle_change(change) |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
265 |
|
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
266 |
case result: Isabelle_Process.Result => handle_result(result) |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
267 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
268 |
case Start(timeout, args) if prover == null => |
39701 | 269 |
if (phase == Session.Inactive || phase == Session.Failed) { |
270 |
phase = Session.Startup |
|
271 |
prover = new Isabelle_Process(system, timeout, self, args:_*) with Isar_Document |
|
272 |
} |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
273 |
|
39701 | 274 |
case Stop => |
275 |
if (phase == Session.Ready) { |
|
276 |
global_state.change(_ => Document.State.init) // FIXME event bus!? |
|
277 |
phase = Session.Shutdown |
|
278 |
prover.terminate |
|
279 |
phase = Session.Inactive |
|
280 |
} |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
281 |
finished = true |
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
282 |
reply(()) |
38850
5c3e5c548f12
session_actor: ignore spurious TIMEOUT (again) -- probably stemming from earlier use of receiveWithin;
wenzelm
parents:
38849
diff
changeset
|
283 |
|
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
284 |
case bad if prover != null => |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
285 |
System.err.println("session_actor: ignoring bad message " + bad) |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
286 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
287 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
288 |
} |
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
289 |
|
34809 | 290 |
|
37129 | 291 |
|
38226
9d8848d70b0a
maintain editor history independently of Swing thread, which is potentially a bottle-neck or might be unavailable (e.g. in batch mode);
wenzelm
parents:
38222
diff
changeset
|
292 |
/** main methods **/ |
34809 | 293 |
|
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
40566
diff
changeset
|
294 |
def start(timeout: Time, args: List[String]) { session_actor ! Start(timeout, args) } |
34820
a8ba6cde13e9
basic setup for synchronous / modal (!) prover startup;
wenzelm
parents:
34819
diff
changeset
|
295 |
|
39630
44181423183a
explicit Session.Phase indication with associated event bus;
wenzelm
parents:
39629
diff
changeset
|
296 |
def stop() { command_change_buffer !? Stop; session_actor !? Stop } |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
297 |
|
39593 | 298 |
def interrupt() { session_actor ! Interrupt_Prover } |
299 |
||
40479 | 300 |
def edit_version(edits: List[Document.Edit_Text]) { session_actor !? Edit_Version(edits) } |
38221 | 301 |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
302 |
def snapshot(name: String, pending_edits: List[Text.Edit]): Document.Snapshot = |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38722
diff
changeset
|
303 |
global_state.peek().snapshot(name, pending_edits) |
34777
91d6089cef88
class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents:
diff
changeset
|
304 |
} |