author | wenzelm |
Sun, 01 Oct 2017 13:07:31 +0200 | |
changeset 66737 | 2edc0c42c883 |
parent 66736 | 148891036469 |
child 66742 | b3422f78270e |
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 |
||
65461
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
9 |
import java.io.{File => JFile} |
62704
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
10 |
import java.nio.ByteBuffer |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
11 |
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
|
12 |
import java.nio.file.StandardOpenOption |
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 |
|
65445
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
22 |
val DRAFT = "Draft" |
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
23 |
|
65360 | 24 |
def is_pure(name: String): Boolean = name == Thy_Header.PURE |
64856 | 25 |
|
65495 | 26 |
object Known |
64856 | 27 |
{ |
65495 | 28 |
val empty: Known = Known() |
65360 | 29 |
|
66701 | 30 |
def make(local_dir: Path, bases: List[Base], |
31 |
theories: List[Document.Node.Name] = Nil, |
|
32 |
loaded_files: List[(String, List[Path])] = Nil): Known = |
|
65427 | 33 |
{ |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
34 |
def bases_iterator(local: Boolean) = |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
35 |
for { |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
36 |
base <- bases.iterator |
65499 | 37 |
(_, name) <- (if (local) base.known.theories_local else base.known.theories).iterator |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
38 |
} yield name |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
39 |
|
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
40 |
def local_theories_iterator = |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
41 |
{ |
65833 | 42 |
val local_path = local_dir.canonical_file.toPath |
66716 | 43 |
theories.iterator.filter(name => name.path.canonical_file.toPath.startsWith(local_path)) |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
44 |
} |
65427 | 45 |
|
65461
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
46 |
val known_theories = |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
47 |
(Map.empty[String, Document.Node.Name] /: (bases_iterator(false) ++ theories.iterator))({ |
65461
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
48 |
case (known, name) => |
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
49 |
known.get(name.theory) match { |
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
50 |
case Some(name1) if name != name1 => |
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
51 |
error("Duplicate theory " + quote(name.node) + " vs. " + quote(name1.node)) |
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
52 |
case _ => known + (name.theory -> name) |
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
53 |
} |
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
54 |
}) |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
55 |
val known_theories_local = |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
56 |
(Map.empty[String, Document.Node.Name] /: |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
57 |
(bases_iterator(true) ++ local_theories_iterator))({ |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
58 |
case (known, name) => known + (name.theory -> name) |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
59 |
}) |
65461
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
60 |
val known_files = |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
61 |
(Map.empty[JFile, List[Document.Node.Name]] /: |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
62 |
(bases_iterator(true) ++ bases_iterator(false) ++ theories.iterator))({ |
65461
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
63 |
case (known, name) => |
66716 | 64 |
val file = name.path.canonical_file |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
65 |
val theories1 = known.getOrElse(file, Nil) |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
66 |
if (theories1.exists(name1 => name.node == name1.node && name.theory == name1.theory)) |
65461
b6c2e30dc018
support for known theories files (according to multiple uses);
wenzelm
parents:
65457
diff
changeset
|
67 |
known |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
68 |
else known + (file -> (name :: theories1)) |
65427 | 69 |
}) |
66701 | 70 |
|
71 |
val known_loaded_files = |
|
72 |
(loaded_files.toMap /: bases.map(base => base.known.loaded_files))(_ ++ _) |
|
73 |
||
65495 | 74 |
Known(known_theories, known_theories_local, |
66701 | 75 |
known_files.iterator.map(p => (p._1, p._2.reverse)).toMap, |
76 |
known_loaded_files) |
|
65427 | 77 |
} |
64856 | 78 |
} |
79 |
||
65495 | 80 |
sealed case class Known( |
65499 | 81 |
theories: Map[String, Document.Node.Name] = Map.empty, |
82 |
theories_local: Map[String, Document.Node.Name] = Map.empty, |
|
66701 | 83 |
files: Map[JFile, List[Document.Node.Name]] = Map.empty, |
84 |
loaded_files: Map[String, List[Path]] = Map.empty) |
|
65355 | 85 |
{ |
65495 | 86 |
def platform_path: Known = |
65499 | 87 |
copy(theories = for ((a, b) <- theories) yield (a, b.map(File.platform_path(_))), |
88 |
theories_local = for ((a, b) <- theories_local) yield (a, b.map(File.platform_path(_))), |
|
89 |
files = for ((a, b) <- files) yield (a, b.map(c => c.map(File.platform_path(_))))) |
|
65524 | 90 |
|
66668
6019cfb8256c
proper standard_path to revert platform_path in JEdit_Sessions.session_base;
wenzelm
parents:
66604
diff
changeset
|
91 |
def standard_path: Known = |
6019cfb8256c
proper standard_path to revert platform_path in JEdit_Sessions.session_base;
wenzelm
parents:
66604
diff
changeset
|
92 |
copy(theories = for ((a, b) <- theories) yield (a, b.map(File.standard_path(_))), |
6019cfb8256c
proper standard_path to revert platform_path in JEdit_Sessions.session_base;
wenzelm
parents:
66604
diff
changeset
|
93 |
theories_local = for ((a, b) <- theories_local) yield (a, b.map(File.standard_path(_))), |
6019cfb8256c
proper standard_path to revert platform_path in JEdit_Sessions.session_base;
wenzelm
parents:
66604
diff
changeset
|
94 |
files = for ((a, b) <- files) yield (a, b.map(c => c.map(File.standard_path(_))))) |
6019cfb8256c
proper standard_path to revert platform_path in JEdit_Sessions.session_base;
wenzelm
parents:
66604
diff
changeset
|
95 |
|
66195 | 96 |
def get_file(file: JFile, bootstrap: Boolean = false): Option[Document.Node.Name] = |
97 |
{ |
|
66234 | 98 |
val res = files.getOrElse(File.canonical(file), Nil).headOption |
66195 | 99 |
if (bootstrap) res.map(_.map_theory(Thy_Header.bootstrap_name(_))) else res |
100 |
} |
|
65495 | 101 |
} |
102 |
||
103 |
object Base |
|
104 |
{ |
|
105 |
def bootstrap(global_theories: Map[String, String]): Base = |
|
106 |
Base( |
|
107 |
global_theories = global_theories, |
|
66720 | 108 |
overall_syntax = Thy_Header.bootstrap_syntax) |
65495 | 109 |
} |
110 |
||
111 |
sealed case class Base( |
|
66571 | 112 |
pos: Position.T = Position.none, |
65495 | 113 |
global_theories: Map[String, String] = Map.empty, |
66717
67dbf5cdc056
more informative loaded_theories: dependencies and syntax;
wenzelm
parents:
66716
diff
changeset
|
114 |
loaded_theories: Graph[String, Outer_Syntax] = Graph.string, |
65495 | 115 |
known: Known = Known.empty, |
66720 | 116 |
overall_syntax: Outer_Syntax = Outer_Syntax.empty, |
65495 | 117 |
sources: List[(Path, SHA1.Digest)] = Nil, |
66571 | 118 |
session_graph: Graph_Display.Graph = Graph_Display.empty_graph, |
66694
41177b124067
tuned signature -- more readable output as Scala value;
wenzelm
parents:
66668
diff
changeset
|
119 |
errors: List[String] = Nil, |
41177b124067
tuned signature -- more readable output as Scala value;
wenzelm
parents:
66668
diff
changeset
|
120 |
imports: Option[Base] = None) |
65495 | 121 |
{ |
122 |
def platform_path: Base = copy(known = known.platform_path) |
|
66668
6019cfb8256c
proper standard_path to revert platform_path in JEdit_Sessions.session_base;
wenzelm
parents:
66604
diff
changeset
|
123 |
def standard_path: Base = copy(known = known.standard_path) |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
124 |
|
66717
67dbf5cdc056
more informative loaded_theories: dependencies and syntax;
wenzelm
parents:
66716
diff
changeset
|
125 |
def loaded_theory(name: String): Boolean = loaded_theories.defined(name) |
66712 | 126 |
def loaded_theory(name: Document.Node.Name): Boolean = loaded_theory(name.theory) |
65432 | 127 |
|
66717
67dbf5cdc056
more informative loaded_theories: dependencies and syntax;
wenzelm
parents:
66716
diff
changeset
|
128 |
def loaded_theory_syntax(name: String): Option[Outer_Syntax] = |
67dbf5cdc056
more informative loaded_theories: dependencies and syntax;
wenzelm
parents:
66716
diff
changeset
|
129 |
if (loaded_theory(name)) Some(loaded_theories.get_node(name)) else None |
67dbf5cdc056
more informative loaded_theories: dependencies and syntax;
wenzelm
parents:
66716
diff
changeset
|
130 |
def loaded_theory_syntax(name: Document.Node.Name): Option[Outer_Syntax] = |
67dbf5cdc056
more informative loaded_theories: dependencies and syntax;
wenzelm
parents:
66716
diff
changeset
|
131 |
loaded_theory_syntax(name.theory) |
67dbf5cdc056
more informative loaded_theories: dependencies and syntax;
wenzelm
parents:
66716
diff
changeset
|
132 |
|
66721
ae38b8c0fdd9
more accurate node_syntax: avoid overall_syntax for PIDE edits;
wenzelm
parents:
66720
diff
changeset
|
133 |
def node_syntax(nodes: Document.Nodes, name: Document.Node.Name): Option[Outer_Syntax] = |
ae38b8c0fdd9
more accurate node_syntax: avoid overall_syntax for PIDE edits;
wenzelm
parents:
66720
diff
changeset
|
134 |
nodes(name).syntax orElse loaded_theory_syntax(name) |
ae38b8c0fdd9
more accurate node_syntax: avoid overall_syntax for PIDE edits;
wenzelm
parents:
66720
diff
changeset
|
135 |
|
65498 | 136 |
def known_theory(name: String): Option[Document.Node.Name] = |
65499 | 137 |
known.theories.get(name) |
65498 | 138 |
|
65432 | 139 |
def dest_known_theories: List[(String, String)] = |
65499 | 140 |
for ((theory, node_name) <- known.theories.toList) |
65432 | 141 |
yield (theory, node_name.node) |
66694
41177b124067
tuned signature -- more readable output as Scala value;
wenzelm
parents:
66668
diff
changeset
|
142 |
|
41177b124067
tuned signature -- more readable output as Scala value;
wenzelm
parents:
66668
diff
changeset
|
143 |
def get_imports: Base = imports getOrElse Base.bootstrap(global_theories) |
65355 | 144 |
} |
64856 | 145 |
|
65495 | 146 |
sealed case class Deps(session_bases: Map[String, Base], all_known: Known) |
65251 | 147 |
{ |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
148 |
def is_empty: Boolean = session_bases.isEmpty |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
149 |
def apply(name: String): Base = session_bases(name) |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
150 |
def sources(name: String): List[SHA1.Digest] = session_bases(name).sources.map(_._2) |
66571 | 151 |
|
152 |
def errors: List[String] = |
|
153 |
(for { |
|
154 |
(name, base) <- session_bases.iterator |
|
155 |
if base.errors.nonEmpty |
|
156 |
} yield cat_lines(base.errors) + |
|
157 |
"\nThe error(s) above occurred in session " + quote(name) + Position.here(base.pos) |
|
158 |
).toList |
|
159 |
||
160 |
def check_errors: Deps = |
|
161 |
errors match { |
|
162 |
case Nil => this |
|
163 |
case errs => error(cat_lines(errs)) |
|
164 |
} |
|
65251 | 165 |
} |
64856 | 166 |
|
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
167 |
def deps(sessions: T, |
65251 | 168 |
progress: Progress = No_Progress, |
169 |
inlined_files: Boolean = false, |
|
170 |
verbose: Boolean = false, |
|
171 |
list_files: Boolean = false, |
|
172 |
check_keywords: Set[String] = Set.empty, |
|
66575
191048506504
clarified signature: provide all_known information uniformly (it is subject to Sessions.T selection);
wenzelm
parents:
66573
diff
changeset
|
173 |
global_theories: Map[String, String] = Map.empty): Deps = |
65372
b722ee40c26c
refer to global_theories from all sessions, before selection;
wenzelm
parents:
65371
diff
changeset
|
174 |
{ |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
175 |
val session_bases = |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
176 |
(Map.empty[String, Base] /: sessions.imports_topological_order)({ |
65519 | 177 |
case (session_bases, info) => |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
178 |
if (progress.stopped) throw Exn.Interrupt() |
65251 | 179 |
|
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
180 |
try { |
65496 | 181 |
val parent_base: Sessions.Base = |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
182 |
info.parent match { |
65475 | 183 |
case None => Base.bootstrap(global_theories) |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
184 |
case Some(parent) => session_bases(parent) |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
185 |
} |
65496 | 186 |
val imports_base: Sessions.Base = |
187 |
parent_base.copy(known = |
|
66701 | 188 |
Known.make(info.dir, parent_base :: info.imports.map(session_bases(_)))) |
65496 | 189 |
|
65532 | 190 |
val resources = new Resources(imports_base) |
65251 | 191 |
|
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
192 |
if (verbose || list_files) { |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
193 |
val groups = |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
194 |
if (info.groups.isEmpty) "" |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
195 |
else info.groups.mkString(" (", " ", ")") |
65519 | 196 |
progress.echo("Session " + info.chapter + "/" + info.name + groups) |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
197 |
} |
65251 | 198 |
|
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
199 |
val thy_deps = |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
200 |
{ |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
201 |
val root_theories = |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
202 |
info.theories.flatMap({ case (_, thys) => |
65517 | 203 |
thys.map({ case (thy, pos) => |
65528 | 204 |
(resources.import_name(info.theory_qualifier, info.dir.implode, thy), pos) }) |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
205 |
}) |
66571 | 206 |
resources.thy_info.dependencies(root_theories) |
65251 | 207 |
} |
208 |
||
66720 | 209 |
val overall_syntax = thy_deps.overall_syntax |
65251 | 210 |
|
66716 | 211 |
val theory_files = thy_deps.names.map(_.path) |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
212 |
val loaded_files = |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
213 |
if (inlined_files) { |
66701 | 214 |
if (Sessions.is_pure(info.name)) { |
66720 | 215 |
(Thy_Header.PURE -> resources.pure_files(overall_syntax, info.dir)) :: |
66701 | 216 |
thy_deps.loaded_files.filterNot(p => p._1 == Thy_Header.PURE) |
217 |
} |
|
218 |
else thy_deps.loaded_files |
|
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
219 |
} |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
220 |
else Nil |
65251 | 221 |
|
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
222 |
val all_files = |
66701 | 223 |
(theory_files ::: loaded_files.flatMap(_._2) ::: |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
224 |
info.files.map(file => info.dir + file) ::: |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
225 |
info.document_files.map(file => info.dir + file._1 + file._2)).map(_.expand) |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
226 |
|
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
227 |
if (list_files) |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
228 |
progress.echo(cat_lines(all_files.map(_.implode).sorted.map(" " + _))) |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
229 |
|
66719
d37efafd55b5
clarified theory syntax vs. overall session syntax;
wenzelm
parents:
66718
diff
changeset
|
230 |
if (check_keywords.nonEmpty) { |
d37efafd55b5
clarified theory syntax vs. overall session syntax;
wenzelm
parents:
66718
diff
changeset
|
231 |
Check_Keywords.check_keywords( |
66720 | 232 |
progress, overall_syntax.keywords, check_keywords, theory_files) |
66719
d37efafd55b5
clarified theory syntax vs. overall session syntax;
wenzelm
parents:
66718
diff
changeset
|
233 |
} |
65251 | 234 |
|
65507
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
235 |
val session_graph: Graph_Display.Graph = |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
236 |
{ |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
237 |
def session_node(name: String): Graph_Display.Node = |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
238 |
Graph_Display.Node("[" + name + "]", "session." + name) |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
239 |
|
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
240 |
def node(name: Document.Node.Name): Graph_Display.Node = |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
241 |
{ |
65528 | 242 |
val qualifier = resources.theory_qualifier(name) |
243 |
if (qualifier == info.theory_qualifier) |
|
65507
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
244 |
Graph_Display.Node(name.theory_base_name, "theory." + name.theory) |
65528 | 245 |
else session_node(qualifier) |
65507
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
246 |
} |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
247 |
|
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
248 |
val imports_subgraph = |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
249 |
sessions.imports_graph.restrict( |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
250 |
sessions.imports_graph.all_preds(info.parent.toList ::: info.imports).toSet) |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
251 |
|
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
252 |
val graph0 = |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
253 |
(Graph_Display.empty_graph /: imports_subgraph.topological_order)( |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
254 |
{ case (g, session) => |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
255 |
val a = session_node(session) |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
256 |
val bs = imports_subgraph.imm_preds(session).toList.map(session_node(_)) |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
257 |
((g /: (a :: bs))(_.default_node(_, Nil)) /: bs)(_.add_edge(_, a)) }) |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
258 |
|
66714 | 259 |
(graph0 /: thy_deps.entries)( |
260 |
{ case (g, entry) => |
|
261 |
val a = node(entry.name) |
|
65507
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
262 |
val bs = |
66714 | 263 |
entry.header.imports.map({ case (name, _) => node(name) }). |
65507
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
264 |
filterNot(_ == a) |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
265 |
((g /: (a :: bs))(_.default_node(_, Nil)) /: bs)(_.add_edge(_, a)) }) |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
266 |
} |
decdb95bd007
clarified session graph: collapse theories from other sessions;
wenzelm
parents:
65500
diff
changeset
|
267 |
|
66701 | 268 |
val known = |
269 |
Known.make(info.dir, List(imports_base), |
|
66715 | 270 |
theories = thy_deps.names, |
66701 | 271 |
loaded_files = loaded_files) |
272 |
||
66604 | 273 |
val sources = |
274 |
for (p <- all_files if p.is_file) yield (p, SHA1.digest(p.file)) |
|
275 |
val sources_errors = |
|
276 |
for (p <- all_files if !p.is_file) yield "No such file: " + p |
|
277 |
||
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
278 |
val base = |
66571 | 279 |
Base( |
280 |
pos = info.pos, |
|
65540
2b73ed8bf4d9
proper imports_resources for import_name: avoid self-referential name resolution;
wenzelm
parents:
65532
diff
changeset
|
281 |
global_theories = global_theories, |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
282 |
loaded_theories = thy_deps.loaded_theories, |
66701 | 283 |
known = known, |
66720 | 284 |
overall_syntax = overall_syntax, |
66604 | 285 |
sources = sources, |
66571 | 286 |
session_graph = session_graph, |
66694
41177b124067
tuned signature -- more readable output as Scala value;
wenzelm
parents:
66668
diff
changeset
|
287 |
errors = thy_deps.errors ::: sources_errors, |
41177b124067
tuned signature -- more readable output as Scala value;
wenzelm
parents:
66668
diff
changeset
|
288 |
imports = Some(imports_base)) |
65251 | 289 |
|
65519 | 290 |
session_bases + (info.name -> base) |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
291 |
} |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
292 |
catch { |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
293 |
case ERROR(msg) => |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
294 |
cat_error(msg, "The error(s) above occurred in session " + |
65519 | 295 |
quote(info.name) + Position.here(info.pos)) |
65463
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
296 |
} |
104502de757c
more informative known_files: known_theories within the local session directory come first;
wenzelm
parents:
65461
diff
changeset
|
297 |
}) |
65251 | 298 |
|
66701 | 299 |
Deps(session_bases, Known.make(Path.current, session_bases.toList.map(_._2))) |
65372
b722ee40c26c
refer to global_theories from all sessions, before selection;
wenzelm
parents:
65371
diff
changeset
|
300 |
} |
65251 | 301 |
|
66571 | 302 |
def session_base_errors( |
65428 | 303 |
options: Options, |
304 |
session: String, |
|
305 |
dirs: List[Path] = Nil, |
|
66701 | 306 |
inlined_files: Boolean = false, |
66571 | 307 |
all_known: Boolean = false): (List[String], Base) = |
65251 | 308 |
{ |
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
309 |
val full_sessions = load(options, dirs = dirs) |
65428 | 310 |
val global_theories = full_sessions.global_theories |
66575
191048506504
clarified signature: provide all_known information uniformly (it is subject to Sessions.T selection);
wenzelm
parents:
66573
diff
changeset
|
311 |
val (_, selected_sessions) = full_sessions.selection(Selection(sessions = List(session))) |
65372
b722ee40c26c
refer to global_theories from all sessions, before selection;
wenzelm
parents:
65371
diff
changeset
|
312 |
|
66575
191048506504
clarified signature: provide all_known information uniformly (it is subject to Sessions.T selection);
wenzelm
parents:
66573
diff
changeset
|
313 |
val sessions: T = if (all_known) full_sessions else selected_sessions |
66701 | 314 |
val deps = |
315 |
Sessions.deps(sessions, inlined_files = inlined_files, global_theories = global_theories) |
|
66575
191048506504
clarified signature: provide all_known information uniformly (it is subject to Sessions.T selection);
wenzelm
parents:
66573
diff
changeset
|
316 |
val base = if (all_known) deps(session).copy(known = deps.all_known) else deps(session) |
191048506504
clarified signature: provide all_known information uniformly (it is subject to Sessions.T selection);
wenzelm
parents:
66573
diff
changeset
|
317 |
(deps.errors, base) |
66571 | 318 |
} |
319 |
||
320 |
def session_base( |
|
321 |
options: Options, |
|
322 |
session: String, |
|
323 |
dirs: List[Path] = Nil, |
|
66701 | 324 |
inlined_files: Boolean = false, |
66571 | 325 |
all_known: Boolean = false): Base = |
326 |
{ |
|
66701 | 327 |
val (errs, base) = |
328 |
session_base_errors(options, session, dirs = dirs, |
|
329 |
inlined_files = inlined_files, all_known = all_known) |
|
66573 | 330 |
if (errs.isEmpty) base else error(cat_lines(errs)) |
65251 | 331 |
} |
332 |
||
333 |
||
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
334 |
/* cumulative session info */ |
62631 | 335 |
|
336 |
sealed case class Info( |
|
65519 | 337 |
name: String, |
62631 | 338 |
chapter: String, |
339 |
select: Boolean, |
|
340 |
pos: Position.T, |
|
341 |
groups: List[String], |
|
342 |
dir: Path, |
|
343 |
parent: Option[String], |
|
344 |
description: String, |
|
345 |
options: Options, |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
346 |
imports: List[String], |
65517 | 347 |
theories: List[(Options, List[(String, Position.T)])], |
65374 | 348 |
global_theories: List[String], |
62631 | 349 |
files: List[Path], |
350 |
document_files: List[(Path, Path)], |
|
351 |
meta_digest: SHA1.Digest) |
|
352 |
{ |
|
353 |
def timeout: Time = Time.seconds(options.real("timeout") * options.real("timeout_scale")) |
|
65456
31e8a86971a8
explicit theory qualifier for session "HOL-Proofs": its theory name space overlaps with session "HOL", even for further imports;
wenzelm
parents:
65445
diff
changeset
|
354 |
|
65520 | 355 |
def theory_qualifier: String = |
65456
31e8a86971a8
explicit theory qualifier for session "HOL-Proofs": its theory name space overlaps with session "HOL", even for further imports;
wenzelm
parents:
65445
diff
changeset
|
356 |
options.string("theory_qualifier") match { |
65520 | 357 |
case "" => name |
65457 | 358 |
case qualifier => qualifier |
65456
31e8a86971a8
explicit theory qualifier for session "HOL-Proofs": its theory name space overlaps with session "HOL", even for further imports;
wenzelm
parents:
65445
diff
changeset
|
359 |
} |
62631 | 360 |
} |
361 |
||
65419 | 362 |
object Selection |
363 |
{ |
|
65422 | 364 |
val empty: Selection = Selection() |
65525 | 365 |
val all: Selection = Selection(all_sessions = true) |
65419 | 366 |
} |
367 |
||
368 |
sealed case class Selection( |
|
369 |
requirements: Boolean = false, |
|
370 |
all_sessions: Boolean = false, |
|
66737
2edc0c42c883
option -B for "isabelle build" and "isabelle imports";
wenzelm
parents:
66736
diff
changeset
|
371 |
base_sessions: List[String] = Nil, |
65419 | 372 |
exclude_session_groups: List[String] = Nil, |
373 |
exclude_sessions: List[String] = Nil, |
|
374 |
session_groups: List[String] = Nil, |
|
375 |
sessions: List[String] = Nil) |
|
376 |
{ |
|
66736 | 377 |
def ++ (other: Selection): Selection = |
65422 | 378 |
Selection( |
379 |
requirements = requirements || other.requirements, |
|
380 |
all_sessions = all_sessions || other.all_sessions, |
|
66737
2edc0c42c883
option -B for "isabelle build" and "isabelle imports";
wenzelm
parents:
66736
diff
changeset
|
381 |
base_sessions = Library.merge(base_sessions, other.base_sessions), |
66736 | 382 |
exclude_session_groups = Library.merge(exclude_session_groups, other.exclude_session_groups), |
383 |
exclude_sessions = Library.merge(exclude_sessions, other.exclude_sessions), |
|
384 |
session_groups = Library.merge(session_groups, other.session_groups), |
|
385 |
sessions = Library.merge(sessions, other.sessions)) |
|
65422 | 386 |
|
65419 | 387 |
def apply(graph: Graph[String, Info]): (List[String], Graph[String, Info]) = |
388 |
{ |
|
389 |
val bad_sessions = |
|
66737
2edc0c42c883
option -B for "isabelle build" and "isabelle imports";
wenzelm
parents:
66736
diff
changeset
|
390 |
SortedSet((base_sessions ::: exclude_sessions ::: sessions). |
2edc0c42c883
option -B for "isabelle build" and "isabelle imports";
wenzelm
parents:
66736
diff
changeset
|
391 |
filterNot(graph.defined(_)): _*).toList |
2edc0c42c883
option -B for "isabelle build" and "isabelle imports";
wenzelm
parents:
66736
diff
changeset
|
392 |
if (bad_sessions.nonEmpty) |
2edc0c42c883
option -B for "isabelle build" and "isabelle imports";
wenzelm
parents:
66736
diff
changeset
|
393 |
error("Undefined session(s): " + commas_quote(bad_sessions)) |
65419 | 394 |
|
395 |
val excluded = |
|
396 |
{ |
|
397 |
val exclude_group = exclude_session_groups.toSet |
|
398 |
val exclude_group_sessions = |
|
399 |
(for { |
|
400 |
(name, (info, _)) <- graph.iterator |
|
401 |
if graph.get_node(name).groups.exists(exclude_group) |
|
402 |
} yield name).toList |
|
403 |
graph.all_succs(exclude_group_sessions ::: exclude_sessions).toSet |
|
404 |
} |
|
405 |
||
406 |
val pre_selected = |
|
407 |
{ |
|
408 |
if (all_sessions) graph.keys |
|
409 |
else { |
|
410 |
val select_group = session_groups.toSet |
|
66737
2edc0c42c883
option -B for "isabelle build" and "isabelle imports";
wenzelm
parents:
66736
diff
changeset
|
411 |
val select = sessions.toSet ++ graph.all_succs(base_sessions) |
65419 | 412 |
(for { |
413 |
(name, (info, _)) <- graph.iterator |
|
414 |
if info.select || select(name) || graph.get_node(name).groups.exists(select_group) |
|
415 |
} yield name).toList |
|
416 |
} |
|
417 |
}.filterNot(excluded) |
|
418 |
||
419 |
val selected = |
|
420 |
if (requirements) (graph.all_preds(pre_selected).toSet -- pre_selected).toList |
|
421 |
else pre_selected |
|
422 |
||
423 |
(selected, graph.restrict(graph.all_preds(selected).toSet)) |
|
424 |
} |
|
425 |
} |
|
426 |
||
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
427 |
def make(infos: Traversable[(String, Info)]): T = |
62631 | 428 |
{ |
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
429 |
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
|
430 |
: Graph[String, Info] = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
431 |
{ |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
432 |
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
|
433 |
{ |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
434 |
if (!g.defined(parent)) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
435 |
error("Bad " + kind + " session " + quote(parent) + " for " + |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
436 |
quote(name) + Position.here(pos)) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
437 |
|
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
438 |
try { g.add_edge_acyclic(parent, name) } |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
439 |
catch { |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
440 |
case exn: Graph.Cycles[_] => |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
441 |
error(cat_lines(exn.cycles.map(cycle => |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
442 |
"Cyclic session dependency of " + |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
443 |
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
|
444 |
} |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
445 |
} |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
446 |
(graph /: graph.iterator) { |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
447 |
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
|
448 |
} |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
449 |
} |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
450 |
|
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
451 |
val graph0 = |
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
452 |
(Graph.string[Info] /: infos) { |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
453 |
case (graph, (name, info)) => |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
454 |
if (graph.defined(name)) |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
455 |
error("Duplicate session " + quote(name) + Position.here(info.pos) + |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
456 |
Position.here(graph.get_node(name).pos)) |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
457 |
else graph.new_node(name, info) |
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
458 |
} |
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
459 |
val graph1 = add_edges(graph0, "parent", _.parent) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
460 |
val graph2 = add_edges(graph1, "imports", _.imports) |
62631 | 461 |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
462 |
new T(graph1, graph2) |
62631 | 463 |
} |
464 |
||
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
465 |
final class T private[Sessions]( |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
466 |
val build_graph: Graph[String, Info], |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
467 |
val imports_graph: Graph[String, Info]) |
62631 | 468 |
{ |
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
469 |
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
|
470 |
def get(name: String): Option[Info] = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
471 |
if (imports_graph.defined(name)) Some(imports_graph.get_node(name)) else None |
62631 | 472 |
|
65457 | 473 |
def global_theories: Map[String, String] = |
65490 | 474 |
(Thy_Header.bootstrap_global_theories.toMap /: |
65424 | 475 |
(for { |
65519 | 476 |
(_, (info, _)) <- imports_graph.iterator |
477 |
thy <- info.global_theories.iterator } |
|
478 |
yield (thy, info)))({ |
|
479 |
case (global, (thy, info)) => |
|
65520 | 480 |
val qualifier = info.theory_qualifier |
65457 | 481 |
global.get(thy) match { |
482 |
case Some(qualifier1) if qualifier != qualifier1 => |
|
483 |
error("Duplicate global theory " + quote(thy) + Position.here(info.pos)) |
|
484 |
case _ => global + (thy -> qualifier) |
|
485 |
} |
|
486 |
}) |
|
65372
b722ee40c26c
refer to global_theories from all sessions, before selection;
wenzelm
parents:
65371
diff
changeset
|
487 |
|
65419 | 488 |
def selection(select: Selection): (List[String], T) = |
62631 | 489 |
{ |
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
490 |
val (_, build_graph1) = select(build_graph) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
491 |
val (selected, imports_graph1) = select(imports_graph) |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
492 |
(selected, new T(build_graph1, imports_graph1)) |
62631 | 493 |
} |
494 |
||
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
495 |
def build_ancestors(name: String): List[String] = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
496 |
build_graph.all_preds(List(name)).tail.reverse |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
497 |
|
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
498 |
def build_descendants(names: List[String]): List[String] = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
499 |
build_graph.all_succs(names) |
62631 | 500 |
|
65519 | 501 |
def build_topological_order: List[Info] = |
502 |
build_graph.topological_order.map(apply(_)) |
|
62631 | 503 |
|
65566 | 504 |
def imports_ancestors(name: String): List[String] = |
505 |
imports_graph.all_preds(List(name)).tail.reverse |
|
506 |
||
65519 | 507 |
def imports_topological_order: List[Info] = |
508 |
imports_graph.topological_order.map(apply(_)) |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
509 |
|
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
510 |
override def toString: String = |
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
511 |
imports_graph.keys_iterator.mkString("Sessions.T(", ", ", ")") |
62631 | 512 |
} |
513 |
||
514 |
||
515 |
/* parser */ |
|
516 |
||
62864 | 517 |
val ROOT = Path.explode("ROOT") |
518 |
val ROOTS = Path.explode("ROOTS") |
|
519 |
||
62631 | 520 |
private val CHAPTER = "chapter" |
521 |
private val SESSION = "session" |
|
522 |
private val IN = "in" |
|
523 |
private val DESCRIPTION = "description" |
|
524 |
private val OPTIONS = "options" |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
525 |
private val SESSIONS = "sessions" |
62631 | 526 |
private val THEORIES = "theories" |
65374 | 527 |
private val GLOBAL = "global" |
62631 | 528 |
private val FILES = "files" |
529 |
private val DOCUMENT_FILES = "document_files" |
|
530 |
||
531 |
lazy val root_syntax = |
|
65374 | 532 |
Outer_Syntax.init() + "(" + ")" + "+" + "," + "=" + "[" + "]" + GLOBAL + IN + |
63443 | 533 |
(CHAPTER, Keyword.THY_DECL) + |
534 |
(SESSION, Keyword.THY_DECL) + |
|
535 |
(DESCRIPTION, Keyword.QUASI_COMMAND) + |
|
536 |
(OPTIONS, Keyword.QUASI_COMMAND) + |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
537 |
(SESSIONS, Keyword.QUASI_COMMAND) + |
63443 | 538 |
(THEORIES, Keyword.QUASI_COMMAND) + |
539 |
(FILES, Keyword.QUASI_COMMAND) + |
|
540 |
(DOCUMENT_FILES, Keyword.QUASI_COMMAND) |
|
62631 | 541 |
|
62968 | 542 |
private object Parser extends Parse.Parser with Options.Parser |
62631 | 543 |
{ |
544 |
private abstract class Entry |
|
545 |
private sealed case class Chapter(name: String) extends Entry |
|
546 |
private sealed case class Session_Entry( |
|
547 |
pos: Position.T, |
|
548 |
name: String, |
|
549 |
groups: List[String], |
|
550 |
path: String, |
|
551 |
parent: Option[String], |
|
552 |
description: String, |
|
553 |
options: List[Options.Spec], |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
554 |
imports: List[String], |
65517 | 555 |
theories: List[(List[Options.Spec], List[((String, Position.T), Boolean)])], |
62631 | 556 |
files: List[String], |
557 |
document_files: List[(String, String)]) extends Entry |
|
65580
66351f79c295
meta_digest without accidental source positions (amending 1544e61e5314): avoid spurious rebuild of unrelated sessions after editing ROOT;
wenzelm
parents:
65566
diff
changeset
|
558 |
{ |
66351f79c295
meta_digest without accidental source positions (amending 1544e61e5314): avoid spurious rebuild of unrelated sessions after editing ROOT;
wenzelm
parents:
65566
diff
changeset
|
559 |
def theories_no_position: List[(List[Options.Spec], List[(String, Boolean)])] = |
66351f79c295
meta_digest without accidental source positions (amending 1544e61e5314): avoid spurious rebuild of unrelated sessions after editing ROOT;
wenzelm
parents:
65566
diff
changeset
|
560 |
theories.map({ case (a, b) => (a, b.map({ case ((c, _), d) => (c, d) })) }) |
66351f79c295
meta_digest without accidental source positions (amending 1544e61e5314): avoid spurious rebuild of unrelated sessions after editing ROOT;
wenzelm
parents:
65566
diff
changeset
|
561 |
} |
62631 | 562 |
|
563 |
private val chapter: Parser[Chapter] = |
|
564 |
{ |
|
565 |
val chapter_name = atom("chapter name", _.is_name) |
|
566 |
||
567 |
command(CHAPTER) ~! chapter_name ^^ { case _ ~ a => Chapter(a) } |
|
568 |
} |
|
569 |
||
570 |
private val session_entry: Parser[Session_Entry] = |
|
571 |
{ |
|
572 |
val session_name = atom("session name", _.is_name) |
|
573 |
||
574 |
val option = |
|
62968 | 575 |
option_name ~ opt($$$("=") ~! option_value ^^ |
576 |
{ case _ ~ x => x }) ^^ { case x ~ y => (x, y) } |
|
62631 | 577 |
val options = $$$("[") ~> rep1sep(option, $$$(",")) <~ $$$("]") |
578 |
||
65374 | 579 |
val global = |
580 |
($$$("(") ~! $$$(GLOBAL) ~ $$$(")")) ^^ { case _ => true } | success(false) |
|
581 |
||
582 |
val theory_entry = |
|
65517 | 583 |
position(theory_name) ~ global ^^ { case x ~ y => (x, y) } |
65374 | 584 |
|
62631 | 585 |
val theories = |
65374 | 586 |
$$$(THEORIES) ~! |
587 |
((options | success(Nil)) ~ rep(theory_entry)) ^^ |
|
588 |
{ case _ ~ (x ~ y) => (x, y) } |
|
62631 | 589 |
|
590 |
val document_files = |
|
591 |
$$$(DOCUMENT_FILES) ~! |
|
592 |
(($$$("(") ~! ($$$(IN) ~! (path ~ $$$(")"))) ^^ |
|
593 |
{ case _ ~ (_ ~ (x ~ _)) => x } | success("document")) ~ |
|
594 |
rep1(path)) ^^ { case _ ~ (x ~ y) => y.map((x, _)) } |
|
595 |
||
596 |
command(SESSION) ~! |
|
597 |
(position(session_name) ~ |
|
598 |
(($$$("(") ~! (rep1(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | success(Nil)) ~ |
|
599 |
(($$$(IN) ~! path ^^ { case _ ~ x => x }) | success(".")) ~ |
|
600 |
($$$("=") ~! |
|
601 |
(opt(session_name ~! $$$("+") ^^ { case x ~ _ => x }) ~ |
|
602 |
(($$$(DESCRIPTION) ~! text ^^ { case _ ~ x => x }) | success("")) ~ |
|
603 |
(($$$(OPTIONS) ~! options ^^ { case _ ~ x => x }) | success(Nil)) ~ |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
604 |
(($$$(SESSIONS) ~! rep(session_name) ^^ { case _ ~ x => x }) | success(Nil)) ~ |
62631 | 605 |
rep1(theories) ~ |
606 |
(($$$(FILES) ~! rep1(path) ^^ { case _ ~ x => x }) | success(Nil)) ~ |
|
607 |
(rep(document_files) ^^ (x => x.flatten))))) ^^ |
|
65420
695d4e22345a
support for static session imports, without affect build hierarchy;
wenzelm
parents:
65419
diff
changeset
|
608 |
{ 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
|
609 |
Session_Entry(pos, a, b, c, d, e, f, g, h, i, j) } |
62631 | 610 |
} |
611 |
||
612 |
def parse(options: Options, select: Boolean, dir: Path): List[(String, Info)] = |
|
613 |
{ |
|
614 |
def make_info(entry_chapter: String, entry: Session_Entry): (String, Info) = |
|
615 |
{ |
|
616 |
try { |
|
617 |
val name = entry.name |
|
618 |
||
65445
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
619 |
if (name == "" || name == DRAFT) error("Bad session name") |
65360 | 620 |
if (is_pure(name) && entry.parent.isDefined) error("Illegal parent session") |
621 |
if (!is_pure(name) && !entry.parent.isDefined) error("Missing parent session") |
|
62631 | 622 |
|
623 |
val session_options = options ++ entry.options |
|
624 |
||
625 |
val theories = |
|
65392
f365f61f2081
uniform import_name, with treatment of global and qualified theories;
wenzelm
parents:
65391
diff
changeset
|
626 |
entry.theories.map({ case (opts, thys) => (session_options ++ opts, thys.map(_._1)) }) |
65374 | 627 |
|
628 |
val global_theories = |
|
65517 | 629 |
for { (_, thys) <- entry.theories; ((thy, pos), global) <- thys if global } |
65374 | 630 |
yield { |
65999 | 631 |
val thy_name = Path.explode(thy).expand.base_name |
65374 | 632 |
if (Long_Name.is_qualified(thy_name)) |
65517 | 633 |
error("Bad qualified name for global theory " + |
634 |
quote(thy_name) + Position.here(pos)) |
|
65374 | 635 |
else thy_name |
636 |
} |
|
637 |
||
62631 | 638 |
val files = entry.files.map(Path.explode(_)) |
639 |
val document_files = |
|
640 |
entry.document_files.map({ case (s1, s2) => (Path.explode(s1), Path.explode(s2)) }) |
|
641 |
||
642 |
val meta_digest = |
|
65580
66351f79c295
meta_digest without accidental source positions (amending 1544e61e5314): avoid spurious rebuild of unrelated sessions after editing ROOT;
wenzelm
parents:
65566
diff
changeset
|
643 |
SHA1.digest((entry_chapter, name, entry.parent, entry.options, entry.imports, |
66351f79c295
meta_digest without accidental source positions (amending 1544e61e5314): avoid spurious rebuild of unrelated sessions after editing ROOT;
wenzelm
parents:
65566
diff
changeset
|
644 |
entry.theories_no_position, entry.files, entry.document_files).toString) |
62631 | 645 |
|
646 |
val info = |
|
65519 | 647 |
Info(name, entry_chapter, select, entry.pos, entry.groups, |
648 |
dir + Path.explode(entry.path), entry.parent, entry.description, session_options, |
|
649 |
entry.imports, theories, global_theories, files, document_files, meta_digest) |
|
62631 | 650 |
|
651 |
(name, info) |
|
652 |
} |
|
653 |
catch { |
|
654 |
case ERROR(msg) => |
|
655 |
error(msg + "\nThe error(s) above occurred in session entry " + |
|
656 |
quote(entry.name) + Position.here(entry.pos)) |
|
657 |
} |
|
658 |
} |
|
659 |
||
660 |
val root = dir + ROOT |
|
661 |
if (root.is_file) { |
|
662 |
val toks = Token.explode(root_syntax.keywords, File.read(root)) |
|
663 |
val start = Token.Pos.file(root.implode) |
|
664 |
||
665 |
parse_all(rep(chapter | session_entry), Token.reader(toks, start)) match { |
|
666 |
case Success(result, _) => |
|
667 |
var entry_chapter = "Unsorted" |
|
668 |
val infos = new mutable.ListBuffer[(String, Info)] |
|
669 |
result.foreach { |
|
670 |
case Chapter(name) => entry_chapter = name |
|
671 |
case entry: Session_Entry => infos += make_info(entry_chapter, entry) |
|
672 |
} |
|
673 |
infos.toList |
|
674 |
case bad => error(bad.toString) |
|
675 |
} |
|
676 |
} |
|
677 |
else Nil |
|
678 |
} |
|
679 |
} |
|
680 |
||
681 |
||
62635 | 682 |
/* load sessions from certain directories */ |
62631 | 683 |
|
684 |
private def is_session_dir(dir: Path): Boolean = |
|
685 |
(dir + ROOT).is_file || (dir + ROOTS).is_file |
|
686 |
||
687 |
private def check_session_dir(dir: Path): Path = |
|
65468
c41791ad75c3
early check and normalization of session directory, e.g. relevant for path information passed to ML process, which may have a different CWD;
wenzelm
parents:
65463
diff
changeset
|
688 |
if (is_session_dir(dir)) File.pwd() + dir.expand |
62631 | 689 |
else error("Bad session root directory: " + dir.toString) |
690 |
||
65561 | 691 |
def directories(dirs: List[Path], select_dirs: List[Path]): List[(Boolean, Path)] = |
692 |
{ |
|
693 |
val default_dirs = Isabelle_System.components().filter(is_session_dir(_)) |
|
694 |
(default_dirs ::: dirs).map((false, _)) ::: select_dirs.map((true, _)) |
|
695 |
} |
|
696 |
||
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
697 |
def load(options: Options, dirs: List[Path] = Nil, select_dirs: List[Path] = Nil): T = |
62631 | 698 |
{ |
62635 | 699 |
def load_dir(select: Boolean, dir: Path): List[(String, Info)] = |
700 |
load_root(select, dir) ::: load_roots(select, dir) |
|
62631 | 701 |
|
62635 | 702 |
def load_root(select: Boolean, dir: Path): List[(String, Info)] = |
62631 | 703 |
Parser.parse(options, select, dir) |
704 |
||
62635 | 705 |
def load_roots(select: Boolean, dir: Path): List[(String, Info)] = |
62631 | 706 |
{ |
707 |
val roots = dir + ROOTS |
|
708 |
if (roots.is_file) { |
|
709 |
for { |
|
710 |
line <- split_lines(File.read(roots)) |
|
711 |
if !(line == "" || line.startsWith("#")) |
|
712 |
dir1 = |
|
713 |
try { check_session_dir(dir + Path.explode(line)) } |
|
714 |
catch { |
|
715 |
case ERROR(msg) => |
|
716 |
error(msg + "\nThe error(s) above occurred in session catalog " + roots.toString) |
|
717 |
} |
|
62635 | 718 |
info <- load_dir(select, dir1) |
62631 | 719 |
} yield info |
720 |
} |
|
721 |
else Nil |
|
722 |
} |
|
723 |
||
65415
8cd54b18b68b
clarified signature: tree structure is not essential;
wenzelm
parents:
65410
diff
changeset
|
724 |
make( |
62631 | 725 |
for { |
65561 | 726 |
(select, dir) <- directories(dirs, select_dirs) |
65468
c41791ad75c3
early check and normalization of session directory, e.g. relevant for path information passed to ML process, which may have a different CWD;
wenzelm
parents:
65463
diff
changeset
|
727 |
info <- load_dir(select, check_session_dir(dir)) |
62631 | 728 |
} yield info) |
729 |
} |
|
62632 | 730 |
|
731 |
||
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
732 |
|
62704
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
733 |
/** 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
|
734 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
735 |
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
|
736 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
737 |
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
|
738 |
{ |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
739 |
if (heap.is_file) { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
740 |
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
|
741 |
try { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
742 |
val len = file.size |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
743 |
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
|
744 |
if (len >= n) { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
745 |
file.position(len - n) |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
746 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
747 |
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
|
748 |
var i = 0 |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
749 |
var m = 0 |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
750 |
do { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
751 |
m = file.read(buf) |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
752 |
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
|
753 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
754 |
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
|
755 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
756 |
if (i == n) { |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
757 |
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
|
758 |
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
|
759 |
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
|
760 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
761 |
else None |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
762 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
763 |
else None |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
764 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
765 |
finally { file.close } |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
766 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
767 |
else None |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
768 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
769 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
770 |
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
|
771 |
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
|
772 |
case None => |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
773 |
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
|
774 |
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
|
775 |
s |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
776 |
case Some(s) => s |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
777 |
} |
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
778 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
779 |
|
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62637
diff
changeset
|
780 |
|
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
781 |
/** persistent store **/ |
62632 | 782 |
|
65296 | 783 |
object Session_Info |
784 |
{ |
|
65326 | 785 |
val session_name = SQL.Column.string("session_name", primary_key = true) |
786 |
||
65296 | 787 |
// Build_Log.Session_Info |
788 |
val session_timing = SQL.Column.bytes("session_timing") |
|
789 |
val command_timings = SQL.Column.bytes("command_timings") |
|
790 |
val ml_statistics = SQL.Column.bytes("ml_statistics") |
|
791 |
val task_statistics = SQL.Column.bytes("task_statistics") |
|
65934 | 792 |
val errors = SQL.Column.bytes("errors") |
65296 | 793 |
val build_log_columns = |
65934 | 794 |
List(session_name, session_timing, command_timings, ml_statistics, task_statistics, errors) |
65296 | 795 |
|
796 |
// Build.Session_Info |
|
797 |
val sources = SQL.Column.string("sources") |
|
798 |
val input_heaps = SQL.Column.string("input_heaps") |
|
799 |
val output_heap = SQL.Column.string("output_heap") |
|
800 |
val return_code = SQL.Column.int("return_code") |
|
801 |
val build_columns = List(sources, input_heaps, output_heap, return_code) |
|
802 |
||
803 |
val table = SQL.Table("isabelle_session_info", build_log_columns ::: build_columns) |
|
804 |
} |
|
805 |
||
62632 | 806 |
def store(system_mode: Boolean = false): Store = new Store(system_mode) |
807 |
||
65857 | 808 |
class Store private[Sessions](system_mode: Boolean) |
62632 | 809 |
{ |
65278 | 810 |
/* file names */ |
811 |
||
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
812 |
def database(name: String): Path = Path.basic("log") + Path.basic(name).ext("db") |
65278 | 813 |
def log(name: String): Path = Path.basic("log") + Path.basic(name) |
814 |
def log_gz(name: String): Path = log(name).ext("gz") |
|
815 |
||
816 |
||
65286 | 817 |
/* SQL database content */ |
65283 | 818 |
|
65857 | 819 |
val xml_cache = new XML.Cache() |
820 |
||
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
821 |
def read_bytes(db: SQL.Database, name: String, column: SQL.Column): Bytes = |
65698 | 822 |
db.using_statement(Session_Info.table.select(List(column), |
65699 | 823 |
Session_Info.session_name.where_equal(name)))(stmt => |
65291
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
824 |
{ |
65740 | 825 |
val res = stmt.execute_query() |
826 |
if (!res.next()) Bytes.empty else res.bytes(column) |
|
65291
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
827 |
}) |
65285 | 828 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
829 |
def read_properties(db: SQL.Database, name: String, column: SQL.Column): List[Properties.T] = |
65857 | 830 |
Properties.uncompress(read_bytes(db, name, column), Some(xml_cache)) |
65286 | 831 |
|
65283 | 832 |
|
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
833 |
/* output */ |
62632 | 834 |
|
835 |
val browser_info: Path = |
|
836 |
if (system_mode) Path.explode("~~/browser_info") |
|
837 |
else Path.explode("$ISABELLE_BROWSER_INFO") |
|
838 |
||
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
839 |
val output_dir: Path = |
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
840 |
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
|
841 |
else Path.explode("$ISABELLE_OUTPUT") |
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
842 |
|
65298 | 843 |
override def toString: String = "Store(output_dir = " + output_dir.expand + ")" |
844 |
||
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
845 |
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
|
846 |
|
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
847 |
|
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
848 |
/* input */ |
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
849 |
|
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
850 |
private val input_dirs = |
62632 | 851 |
if (system_mode) List(output_dir) |
62633 | 852 |
else { |
853 |
val ml_ident = Path.explode("$ML_IDENTIFIER").expand |
|
854 |
output_dir :: Path.split(Isabelle_System.getenv_strict("ISABELLE_PATH")).map(_ + ml_ident) |
|
855 |
} |
|
62632 | 856 |
|
65291
57c85c83c11b
maintain persistent session info in SQLite database instead of log file;
wenzelm
parents:
65288
diff
changeset
|
857 |
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
|
858 |
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
|
859 |
(dir + database(name), read_heap_digest(dir + Path.basic(name)))) |
62632 | 860 |
|
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
861 |
def find_database(name: String): Option[Path] = |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
862 |
input_dirs.map(_ + database(name)).find(_.is_file) |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
863 |
|
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62636
diff
changeset
|
864 |
def heap(name: String): Path = |
65288 | 865 |
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
|
866 |
error("Unknown logic " + quote(name) + " -- no heap file found in:\n" + |
62769 | 867 |
cat_lines(input_dirs.map(dir => " " + dir.expand.implode))) |
65287 | 868 |
|
869 |
||
65296 | 870 |
/* session info */ |
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
871 |
|
65296 | 872 |
def write_session_info( |
65318
342efc382558
eliminated somewhat redundant inlined name (despite a7aa17a1f721);
wenzelm
parents:
65298
diff
changeset
|
873 |
db: SQL.Database, |
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
874 |
name: String, |
65318
342efc382558
eliminated somewhat redundant inlined name (despite a7aa17a1f721);
wenzelm
parents:
65298
diff
changeset
|
875 |
build_log: Build_Log.Session_Info, |
342efc382558
eliminated somewhat redundant inlined name (despite a7aa17a1f721);
wenzelm
parents:
65298
diff
changeset
|
876 |
build: Build.Session_Info) |
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
877 |
{ |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
878 |
db.transaction { |
65296 | 879 |
db.create_table(Session_Info.table) |
65698 | 880 |
db.using_statement( |
65699 | 881 |
Session_Info.table.delete(Session_Info.session_name.where_equal(name)))(_.execute) |
65698 | 882 |
db.using_statement(Session_Info.table.insert())(stmt => |
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
883 |
{ |
65748 | 884 |
stmt.string(1) = name |
65857 | 885 |
stmt.bytes(2) = Properties.encode(build_log.session_timing) |
886 |
stmt.bytes(3) = Properties.compress(build_log.command_timings) |
|
887 |
stmt.bytes(4) = Properties.compress(build_log.ml_statistics) |
|
888 |
stmt.bytes(5) = Properties.compress(build_log.task_statistics) |
|
65937 | 889 |
stmt.bytes(6) = Build_Log.compress_errors(build_log.errors) |
65934 | 890 |
stmt.string(7) = cat_lines(build.sources) |
891 |
stmt.string(8) = cat_lines(build.input_heaps) |
|
892 |
stmt.string(9) = build.output_heap getOrElse "" |
|
893 |
stmt.int(10) = build.return_code |
|
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
894 |
stmt.execute() |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
895 |
}) |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
896 |
} |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
897 |
} |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
898 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
899 |
def read_session_timing(db: SQL.Database, name: String): Properties.T = |
65857 | 900 |
Properties.decode(read_bytes(db, name, Session_Info.session_timing), Some(xml_cache)) |
65286 | 901 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
902 |
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
|
903 |
read_properties(db, name, Session_Info.command_timings) |
65286 | 904 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
905 |
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
|
906 |
read_properties(db, name, Session_Info.ml_statistics) |
65286 | 907 |
|
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
908 |
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
|
909 |
read_properties(db, name, Session_Info.task_statistics) |
65286 | 910 |
|
65937 | 911 |
def read_errors(db: SQL.Database, name: String): List[String] = |
912 |
Build_Log.uncompress_errors(read_bytes(db, name, Session_Info.errors)) |
|
913 |
||
65320
52861eebf58d
access table via session_name: db may in principle contain multiple entries;
wenzelm
parents:
65318
diff
changeset
|
914 |
def read_build(db: SQL.Database, name: String): Option[Build.Session_Info] = |
65698 | 915 |
db.using_statement(Session_Info.table.select(Session_Info.build_columns, |
65699 | 916 |
Session_Info.session_name.where_equal(name)))(stmt => |
65285 | 917 |
{ |
65740 | 918 |
val res = stmt.execute_query() |
919 |
if (!res.next()) None |
|
65285 | 920 |
else { |
921 |
Some( |
|
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
922 |
Build.Session_Info( |
65740 | 923 |
split_lines(res.string(Session_Info.sources)), |
924 |
split_lines(res.string(Session_Info.input_heaps)), |
|
925 |
res.string(Session_Info.output_heap) match { case "" => None case s => Some(s) }, |
|
926 |
res.int(Session_Info.return_code))) |
|
65281
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
927 |
} |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
928 |
}) |
c70e7d24a16d
SQL database operations for combined session info;
wenzelm
parents:
65278
diff
changeset
|
929 |
} |
62631 | 930 |
} |