author | wenzelm |
Fri, 20 Jan 2023 20:26:42 +0100 | |
changeset 77029 | 1046a69fabaa |
parent 76906 | 2ccc5d380d88 |
child 77112 | 6f2ddbff972c |
permissions | -rw-r--r-- |
69012 | 1 |
/* Title: Pure/PIDE/headless.scala |
67054 | 2 |
Author: Makarius |
3 |
||
69012 | 4 |
Headless PIDE session and resources from file-system. |
67054 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
67925 | 10 |
import java.io.{File => JFile} |
11 |
||
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
12 |
import scala.annotation.tailrec |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
13 |
import scala.collection.mutable |
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
14 |
|
67925 | 15 |
|
75393 | 16 |
object Headless { |
69012 | 17 |
/** session **/ |
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
18 |
|
68916 | 19 |
private def stable_snapshot( |
75393 | 20 |
state: Document.State, |
21 |
version: Document.Version, |
|
22 |
name: Document.Node.Name |
|
23 |
): Document.Snapshot = { |
|
68916 | 24 |
val snapshot = state.snapshot(name) |
25 |
assert(version.id == snapshot.version.id) |
|
26 |
snapshot |
|
27 |
} |
|
28 |
||
69013 | 29 |
class Use_Theories_Result private[Headless]( |
67883 | 30 |
val state: Document.State, |
67889 | 31 |
val version: Document.Version, |
68925 | 32 |
val nodes: List[(Document.Node.Name, Document_Status.Node_Status)], |
75393 | 33 |
val nodes_committed: List[(Document.Node.Name, Document_Status.Node_Status)] |
34 |
) { |
|
35 |
def nodes_pending: List[(Document.Node.Name, Document_Status.Node_Status)] = { |
|
69032
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69013
diff
changeset
|
36 |
val committed = nodes_committed.iterator.map(_._1).toSet |
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69013
diff
changeset
|
37 |
nodes.filter(p => !committed(p._1)) |
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69013
diff
changeset
|
38 |
} |
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69013
diff
changeset
|
39 |
|
68925 | 40 |
def snapshot(name: Document.Node.Name): Document.Snapshot = |
41 |
stable_snapshot(state, version, name) |
|
42 |
||
43 |
def ok: Boolean = |
|
44 |
(nodes.iterator ++ nodes_committed.iterator).forall({ case (_, st) => st.ok }) |
|
67879 | 45 |
} |
46 |
||
69520 | 47 |
class Session private[Headless]( |
48 |
session_name: String, |
|
49 |
_session_options: => Options, |
|
75393 | 50 |
override val resources: Resources) |
51 |
extends isabelle.Session(_session_options, resources) { |
|
69520 | 52 |
session => |
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
|
53 |
|
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
54 |
|
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
55 |
private def loaded_theory(name: Document.Node.Name): Boolean = |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
56 |
resources.session_base.loaded_theory(name.theory) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
57 |
|
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
58 |
|
69520 | 59 |
/* options */ |
60 |
||
70787 | 61 |
override def consolidate_delay: Time = session_options.seconds("headless_consolidate_delay") |
62 |
override def prune_delay: Time = session_options.seconds("headless_prune_delay") |
|
63 |
||
69520 | 64 |
def default_check_delay: Time = session_options.seconds("headless_check_delay") |
65 |
def default_check_limit: Int = session_options.int("headless_check_limit") |
|
66 |
def default_nodes_status_delay: Time = session_options.seconds("headless_nodes_status_delay") |
|
67 |
def default_watchdog_timeout: Time = session_options.seconds("headless_watchdog_timeout") |
|
68 |
def default_commit_cleanup_delay: Time = session_options.seconds("headless_commit_cleanup_delay") |
|
67063 | 69 |
|
68922 | 70 |
|
71 |
/* temporary directory */ |
|
72 |
||
67925 | 73 |
val tmp_dir: JFile = Isabelle_System.tmp_dir("server_session") |
67946 | 74 |
val tmp_dir_name: String = File.path(tmp_dir).implode |
67925 | 75 |
|
68923 | 76 |
def master_directory(master_dir: String): String = |
77 |
proper_string(master_dir) getOrElse tmp_dir_name |
|
78 |
||
67945 | 79 |
override def toString: String = session_name |
80 |
||
75393 | 81 |
override def stop(): Process_Result = { |
67925 | 82 |
try { super.stop() } |
83 |
finally { Isabelle_System.rm_tree(tmp_dir) } |
|
84 |
} |
|
85 |
||
67936 | 86 |
|
87 |
/* theories */ |
|
88 |
||
75393 | 89 |
private object Load_State { |
70872 | 90 |
def finished: Load_State = Load_State(Nil, Nil, 0) |
91 |
||
92 |
def count_file(name: Document.Node.Name): Long = |
|
70873 | 93 |
if (loaded_theory(name)) 0 else name.path.file.length |
70872 | 94 |
} |
95 |
||
96 |
private case class Load_State( |
|
75393 | 97 |
pending: List[Document.Node.Name], |
98 |
rest: List[Document.Node.Name], |
|
99 |
load_limit: Long |
|
100 |
) { |
|
70872 | 101 |
def next( |
102 |
dep_graph: Document.Node.Name.Graph[Unit], |
|
76310
c5c747ce46d2
proper update of non-committed theories (see also 2bf1d0e57695, 2a1583baaaa0);
wenzelm
parents:
76309
diff
changeset
|
103 |
consolidated: Document.Node.Name => Boolean |
75393 | 104 |
): (List[Document.Node.Name], Load_State) = { |
105 |
def load_requirements( |
|
106 |
pending1: List[Document.Node.Name], |
|
107 |
rest1: List[Document.Node.Name] |
|
108 |
) : (List[Document.Node.Name], Load_State) = { |
|
76310
c5c747ce46d2
proper update of non-committed theories (see also 2bf1d0e57695, 2a1583baaaa0);
wenzelm
parents:
76309
diff
changeset
|
109 |
val load_theories = dep_graph.all_preds_rev(pending1) |
70872 | 110 |
(load_theories, Load_State(pending1, rest1, load_limit)) |
111 |
} |
|
112 |
||
76310
c5c747ce46d2
proper update of non-committed theories (see also 2bf1d0e57695, 2a1583baaaa0);
wenzelm
parents:
76309
diff
changeset
|
113 |
if (!pending.forall(consolidated)) (Nil, this) |
70872 | 114 |
else if (rest.isEmpty) (Nil, Load_State.finished) |
115 |
else if (load_limit == 0) load_requirements(rest, Nil) |
|
116 |
else { |
|
117 |
val reachable = |
|
118 |
dep_graph.reachable_limit(load_limit, Load_State.count_file, dep_graph.imm_preds, rest) |
|
119 |
val (pending1, rest1) = rest.partition(reachable) |
|
120 |
load_requirements(pending1, rest1) |
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
||
68914 | 125 |
private sealed case class Use_Theories_State( |
70697 | 126 |
dep_graph: Document.Node.Name.Graph[Unit], |
70765 | 127 |
load_state: Load_State, |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
128 |
watchdog_timeout: Time, |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
129 |
commit: Option[(Document.Snapshot, Document_Status.Node_Status) => Unit], |
68914 | 130 |
last_update: Time = Time.now(), |
131 |
nodes_status: Document_Status.Nodes_Status = Document_Status.Nodes_Status.empty, |
|
68925 | 132 |
already_committed: Map[Document.Node.Name, Document_Status.Node_Status] = Map.empty, |
76320 | 133 |
changed_nodes: Set[Document.Node.Name] = Set.empty, |
134 |
changed_assignment: Boolean = false, |
|
75393 | 135 |
result: Option[Exn.Result[Use_Theories_Result]] = None |
136 |
) { |
|
76316 | 137 |
def nodes_status_update(state: Document.State, version: Document.Version, |
138 |
domain: Option[Set[Document.Node.Name]] = None, |
|
76319 | 139 |
trim: Boolean = false |
76316 | 140 |
): (Boolean, Use_Theories_State) = { |
141 |
val (nodes_status_changed, nodes_status1) = |
|
142 |
nodes_status.update(resources, state, version, domain = domain, trim = trim) |
|
76319 | 143 |
val st1 = copy(last_update = Time.now(), nodes_status = nodes_status1) |
76316 | 144 |
(nodes_status_changed, st1) |
145 |
} |
|
68914 | 146 |
|
76320 | 147 |
def changed( |
148 |
nodes: IterableOnce[Document.Node.Name], |
|
149 |
assignment: Boolean |
|
150 |
): Use_Theories_State = { |
|
151 |
copy( |
|
152 |
changed_nodes = changed_nodes ++ nodes, |
|
153 |
changed_assignment = changed_assignment || assignment) |
|
154 |
} |
|
155 |
||
156 |
def reset_changed: Use_Theories_State = |
|
157 |
if (changed_nodes.isEmpty && !changed_assignment) this |
|
158 |
else copy(changed_nodes = Set.empty, changed_assignment = false) |
|
159 |
||
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
160 |
def watchdog: Boolean = |
68914 | 161 |
watchdog_timeout > Time.zero && Time.now() - last_update > watchdog_timeout |
162 |
||
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
163 |
def finished_result: Boolean = result.isDefined |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
164 |
|
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
165 |
def join_result: Option[(Exn.Result[Use_Theories_Result], Use_Theories_State)] = |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
166 |
if (finished_result) Some((result.get, this)) else None |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
167 |
|
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
168 |
def cancel_result: Use_Theories_State = |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
169 |
if (finished_result) this else copy(result = Some(Exn.Exn(Exn.Interrupt()))) |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
170 |
|
75393 | 171 |
def clean_theories: (List[Document.Node.Name], Use_Theories_State) = { |
172 |
@tailrec def frontier( |
|
173 |
base: List[Document.Node.Name], |
|
174 |
front: Set[Document.Node.Name] |
|
175 |
) : Set[Document.Node.Name] = { |
|
70698 | 176 |
val add = base.filter(name => dep_graph.imm_succs(name).forall(front)) |
177 |
if (add.isEmpty) front |
|
178 |
else { |
|
179 |
val preds = add.map(dep_graph.imm_preds) |
|
73359 | 180 |
val base1 = |
181 |
preds.tail.foldLeft(preds.head)(_ ++ _).toList.filter(already_committed.keySet) |
|
70698 | 182 |
frontier(base1, front ++ add) |
183 |
} |
|
184 |
} |
|
185 |
||
70763
5fae55752c70
tuned messages (again) -- avoid confusion wrt. total remaining size;
wenzelm
parents:
70710
diff
changeset
|
186 |
if (already_committed.isEmpty) (Nil, this) |
70698 | 187 |
else { |
70705 | 188 |
val base = |
189 |
(for { |
|
190 |
(name, (_, (_, succs))) <- dep_graph.iterator |
|
191 |
if succs.isEmpty && already_committed.isDefinedAt(name) |
|
192 |
} yield name).toList |
|
193 |
val clean = frontier(base, Set.empty) |
|
70763
5fae55752c70
tuned messages (again) -- avoid confusion wrt. total remaining size;
wenzelm
parents:
70710
diff
changeset
|
194 |
if (clean.isEmpty) (Nil, this) |
70698 | 195 |
else { |
70763
5fae55752c70
tuned messages (again) -- avoid confusion wrt. total remaining size;
wenzelm
parents:
70710
diff
changeset
|
196 |
(dep_graph.topological_order.filter(clean), |
70699 | 197 |
copy(dep_graph = dep_graph.exclude(clean))) |
70698 | 198 |
} |
199 |
} |
|
200 |
} |
|
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
201 |
|
76315 | 202 |
private def consolidated( |
203 |
state: Document.State, |
|
204 |
version: Document.Version, |
|
205 |
name: Document.Node.Name |
|
206 |
): Boolean = { |
|
207 |
loaded_theory(name) || |
|
208 |
nodes_status.quasi_consolidated(name) || |
|
209 |
(if (commit.isDefined) already_committed.isDefinedAt(name) |
|
210 |
else state.node_consolidated(version, name)) |
|
211 |
} |
|
212 |
||
75393 | 213 |
def check( |
214 |
state: Document.State, |
|
215 |
version: Document.Version, |
|
216 |
beyond_limit: Boolean |
|
217 |
) : (List[Document.Node.Name], Use_Theories_State) = { |
|
76315 | 218 |
val st1 = |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
219 |
commit match { |
76315 | 220 |
case None => this |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
221 |
case Some(commit_fn) => |
76315 | 222 |
copy(already_committed = |
223 |
dep_graph.topological_order.foldLeft(already_committed) { |
|
224 |
case (committed, name) => |
|
225 |
def parents_committed: Boolean = |
|
226 |
version.nodes(name).header.imports.forall(parent => |
|
227 |
loaded_theory(parent) || committed.isDefinedAt(parent)) |
|
228 |
if (!committed.isDefinedAt(name) && parents_committed && |
|
229 |
state.node_consolidated(version, name)) { |
|
230 |
val snapshot = stable_snapshot(state, version, name) |
|
231 |
val status = Document_Status.Node_Status.make(state, version, name) |
|
232 |
commit_fn(snapshot, status) |
|
233 |
committed + (name -> status) |
|
234 |
} |
|
235 |
else committed |
|
236 |
}) |
|
68916 | 237 |
} |
238 |
||
76310
c5c747ce46d2
proper update of non-committed theories (see also 2bf1d0e57695, 2a1583baaaa0);
wenzelm
parents:
76309
diff
changeset
|
239 |
def committed(name: Document.Node.Name): Boolean = |
76315 | 240 |
loaded_theory(name) || st1.already_committed.isDefinedAt(name) |
76310
c5c747ce46d2
proper update of non-committed theories (see also 2bf1d0e57695, 2a1583baaaa0);
wenzelm
parents:
76309
diff
changeset
|
241 |
|
76323
3637a0d06fe1
avoid result based on outdated state, e.g. relevant for use_theories with changed files;
wenzelm
parents:
76320
diff
changeset
|
242 |
val (load_theories0, load_state1) = |
3637a0d06fe1
avoid result based on outdated state, e.g. relevant for use_theories with changed files;
wenzelm
parents:
76320
diff
changeset
|
243 |
load_state.next(dep_graph, consolidated(state, version, _)) |
3637a0d06fe1
avoid result based on outdated state, e.g. relevant for use_theories with changed files;
wenzelm
parents:
76320
diff
changeset
|
244 |
|
3637a0d06fe1
avoid result based on outdated state, e.g. relevant for use_theories with changed files;
wenzelm
parents:
76320
diff
changeset
|
245 |
val load_theories = load_theories0.filterNot(committed) |
3637a0d06fe1
avoid result based on outdated state, e.g. relevant for use_theories with changed files;
wenzelm
parents:
76320
diff
changeset
|
246 |
|
76320 | 247 |
val result1 = { |
248 |
val stopped = beyond_limit || watchdog |
|
76323
3637a0d06fe1
avoid result based on outdated state, e.g. relevant for use_theories with changed files;
wenzelm
parents:
76320
diff
changeset
|
249 |
if (!finished_result && load_theories.isEmpty && |
76320 | 250 |
(stopped || dep_graph.keys_iterator.forall(consolidated(state, version, _))) |
76315 | 251 |
) { |
76320 | 252 |
@tailrec def make_nodes( |
253 |
input: List[Document.Node.Name], |
|
254 |
output: List[(Document.Node.Name, Document_Status.Node_Status)] |
|
255 |
): Option[List[(Document.Node.Name, Document_Status.Node_Status)]] = { |
|
256 |
input match { |
|
257 |
case name :: rest => |
|
258 |
if (loaded_theory(name)) make_nodes(rest, output) |
|
259 |
else { |
|
260 |
val status = Document_Status.Node_Status.make(state, version, name) |
|
76328 | 261 |
val ok = if (commit.isDefined) committed(name) else status.consolidated |
262 |
if (stopped || ok) make_nodes(rest, (name -> status) :: output) else None |
|
76320 | 263 |
} |
264 |
case Nil => Some(output) |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
for (nodes <- make_nodes(dep_graph.topological_order.reverse, Nil)) yield { |
|
269 |
val nodes_committed = |
|
270 |
(for { |
|
271 |
name <- dep_graph.keys_iterator |
|
272 |
status <- st1.already_committed.get(name) |
|
273 |
} yield name -> status).toList |
|
274 |
Exn.Res(new Use_Theories_Result(state, version, nodes, nodes_committed)) |
|
275 |
} |
|
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
276 |
} |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
277 |
else result |
76320 | 278 |
} |
68925 | 279 |
|
76323
3637a0d06fe1
avoid result based on outdated state, e.g. relevant for use_theories with changed files;
wenzelm
parents:
76320
diff
changeset
|
280 |
(load_theories, st1.copy(result = result1, load_state = load_state1)) |
68916 | 281 |
} |
68914 | 282 |
} |
283 |
||
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
284 |
def use_theories( |
67940
b4e80f062fbf
clarified signature -- eliminated somewhat pointless positions;
wenzelm
parents:
67939
diff
changeset
|
285 |
theories: List[String], |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
286 |
qualifier: String = Sessions.DRAFT, |
67881 | 287 |
master_dir: String = "", |
69920 | 288 |
unicode_symbols: Boolean = false, |
68943 | 289 |
check_delay: Time = default_check_delay, |
69520 | 290 |
check_limit: Int = default_check_limit, |
68947 | 291 |
watchdog_timeout: Time = default_watchdog_timeout, |
68943 | 292 |
nodes_status_delay: Time = default_nodes_status_delay, |
69458 | 293 |
id: UUID.T = UUID.random(), |
68916 | 294 |
// commit: must not block, must not fail |
295 |
commit: Option[(Document.Snapshot, Document_Status.Node_Status) => Unit] = None, |
|
68981 | 296 |
commit_cleanup_delay: Time = default_commit_cleanup_delay, |
75393 | 297 |
progress: Progress = new Progress |
298 |
): Use_Theories_Result = { |
|
299 |
val dependencies = { |
|
68894
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
300 |
val import_names = |
68923 | 301 |
theories.map(thy => |
302 |
resources.import_name(qualifier, master_directory(master_dir), thy) -> Position.none) |
|
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
303 |
resources.dependencies(import_names, progress = progress).check_errors |
68894
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
304 |
} |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
305 |
val dep_theories = dependencies.theories |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
306 |
val dep_theories_set = dep_theories.toSet |
76886
f405fcc3db33
clarified signature: avoid somewhat fragile Document.Node.Name.master_dir_path;
wenzelm
parents:
76858
diff
changeset
|
307 |
val dep_files = dependencies.loaded_files |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
308 |
|
75393 | 309 |
val use_theories_state = { |
70797 | 310 |
val dep_graph = dependencies.theory_graph |
311 |
||
312 |
val maximals = dep_graph.maximals |
|
313 |
val rest = |
|
314 |
if (maximals.isEmpty || maximals.tail.isEmpty) maximals |
|
315 |
else { |
|
70800 | 316 |
val depth = dep_graph.node_depth(Load_State.count_file) |
70797 | 317 |
maximals.sortBy(node => - depth(node)) |
318 |
} |
|
70800 | 319 |
val load_limit = |
70801 | 320 |
if (commit.isDefined) (session_options.real("headless_load_limit") * 1024 * 1024).round |
70800 | 321 |
else 0 |
70798 | 322 |
val load_state = Load_State(Nil, rest, load_limit) |
70797 | 323 |
|
324 |
Synchronized(Use_Theories_State(dep_graph, load_state, watchdog_timeout, commit)) |
|
325 |
} |
|
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
326 |
|
76320 | 327 |
def check_state( |
328 |
beyond_limit: Boolean = false, |
|
329 |
state: Document.State = session.get_state() |
|
330 |
): Unit = { |
|
70797 | 331 |
for { |
332 |
version <- state.stable_tip_version |
|
333 |
load_theories = use_theories_state.change_result(_.check(state, version, beyond_limit)) |
|
334 |
if load_theories.nonEmpty |
|
335 |
} resources.load_theories(session, id, load_theories, dep_files, unicode_symbols, progress) |
|
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
336 |
} |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
337 |
|
76311
fab6568b119d
more robust: active consumer for check_state/check_progress;
wenzelm
parents:
76310
diff
changeset
|
338 |
lazy 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
|
339 |
var check_count = 0 |
75393 | 340 |
Event_Timer.request(Time.now(), repeat = Some(check_delay)) { |
341 |
if (progress.stopped) use_theories_state.change(_.cancel_result) |
|
342 |
else { |
|
343 |
check_count += 1 |
|
344 |
check_state(check_limit > 0 && check_count > check_limit) |
|
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
|
345 |
} |
75393 | 346 |
} |
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
|
347 |
} |
67894
fee080c4045f
more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents:
67893
diff
changeset
|
348 |
|
75393 | 349 |
val consumer = { |
68906 | 350 |
val delay_nodes_status = |
71704 | 351 |
Delay.first(nodes_status_delay max Time.zero) { |
69818
60d0ee8f2ddb
more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents:
69817
diff
changeset
|
352 |
progress.nodes_status(use_theories_state.value.nodes_status) |
68906 | 353 |
} |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
354 |
|
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
355 |
val delay_commit_clean = |
71704 | 356 |
Delay.first(commit_cleanup_delay max Time.zero) { |
70763
5fae55752c70
tuned messages (again) -- avoid confusion wrt. total remaining size;
wenzelm
parents:
70710
diff
changeset
|
357 |
val clean_theories = use_theories_state.change_result(_.clean_theories) |
76330 | 358 |
if (clean_theories.nonEmpty && session.is_ready) { |
70770 | 359 |
progress.echo("Removing " + clean_theories.length + " theories ...") |
70702 | 360 |
resources.clean_theories(session, id, clean_theories) |
361 |
} |
|
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
362 |
} |
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
363 |
|
76320 | 364 |
isabelle.Session.Consumer[isabelle.Session.Commands_Changed](getClass.getName) { changed => |
365 |
val state = session.get_state() |
|
366 |
||
367 |
def apply_changed(st: Use_Theories_State): Use_Theories_State = |
|
368 |
st.changed(changed.nodes.iterator.filter(dep_theories_set), changed.assignment) |
|
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
369 |
|
76320 | 370 |
state.stable_tip_version match { |
371 |
case None => use_theories_state.change(apply_changed) |
|
372 |
case Some(version) => |
|
76329 | 373 |
val theory_progress = |
75394 | 374 |
use_theories_state.change_result { st => |
76320 | 375 |
val changed_st = apply_changed(st) |
376 |
||
75393 | 377 |
val domain = |
378 |
if (st.nodes_status.is_empty) dep_theories_set |
|
76320 | 379 |
else changed_st.changed_nodes |
68899 | 380 |
|
76316 | 381 |
val (nodes_status_changed, st1) = |
76320 | 382 |
st.reset_changed.nodes_status_update(state, version, |
383 |
domain = Some(domain), trim = changed_st.changed_assignment) |
|
68899 | 384 |
|
75393 | 385 |
if (nodes_status_delay >= Time.zero && nodes_status_changed) { |
386 |
delay_nodes_status.invoke() |
|
387 |
} |
|
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
388 |
|
75393 | 389 |
val theory_progress = |
390 |
(for { |
|
76716
a7602257a825
clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents:
76702
diff
changeset
|
391 |
(name, node_status) <- st1.nodes_status.present().iterator |
a7602257a825
clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents:
76702
diff
changeset
|
392 |
if !node_status.is_empty && changed_st.changed_nodes(name) && |
a7602257a825
clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents:
76702
diff
changeset
|
393 |
!st.already_committed.isDefinedAt(name) |
75393 | 394 |
p1 = node_status.percentage |
75742 | 395 |
if p1 > 0 && !st.nodes_status.get(name).map(_.percentage).contains(p1) |
75393 | 396 |
} yield Progress.Theory(name.theory, percentage = Some(p1))).toList |
68903 | 397 |
|
76329 | 398 |
if (commit.isDefined && commit_cleanup_delay > Time.zero) { |
399 |
if (st1.finished_result) delay_commit_clean.revoke() |
|
400 |
else delay_commit_clean.invoke() |
|
401 |
} |
|
402 |
||
403 |
(theory_progress, st1) |
|
75394 | 404 |
} |
68330 | 405 |
|
71601 | 406 |
theory_progress.foreach(progress.theory) |
68903 | 407 |
|
76320 | 408 |
check_state(state = state) |
409 |
} |
|
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
410 |
} |
68906 | 411 |
} |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
412 |
|
67892 | 413 |
try { |
414 |
session.commands_changed += consumer |
|
76311
fab6568b119d
more robust: active consumer for check_state/check_progress;
wenzelm
parents:
76310
diff
changeset
|
415 |
check_progress |
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
416 |
use_theories_state.guarded_access(_.join_result) |
73367 | 417 |
check_progress.cancel() |
67892 | 418 |
} |
419 |
finally { |
|
68907 | 420 |
session.commands_changed -= consumer |
67892 | 421 |
resources.unload_theories(session, id, dep_theories) |
422 |
} |
|
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
423 |
|
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
424 |
Exn.release(use_theories_state.guarded_access(_.join_result)) |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
425 |
} |
67936 | 426 |
|
67939 | 427 |
def purge_theories( |
68915 | 428 |
theories: List[String], |
429 |
qualifier: String = Sessions.DRAFT, |
|
430 |
master_dir: String = "", |
|
75393 | 431 |
all: Boolean = false |
432 |
): (List[Document.Node.Name], List[Document.Node.Name]) = { |
|
68923 | 433 |
val nodes = |
434 |
if (all) None |
|
435 |
else Some(theories.map(resources.import_name(qualifier, master_directory(master_dir), _))) |
|
75743 | 436 |
resources.purge_theories(nodes) |
68915 | 437 |
} |
67063 | 438 |
} |
439 |
||
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
440 |
|
67054 | 441 |
|
69012 | 442 |
/** resources **/ |
68922 | 443 |
|
75393 | 444 |
object Resources { |
76656 | 445 |
def apply( |
446 |
options: Options, |
|
76657 | 447 |
session_background: Sessions.Background, |
76656 | 448 |
log: Logger = No_Logger |
76657 | 449 |
): Resources = new Resources(options, session_background, log = log) |
69536 | 450 |
|
451 |
def make( |
|
452 |
options: Options, |
|
453 |
session_name: String, |
|
454 |
session_dirs: List[Path] = Nil, |
|
455 |
include_sessions: List[String] = Nil, |
|
71726
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71704
diff
changeset
|
456 |
progress: Progress = new Progress, |
75393 | 457 |
log: Logger = No_Logger |
458 |
): Resources = { |
|
76657 | 459 |
val session_background = |
76656 | 460 |
Sessions.background(options, session_name, dirs = session_dirs, |
69536 | 461 |
include_sessions = include_sessions, progress = progress) |
76657 | 462 |
apply(options, session_background, log = log) |
69536 | 463 |
} |
464 |
||
69012 | 465 |
final class Theory private[Headless]( |
466 |
val node_name: Document.Node.Name, |
|
467 |
val node_header: Document.Node.Header, |
|
468 |
val text: String, |
|
75393 | 469 |
val node_required: Boolean |
470 |
) { |
|
69012 | 471 |
override def toString: String = node_name.toString |
68922 | 472 |
|
76702 | 473 |
def node_perspective: Document.Node.Perspective_Text.T = |
69012 | 474 |
Document.Node.Perspective(node_required, Text.Perspective.empty, Document.Node.Overlays.empty) |
68922 | 475 |
|
69012 | 476 |
def make_edits(text_edits: List[Text.Edit]): List[Document.Edit_Text] = |
477 |
List(node_name -> Document.Node.Deps(node_header), |
|
478 |
node_name -> Document.Node.Edits(text_edits), |
|
479 |
node_name -> node_perspective) |
|
68922 | 480 |
|
75393 | 481 |
def node_edits(old: Option[Theory]): List[Document.Edit_Text] = { |
69012 | 482 |
val (text_edits, old_required) = |
483 |
if (old.isEmpty) (Text.Edit.inserts(0, text), false) |
|
484 |
else (Text.Edit.replace(0, old.get.text, text), old.get.node_required) |
|
67887
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
485 |
|
69012 | 486 |
if (text_edits.isEmpty && node_required == old_required) Nil |
487 |
else make_edits(text_edits) |
|
488 |
} |
|
67887
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
489 |
|
69012 | 490 |
def purge_edits: List[Document.Edit_Text] = |
491 |
make_edits(Text.Edit.removes(0, text)) |
|
67936 | 492 |
|
76312 | 493 |
def set_required(required: Boolean): Theory = |
69012 | 494 |
if (required == node_required) this |
495 |
else new Theory(node_name, node_header, text, required) |
|
67936 | 496 |
} |
497 |
||
69012 | 498 |
sealed case class State( |
76904
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
wenzelm
parents:
76902
diff
changeset
|
499 |
blobs: Map[Document.Node.Name, Document.Blobs.Item] = Map.empty, |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
500 |
theories: Map[Document.Node.Name, Theory] = Map.empty, |
75393 | 501 |
required: Multi_Map[Document.Node.Name, UUID.T] = Multi_Map.empty |
502 |
) { |
|
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
503 |
/* blobs */ |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
504 |
|
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
505 |
def doc_blobs: Document.Blobs = Document.Blobs(blobs) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
506 |
|
75393 | 507 |
def update_blobs(names: List[Document.Node.Name]): (Document.Blobs, State) = { |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
508 |
val new_blobs = |
75394 | 509 |
names.flatMap { name => |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
510 |
val bytes = Bytes.read(name.path) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
511 |
blobs.get(name) match { |
76902 | 512 |
case Some(blob) if blob.bytes == bytes => None |
76906 | 513 |
case _ => |
514 |
val text = bytes.text |
|
515 |
val blob = Document.Blobs.Item(bytes, text, Symbol.Text_Chunk(text), changed = true) |
|
516 |
Some(name -> blob) |
|
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
517 |
} |
75394 | 518 |
} |
73359 | 519 |
val blobs1 = new_blobs.foldLeft(blobs)(_ + _) |
520 |
val blobs2 = new_blobs.foldLeft(blobs) { case (map, (a, b)) => map + (a -> b.unchanged) } |
|
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
521 |
(Document.Blobs(blobs1), copy(blobs = blobs2)) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
522 |
} |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
523 |
|
75393 | 524 |
def blob_edits( |
525 |
name: Document.Node.Name, |
|
76904
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
wenzelm
parents:
76902
diff
changeset
|
526 |
old_blob: Option[Document.Blobs.Item] |
75393 | 527 |
) : List[Document.Edit_Text] = { |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
528 |
val blob = blobs.getOrElse(name, error("Missing blob " + quote(name.toString))) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
529 |
val text_edits = |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
530 |
old_blob match { |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
531 |
case None => List(Text.Edit.insert(0, blob.source)) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
532 |
case Some(blob0) => Text.Edit.replace(0, blob0.source, blob.source) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
533 |
} |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
534 |
if (text_edits.isEmpty) Nil |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
535 |
else List(name -> Document.Node.Blob(blob), name -> Document.Node.Edits(text_edits)) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
536 |
} |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
537 |
|
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
538 |
|
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
539 |
/* theories */ |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
540 |
|
70674
29bb1ebb188f
clarified signature: proper Document.Node.Ordering conforming to equality (e.g. required in situations where theory names are ambiguous due to overlapping session directories);
wenzelm
parents:
70657
diff
changeset
|
541 |
lazy val theory_graph: Document.Node.Name.Graph[Unit] = |
29bb1ebb188f
clarified signature: proper Document.Node.Ordering conforming to equality (e.g. required in situations where theory names are ambiguous due to overlapping session directories);
wenzelm
parents:
70657
diff
changeset
|
542 |
Document.Node.Name.make_graph( |
69012 | 543 |
for ((name, theory) <- theories.toList) |
71601 | 544 |
yield ((name, ()), theory.node_header.imports.filter(theories.isDefinedAt))) |
67056 | 545 |
|
69012 | 546 |
def is_required(name: Document.Node.Name): Boolean = required.isDefinedAt(name) |
547 |
||
69458 | 548 |
def insert_required(id: UUID.T, names: List[Document.Node.Name]): State = |
73359 | 549 |
copy(required = names.foldLeft(required)(_.insert(_, id))) |
69012 | 550 |
|
69458 | 551 |
def remove_required(id: UUID.T, names: List[Document.Node.Name]): State = |
73359 | 552 |
copy(required = names.foldLeft(required)(_.remove(_, id))) |
68958 | 553 |
|
76317 | 554 |
def update_theories(update: List[Theory]): State = |
69012 | 555 |
copy(theories = |
73359 | 556 |
update.foldLeft(theories) { |
76317 | 557 |
case (thys, thy) => |
558 |
thys.get(thy.node_name) match { |
|
73359 | 559 |
case Some(thy1) if thy1 == thy => thys |
76317 | 560 |
case _ => thys + (thy.node_name -> thy) |
73359 | 561 |
} |
562 |
}) |
|
69012 | 563 |
|
75393 | 564 |
def remove_theories(remove: List[Document.Node.Name]): State = { |
73120
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents:
72816
diff
changeset
|
565 |
require(remove.forall(name => !is_required(name)), "attempt to remove required nodes") |
69012 | 566 |
copy(theories = theories -- remove) |
567 |
} |
|
568 |
||
75393 | 569 |
def unload_theories( |
570 |
id: UUID.T, |
|
571 |
theories: List[Document.Node.Name] |
|
572 |
) : (List[Document.Edit_Text], State) = { |
|
70649 | 573 |
val st1 = remove_required(id, theories) |
67893 | 574 |
val theory_edits = |
69012 | 575 |
for { |
70649 | 576 |
node_name <- theories |
69012 | 577 |
theory <- st1.theories.get(node_name) |
578 |
} |
|
67893 | 579 |
yield { |
76312 | 580 |
val theory1 = theory.set_required(st1.is_required(node_name)) |
69012 | 581 |
val edits = theory1.node_edits(Some(theory)) |
76317 | 582 |
(theory1, edits) |
67893 | 583 |
} |
76317 | 584 |
(theory_edits.flatMap(_._2), st1.update_theories(theory_edits.map(_._1))) |
69012 | 585 |
} |
586 |
||
75393 | 587 |
def purge_theories( |
588 |
nodes: Option[List[Document.Node.Name]] |
|
589 |
) : ((List[Document.Node.Name], List[Document.Node.Name], List[Document.Edit_Text]), State) = { |
|
69012 | 590 |
val all_nodes = theory_graph.topological_order |
71601 | 591 |
val purge = nodes.getOrElse(all_nodes).filterNot(is_required).toSet |
69012 | 592 |
|
593 |
val retain = theory_graph.all_preds(all_nodes.filterNot(purge)).toSet |
|
594 |
val (retained, purged) = all_nodes.partition(retain) |
|
70783 | 595 |
val purge_edits = purged.flatMap(name => theories(name).purge_edits) |
69012 | 596 |
|
70783 | 597 |
((purged, retained, purge_edits), remove_theories(purged)) |
69012 | 598 |
} |
599 |
} |
|
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
600 |
} |
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
601 |
|
69536 | 602 |
class Resources private[Headless]( |
76665 | 603 |
val options: Options, |
604 |
session_background: Sessions.Background, |
|
605 |
log: Logger = No_Logger) |
|
606 |
extends isabelle.Resources(session_background.check_errors, log = log) { |
|
69012 | 607 |
resources => |
608 |
||
71600 | 609 |
val store: Sessions.Store = Sessions.store(options) |
610 |
||
69536 | 611 |
|
612 |
/* session */ |
|
613 |
||
75393 | 614 |
def start_session( |
615 |
print_mode: List[String] = Nil, |
|
616 |
progress: Progress = new Progress |
|
617 |
): Session = { |
|
76656 | 618 |
val session_name = session_background.session_name |
75921
423021c98500
clarified signature: Sessions.Base_Info follows Sessions.Base;
wenzelm
parents:
75920
diff
changeset
|
619 |
val session = new Session(session_name, options, resources) |
69536 | 620 |
|
75921
423021c98500
clarified signature: Sessions.Base_Info follows Sessions.Base;
wenzelm
parents:
75920
diff
changeset
|
621 |
progress.echo("Starting session " + session_name + " ...") |
76729 | 622 |
Isabelle_Process.start(store, options, session, session_background, |
75921
423021c98500
clarified signature: Sessions.Base_Info follows Sessions.Base;
wenzelm
parents:
75920
diff
changeset
|
623 |
logic = session_name, modes = print_mode).await_startup() |
69536 | 624 |
|
71604 | 625 |
session |
69536 | 626 |
} |
627 |
||
628 |
||
629 |
/* theories */ |
|
630 |
||
69012 | 631 |
private val state = Synchronized(Resources.State()) |
632 |
||
633 |
def load_theories( |
|
634 |
session: Session, |
|
69458 | 635 |
id: UUID.T, |
70649 | 636 |
theories: List[Document.Node.Name], |
637 |
files: List[Document.Node.Name], |
|
69920 | 638 |
unicode_symbols: Boolean, |
75393 | 639 |
progress: Progress |
640 |
): Unit = { |
|
69012 | 641 |
val loaded_theories = |
70649 | 642 |
for (node_name <- theories) |
69012 | 643 |
yield { |
644 |
val path = node_name.path |
|
645 |
if (!node_name.is_theory) error("Not a theory file: " + path) |
|
646 |
||
647 |
progress.expose_interrupt() |
|
69920 | 648 |
val text0 = File.read(path) |
649 |
val text = if (unicode_symbols) Symbol.decode(text0) else text0 |
|
72772 | 650 |
val node_header = resources.check_thy(node_name, Scan.char_reader(text)) |
69012 | 651 |
new Resources.Theory(node_name, node_header, text, true) |
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
652 |
} |
69012 | 653 |
|
654 |
val loaded = loaded_theories.length |
|
655 |
if (loaded > 1) progress.echo("Loading " + loaded + " theories ...") |
|
656 |
||
75394 | 657 |
state.change { st => |
75393 | 658 |
val (doc_blobs1, st1) = st.insert_required(id, theories).update_blobs(files) |
659 |
val theory_edits = |
|
660 |
for (theory <- loaded_theories) |
|
661 |
yield { |
|
662 |
val node_name = theory.node_name |
|
76313
f67e8a557b7d
tuned: clarified old_theory (in contrast to 4d5342898b1);
wenzelm
parents:
76312
diff
changeset
|
663 |
val old_theory = st.theories.get(node_name) |
76312 | 664 |
val theory1 = theory.set_required(st1.is_required(node_name)) |
76313
f67e8a557b7d
tuned: clarified old_theory (in contrast to 4d5342898b1);
wenzelm
parents:
76312
diff
changeset
|
665 |
val edits = theory1.node_edits(old_theory) |
76317 | 666 |
(theory1, edits) |
75393 | 667 |
} |
668 |
val file_edits = |
|
669 |
for { node_name <- files if doc_blobs1.changed(node_name) } |
|
670 |
yield st1.blob_edits(node_name, st.blobs.get(node_name)) |
|
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
671 |
|
76317 | 672 |
session.update(doc_blobs1, theory_edits.flatMap(_._2) ::: file_edits.flatten) |
673 |
st1.update_theories(theory_edits.map(_._1)) |
|
75394 | 674 |
} |
69012 | 675 |
} |
67936 | 676 |
|
75393 | 677 |
def unload_theories(session: Session, id: UUID.T, theories: List[Document.Node.Name]): Unit = { |
75394 | 678 |
state.change { st => |
75743 | 679 |
val (edits, st1) = st.unload_theories(id, theories) |
70783 | 680 |
session.update(st.doc_blobs, edits) |
681 |
st1 |
|
75394 | 682 |
} |
69012 | 683 |
} |
684 |
||
75393 | 685 |
def clean_theories(session: Session, id: UUID.T, theories: List[Document.Node.Name]): Unit = { |
75394 | 686 |
state.change { st => |
75743 | 687 |
val (edits1, st1) = st.unload_theories(id, theories) |
688 |
val ((_, _, edits2), st2) = st1.purge_theories(None) |
|
70783 | 689 |
session.update(st.doc_blobs, edits1 ::: edits2) |
690 |
st2 |
|
75394 | 691 |
} |
69012 | 692 |
} |
693 |
||
75393 | 694 |
def purge_theories( |
695 |
nodes: Option[List[Document.Node.Name]] |
|
696 |
) : (List[Document.Node.Name], List[Document.Node.Name]) = { |
|
75394 | 697 |
state.change_result { st => |
75743 | 698 |
val ((purged, retained, _), st1) = st.purge_theories(nodes) |
70783 | 699 |
((purged, retained), st1) |
75394 | 700 |
} |
69012 | 701 |
} |
67936 | 702 |
} |
67054 | 703 |
} |