author | wenzelm |
Mon, 30 Sep 2019 12:52:16 +0200 | |
changeset 70769 | 9514fdbb8abe |
parent 70768 | 670bb96096a7 |
child 70770 | 8abda6f6b700 |
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 |
|
69012 | 16 |
object Headless |
67054 | 17 |
{ |
69012 | 18 |
/** session **/ |
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
19 |
|
68916 | 20 |
private def stable_snapshot( |
21 |
state: Document.State, version: Document.Version, name: Document.Node.Name): Document.Snapshot = |
|
22 |
{ |
|
23 |
val snapshot = state.snapshot(name) |
|
24 |
assert(version.id == snapshot.version.id) |
|
25 |
snapshot |
|
26 |
} |
|
27 |
||
69013 | 28 |
class Use_Theories_Result private[Headless]( |
67883 | 29 |
val state: Document.State, |
67889 | 30 |
val version: Document.Version, |
68925 | 31 |
val nodes: List[(Document.Node.Name, Document_Status.Node_Status)], |
32 |
val nodes_committed: List[(Document.Node.Name, Document_Status.Node_Status)]) |
|
67879 | 33 |
{ |
69032
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69013
diff
changeset
|
34 |
def nodes_pending: List[(Document.Node.Name, Document_Status.Node_Status)] = |
90bb4cabe1e8
clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents:
69013
diff
changeset
|
35 |
{ |
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 |
||
70765 | 47 |
private sealed abstract class Load_State |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
48 |
{ |
70769
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
49 |
type Result = (List[Document.Node.Name], Load_State) |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
50 |
|
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
51 |
def next(dep_graph: Document.Node.Name.Graph[Unit], finished: Document.Node.Name => Boolean) |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
52 |
: Result = |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
53 |
{ |
70769
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
54 |
def load_checkpoints(checkpoints: List[Document.Node.Name]): Result = |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
55 |
Load_Init(checkpoints).next(dep_graph, finished) |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
56 |
|
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
57 |
def load_requirements( |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
58 |
pending: List[Document.Node.Name], checkpoints: List[Document.Node.Name]): Result = |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
59 |
{ |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
60 |
if (pending.nonEmpty) { |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
61 |
val requirements = dep_graph.all_preds(pending).reverse |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
62 |
(requirements, Load_Bulk(pending, checkpoints)) |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
63 |
} |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
64 |
else load_checkpoints(checkpoints) |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
65 |
} |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
66 |
|
70765 | 67 |
val (load_theories, st1) = |
68 |
this match { |
|
69 |
case Load_Init(Nil) => |
|
70769
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
70 |
val pending = dep_graph.maximals.filterNot(finished) |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
71 |
if (pending.isEmpty) (Nil, Load_Finished) else load_requirements(pending, Nil) |
70767 | 72 |
case Load_Init(target :: checkpoints) => |
73 |
(dep_graph.all_preds(List(target)).reverse, Load_Target(target, checkpoints)) |
|
70768 | 74 |
case Load_Target(pending, checkpoints) if finished(pending) => |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
75 |
val dep_graph1 = |
70767 | 76 |
if (checkpoints.isEmpty) dep_graph |
77 |
else dep_graph.exclude(dep_graph.all_succs(checkpoints).toSet) |
|
70769
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
78 |
val dep_graph2 = |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
79 |
dep_graph1.restrict(dep_graph.all_succs(List(pending)).toSet) |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
80 |
val pending2 = |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
81 |
dep_graph.maximals.filter(node => dep_graph2.defined(node) && !finished(node)) |
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
82 |
load_requirements(pending2, checkpoints) |
70768 | 83 |
case Load_Bulk(pending, checkpoints) if pending.forall(finished) => |
70769
9514fdbb8abe
clarified incremental loading: requirements based on maximal nodes;
wenzelm
parents:
70768
diff
changeset
|
84 |
load_checkpoints(checkpoints) |
70765 | 85 |
case st => (Nil, st) |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
86 |
} |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
87 |
(load_theories.filterNot(finished), st1) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
88 |
} |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
89 |
} |
70765 | 90 |
private case class Load_Init(checkpoints: List[Document.Node.Name]) extends Load_State |
91 |
private case class Load_Target( |
|
70768 | 92 |
pending: Document.Node.Name, checkpoints: List[Document.Node.Name]) extends Load_State |
93 |
private case class Load_Bulk( |
|
94 |
pending: List[Document.Node.Name], checkpoints: List[Document.Node.Name]) extends Load_State |
|
70765 | 95 |
private case object Load_Finished extends Load_State |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
96 |
|
69520 | 97 |
class Session private[Headless]( |
98 |
session_name: String, |
|
99 |
_session_options: => Options, |
|
100 |
override val resources: Resources) extends isabelle.Session(_session_options, resources) |
|
101 |
{ |
|
102 |
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
|
103 |
|
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 |
|
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
105 |
private def loaded_theory(name: Document.Node.Name): Boolean = |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
106 |
resources.session_base.loaded_theory(name.theory) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
107 |
|
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
108 |
|
69520 | 109 |
/* options */ |
110 |
||
111 |
def default_check_delay: Time = session_options.seconds("headless_check_delay") |
|
112 |
def default_check_limit: Int = session_options.int("headless_check_limit") |
|
113 |
def default_nodes_status_delay: Time = session_options.seconds("headless_nodes_status_delay") |
|
114 |
def default_watchdog_timeout: Time = session_options.seconds("headless_watchdog_timeout") |
|
115 |
def default_commit_cleanup_delay: Time = session_options.seconds("headless_commit_cleanup_delay") |
|
67063 | 116 |
|
68922 | 117 |
|
118 |
/* temporary directory */ |
|
119 |
||
67925 | 120 |
val tmp_dir: JFile = Isabelle_System.tmp_dir("server_session") |
67946 | 121 |
val tmp_dir_name: String = File.path(tmp_dir).implode |
67925 | 122 |
|
68923 | 123 |
def master_directory(master_dir: String): String = |
124 |
proper_string(master_dir) getOrElse tmp_dir_name |
|
125 |
||
67945 | 126 |
override def toString: String = session_name |
127 |
||
67925 | 128 |
override def stop(): Process_Result = |
129 |
{ |
|
130 |
try { super.stop() } |
|
131 |
finally { Isabelle_System.rm_tree(tmp_dir) } |
|
132 |
} |
|
133 |
||
67936 | 134 |
|
135 |
/* theories */ |
|
136 |
||
68914 | 137 |
private sealed case class Use_Theories_State( |
70697 | 138 |
dep_graph: Document.Node.Name.Graph[Unit], |
70765 | 139 |
load_state: Load_State, |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
140 |
watchdog_timeout: Time, |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
141 |
commit: Option[(Document.Snapshot, Document_Status.Node_Status) => Unit], |
68914 | 142 |
last_update: Time = Time.now(), |
143 |
nodes_status: Document_Status.Nodes_Status = Document_Status.Nodes_Status.empty, |
|
68925 | 144 |
already_committed: Map[Document.Node.Name, Document_Status.Node_Status] = Map.empty, |
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
145 |
result: Option[Exn.Result[Use_Theories_Result]] = None) |
68914 | 146 |
{ |
147 |
def update(new_nodes_status: Document_Status.Nodes_Status): Use_Theories_State = |
|
148 |
copy(last_update = Time.now(), nodes_status = new_nodes_status) |
|
149 |
||
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
150 |
def watchdog: Boolean = |
68914 | 151 |
watchdog_timeout > Time.zero && Time.now() - last_update > watchdog_timeout |
152 |
||
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
153 |
def finished_result: Boolean = result.isDefined |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
154 |
|
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
155 |
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
|
156 |
if (finished_result) Some((result.get, this)) else None |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
157 |
|
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
158 |
def cancel_result: Use_Theories_State = |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
159 |
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
|
160 |
|
70763
5fae55752c70
tuned messages (again) -- avoid confusion wrt. total remaining size;
wenzelm
parents:
70710
diff
changeset
|
161 |
def clean_theories: (List[Document.Node.Name], Use_Theories_State) = |
70698 | 162 |
{ |
163 |
@tailrec def frontier(base: List[Document.Node.Name], front: Set[Document.Node.Name]) |
|
164 |
: Set[Document.Node.Name] = |
|
165 |
{ |
|
166 |
val add = base.filter(name => dep_graph.imm_succs(name).forall(front)) |
|
167 |
if (add.isEmpty) front |
|
168 |
else { |
|
169 |
val preds = add.map(dep_graph.imm_preds) |
|
170 |
val base1 = (preds.head /: preds.tail)(_ ++ _).toList.filter(already_committed.keySet) |
|
171 |
frontier(base1, front ++ add) |
|
172 |
} |
|
173 |
} |
|
174 |
||
70763
5fae55752c70
tuned messages (again) -- avoid confusion wrt. total remaining size;
wenzelm
parents:
70710
diff
changeset
|
175 |
if (already_committed.isEmpty) (Nil, this) |
70698 | 176 |
else { |
70705 | 177 |
val base = |
178 |
(for { |
|
179 |
(name, (_, (_, succs))) <- dep_graph.iterator |
|
180 |
if succs.isEmpty && already_committed.isDefinedAt(name) |
|
181 |
} yield name).toList |
|
182 |
val clean = frontier(base, Set.empty) |
|
70763
5fae55752c70
tuned messages (again) -- avoid confusion wrt. total remaining size;
wenzelm
parents:
70710
diff
changeset
|
183 |
if (clean.isEmpty) (Nil, this) |
70698 | 184 |
else { |
70763
5fae55752c70
tuned messages (again) -- avoid confusion wrt. total remaining size;
wenzelm
parents:
70710
diff
changeset
|
185 |
(dep_graph.topological_order.filter(clean), |
70699 | 186 |
copy(dep_graph = dep_graph.exclude(clean))) |
70698 | 187 |
} |
188 |
} |
|
189 |
} |
|
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
190 |
|
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
191 |
def check(state: Document.State, version: Document.Version, beyond_limit: Boolean) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
192 |
: (List[Document.Node.Name], Use_Theories_State) = |
68916 | 193 |
{ |
69035 | 194 |
val already_committed1 = |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
195 |
commit match { |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
196 |
case None => already_committed |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
197 |
case Some(commit_fn) => |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
198 |
(already_committed /: dep_graph.topological_order)( |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
199 |
{ case (committed, name) => |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
200 |
def parents_committed: Boolean = |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
201 |
version.nodes(name).header.imports.forall(parent => |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
202 |
loaded_theory(parent) || committed.isDefinedAt(parent)) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
203 |
if (!committed.isDefinedAt(name) && parents_committed && |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
204 |
state.node_consolidated(version, name)) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
205 |
{ |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
206 |
val snapshot = stable_snapshot(state, version, name) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
207 |
val status = Document_Status.Node_Status.make(state, version, name) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
208 |
commit_fn(snapshot, status) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
209 |
committed + (name -> status) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
210 |
} |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
211 |
else committed |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
212 |
}) |
68916 | 213 |
} |
214 |
||
70657 | 215 |
def finished_theory(name: Document.Node.Name): Boolean = |
216 |
loaded_theory(name) || |
|
70704 | 217 |
(if (commit.isDefined) already_committed1.isDefinedAt(name) |
218 |
else state.node_consolidated(version, name)) |
|
70657 | 219 |
|
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
220 |
val result1 = |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
221 |
if (!finished_result && |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
222 |
(beyond_limit || watchdog || |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
223 |
dep_graph.keys_iterator.forall(name => |
70657 | 224 |
finished_theory(name) || nodes_status.quasi_consolidated(name)))) |
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
225 |
{ |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
226 |
val nodes = |
70657 | 227 |
(for { |
228 |
name <- dep_graph.keys_iterator |
|
229 |
if !loaded_theory(name) |
|
230 |
} yield { (name -> Document_Status.Node_Status.make(state, version, name)) }).toList |
|
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
231 |
val nodes_committed = |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
232 |
(for { |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
233 |
name <- dep_graph.keys_iterator |
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
234 |
status <- already_committed1.get(name) |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
235 |
} yield (name -> status)).toList |
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
236 |
Some(Exn.Res(new Use_Theories_Result(state, version, nodes, nodes_committed))) |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
237 |
} |
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
238 |
else result |
68925 | 239 |
|
70765 | 240 |
val (load_theories, load_state1) = load_state.next(dep_graph, finished_theory(_)) |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
241 |
|
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
242 |
(load_theories, |
70765 | 243 |
copy(already_committed = already_committed1, result = result1, load_state = load_state1)) |
68916 | 244 |
} |
68914 | 245 |
} |
246 |
||
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
247 |
def use_theories( |
67940
b4e80f062fbf
clarified signature -- eliminated somewhat pointless positions;
wenzelm
parents:
67939
diff
changeset
|
248 |
theories: List[String], |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
249 |
qualifier: String = Sessions.DRAFT, |
67881 | 250 |
master_dir: String = "", |
69920 | 251 |
unicode_symbols: Boolean = false, |
68943 | 252 |
check_delay: Time = default_check_delay, |
69520 | 253 |
check_limit: Int = default_check_limit, |
68947 | 254 |
watchdog_timeout: Time = default_watchdog_timeout, |
68943 | 255 |
nodes_status_delay: Time = default_nodes_status_delay, |
69458 | 256 |
id: UUID.T = UUID.random(), |
70625
1ae987cc052f
support for share_common_data after define_command and before actual update: this affects string particles of command tokens;
wenzelm
parents:
69920
diff
changeset
|
257 |
share_common_data: Boolean = false, |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
258 |
checkpoints: Set[Document.Node.Name] = Set.empty, |
68916 | 259 |
// commit: must not block, must not fail |
260 |
commit: Option[(Document.Snapshot, Document_Status.Node_Status) => Unit] = None, |
|
68981 | 261 |
commit_cleanup_delay: Time = default_commit_cleanup_delay, |
69013 | 262 |
progress: Progress = No_Progress): Use_Theories_Result = |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
263 |
{ |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
264 |
val dependencies = |
68894
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
265 |
{ |
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
266 |
val import_names = |
68923 | 267 |
theories.map(thy => |
268 |
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
|
269 |
resources.dependencies(import_names, progress = progress).check_errors |
68894
1dbdad1b57a5
more robust: load_theories after consumer is installed;
wenzelm
parents:
68888
diff
changeset
|
270 |
} |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
271 |
val dep_theories = dependencies.theories |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
272 |
val dep_theories_set = dep_theories.toSet |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
273 |
val dep_files = |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
274 |
dependencies.loaded_files(false).flatMap(_._2). |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
275 |
map(path => Document.Node.Name(resources.append("", path))) |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
276 |
|
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
277 |
val use_theories_state = |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
278 |
{ |
70765 | 279 |
val load_state = |
280 |
Load_Init( |
|
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
281 |
if (checkpoints.isEmpty) Nil |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
282 |
else dependencies.theory_graph.topological_order.filter(checkpoints(_))) |
70697 | 283 |
Synchronized( |
70765 | 284 |
Use_Theories_State(dependencies.theory_graph, load_state, watchdog_timeout, commit)) |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
285 |
} |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
286 |
|
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
287 |
def check_state(beyond_limit: Boolean = false) |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
288 |
{ |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
289 |
val state = session.current_state() |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
290 |
for (version <- state.stable_tip_version) { |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
291 |
val load_theories = use_theories_state.change_result(_.check(state, version, beyond_limit)) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
292 |
if (load_theories.nonEmpty) { |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
293 |
resources.load_theories( |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
294 |
session, id, load_theories, dep_files, unicode_symbols, share_common_data, progress) |
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
295 |
} |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
296 |
} |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
297 |
} |
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
298 |
|
67894
fee080c4045f
more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents:
67893
diff
changeset
|
299 |
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
|
300 |
{ |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
301 |
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
|
302 |
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
|
303 |
{ |
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
304 |
if (progress.stopped) use_theories_state.change(_.cancel_result) |
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
|
305 |
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
|
306 |
check_count += 1 |
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
307 |
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
|
308 |
} |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
309 |
} |
03e104be99af
added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents:
68365
diff
changeset
|
310 |
} |
67894
fee080c4045f
more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents:
67893
diff
changeset
|
311 |
|
68906 | 312 |
val consumer = |
313 |
{ |
|
314 |
val delay_nodes_status = |
|
315 |
Standard_Thread.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
|
316 |
progress.nodes_status(use_theories_state.value.nodes_status) |
68906 | 317 |
} |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
318 |
|
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
319 |
val delay_commit_clean = |
68981 | 320 |
Standard_Thread.delay_first(commit_cleanup_delay max Time.zero) { |
70763
5fae55752c70
tuned messages (again) -- avoid confusion wrt. total remaining size;
wenzelm
parents:
70710
diff
changeset
|
321 |
val clean_theories = use_theories_state.change_result(_.clean_theories) |
70702 | 322 |
if (clean_theories.nonEmpty) { |
70766 | 323 |
progress.echo("Removing " + clean_theories.length + " theories") |
70702 | 324 |
resources.clean_theories(session, id, clean_theories) |
325 |
} |
|
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
326 |
} |
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
327 |
|
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
328 |
Session.Consumer[Session.Commands_Changed](getClass.getName) { |
68330 | 329 |
case changed => |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
330 |
if (changed.nodes.exists(dep_theories_set)) { |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
331 |
val snapshot = session.snapshot() |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
332 |
val state = snapshot.state |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
333 |
val version = snapshot.version |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68758
diff
changeset
|
334 |
|
68957 | 335 |
val theory_progress = |
68914 | 336 |
use_theories_state.change_result(st => |
68903 | 337 |
{ |
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
338 |
val domain = |
68914 | 339 |
if (st.nodes_status.is_empty) dep_theories_set |
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
340 |
else changed.nodes.iterator.filter(dep_theories_set).toSet |
68899 | 341 |
|
68903 | 342 |
val (nodes_status_changed, nodes_status1) = |
69255
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69035
diff
changeset
|
343 |
st.nodes_status.update(resources, state, version, |
68903 | 344 |
domain = Some(domain), trim = changed.assignment) |
68899 | 345 |
|
68903 | 346 |
if (nodes_status_delay >= Time.zero && nodes_status_changed) { |
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
347 |
delay_nodes_status.invoke |
68899 | 348 |
} |
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68771
diff
changeset
|
349 |
|
68957 | 350 |
val theory_progress = |
68905 | 351 |
(for { |
69818
60d0ee8f2ddb
more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents:
69817
diff
changeset
|
352 |
(name, node_status) <- nodes_status1.present.iterator |
68959 | 353 |
if changed.nodes.contains(name) && !st.already_committed.isDefinedAt(name) |
68962 | 354 |
p1 = node_status.percentage |
355 |
if p1 > 0 && Some(p1) != st.nodes_status.get(name).map(_.percentage) |
|
356 |
} yield Progress.Theory(name.theory, percentage = Some(p1))).toList |
|
68903 | 357 |
|
68957 | 358 |
(theory_progress, st.update(nodes_status1)) |
68903 | 359 |
}) |
68330 | 360 |
|
68957 | 361 |
theory_progress.foreach(progress.theory(_)) |
68903 | 362 |
|
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
363 |
check_state() |
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
364 |
|
68981 | 365 |
if (commit.isDefined && commit_cleanup_delay > Time.zero) { |
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
366 |
if (use_theories_state.value.finished_result) |
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
367 |
delay_commit_clean.revoke |
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
368 |
else delay_commit_clean.invoke |
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
369 |
} |
68330 | 370 |
} |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
371 |
} |
68906 | 372 |
} |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
373 |
|
67892 | 374 |
try { |
375 |
session.commands_changed += consumer |
|
70653
f7c5b30fc432
load theories in stages, to reduce ML heap requirements;
wenzelm
parents:
70649
diff
changeset
|
376 |
check_state() |
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
377 |
use_theories_state.guarded_access(_.join_result) |
67894
fee080c4045f
more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents:
67893
diff
changeset
|
378 |
check_progress.cancel |
67892 | 379 |
} |
380 |
finally { |
|
68907 | 381 |
session.commands_changed -= consumer |
67892 | 382 |
resources.unload_theories(session, id, dep_theories) |
383 |
} |
|
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
384 |
|
70644
b23a6dfcfd57
clarified state variable: avoid extra mutability via Promise;
wenzelm
parents:
70640
diff
changeset
|
385 |
Exn.release(use_theories_state.guarded_access(_.join_result)) |
67064
fb487246ef4f
synchronous use_theories, based on consolidated_state;
wenzelm
parents:
67063
diff
changeset
|
386 |
} |
67936 | 387 |
|
67939 | 388 |
def purge_theories( |
68915 | 389 |
theories: List[String], |
390 |
qualifier: String = Sessions.DRAFT, |
|
391 |
master_dir: String = "", |
|
392 |
all: Boolean = false): (List[Document.Node.Name], List[Document.Node.Name]) = |
|
393 |
{ |
|
68923 | 394 |
val nodes = |
395 |
if (all) None |
|
396 |
else Some(theories.map(resources.import_name(qualifier, master_directory(master_dir), _))) |
|
68915 | 397 |
resources.purge_theories(session, nodes) |
398 |
} |
|
67063 | 399 |
} |
400 |
||
67061
2efa25302f34
synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents:
67059
diff
changeset
|
401 |
|
67054 | 402 |
|
69012 | 403 |
/** resources **/ |
68922 | 404 |
|
69012 | 405 |
object Resources |
406 |
{ |
|
69536 | 407 |
def apply(base_info: Sessions.Base_Info, log: Logger = No_Logger): Resources = |
408 |
new Resources(base_info, log = log) |
|
409 |
||
410 |
def make( |
|
411 |
options: Options, |
|
412 |
session_name: String, |
|
413 |
session_dirs: List[Path] = Nil, |
|
414 |
include_sessions: List[String] = Nil, |
|
415 |
progress: Progress = No_Progress, |
|
416 |
log: Logger = No_Logger): Resources = |
|
417 |
{ |
|
418 |
val base_info = |
|
419 |
Sessions.base_info(options, session_name, dirs = session_dirs, |
|
420 |
include_sessions = include_sessions, progress = progress) |
|
421 |
apply(base_info, log = log) |
|
422 |
} |
|
423 |
||
69012 | 424 |
final class Theory private[Headless]( |
425 |
val node_name: Document.Node.Name, |
|
426 |
val node_header: Document.Node.Header, |
|
427 |
val text: String, |
|
428 |
val node_required: Boolean) |
|
68922 | 429 |
{ |
69012 | 430 |
override def toString: String = node_name.toString |
68922 | 431 |
|
69012 | 432 |
def node_perspective: Document.Node.Perspective_Text = |
433 |
Document.Node.Perspective(node_required, Text.Perspective.empty, Document.Node.Overlays.empty) |
|
68922 | 434 |
|
69012 | 435 |
def make_edits(text_edits: List[Text.Edit]): List[Document.Edit_Text] = |
436 |
List(node_name -> Document.Node.Deps(node_header), |
|
437 |
node_name -> Document.Node.Edits(text_edits), |
|
438 |
node_name -> node_perspective) |
|
68922 | 439 |
|
69012 | 440 |
def node_edits(old: Option[Theory]): List[Document.Edit_Text] = |
441 |
{ |
|
442 |
val (text_edits, old_required) = |
|
443 |
if (old.isEmpty) (Text.Edit.inserts(0, text), false) |
|
444 |
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
|
445 |
|
69012 | 446 |
if (text_edits.isEmpty && node_required == old_required) Nil |
447 |
else make_edits(text_edits) |
|
448 |
} |
|
67887
a4d5342898b1
unload_theories: actually observe required state;
wenzelm
parents:
67885
diff
changeset
|
449 |
|
69012 | 450 |
def purge_edits: List[Document.Edit_Text] = |
451 |
make_edits(Text.Edit.removes(0, text)) |
|
67936 | 452 |
|
69012 | 453 |
def required(required: Boolean): Theory = |
454 |
if (required == node_required) this |
|
455 |
else new Theory(node_name, node_header, text, required) |
|
67936 | 456 |
} |
457 |
||
69012 | 458 |
sealed case class State( |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
459 |
blobs: Map[Document.Node.Name, Document.Blob] = Map.empty, |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
460 |
theories: Map[Document.Node.Name, Theory] = Map.empty, |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
461 |
required: Multi_Map[Document.Node.Name, UUID.T] = Multi_Map.empty) |
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
462 |
{ |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
463 |
/* blobs */ |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
464 |
|
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
465 |
def doc_blobs: Document.Blobs = Document.Blobs(blobs) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
466 |
|
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
467 |
def update_blobs(names: List[Document.Node.Name]): (Document.Blobs, State) = |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
468 |
{ |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
469 |
val new_blobs = |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
470 |
names.flatMap(name => |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
471 |
{ |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
472 |
val bytes = Bytes.read(name.path) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
473 |
def new_blob: Document.Blob = |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
474 |
{ |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
475 |
val text = bytes.text |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
476 |
Document.Blob(bytes, text, Symbol.Text_Chunk(text), changed = true) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
477 |
} |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
478 |
blobs.get(name) match { |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
479 |
case Some(blob) => if (blob.bytes == bytes) None else Some(name -> new_blob) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
480 |
case None => Some(name -> new_blob) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
481 |
} |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
482 |
}) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
483 |
val blobs1 = (blobs /: new_blobs)(_ + _) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
484 |
val blobs2 = (blobs /: new_blobs)({ case (map, (a, b)) => map + (a -> b.unchanged) }) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
485 |
(Document.Blobs(blobs1), copy(blobs = blobs2)) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
486 |
} |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
487 |
|
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
488 |
def blob_edits(name: Document.Node.Name, old_blob: Option[Document.Blob]) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
489 |
: List[Document.Edit_Text] = |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
490 |
{ |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
491 |
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
|
492 |
val text_edits = |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
493 |
old_blob match { |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
494 |
case None => List(Text.Edit.insert(0, blob.source)) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
495 |
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
|
496 |
} |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
497 |
if (text_edits.isEmpty) Nil |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
498 |
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
|
499 |
} |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
500 |
|
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
501 |
|
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
502 |
/* theories */ |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
503 |
|
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
|
504 |
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
|
505 |
Document.Node.Name.make_graph( |
69012 | 506 |
for ((name, theory) <- theories.toList) |
70647 | 507 |
yield ((name, ()), theory.node_header.imports.filter(theories.isDefinedAt(_)))) |
67056 | 508 |
|
69012 | 509 |
def is_required(name: Document.Node.Name): Boolean = required.isDefinedAt(name) |
510 |
||
69458 | 511 |
def insert_required(id: UUID.T, names: List[Document.Node.Name]): State = |
69012 | 512 |
copy(required = (required /: names)(_.insert(_, id))) |
513 |
||
69458 | 514 |
def remove_required(id: UUID.T, names: List[Document.Node.Name]): State = |
69012 | 515 |
copy(required = (required /: names)(_.remove(_, id))) |
68958 | 516 |
|
69012 | 517 |
def update_theories(update: List[(Document.Node.Name, Theory)]): State = |
518 |
copy(theories = |
|
519 |
(theories /: update)({ case (thys, (name, thy)) => |
|
520 |
thys.get(name) match { |
|
521 |
case Some(thy1) if thy1 == thy => thys |
|
522 |
case _ => thys + (name -> thy) |
|
523 |
} |
|
524 |
})) |
|
525 |
||
526 |
def remove_theories(remove: List[Document.Node.Name]): State = |
|
67893 | 527 |
{ |
69012 | 528 |
require(remove.forall(name => !is_required(name))) |
529 |
copy(theories = theories -- remove) |
|
530 |
} |
|
531 |
||
70649 | 532 |
def unload_theories(session: Session, id: UUID.T, theories: List[Document.Node.Name]) |
69458 | 533 |
: State = |
69012 | 534 |
{ |
70649 | 535 |
val st1 = remove_required(id, theories) |
67893 | 536 |
val theory_edits = |
69012 | 537 |
for { |
70649 | 538 |
node_name <- theories |
69012 | 539 |
theory <- st1.theories.get(node_name) |
540 |
} |
|
67893 | 541 |
yield { |
542 |
val theory1 = theory.required(st1.is_required(node_name)) |
|
69012 | 543 |
val edits = theory1.node_edits(Some(theory)) |
67893 | 544 |
(edits, (node_name, theory1)) |
545 |
} |
|
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
546 |
session.update(doc_blobs, theory_edits.flatMap(_._1)) |
67893 | 547 |
st1.update_theories(theory_edits.map(_._2)) |
69012 | 548 |
} |
549 |
||
550 |
def purge_theories(session: Session, nodes: List[Document.Node.Name]) |
|
551 |
: ((List[Document.Node.Name], List[Document.Node.Name]), State) = |
|
552 |
{ |
|
553 |
val all_nodes = theory_graph.topological_order |
|
554 |
val purge = nodes.filterNot(is_required(_)).toSet |
|
555 |
||
556 |
val retain = theory_graph.all_preds(all_nodes.filterNot(purge)).toSet |
|
557 |
val (retained, purged) = all_nodes.partition(retain) |
|
558 |
||
559 |
val purge_edits = purged.flatMap(name => theories(name).purge_edits) |
|
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
560 |
session.update(doc_blobs, purge_edits) |
69012 | 561 |
|
562 |
((purged, retained), remove_theories(purged)) |
|
563 |
} |
|
564 |
} |
|
68936
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
565 |
} |
90c08c7bab9c
continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents:
68935
diff
changeset
|
566 |
|
69536 | 567 |
class Resources private[Headless]( |
568 |
val session_base_info: Sessions.Base_Info, |
|
569 |
log: Logger = No_Logger) |
|
70683
8c7706b053c7
find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents:
70674
diff
changeset
|
570 |
extends isabelle.Resources( |
8c7706b053c7
find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents:
70674
diff
changeset
|
571 |
session_base_info.sessions_structure, session_base_info.check_base, log = log) |
67884
43af581d7d8e
unload_theories after consolidation -- reset node_required;
wenzelm
parents:
67883
diff
changeset
|
572 |
{ |
69012 | 573 |
resources => |
574 |
||
69538
faf547d2834c
clarified signature, notably cascade of dump_options, deps, resources, session;
wenzelm
parents:
69536
diff
changeset
|
575 |
def options: Options = session_base_info.options |
faf547d2834c
clarified signature, notably cascade of dump_options, deps, resources, session;
wenzelm
parents:
69536
diff
changeset
|
576 |
|
69536 | 577 |
|
578 |
/* session */ |
|
579 |
||
580 |
def start_session(print_mode: List[String] = Nil, progress: Progress = No_Progress): Session = |
|
581 |
{ |
|
582 |
val session = new Session(session_base_info.session, options, resources) |
|
583 |
||
584 |
val session_error = Future.promise[String] |
|
585 |
var session_phase: Session.Consumer[Session.Phase] = null |
|
586 |
session_phase = |
|
587 |
Session.Consumer(getClass.getName) { |
|
588 |
case Session.Ready => |
|
589 |
session.phase_changed -= session_phase |
|
590 |
session_error.fulfill("") |
|
591 |
case Session.Terminated(result) if !result.ok => |
|
592 |
session.phase_changed -= session_phase |
|
593 |
session_error.fulfill("Session start failed: return code " + result.rc) |
|
594 |
case _ => |
|
595 |
} |
|
596 |
session.phase_changed += session_phase |
|
597 |
||
598 |
progress.echo("Starting session " + session_base_info.session + " ...") |
|
599 |
Isabelle_Process.start(session, options, |
|
600 |
logic = session_base_info.session, dirs = session_base_info.dirs, modes = print_mode) |
|
601 |
||
602 |
session_error.join match { |
|
603 |
case "" => session |
|
604 |
case msg => session.stop(); error(msg) |
|
605 |
} |
|
606 |
} |
|
607 |
||
608 |
||
609 |
/* theories */ |
|
610 |
||
69012 | 611 |
private val state = Synchronized(Resources.State()) |
612 |
||
613 |
def load_theories( |
|
614 |
session: Session, |
|
69458 | 615 |
id: UUID.T, |
70649 | 616 |
theories: List[Document.Node.Name], |
617 |
files: List[Document.Node.Name], |
|
69920 | 618 |
unicode_symbols: Boolean, |
70625
1ae987cc052f
support for share_common_data after define_command and before actual update: this affects string particles of command tokens;
wenzelm
parents:
69920
diff
changeset
|
619 |
share_common_data: Boolean, |
69012 | 620 |
progress: Progress) |
621 |
{ |
|
622 |
val loaded_theories = |
|
70649 | 623 |
for (node_name <- theories) |
69012 | 624 |
yield { |
625 |
val path = node_name.path |
|
626 |
if (!node_name.is_theory) error("Not a theory file: " + path) |
|
627 |
||
628 |
progress.expose_interrupt() |
|
69920 | 629 |
val text0 = File.read(path) |
630 |
val text = if (unicode_symbols) Symbol.decode(text0) else text0 |
|
69012 | 631 |
val node_header = resources.check_thy_reader(node_name, Scan.char_reader(text)) |
632 |
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
|
633 |
} |
69012 | 634 |
|
635 |
val loaded = loaded_theories.length |
|
636 |
if (loaded > 1) progress.echo("Loading " + loaded + " theories ...") |
|
637 |
||
638 |
state.change(st => |
|
639 |
{ |
|
70649 | 640 |
val (doc_blobs1, st1) = st.insert_required(id, theories).update_blobs(files) |
69012 | 641 |
val theory_edits = |
642 |
for (theory <- loaded_theories) |
|
643 |
yield { |
|
644 |
val node_name = theory.node_name |
|
645 |
val theory1 = theory.required(st1.is_required(node_name)) |
|
646 |
val edits = theory1.node_edits(st1.theories.get(node_name)) |
|
647 |
(edits, (node_name, theory1)) |
|
648 |
} |
|
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
649 |
val file_edits = |
70649 | 650 |
for { node_name <- files if doc_blobs1.changed(node_name) } |
69562
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
651 |
yield st1.blob_edits(node_name, st.blobs.get(node_name)) |
636b3c03a61a
include loaded_files as doc_blobs (without purging);
wenzelm
parents:
69538
diff
changeset
|
652 |
|
70625
1ae987cc052f
support for share_common_data after define_command and before actual update: this affects string particles of command tokens;
wenzelm
parents:
69920
diff
changeset
|
653 |
session.update(doc_blobs1, theory_edits.flatMap(_._1) ::: file_edits.flatten, |
1ae987cc052f
support for share_common_data after define_command and before actual update: this affects string particles of command tokens;
wenzelm
parents:
69920
diff
changeset
|
654 |
share_common_data = share_common_data) |
69012 | 655 |
st1.update_theories(theory_edits.map(_._2)) |
656 |
}) |
|
657 |
} |
|
67936 | 658 |
|
70649 | 659 |
def unload_theories(session: Session, id: UUID.T, theories: List[Document.Node.Name]) |
69012 | 660 |
{ |
70649 | 661 |
state.change(_.unload_theories(session, id, theories)) |
69012 | 662 |
} |
663 |
||
70698 | 664 |
def clean_theories(session: Session, id: UUID.T, theories: List[Document.Node.Name]) |
69012 | 665 |
{ |
666 |
state.change(st => |
|
70698 | 667 |
st.unload_theories(session, id, theories).purge_theories(session, theories)._2 |
668 |
) |
|
69012 | 669 |
} |
670 |
||
671 |
def purge_theories(session: Session, nodes: Option[List[Document.Node.Name]]) |
|
672 |
: (List[Document.Node.Name], List[Document.Node.Name]) = |
|
673 |
{ |
|
674 |
state.change_result(st => st.purge_theories(session, nodes getOrElse st.theory_graph.keys)) |
|
675 |
} |
|
67936 | 676 |
} |
67054 | 677 |
} |