author | wenzelm |
Sat, 08 Apr 2017 20:56:41 +0200 | |
changeset 65441 | 9425e4d8bdb6 |
parent 65432 | d938705819bb |
child 65445 | e9e7f5f5794c |
permissions | -rw-r--r-- |
62631 | 1 |
/* Title: Pure/Thy/sessions.scala |
2 |
Author: Makarius |
|
3 |
||
65430 | 4 |
Cumulative session information. |
62631 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
62704
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
9 |
import java.nio.ByteBuffer |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
10 |
import java.nio.channels.FileChannel |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
11 |
import java.nio.file.StandardOpenOption |
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
12 |
import java.sql.PreparedStatement |
62631 | 13 |
|
14 |
import scala.collection.SortedSet |
|
15 |
import scala.collection.mutable |
|
16 |
||
17 |
||
18 |
object Sessions |
|
19 |
{ |
|
65360 | 20 |
/* base info and source dependencies */ |
62883 | 21 |
|
65360 | 22 |
def is_pure(name: String): Boolean = name == Thy_Header.PURE |
64856 | 23 |
|
24 |
object Base |
|
25 |
{ |
|
65360 | 26 |
def pure(options: Options): Base = session_base(options, Thy_Header.PURE) |
27 |
||
64856 | 28 |
lazy val bootstrap: Base = |
29 |
Base(keywords = Thy_Header.bootstrap_header, syntax = Thy_Header.bootstrap_syntax) |
|
65427 | 30 |
|
31 |
private[Sessions] def known_theories(bases: Iterable[Base], names: Iterable[Document.Node.Name]) |
|
32 |
: Map[String, Document.Node.Name] = |
|
33 |
{ |
|
34 |
val bases_iterator = |
|
35 |
for { base <- bases.iterator; (_, name) <- base.known_theories.iterator } |
|
36 |
yield name |
|
37 |
||
38 |
(Map.empty[String, Document.Node.Name] /: (bases_iterator ++ names.iterator))({ |
|
39 |
case (known, name) => |
|
40 |
known.get(name.theory) match { |
|
41 |
case Some(name1) if name != name1 => |
|
42 |
error("Duplicate theory " + quote(name.node) + " vs. " + quote(name1.node)) |
|
65429
fcff401fb609
more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents:
65428
diff
changeset
|
43 |
case _ => known + (name.theory -> name) |
65427 | 44 |
} |
45 |
}) |
|
46 |
} |
|
64856 | 47 |
} |
48 |
||
49 |
sealed case class Base( |
|
65372
b722ee40c26c
refer to global_theories from all sessions, before selection;
wenzelm
parents:
65371
diff
changeset
|
50 |
global_theories: Set[String] = Set.empty, |
65429
fcff401fb609
more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents:
65428
diff
changeset
|
51 |
loaded_theories: Map[String, Document.Node.Name] = Map.empty, |
64856 | 52 |
known_theories: Map[String, Document.Node.Name] = Map.empty, |
53 |
keywords: Thy_Header.Keywords = Nil, |
|
54 |
syntax: Outer_Syntax = Outer_Syntax.empty, |
|
55 |
sources: List[(Path, SHA1.Digest)] = Nil, |
|
56 |
session_graph: Graph_Display.Graph = Graph_Display.empty_graph) |
|
65355 | 57 |
{ |
58 |
def loaded_theory(name: Document.Node.Name): Boolean = |
|
65429
fcff401fb609
more explicit lookup of loaded_theories: base names allowed here;
wenzelm
parents:
65428
diff
changeset
|
59 |
loaded_theories.isDefinedAt(name.theory) |
65432 | 60 |
|
65441 | 61 |
def dest_loaded_theories: List[(String, String)] = |
62 |
for ((theory, node_name) <- loaded_theories.toList) |
|
63 |
yield (theory, node_name.node) |
|
64 |
||
65432 | 65 |
def dest_known_theories: List[(String, String)] = |
66 |
for ((theory, node_name) <- known_theories.toList) |
|
67 |
yield (theory, node_name.node) |
|
65355 | 68 |
} |
64856 | 69 |
|
65406 | 70 |
sealed case class Deps(sessions: Map[String, Base]) |
65251 | 71 |
{ |
65406 | 72 |
def is_empty: Boolean = sessions.isEmpty |
73 |
def apply(name: String): Base = sessions(name) |
|
74 |
def sources(name: String): List[SHA1.Digest] = sessions(name).sources.map(_._2) |
|
65428 | 75 |
|
76 |
def all_known_theories: Map[String, Document.Node.Name] = |
|
77 |
Base.known_theories(sessions.toList.map(_._2), Nil) |
|
65251 | 78 |
} |
64856 | 79 |
|
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
80 |
def deps(sessions: T, |
65251 | 81 |
progress: Progress = No_Progress, |
82 |
inlined_files: Boolean = false, |
|
83 |
verbose: Boolean = false, |
|
84 |
list_files: Boolean = false, |
|
85 |
check_keywords: Set[String] = Set.empty, |
|
65406 | 86 |
global_theories: Set[String] = Set.empty): Deps = |
65372
b722ee40c26c
refer to global_theories from all sessions, before selection;
wenzelm
parents:
65371
diff
changeset
|
87 |
{ |
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
88 |
Deps((Map.empty[String, Base] /: sessions.imports_topological_order)({ |
65425 | 89 |
case (sessions, (session_name, info)) => |
65410 | 90 |
if (progress.stopped) throw Exn.Interrupt() |
65251 | 91 |
|
65410 | 92 |
try { |
93 |
val parent_base = |
|
94 |
info.parent match { |
|
95 |
case None => Base.bootstrap |
|
96 |
case Some(parent) => sessions(parent) |
|
97 |
} |
|
65441 | 98 |
val resources = new Resources(parent_base, default_qualifier = session_name) |
65251 | 99 |
|
65410 | 100 |
if (verbose || list_files) { |
101 |
val groups = |
|
102 |
if (info.groups.isEmpty) "" |
|
103 |
else info.groups.mkString(" (", " ", ")") |
|
65425 | 104 |
progress.echo("Session " + info.chapter + "/" + session_name + groups) |
65410 | 105 |
} |
65251 | 106 |
|
65410 | 107 |
val thy_deps = |
108 |
{ |
|
109 |
val root_theories = |
|
110 |
info.theories.flatMap({ case (_, thys) => |
|
111 |
thys.map(thy => (resources.import_name(info.dir.implode, thy), info.pos)) |
|
112 |
}) |
|
113 |
val thy_deps = resources.thy_info.dependencies(root_theories) |
|
65251 | 114 |
|
65410 | 115 |
thy_deps.errors match { |
116 |
case Nil => thy_deps |
|
117 |
case errs => error(cat_lines(errs)) |
|
65251 | 118 |
} |
65410 | 119 |
} |
65251 | 120 |
|
65410 | 121 |
val syntax = thy_deps.syntax |
65251 | 122 |
|
65410 | 123 |
val theory_files = thy_deps.deps.map(dep => Path.explode(dep.name.node)) |
124 |
val loaded_files = |
|
125 |
if (inlined_files) { |
|
126 |
val pure_files = |
|
65425 | 127 |
if (is_pure(session_name)) { |
65410 | 128 |
val roots = Thy_Header.ml_roots.map(p => info.dir + Path.explode(p._1)) |
129 |
val files = |
|
130 |
roots.flatMap(root => resources.loaded_files(syntax, File.read(root))). |
|
131 |
map(file => info.dir + Path.explode(file)) |
|
132 |
roots ::: files |
|
133 |
} |
|
134 |
else Nil |
|
135 |
pure_files ::: thy_deps.loaded_files |
|
136 |
} |
|
137 |
else Nil |
|
65251 | 138 |
|
65410 | 139 |
val all_files = |
140 |
(theory_files ::: loaded_files ::: |
|
141 |
info.files.map(file => info.dir + file) ::: |
|
142 |
info.document_files.map(file => info.dir + file._1 + file._2)).map(_.expand) |
|
65251 | 143 |
|
65410 | 144 |
if (list_files) |
145 |
progress.echo(cat_lines(all_files.map(_.implode).sorted.map(" " + _))) |
|
65251 | 146 |
|
65410 | 147 |
if (check_keywords.nonEmpty) |
148 |
Check_Keywords.check_keywords(progress, syntax.keywords, check_keywords, theory_files) |
|
65251 | 149 |
|
65410 | 150 |
val base = |
151 |
Base(global_theories = global_theories, |
|
65432 | 152 |
loaded_theories = thy_deps.loaded_theories, |
153 |
known_theories = |
|
154 |
Base.known_theories( |
|
155 |
parent_base :: info.imports.map(sessions(_)), thy_deps.deps.map(_.name)), |
|
156 |
keywords = thy_deps.keywords, |
|
65410 | 157 |
syntax = syntax, |
158 |
sources = all_files.map(p => (p, SHA1.digest(p.file))), |
|
159 |
session_graph = thy_deps.session_graph(info.parent getOrElse "", parent_base)) |
|
65251 | 160 |
|
65425 | 161 |
sessions + (session_name -> base) |
65410 | 162 |
} |
163 |
catch { |
|
164 |
case ERROR(msg) => |
|
165 |
cat_error(msg, "The error(s) above occurred in session " + |
|
65425 | 166 |
quote(session_name) + Position.here(info.pos)) |
65410 | 167 |
} |
168 |
})) |
|
65372
b722ee40c26c
refer to global_theories from all sessions, before selection;
wenzelm
parents:
65371
diff
changeset
|
169 |
} |
65251 | 170 |
|
65428 | 171 |
def session_base( |
172 |
options: Options, |
|
173 |
session: String, |
|
174 |
dirs: List[Path] = Nil, |
|
175 |
all_known_theories: Boolean = false): Base = |
|
65251 | 176 |
{ |
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
177 |
val full_sessions = load(options, dirs = dirs) |
65428 | 178 |
val global_theories = full_sessions.global_theories |
179 |
val selected_sessions = full_sessions.selection(Selection(sessions = List(session)))._2 |
|
65372
b722ee40c26c
refer to global_theories from all sessions, before selection;
wenzelm
parents:
65371
diff
changeset
|
180 |
|
65428 | 181 |
if (all_known_theories) { |
182 |
val deps = Sessions.deps(full_sessions, global_theories = global_theories) |
|
183 |
deps(session).copy(known_theories = deps.all_known_theories) |
|
184 |
} |
|
185 |
else |
|
186 |
deps(selected_sessions, global_theories = global_theories)(session) |
|
65251 | 187 |
} |
188 |
||
189 |
||
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
190 |
/* cumulative session info */ |
62631 | 191 |
|
192 |
sealed case class Info( |
|
193 |
chapter: String, |
|
194 |
select: Boolean, |
|
195 |
pos: Position.T, |
|
196 |
groups: List[String], |
|
197 |
dir: Path, |
|
198 |
parent: Option[String], |
|
199 |
description: String, |
|
200 |
options: Options, |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
201 |
imports: List[String], |
65392
f365f61f2081
uniform import_name, with treatment of global and qualified theories;
wenzelm
parents:
65391
diff
changeset
|
202 |
theories: List[(Options, List[String])], |
65374 | 203 |
global_theories: List[String], |
62631 | 204 |
files: List[Path], |
205 |
document_files: List[(Path, Path)], |
|
206 |
meta_digest: SHA1.Digest) |
|
207 |
{ |
|
208 |
def timeout: Time = Time.seconds(options.real("timeout") * options.real("timeout_scale")) |
|
209 |
} |
|
210 |
||
65419 | 211 |
object Selection |
212 |
{ |
|
65422 | 213 |
val empty: Selection = Selection() |
65419 | 214 |
} |
215 |
||
216 |
sealed case class Selection( |
|
217 |
requirements: Boolean = false, |
|
218 |
all_sessions: Boolean = false, |
|
219 |
exclude_session_groups: List[String] = Nil, |
|
220 |
exclude_sessions: List[String] = Nil, |
|
221 |
session_groups: List[String] = Nil, |
|
222 |
sessions: List[String] = Nil) |
|
223 |
{ |
|
65422 | 224 |
def + (other: Selection): Selection = |
225 |
Selection( |
|
226 |
requirements = requirements || other.requirements, |
|
227 |
all_sessions = all_sessions || other.all_sessions, |
|
228 |
exclude_session_groups = exclude_session_groups ::: other.exclude_session_groups, |
|
229 |
exclude_sessions = exclude_sessions ::: other.exclude_sessions, |
|
230 |
session_groups = session_groups ::: other.session_groups, |
|
231 |
sessions = sessions ::: other.sessions) |
|
232 |
||
65419 | 233 |
def apply(graph: Graph[String, Info]): (List[String], Graph[String, Info]) = |
234 |
{ |
|
235 |
val bad_sessions = |
|
236 |
SortedSet((exclude_sessions ::: sessions).filterNot(graph.defined(_)): _*).toList |
|
237 |
if (bad_sessions.nonEmpty) error("Undefined session(s): " + commas_quote(bad_sessions)) |
|
238 |
||
239 |
val excluded = |
|
240 |
{ |
|
241 |
val exclude_group = exclude_session_groups.toSet |
|
242 |
val exclude_group_sessions = |
|
243 |
(for { |
|
244 |
(name, (info, _)) <- graph.iterator |
|
245 |
if graph.get_node(name).groups.exists(exclude_group) |
|
246 |
} yield name).toList |
|
247 |
graph.all_succs(exclude_group_sessions ::: exclude_sessions).toSet |
|
248 |
} |
|
249 |
||
250 |
val pre_selected = |
|
251 |
{ |
|
252 |
if (all_sessions) graph.keys |
|
253 |
else { |
|
254 |
val select_group = session_groups.toSet |
|
255 |
val select = sessions.toSet |
|
256 |
(for { |
|
257 |
(name, (info, _)) <- graph.iterator |
|
258 |
if info.select || select(name) || graph.get_node(name).groups.exists(select_group) |
|
259 |
} yield name).toList |
|
260 |
} |
|
261 |
}.filterNot(excluded) |
|
262 |
||
263 |
val selected = |
|
264 |
if (requirements) (graph.all_preds(pre_selected).toSet -- pre_selected).toList |
|
265 |
else pre_selected |
|
266 |
||
267 |
(selected, graph.restrict(graph.all_preds(selected).toSet)) |
|
268 |
} |
|
269 |
} |
|
270 |
||
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
271 |
def make(infos: Traversable[(String, Info)]): T = |
62631 | 272 |
{ |
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
273 |
def add_edges(graph: Graph[String, Info], kind: String, edges: Info => Traversable[String]) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
274 |
: Graph[String, Info] = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
275 |
{ |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
276 |
def add_edge(pos: Position.T, name: String, g: Graph[String, Info], parent: String) = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
277 |
{ |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
278 |
if (!g.defined(parent)) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
279 |
error("Bad " + kind + " session " + quote(parent) + " for " + |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
280 |
quote(name) + Position.here(pos)) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
281 |
|
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
282 |
try { g.add_edge_acyclic(parent, name) } |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
283 |
catch { |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
284 |
case exn: Graph.Cycles[_] => |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
285 |
error(cat_lines(exn.cycles.map(cycle => |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
286 |
"Cyclic session dependency of " + |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
287 |
cycle.map(c => quote(c.toString)).mkString(" via "))) + Position.here(pos)) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
288 |
} |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
289 |
} |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
290 |
(graph /: graph.iterator) { |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
291 |
case (g, (name, (info, _))) => (g /: edges(info))(add_edge(info.pos, name, _, _)) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
292 |
} |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
293 |
} |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
294 |
|
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
295 |
val graph0 = |
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
296 |
(Graph.string[Info] /: infos) { |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
297 |
case (graph, (name, info)) => |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
298 |
if (graph.defined(name)) |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
299 |
error("Duplicate session " + quote(name) + Position.here(info.pos) + |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
300 |
Position.here(graph.get_node(name).pos)) |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
301 |
else graph.new_node(name, info) |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
302 |
} |
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
303 |
val graph1 = add_edges(graph0, "parent", _.parent) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
304 |
val graph2 = add_edges(graph1, "imports", _.imports) |
62631 | 305 |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
306 |
new T(graph1, graph2) |
62631 | 307 |
} |
308 |
||
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
309 |
final class T private[Sessions]( |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
310 |
val build_graph: Graph[String, Info], |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
311 |
val imports_graph: Graph[String, Info]) |
62631 | 312 |
{ |
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
313 |
def apply(name: String): Info = imports_graph.get_node(name) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
314 |
def get(name: String): Option[Info] = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
315 |
if (imports_graph.defined(name)) Some(imports_graph.get_node(name)) else None |
62631 | 316 |
|
65372
b722ee40c26c
refer to global_theories from all sessions, before selection;
wenzelm
parents:
65371
diff
changeset
|
317 |
def global_theories: Set[String] = |
65424 | 318 |
(Set.empty[String] /: |
319 |
(for { |
|
320 |
(_, (info, _)) <- imports_graph.iterator |
|
321 |
thy <- info.global_theories.iterator } |
|
322 |
yield (thy, info.pos)))( |
|
323 |
{ case (set, (thy, pos)) => |
|
324 |
if (set.contains(thy)) |
|
325 |
error("Duplicate declaration of global theory " + quote(thy) + Position.here(pos)) |
|
326 |
else set + thy |
|
327 |
}) |
|
65372
b722ee40c26c
refer to global_theories from all sessions, before selection;
wenzelm
parents:
65371
diff
changeset
|
328 |
|
65419 | 329 |
def selection(select: Selection): (List[String], T) = |
62631 | 330 |
{ |
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
331 |
val (_, build_graph1) = select(build_graph) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
332 |
val (selected, imports_graph1) = select(imports_graph) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
333 |
(selected, new T(build_graph1, imports_graph1)) |
62631 | 334 |
} |
335 |
||
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
336 |
def build_ancestors(name: String): List[String] = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
337 |
build_graph.all_preds(List(name)).tail.reverse |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
338 |
|
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
339 |
def build_descendants(names: List[String]): List[String] = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
340 |
build_graph.all_succs(names) |
62631 | 341 |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
342 |
def build_topological_order: List[(String, Info)] = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
343 |
build_graph.topological_order.map(name => (name, apply(name))) |
62631 | 344 |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
345 |
def imports_topological_order: List[(String, Info)] = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
346 |
imports_graph.topological_order.map(name => (name, apply(name))) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
347 |
|
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
348 |
override def toString: String = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
349 |
imports_graph.keys_iterator.mkString("Sessions.T(", ", ", ")") |
62631 | 350 |
} |
351 |
||
352 |
||
353 |
/* parser */ |
|
354 |
||
62864 | 355 |
val ROOT = Path.explode("ROOT") |
356 |
val ROOTS = Path.explode("ROOTS") |
|
357 |
||
62631 | 358 |
private val CHAPTER = "chapter" |
359 |
private val SESSION = "session" |
|
360 |
private val IN = "in" |
|
361 |
private val DESCRIPTION = "description" |
|
362 |
private val OPTIONS = "options" |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
363 |
private val SESSIONS = "sessions" |
62631 | 364 |
private val THEORIES = "theories" |
65374 | 365 |
private val GLOBAL = "global" |
62631 | 366 |
private val FILES = "files" |
367 |
private val DOCUMENT_FILES = "document_files" |
|
368 |
||
369 |
lazy val root_syntax = |
|
65374 | 370 |
Outer_Syntax.init() + "(" + ")" + "+" + "," + "=" + "[" + "]" + GLOBAL + IN + |
63443 | 371 |
(CHAPTER, Keyword.THY_DECL) + |
372 |
(SESSION, Keyword.THY_DECL) + |
|
373 |
(DESCRIPTION, Keyword.QUASI_COMMAND) + |
|
374 |
(OPTIONS, Keyword.QUASI_COMMAND) + |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
375 |
(SESSIONS, Keyword.QUASI_COMMAND) + |
63443 | 376 |
(THEORIES, Keyword.QUASI_COMMAND) + |
377 |
(FILES, Keyword.QUASI_COMMAND) + |
|
378 |
(DOCUMENT_FILES, Keyword.QUASI_COMMAND) |
|
62631 | 379 |
|
62968 | 380 |
private object Parser extends Parse.Parser with Options.Parser |
62631 | 381 |
{ |
382 |
private abstract class Entry |
|
383 |
private sealed case class Chapter(name: String) extends Entry |
|
384 |
private sealed case class Session_Entry( |
|
385 |
pos: Position.T, |
|
386 |
name: String, |
|
387 |
groups: List[String], |
|
388 |
path: String, |
|
389 |
parent: Option[String], |
|
390 |
description: String, |
|
391 |
options: List[Options.Spec], |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
392 |
imports: List[String], |
65374 | 393 |
theories: List[(List[Options.Spec], List[(String, Boolean)])], |
62631 | 394 |
files: List[String], |
395 |
document_files: List[(String, String)]) extends Entry |
|
396 |
||
397 |
private val chapter: Parser[Chapter] = |
|
398 |
{ |
|
399 |
val chapter_name = atom("chapter name", _.is_name) |
|
400 |
||
401 |
command(CHAPTER) ~! chapter_name ^^ { case _ ~ a => Chapter(a) } |
|
402 |
} |
|
403 |
||
404 |
private val session_entry: Parser[Session_Entry] = |
|
405 |
{ |
|
406 |
val session_name = atom("session name", _.is_name) |
|
407 |
||
408 |
val option = |
|
62968 | 409 |
option_name ~ opt($$$("=") ~! option_value ^^ |
410 |
{ case _ ~ x => x }) ^^ { case x ~ y => (x, y) } |
|
62631 | 411 |
val options = $$$("[") ~> rep1sep(option, $$$(",")) <~ $$$("]") |
412 |
||
65374 | 413 |
val global = |
414 |
($$$("(") ~! $$$(GLOBAL) ~ $$$(")")) ^^ { case _ => true } | success(false) |
|
415 |
||
416 |
val theory_entry = |
|
417 |
theory_name ~ global ^^ { case x ~ y => (x, y) } |
|
418 |
||
62631 | 419 |
val theories = |
65374 | 420 |
$$$(THEORIES) ~! |
421 |
((options | success(Nil)) ~ rep(theory_entry)) ^^ |
|
422 |
{ case _ ~ (x ~ y) => (x, y) } |
|
62631 | 423 |
|
424 |
val document_files = |
|
425 |
$$$(DOCUMENT_FILES) ~! |
|
426 |
(($$$("(") ~! ($$$(IN) ~! (path ~ $$$(")"))) ^^ |
|
427 |
{ case _ ~ (_ ~ (x ~ _)) => x } | success("document")) ~ |
|
428 |
rep1(path)) ^^ { case _ ~ (x ~ y) => y.map((x, _)) } |
|
429 |
||
430 |
command(SESSION) ~! |
|
431 |
(position(session_name) ~ |
|
432 |
(($$$("(") ~! (rep1(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | success(Nil)) ~ |
|
433 |
(($$$(IN) ~! path ^^ { case _ ~ x => x }) | success(".")) ~ |
|
434 |
($$$("=") ~! |
|
435 |
(opt(session_name ~! $$$("+") ^^ { case x ~ _ => x }) ~ |
|
436 |
(($$$(DESCRIPTION) ~! text ^^ { case _ ~ x => x }) | success("")) ~ |
|
437 |
(($$$(OPTIONS) ~! options ^^ { case _ ~ x => x }) | success(Nil)) ~ |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
438 |
(($$$(SESSIONS) ~! rep(session_name) ^^ { case _ ~ x => x }) | success(Nil)) ~ |
62631 | 439 |
rep1(theories) ~ |
440 |
(($$$(FILES) ~! rep1(path) ^^ { case _ ~ x => x }) | success(Nil)) ~ |
|
441 |
(rep(document_files) ^^ (x => x.flatten))))) ^^ |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
442 |
{ case _ ~ ((a, pos) ~ b ~ c ~ (_ ~ (d ~ e ~ f ~ g ~ h ~ i ~ j))) => |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
443 |
Session_Entry(pos, a, b, c, d, e, f, g, h, i, j) } |
62631 | 444 |
} |
445 |
||
446 |
def parse(options: Options, select: Boolean, dir: Path): List[(String, Info)] = |
|
447 |
{ |
|
448 |
def make_info(entry_chapter: String, entry: Session_Entry): (String, Info) = |
|
449 |
{ |
|
450 |
try { |
|
451 |
val name = entry.name |
|
452 |
||
453 |
if (name == "") error("Bad session name") |
|
65360 | 454 |
if (is_pure(name) && entry.parent.isDefined) error("Illegal parent session") |
455 |
if (!is_pure(name) && !entry.parent.isDefined) error("Missing parent session") |
|
62631 | 456 |
|
457 |
val session_options = options ++ entry.options |
|
458 |
||
459 |
val theories = |
|
65392
f365f61f2081
uniform import_name, with treatment of global and qualified theories;
wenzelm
parents:
65391
diff
changeset
|
460 |
entry.theories.map({ case (opts, thys) => (session_options ++ opts, thys.map(_._1)) }) |
65374 | 461 |
|
462 |
val global_theories = |
|
463 |
for { (_, thys) <- entry.theories; (thy, global) <- thys if global } |
|
464 |
yield { |
|
465 |
val thy_name = Path.explode(thy).expand.base.implode |
|
466 |
if (Long_Name.is_qualified(thy_name)) |
|
467 |
error("Bad qualified name for global theory " + quote(thy_name)) |
|
468 |
else thy_name |
|
469 |
} |
|
470 |
||
62631 | 471 |
val files = entry.files.map(Path.explode(_)) |
472 |
val document_files = |
|
473 |
entry.document_files.map({ case (s1, s2) => (Path.explode(s1), Path.explode(s2)) }) |
|
474 |
||
475 |
val meta_digest = |
|
476 |
SHA1.digest((entry_chapter, name, entry.parent, entry.options, |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
477 |
entry.imports, entry.theories, entry.files, entry.document_files).toString) |
62631 | 478 |
|
479 |
val info = |
|
480 |
Info(entry_chapter, select, entry.pos, entry.groups, dir + Path.explode(entry.path), |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
481 |
entry.parent, entry.description, session_options, entry.imports, theories, |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
482 |
global_theories, files, document_files, meta_digest) |
62631 | 483 |
|
484 |
(name, info) |
|
485 |
} |
|
486 |
catch { |
|
487 |
case ERROR(msg) => |
|
488 |
error(msg + "\nThe error(s) above occurred in session entry " + |
|
489 |
quote(entry.name) + Position.here(entry.pos)) |
|
490 |
} |
|
491 |
} |
|
492 |
||
493 |
val root = dir + ROOT |
|
494 |
if (root.is_file) { |
|
495 |
val toks = Token.explode(root_syntax.keywords, File.read(root)) |
|
496 |
val start = Token.Pos.file(root.implode) |
|
497 |
||
498 |
parse_all(rep(chapter | session_entry), Token.reader(toks, start)) match { |
|
499 |
case Success(result, _) => |
|
500 |
var entry_chapter = "Unsorted" |
|
501 |
val infos = new mutable.ListBuffer[(String, Info)] |
|
502 |
result.foreach { |
|
503 |
case Chapter(name) => entry_chapter = name |
|
504 |
case entry: Session_Entry => infos += make_info(entry_chapter, entry) |
|
505 |
} |
|
506 |
infos.toList |
|
507 |
case bad => error(bad.toString) |
|
508 |
} |
|
509 |
} |
|
510 |
else Nil |
|
511 |
} |
|
512 |
} |
|
513 |
||
514 |
||
62635 | 515 |
/* load sessions from certain directories */ |
62631 | 516 |
|
517 |
private def is_session_dir(dir: Path): Boolean = |
|
518 |
(dir + ROOT).is_file || (dir + ROOTS).is_file |
|
519 |
||
520 |
private def check_session_dir(dir: Path): Path = |
|
521 |
if (is_session_dir(dir)) dir |
|
522 |
else error("Bad session root directory: " + dir.toString) |
|
523 |
||
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
524 |
def load(options: Options, dirs: List[Path] = Nil, select_dirs: List[Path] = Nil): T = |
62631 | 525 |
{ |
62635 | 526 |
def load_dir(select: Boolean, dir: Path): List[(String, Info)] = |
527 |
load_root(select, dir) ::: load_roots(select, dir) |
|
62631 | 528 |
|
62635 | 529 |
def load_root(select: Boolean, dir: Path): List[(String, Info)] = |
62631 | 530 |
Parser.parse(options, select, dir) |
531 |
||
62635 | 532 |
def load_roots(select: Boolean, dir: Path): List[(String, Info)] = |
62631 | 533 |
{ |
534 |
val roots = dir + ROOTS |
|
535 |
if (roots.is_file) { |
|
536 |
for { |
|
537 |
line <- split_lines(File.read(roots)) |
|
538 |
if !(line == "" || line.startsWith("#")) |
|
539 |
dir1 = |
|
540 |
try { check_session_dir(dir + Path.explode(line)) } |
|
541 |
catch { |
|
542 |
case ERROR(msg) => |
|
543 |
error(msg + "\nThe error(s) above occurred in session catalog " + roots.toString) |
|
544 |
} |
|
62635 | 545 |
info <- load_dir(select, dir1) |
62631 | 546 |
} yield info |
547 |
} |
|
548 |
else Nil |
|
549 |
} |
|
550 |
||
551 |
val default_dirs = Isabelle_System.components().filter(is_session_dir(_)) |
|
552 |
dirs.foreach(check_session_dir(_)) |
|
553 |
select_dirs.foreach(check_session_dir(_)) |
|
554 |
||
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
555 |
make( |
62631 | 556 |
for { |
557 |
(select, dir) <- (default_dirs ::: dirs).map((false, _)) ::: select_dirs.map((true, _)) |
|
62635 | 558 |
info <- load_dir(select, dir) |
62631 | 559 |
} yield info) |
560 |
} |
|
62632 | 561 |
|
562 |
||
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
563 |
|
62704
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
564 |
/** heap file with SHA1 digest **/ |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
565 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
566 |
private val sha1_prefix = "SHA1:" |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
567 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
568 |
def read_heap_digest(heap: Path): Option[String] = |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
569 |
{ |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
570 |
if (heap.is_file) { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
571 |
val file = FileChannel.open(heap.file.toPath, StandardOpenOption.READ) |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
572 |
try { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
573 |
val len = file.size |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
574 |
val n = sha1_prefix.length + SHA1.digest_length |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
575 |
if (len >= n) { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
576 |
file.position(len - n) |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
577 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
578 |
val buf = ByteBuffer.allocate(n) |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
579 |
var i = 0 |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
580 |
var m = 0 |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
581 |
do { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
582 |
m = file.read(buf) |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
583 |
if (m != -1) i += m |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
584 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
585 |
while (m != -1 && n > i) |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
586 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
587 |
if (i == n) { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
588 |
val prefix = new String(buf.array(), 0, sha1_prefix.length, UTF8.charset) |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
589 |
val s = new String(buf.array(), sha1_prefix.length, SHA1.digest_length, UTF8.charset) |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
590 |
if (prefix == sha1_prefix) Some(s) else None |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
591 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
592 |
else None |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
593 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
594 |
else None |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
595 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
596 |
finally { file.close } |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
597 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
598 |
else None |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
599 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
600 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
601 |
def write_heap_digest(heap: Path): String = |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
602 |
read_heap_digest(heap) match { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
603 |
case None => |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
604 |
val s = SHA1.digest(heap).rep |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
605 |
File.append(heap, sha1_prefix + s) |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
606 |
s |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
607 |
case Some(s) => s |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
608 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
609 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
610 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
611 |
|
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
612 |
/** persistent store **/ |
62632 | 613 |
|
65296 | 614 |
object Session_Info |
615 |
{ |
|
65326 | 616 |
val session_name = SQL.Column.string("session_name", primary_key = true) |
617 |
||
65296 | 618 |
// Build_Log.Session_Info |
619 |
val session_timing = SQL.Column.bytes("session_timing") |
|
620 |
val command_timings = SQL.Column.bytes("command_timings") |
|
621 |
val ml_statistics = SQL.Column.bytes("ml_statistics") |
|
622 |
val task_statistics = SQL.Column.bytes("task_statistics") |
|
623 |
val build_log_columns = |
|
624 |
List(session_name, session_timing, command_timings, ml_statistics, task_statistics) |
|
625 |
||
626 |
// Build.Session_Info |
|
627 |
val sources = SQL.Column.string("sources") |
|
628 |
val input_heaps = SQL.Column.string("input_heaps") |
|
629 |
val output_heap = SQL.Column.string("output_heap") |
|
630 |
val return_code = SQL.Column.int("return_code") |
|
631 |
val build_columns = List(sources, input_heaps, output_heap, return_code) |
|
632 |
||
633 |
val table = SQL.Table("isabelle_session_info", build_log_columns ::: build_columns) |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
634 |
|
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
635 |
def where_session_name(name: String): String = |
65322 | 636 |
"WHERE " + session_name.sql_name + " = " + SQL.quote_string(name) |
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
637 |
|
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
638 |
def select_statement(db: SQL.Database, name: String, columns: List[SQL.Column]) |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
639 |
: PreparedStatement = |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
640 |
db.select_statement(table, columns, where_session_name(name)) |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
641 |
|
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
642 |
def delete_statement(db: SQL.Database, name: String): PreparedStatement = |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
643 |
db.delete_statement(table, where_session_name(name)) |
65296 | 644 |
} |
645 |
||
62632 | 646 |
def store(system_mode: Boolean = false): Store = new Store(system_mode) |
647 |
||
63996 | 648 |
class Store private[Sessions](system_mode: Boolean) |
62632 | 649 |
{ |
65278 | 650 |
/* file names */ |
651 |
||
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
652 |
def database(name: String): Path = Path.basic("log") + Path.basic(name).ext("db") |
65278 | 653 |
def log(name: String): Path = Path.basic("log") + Path.basic(name) |
654 |
def log_gz(name: String): Path = log(name).ext("gz") |
|
655 |
||
656 |
||
65286 | 657 |
/* SQL database content */ |
65283 | 658 |
|
659 |
val xml_cache: XML.Cache = new XML.Cache() |
|
660 |
||
661 |
def encode_properties(ps: Properties.T): Bytes = |
|
662 |
Bytes(YXML.string_of_body(XML.Encode.properties(ps))) |
|
663 |
||
664 |
def decode_properties(bs: Bytes): Properties.T = |
|
665 |
xml_cache.props(XML.Decode.properties(YXML.parse_body(bs.text))) |
|
666 |
||
667 |
def compress_properties(ps: List[Properties.T], options: XZ.Options = XZ.options()): Bytes = |
|
668 |
{ |
|
669 |
if (ps.isEmpty) Bytes.empty |
|
670 |
else Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.properties)(ps))).compress(options) |
|
671 |
} |
|
672 |
||
673 |
def uncompress_properties(bs: Bytes): List[Properties.T] = |
|
674 |
{ |
|
675 |
if (bs.isEmpty) Nil |
|
676 |
else |
|
677 |
XML.Decode.list(XML.Decode.properties)(YXML.parse_body(bs.uncompress().text)). |
|
678 |
map(xml_cache.props(_)) |
|
679 |
} |
|
680 |
||
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
681 |
def read_bytes(db: SQL.Database, name: String, column: SQL.Column): Bytes = |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
682 |
using(Session_Info.select_statement(db, name, List(column)))(stmt => |
65291
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
683 |
{ |
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
684 |
val rs = stmt.executeQuery |
65324 | 685 |
if (!rs.next) Bytes.empty else db.bytes(rs, column) |
65291
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
686 |
}) |
65285 | 687 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
688 |
def read_properties(db: SQL.Database, name: String, column: SQL.Column): List[Properties.T] = |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
689 |
uncompress_properties(read_bytes(db, name, column)) |
65286 | 690 |
|
65283 | 691 |
|
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
692 |
/* output */ |
62632 | 693 |
|
694 |
val browser_info: Path = |
|
695 |
if (system_mode) Path.explode("~~/browser_info") |
|
696 |
else Path.explode("$ISABELLE_BROWSER_INFO") |
|
697 |
||
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
698 |
val output_dir: Path = |
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
699 |
if (system_mode) Path.explode("~~/heaps/$ML_IDENTIFIER") |
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
700 |
else Path.explode("$ISABELLE_OUTPUT") |
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
701 |
|
65298 | 702 |
override def toString: String = "Store(output_dir = " + output_dir.expand + ")" |
703 |
||
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
704 |
def prepare_output() { Isabelle_System.mkdirs(output_dir + Path.basic("log")) } |
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
705 |
|
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
706 |
|
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
707 |
/* input */ |
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
708 |
|
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
709 |
private val input_dirs = |
62632 | 710 |
if (system_mode) List(output_dir) |
62633 | 711 |
else { |
712 |
val ml_ident = Path.explode("$ML_IDENTIFIER").expand |
|
713 |
output_dir :: Path.split(Isabelle_System.getenv_strict("ISABELLE_PATH")).map(_ + ml_ident) |
|
714 |
} |
|
62632 | 715 |
|
65291
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
716 |
def find_database_heap(name: String): Option[(Path, Option[String])] = |
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
717 |
input_dirs.find(dir => (dir + database(name)).is_file).map(dir => |
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
718 |
(dir + database(name), read_heap_digest(dir + Path.basic(name)))) |
62632 | 719 |
|
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
720 |
def find_database(name: String): Option[Path] = |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
721 |
input_dirs.map(_ + database(name)).find(_.is_file) |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
722 |
|
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
723 |
def heap(name: String): Path = |
65288 | 724 |
input_dirs.map(_ + Path.basic(name)).find(_.is_file) getOrElse |
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
725 |
error("Unknown logic " + quote(name) + " -- no heap file found in:\n" + |
62769 | 726 |
cat_lines(input_dirs.map(dir => " " + dir.expand.implode))) |
65287 | 727 |
|
728 |
||
65296 | 729 |
/* session info */ |
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
730 |
|
65296 | 731 |
def write_session_info( |
65318
342efc382558
eliminated somewhat redundant inlined name (despite a7aa17a1f721);
wenzelm
parents:
65298
diff
changeset
|
732 |
db: SQL.Database, |
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
733 |
name: String, |
65318
342efc382558
eliminated somewhat redundant inlined name (despite a7aa17a1f721);
wenzelm
parents:
65298
diff
changeset
|
734 |
build_log: Build_Log.Session_Info, |
342efc382558
eliminated somewhat redundant inlined name (despite a7aa17a1f721);
wenzelm
parents:
65298
diff
changeset
|
735 |
build: Build.Session_Info) |
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
736 |
{ |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
737 |
db.transaction { |
65296 | 738 |
db.create_table(Session_Info.table) |
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
739 |
using(Session_Info.delete_statement(db, name))(_.execute) |
65296 | 740 |
using(db.insert_statement(Session_Info.table))(stmt => |
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
741 |
{ |
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
742 |
db.set_string(stmt, 1, name) |
65296 | 743 |
db.set_bytes(stmt, 2, encode_properties(build_log.session_timing)) |
744 |
db.set_bytes(stmt, 3, compress_properties(build_log.command_timings)) |
|
745 |
db.set_bytes(stmt, 4, compress_properties(build_log.ml_statistics)) |
|
746 |
db.set_bytes(stmt, 5, compress_properties(build_log.task_statistics)) |
|
65284 | 747 |
db.set_string(stmt, 6, cat_lines(build.sources)) |
748 |
db.set_string(stmt, 7, cat_lines(build.input_heaps)) |
|
65291
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
749 |
db.set_string(stmt, 8, build.output_heap getOrElse "") |
65283 | 750 |
db.set_int(stmt, 9, build.return_code) |
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
751 |
stmt.execute() |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
752 |
}) |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
753 |
} |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
754 |
} |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
755 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
756 |
def read_session_timing(db: SQL.Database, name: String): Properties.T = |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
757 |
decode_properties(read_bytes(db, name, Session_Info.session_timing)) |
65286 | 758 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
759 |
def read_command_timings(db: SQL.Database, name: String): List[Properties.T] = |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
760 |
read_properties(db, name, Session_Info.command_timings) |
65286 | 761 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
762 |
def read_ml_statistics(db: SQL.Database, name: String): List[Properties.T] = |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
763 |
read_properties(db, name, Session_Info.ml_statistics) |
65286 | 764 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
765 |
def read_task_statistics(db: SQL.Database, name: String): List[Properties.T] = |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
766 |
read_properties(db, name, Session_Info.task_statistics) |
65286 | 767 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
768 |
def read_build_log(db: SQL.Database, name: String, |
65291
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
769 |
command_timings: Boolean = false, |
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
770 |
ml_statistics: Boolean = false, |
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
771 |
task_statistics: Boolean = false): Build_Log.Session_Info = |
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
772 |
{ |
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
773 |
Build_Log.Session_Info( |
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
774 |
session_timing = read_session_timing(db, name), |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
775 |
command_timings = if (command_timings) read_command_timings(db, name) else Nil, |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
776 |
ml_statistics = if (ml_statistics) read_ml_statistics(db, name) else Nil, |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
777 |
task_statistics = if (task_statistics) read_task_statistics(db, name) else Nil) |
65291
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
778 |
} |
65285 | 779 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
780 |
def read_build(db: SQL.Database, name: String): Option[Build.Session_Info] = |
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
781 |
using(Session_Info.select_statement(db, name, Session_Info.build_columns))(stmt => |
65285 | 782 |
{ |
783 |
val rs = stmt.executeQuery |
|
784 |
if (!rs.next) None |
|
785 |
else { |
|
786 |
Some( |
|
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
787 |
Build.Session_Info( |
65324 | 788 |
split_lines(db.string(rs, Session_Info.sources)), |
789 |
split_lines(db.string(rs, Session_Info.input_heaps)), |
|
790 |
db.string(rs, Session_Info.output_heap) match { case "" => None case s => Some(s) }, |
|
791 |
db.int(rs, Session_Info.return_code))) |
|
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
792 |
} |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
793 |
}) |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
794 |
} |
62631 | 795 |
} |