author | wenzelm |
Mon, 03 Sep 2018 16:23:26 +0200 | |
changeset 68894 | 1dbdad1b57a5 |
parent 68888 | 4fe165254e20 |
child 68899 | b15b03c13dbb |
permissions | -rw-r--r-- |
67054 | 1 |
/* Title: Pure/Thy/thy_resources.scala |
2 |
Author: Makarius |
|
3 |
||
67892 | 4 |
PIDE resources for theory files: load/unload theories via PIDE document updates. |
67054 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
67925 | 10 |
import java.io.{File => JFile} |
11 |
||
12 |
||
67054 | 13 |
object Thy_Resources |
14 |
{ |
|
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
15 |
/* PIDE session */ |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
16 |
|
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
17 |
def start_session( |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
18 |
options: Options, |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
19 |
session_name: String, |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
20 |
session_dirs: List[Path] = Nil, |
67948 | 21 |
include_sessions: List[String] = Nil, |
67062
ee0823ce2828
optional session_base, e.g. from existing Sessions.Deps in build.scala;
wenzelm
parents:
67061
diff
changeset
|
22 |
session_base: Option[Sessions.Base] = None, |
67869 | 23 |
print_mode: List[String] = Nil, |
24 |
progress: Progress = No_Progress, |
|
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
25 |
log: Logger = No_Logger): Session = |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
26 |
{ |
67062
ee0823ce2828
optional session_base, e.g. from existing Sessions.Deps in build.scala;
wenzelm
parents:
67061
diff
changeset
|
27 |
val base = |
ee0823ce2828
optional session_base, e.g. from existing Sessions.Deps in build.scala;
wenzelm
parents:
67061
diff
changeset
|
28 |
session_base getOrElse |
67948 | 29 |
Sessions.base_info(options, session_name, include_sessions = include_sessions, |
30 |
progress = progress, dirs = session_dirs).check_base |
|
67062
ee0823ce2828
optional session_base, e.g. from existing Sessions.Deps in build.scala;
wenzelm
parents:
67061
diff
changeset
|
31 |
val resources = new Thy_Resources(base, log = log) |
67945 | 32 |
val session = new Session(session_name, options, resources) |
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
33 |
|
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
34 |
val session_error = Future.promise[String] |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
35 |
var session_phase: Session.Consumer[Session.Phase] = null |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
36 |
session_phase = |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
37 |
Session.Consumer(getClass.getName) { |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
38 |
case Session.Ready => |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
39 |
session.phase_changed -= session_phase |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
40 |
session_error.fulfill("") |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
41 |
case Session.Terminated(result) if !result.ok => |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
42 |
session.phase_changed -= session_phase |
67869 | 43 |
session_error.fulfill("Session start failed: return code " + result.rc) |
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
44 |
case _ => |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
45 |
} |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
46 |
session.phase_changed += session_phase |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
47 |
|
67877 | 48 |
progress.echo("Starting " + session_name + " ...") |
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
49 |
Isabelle_Process.start(session, options, |
67869 | 50 |
logic = session_name, dirs = session_dirs, modes = print_mode) |
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
51 |
|
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
52 |
session_error.join match { |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
53 |
case "" => session |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
54 |
case msg => session.stop(); error(msg) |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
55 |
} |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
56 |
} |
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
57 |
|
68365 | 58 |
class Theories_Result private[Thy_Resources]( |
67883 | 59 |
val state: Document.State, |
67889 | 60 |
val version: Document.Version, |
68758 | 61 |
val nodes: List[(Document.Node.Name, Document_Status.Node_Status)]) |
67879 | 62 |
{ |
68319 | 63 |
def node_names: List[Document.Node.Name] = nodes.map(_._1) |
67883 | 64 |
def ok: Boolean = nodes.forall({ case (_, st) => st.ok }) |
67897 | 65 |
|
68365 | 66 |
def snapshot(node_name: Document.Node.Name): Document.Snapshot = |
67897 | 67 |
{ |
68365 | 68 |
val snapshot = state.snapshot(node_name) |
69 |
assert(version.id == snapshot.version.id) |
|
70 |
snapshot |
|
68106 | 71 |
} |
67879 | 72 |
} |
73 |
||
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
74 |
val default_check_delay: Double = 0.5 |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
75 |
val default_nodes_status_delay: Double = -1.0 |
68694
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
76 |
|
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
77 |
|
67063 | 78 |
class Session private[Thy_Resources]( |
67945 | 79 |
session_name: String, |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
80 |
session_options: Options, |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
81 |
override val resources: Thy_Resources) extends isabelle.Session(session_options, resources) |
67063 | 82 |
{ |
83 |
session => |
|
84 |
||
67925 | 85 |
val tmp_dir: JFile = Isabelle_System.tmp_dir("server_session") |
67946 | 86 |
val tmp_dir_name: String = File.path(tmp_dir).implode |
67925 | 87 |
|
67945 | 88 |
override def toString: String = session_name |
89 |
||
67925 | 90 |
override def stop(): Process_Result = |
91 |
{ |
|
92 |
try { super.stop() } |
|
93 |
finally { Isabelle_System.rm_tree(tmp_dir) } |
|
94 |
} |
|
95 |
||
67936 | 96 |
|
97 |
/* theories */ |
|
98 |
||
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
99 |
def use_theories( |
67940
b4e80f062fbf
clarified signature -- eliminated somewhat pointless positions;
wenzelm
parents:
67939
diff
changeset
|
100 |
theories: List[String], |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
101 |
qualifier: String = Sessions.DRAFT, |
67881 | 102 |
master_dir: String = "", |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
103 |
check_delay: Time = Time.seconds(default_check_delay), |
68694
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
104 |
check_limit: Int = 0, |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
105 |
nodes_status_delay: Time = Time.seconds(default_nodes_status_delay), |
67885 | 106 |
id: UUID = UUID(), |
67881 | 107 |
progress: Progress = No_Progress): Theories_Result = |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
108 |
{ |
67887
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
109 |
val dep_theories = |
68894
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
110 |
{ |
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
111 |
val master = proper_string(master_dir) getOrElse tmp_dir_name |
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
112 |
val import_names = |
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
113 |
theories.map(thy => resources.import_name(qualifier, master, thy) -> Position.none) |
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
114 |
resources.dependencies(import_names, progress = progress).check_errors.theories |
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
115 |
} |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
116 |
val dep_theories_set = dep_theories.toSet |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
117 |
|
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
118 |
val nodes_status_update = Synchronized(Document_Status.Nodes_Status.empty_update) |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
119 |
|
67879 | 120 |
val result = Future.promise[Theories_Result] |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
121 |
|
68694
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
122 |
def check_state(beyond_limit: Boolean = false) |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
123 |
{ |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
124 |
val state = session.current_state() |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
125 |
state.stable_tip_version match { |
68694
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
126 |
case Some(version) => |
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
127 |
if (beyond_limit || |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
128 |
dep_theories.forall(name => |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
129 |
state.node_consolidated(version, name) || |
68884
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
wenzelm
parents:
68883
diff
changeset
|
130 |
nodes_status_update.value._1.quasi_consolidated(name))) |
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
131 |
{ |
68694
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
132 |
val nodes = |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
133 |
for (name <- dep_theories) |
68758 | 134 |
yield (name -> Document_Status.Node_Status.make(state, version, name)) |
68694
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
135 |
try { result.fulfill(new Theories_Result(state, version, nodes)) } |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
136 |
catch { case _: IllegalStateException => } |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
137 |
} |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
138 |
case None => |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
139 |
} |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
140 |
} |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
141 |
|
67894
fee080c4045f
more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents:
67893
diff
changeset
|
142 |
val check_progress = |
68694
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
143 |
{ |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
144 |
var check_count = 0 |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
145 |
Event_Timer.request(Time.now(), repeat = Some(check_delay)) |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
146 |
{ |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
147 |
if (progress.stopped) result.cancel |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
148 |
else { |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
149 |
check_count += 1 |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
150 |
check_state(check_limit > 0 && check_count > check_limit) |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
151 |
} |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
152 |
} |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
153 |
} |
67894
fee080c4045f
more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents:
67893
diff
changeset
|
154 |
|
68330 | 155 |
val theories_progress = Synchronized(Set.empty[Document.Node.Name]) |
156 |
||
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
157 |
val delay_nodes_status = |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
158 |
Standard_Thread.delay_first(nodes_status_delay max Time.zero) { |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
159 |
val (nodes_status, names) = nodes_status_update.value |
68888 | 160 |
progress.nodes_status(nodes_status, names) |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
161 |
} |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
162 |
|
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
163 |
val consumer = |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
164 |
Session.Consumer[Session.Commands_Changed](getClass.getName) { |
68330 | 165 |
case changed => |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
166 |
if (changed.nodes.exists(dep_theories_set)) { |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
167 |
val snapshot = session.snapshot() |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
168 |
val state = snapshot.state |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
169 |
val version = snapshot.version |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
170 |
|
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
171 |
nodes_status_update.change( |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
172 |
{ case upd @ (nodes_status, _) => |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
173 |
val domain = |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
174 |
if (nodes_status.is_empty) dep_theories_set |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
175 |
else changed.nodes.iterator.filter(dep_theories_set).toSet |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
176 |
val upd1 = |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
177 |
nodes_status.update(resources.session_base, state, version, |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
178 |
domain = Some(domain), trim = changed.assignment).getOrElse(upd) |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
179 |
if (nodes_status_delay >= Time.zero && upd != upd1) |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
180 |
delay_nodes_status.invoke |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
181 |
|
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
182 |
upd1 |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
183 |
}) |
68330 | 184 |
|
185 |
val check_theories = |
|
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
186 |
(for { |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
187 |
command <- changed.commands.iterator |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
188 |
if dep_theories_set(command.node_name) && command.potentially_initialized |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
189 |
} yield command.node_name).toSet |
68330 | 190 |
|
191 |
if (check_theories.nonEmpty) { |
|
192 |
val initialized = |
|
193 |
theories_progress.change_result(theories => |
|
194 |
{ |
|
195 |
val initialized = |
|
196 |
(check_theories -- theories).toList.filter(name => |
|
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
197 |
Document_Status.Node_Status.make(state, version, name).initialized) |
68330 | 198 |
(initialized, theories ++ initialized) |
199 |
}) |
|
200 |
initialized.map(_.theory).sorted.foreach(progress.theory("", _)) |
|
201 |
} |
|
202 |
||
68694
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
203 |
check_state() |
68330 | 204 |
} |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
205 |
} |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
206 |
|
67892 | 207 |
try { |
208 |
session.commands_changed += consumer |
|
68894
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
209 |
resources.load_theories(session, id, dep_theories, progress) |
67892 | 210 |
result.join_result |
67894
fee080c4045f
more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents:
67893
diff
changeset
|
211 |
check_progress.cancel |
67892 | 212 |
session.commands_changed -= consumer |
213 |
} |
|
214 |
finally { |
|
215 |
resources.unload_theories(session, id, dep_theories) |
|
216 |
} |
|
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
217 |
|
67879 | 218 |
result.join |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
219 |
} |
67936 | 220 |
|
67939 | 221 |
def purge_theories( |
222 |
theories: List[String], |
|
223 |
qualifier: String = Sessions.DRAFT, |
|
224 |
master_dir: String = "", |
|
67943 | 225 |
all: Boolean = false): (List[Document.Node.Name], List[Document.Node.Name]) = |
67939 | 226 |
resources.purge_theories(session, theories = theories, qualifier = qualifier, |
67946 | 227 |
master_dir = proper_string(master_dir) getOrElse tmp_dir_name, all = all) |
67063 | 228 |
} |
229 |
||
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
230 |
|
67054 | 231 |
/* internal state */ |
232 |
||
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
233 |
sealed case class State( |
67887
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
234 |
required: Multi_Map[Document.Node.Name, UUID] = Multi_Map.empty, |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
235 |
theories: Map[Document.Node.Name, Theory] = Map.empty) |
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
236 |
{ |
67887
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
237 |
def is_required(name: Document.Node.Name): Boolean = required.isDefinedAt(name) |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
238 |
|
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
239 |
def insert_required(id: UUID, names: List[Document.Node.Name]): State = |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
240 |
copy(required = (required /: names)(_.insert(_, id))) |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
241 |
|
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
242 |
def remove_required(id: UUID, names: List[Document.Node.Name]): State = |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
243 |
copy(required = (required /: names)(_.remove(_, id))) |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
244 |
|
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
245 |
def update_theories(update: List[(Document.Node.Name, Theory)]): State = |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
246 |
copy(theories = |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
247 |
(theories /: update)({ case (thys, (name, thy)) => |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
248 |
thys.get(name) match { |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
249 |
case Some(thy1) if thy1 == thy => thys |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
250 |
case _ => thys + (name -> thy) |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
251 |
} |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
252 |
})) |
67936 | 253 |
|
254 |
def remove_theories(remove: List[Document.Node.Name]): State = |
|
255 |
{ |
|
256 |
require(remove.forall(name => !is_required(name))) |
|
257 |
copy(theories = theories -- remove) |
|
258 |
} |
|
259 |
||
260 |
lazy val theories_graph: Graph[Document.Node.Name, Unit] = |
|
261 |
{ |
|
262 |
val entries = |
|
263 |
for ((name, theory) <- theories.toList) |
|
264 |
yield ((name, ()), theory.node_header.imports.map(_._1).filter(theories.isDefinedAt(_))) |
|
265 |
Graph.make(entries, symmetric = true)(Document.Node.Name.Ordering) |
|
266 |
} |
|
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
267 |
} |
67058
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
268 |
|
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
269 |
final class Theory private[Thy_Resources]( |
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
270 |
val node_name: Document.Node.Name, |
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
271 |
val node_header: Document.Node.Header, |
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
272 |
val text: String, |
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
273 |
val node_required: Boolean) |
67056 | 274 |
{ |
67058
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
275 |
override def toString: String = node_name.toString |
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
276 |
|
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
277 |
def node_perspective: Document.Node.Perspective_Text = |
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
278 |
Document.Node.Perspective(node_required, Text.Perspective.empty, Document.Node.Overlays.empty) |
67058
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
279 |
|
67936 | 280 |
def make_edits(text_edits: List[Text.Edit]): List[Document.Edit_Text] = |
281 |
List(node_name -> Document.Node.Deps(node_header), |
|
282 |
node_name -> Document.Node.Edits(text_edits), |
|
283 |
node_name -> node_perspective) |
|
284 |
||
67058
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
285 |
def node_edits(old: Option[Theory]): List[Document.Edit_Text] = |
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
286 |
{ |
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
287 |
val (text_edits, old_required) = |
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
288 |
if (old.isEmpty) (Text.Edit.inserts(0, text), false) |
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
289 |
else (Text.Edit.replace(0, old.get.text, text), old.get.node_required) |
67058
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
290 |
|
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
291 |
if (text_edits.isEmpty && node_required == old_required) Nil |
67936 | 292 |
else make_edits(text_edits) |
67058
03d4954c68bb
simplified: eliminated pointless Thy_Document_Model;
wenzelm
parents:
67057
diff
changeset
|
293 |
} |
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
294 |
|
67936 | 295 |
def purge_edits: List[Document.Edit_Text] = |
296 |
make_edits(Text.Edit.removes(0, text)) |
|
297 |
||
67887
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
298 |
def required(required: Boolean): Theory = |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
299 |
if (required == node_required) this |
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
300 |
else new Theory(node_name, node_header, text, required) |
67056 | 301 |
} |
67054 | 302 |
} |
303 |
||
67056 | 304 |
class Thy_Resources(session_base: Sessions.Base, log: Logger = No_Logger) |
67054 | 305 |
extends Resources(session_base, log = log) |
306 |
{ |
|
67056 | 307 |
resources => |
308 |
||
67054 | 309 |
private val state = Synchronized(Thy_Resources.State()) |
67056 | 310 |
|
311 |
def load_theories( |
|
312 |
session: Session, |
|
67885 | 313 |
id: UUID, |
68894
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
314 |
dep_theories: List[Document.Node.Name], |
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
315 |
progress: Progress) |
67056 | 316 |
{ |
67888 | 317 |
|
318 |
val loaded_theories = |
|
319 |
for (node_name <- dep_theories) |
|
320 |
yield { |
|
321 |
val path = node_name.path |
|
322 |
if (!node_name.is_theory) error("Not a theory file: " + path) |
|
323 |
||
324 |
progress.expose_interrupt() |
|
325 |
val text = File.read(path) |
|
326 |
val node_header = resources.check_thy_reader(node_name, Scan.char_reader(text)) |
|
327 |
new Thy_Resources.Theory(node_name, node_header, text, true) |
|
328 |
} |
|
67056 | 329 |
|
67893 | 330 |
state.change(st => |
331 |
{ |
|
332 |
val st1 = st.insert_required(id, dep_theories) |
|
333 |
val theory_edits = |
|
334 |
for (theory <- loaded_theories) |
|
335 |
yield { |
|
336 |
val node_name = theory.node_name |
|
337 |
val theory1 = theory.required(st1.is_required(node_name)) |
|
338 |
val edits = theory1.node_edits(st1.theories.get(node_name)) |
|
339 |
(edits, (node_name, theory1)) |
|
340 |
} |
|
341 |
session.update(Document.Blobs.empty, theory_edits.flatMap(_._1)) |
|
342 |
st1.update_theories(theory_edits.map(_._2)) |
|
343 |
}) |
|
67056 | 344 |
} |
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
345 |
|
67887
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
346 |
def unload_theories(session: Session, id: UUID, dep_theories: List[Document.Node.Name]) |
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
347 |
{ |
67893 | 348 |
state.change(st => |
349 |
{ |
|
350 |
val st1 = st.remove_required(id, dep_theories) |
|
351 |
val theory_edits = |
|
352 |
for { |
|
353 |
node_name <- dep_theories |
|
354 |
theory <- st1.theories.get(node_name) |
|
355 |
} |
|
356 |
yield { |
|
357 |
val theory1 = theory.required(st1.is_required(node_name)) |
|
358 |
val edits = theory1.node_edits(Some(theory)) |
|
359 |
(edits, (node_name, theory1)) |
|
360 |
} |
|
361 |
session.update(Document.Blobs.empty, theory_edits.flatMap(_._1)) |
|
362 |
st1.update_theories(theory_edits.map(_._2)) |
|
363 |
}) |
|
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
364 |
} |
67936 | 365 |
|
67939 | 366 |
def purge_theories(session: Session, |
367 |
theories: List[String], |
|
67946 | 368 |
qualifier: String, |
369 |
master_dir: String, |
|
370 |
all: Boolean): (List[Document.Node.Name], List[Document.Node.Name]) = |
|
67936 | 371 |
{ |
67939 | 372 |
val nodes = theories.map(import_name(qualifier, master_dir, _)) |
373 |
||
67936 | 374 |
state.change_result(st => |
375 |
{ |
|
376 |
val graph = st.theories_graph |
|
377 |
val all_nodes = graph.topological_order |
|
378 |
||
379 |
val purge = |
|
67939 | 380 |
(if (all) all_nodes else nodes.filter(graph.defined(_))). |
67936 | 381 |
filterNot(st.is_required(_)).toSet |
67943 | 382 |
|
383 |
val retain = graph.all_preds(all_nodes.filterNot(purge)).toSet |
|
384 |
val (retained, purged) = all_nodes.partition(retain) |
|
67936 | 385 |
|
386 |
val purge_edits = purged.flatMap(name => st.theories(name).purge_edits) |
|
387 |
session.update(Document.Blobs.empty, purge_edits) |
|
388 |
||
67943 | 389 |
((purged, retained), st.remove_theories(purged)) |
67936 | 390 |
}) |
391 |
} |
|
67054 | 392 |
} |