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