author | wenzelm |
Mon, 25 Mar 2019 19:50:52 +0100 | |
changeset 69982 | f150253cb201 |
parent 69981 | 3dced198b9ec |
child 69995 | 2d5c313e8582 |
permissions | -rw-r--r-- |
66820 | 1 |
/* Title: Pure/Admin/afp.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Administrative support for the Archive of Formal Proofs. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
69980 | 10 |
import java.time.LocalDate |
69974 | 11 |
import scala.collection.immutable.SortedMap |
12 |
||
13 |
||
66820 | 14 |
object AFP |
15 |
{ |
|
69787
60b5a4731695
clarified URL -- avoid odd certificate problem with api.media.atlassian.com;
wenzelm
parents:
69693
diff
changeset
|
16 |
val repos_source = "https://isabelle.sketis.net/repos/afp-devel" |
66854 | 17 |
|
69693 | 18 |
val groups: Map[String, String] = |
19 |
Map("large" -> "full 64-bit memory model or word arithmetic required", |
|
20 |
"slow" -> "CPU time much higher than 60min (on mid-range hardware)", |
|
21 |
"very_slow" -> "elapsed time of many hours (on high-end hardware)") |
|
22 |
||
23 |
def groups_bulky: List[String] = List("large", "slow") |
|
24 |
||
66824 | 25 |
def init(options: Options, base_dir: Path = Path.explode("$AFP_BASE")): AFP = |
26 |
new AFP(options, base_dir) |
|
66820 | 27 |
|
69973 | 28 |
|
29 |
/* entries */ |
|
30 |
||
69977
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
31 |
def parse_date(s: String): Date = |
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
32 |
{ |
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
33 |
val t = Date.Formatter.pattern("uuuu-MM-dd").parse(s) |
69980 | 34 |
Date(LocalDate.from(t).atStartOfDay(Date.timezone_berlin)) |
69977
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
35 |
} |
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
36 |
|
69975 | 37 |
sealed case class Entry(name: String, metadata: Properties.T, sessions: List[String]) |
69976 | 38 |
{ |
69977
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
39 |
def get(prop: String): Option[String] = Properties.get(metadata, prop) |
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
40 |
def get_string(prop: String): String = get(prop).getOrElse("") |
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
41 |
def get_strings(prop: String): List[String] = |
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
42 |
Library.space_explode(',', get_string(prop)).map(_.trim).filter(_.nonEmpty) |
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
43 |
|
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
44 |
def title: String = get_string("title") |
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
45 |
def authors: List[String] = get_strings("author") |
69981 | 46 |
def date: Date = |
47 |
parse_date(get("date").getOrElse(error("Missing date for entry " + quote(name)))) |
|
69977
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
48 |
def topics: List[String] = get_strings("topic") |
69981 | 49 |
def `abstract`: String = get_string("abstract").trim |
69977
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
50 |
def maintainers: List[String] = get_strings("notify") |
3c166df11085
clarified signature: explicitly typed interfaces;
wenzelm
parents:
69976
diff
changeset
|
51 |
def contributors: List[String] = get_strings("contributors") |
69981 | 52 |
def license: String = get("license").getOrElse("BSD") |
69982 | 53 |
|
54 |
def rdf_meta_data: Properties.T = |
|
55 |
RDF.meta_data( |
|
56 |
proper_string(title).map(Markup.META_TITLE -> _).toList ::: |
|
57 |
authors.map(Markup.META_CREATOR -> _) ::: |
|
58 |
contributors.map(Markup.META_CONTRIBUTOR -> _) ::: |
|
59 |
List(Markup.META_DATE -> RDF.date_format(date)) ::: |
|
60 |
List(Markup.META_LICENSE -> license) ::: |
|
61 |
proper_string(`abstract`).map(Markup.META_DESCRIPTION -> _).toList) |
|
69976 | 62 |
} |
66820 | 63 |
} |
64 |
||
66824 | 65 |
class AFP private(options: Options, val base_dir: Path) |
66820 | 66 |
{ |
66821 | 67 |
override def toString: String = base_dir.expand.toString |
68 |
||
66820 | 69 |
val main_dir: Path = base_dir + Path.explode("thys") |
70 |
||
66821 | 71 |
|
69975 | 72 |
/* metadata */ |
73 |
||
74 |
private val entry_metadata: Map[String, Properties.T] = |
|
75 |
{ |
|
76 |
val metadata_file = base_dir + Path.explode("metadata/metadata") |
|
77 |
||
78 |
var result = Map.empty[String, Properties.T] |
|
79 |
var section = "" |
|
80 |
var props = List.empty[Properties.Entry] |
|
81 |
||
82 |
val Section = """^\[(\S+)\]\s*$""".r |
|
83 |
val Property = """^(\S+)\s*=(.*)$""".r |
|
84 |
val Extra_Line = """^\s+(.*)$""".r |
|
85 |
val Blank_Line = """^\s*$""".r |
|
86 |
||
87 |
def flush() |
|
88 |
{ |
|
69978
4ecdd3eaec04
proper treatment of empty extra lines (amending 98a440cfbb2b);
wenzelm
parents:
69977
diff
changeset
|
89 |
if (section != "") result += (section -> props.reverse.filter(p => p._2.nonEmpty)) |
69975 | 90 |
section = "" |
91 |
props = Nil |
|
92 |
} |
|
93 |
||
94 |
for ((line, i) <- split_lines(File.read(metadata_file)).zipWithIndex) |
|
95 |
{ |
|
96 |
def err(msg: String): Nothing = |
|
97 |
error(msg + Position.here(Position.Line_File(i + 1, metadata_file.expand.implode))) |
|
98 |
||
99 |
line match { |
|
100 |
case Section(name) => flush(); section = name |
|
101 |
case Property(a, b) => |
|
102 |
if (section == "") err("Property without a section") |
|
69978
4ecdd3eaec04
proper treatment of empty extra lines (amending 98a440cfbb2b);
wenzelm
parents:
69977
diff
changeset
|
103 |
props = (a -> b.trim) :: props |
69975 | 104 |
case Extra_Line(line) => |
105 |
props match { |
|
106 |
case Nil => err("Extra line without a property") |
|
107 |
case (a, b) :: rest => props = (a, b + "\n" + line.trim) :: rest |
|
108 |
} |
|
109 |
case Blank_Line() => |
|
110 |
case _ => err("Bad input") |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
flush() |
|
115 |
result |
|
116 |
} |
|
117 |
||
118 |
||
69979 | 119 |
/* entries */ |
66821 | 120 |
|
69974 | 121 |
val entries_map: SortedMap[String, AFP.Entry] = |
69973 | 122 |
{ |
123 |
val entries = |
|
69974 | 124 |
for (name <- Sessions.parse_roots(main_dir + Sessions.ROOTS)) yield { |
69973 | 125 |
val metadata = |
126 |
entry_metadata.getOrElse(name, error("Entry without metadata: " + quote(name))) |
|
127 |
val sessions = |
|
128 |
Sessions.parse_root_entries(main_dir + Path.explode(name) + Sessions.ROOT).map(_.name) |
|
129 |
AFP.Entry(name, metadata, sessions) |
|
69974 | 130 |
} |
69973 | 131 |
|
69974 | 132 |
val entries_map = |
133 |
(SortedMap.empty[String, AFP.Entry] /: entries)({ case (m, e) => m + (e.name -> e) }) |
|
134 |
||
69973 | 135 |
val extra_metadata = |
69974 | 136 |
(for ((name, _) <- entry_metadata.iterator if !entries_map.isDefinedAt(name)) yield name). |
137 |
toList.sorted |
|
138 |
if (extra_metadata.nonEmpty) |
|
139 |
error("Meta data without entry: " + commas_quote(extra_metadata)) |
|
69973 | 140 |
|
69974 | 141 |
entries_map |
69973 | 142 |
} |
66820 | 143 |
|
69974 | 144 |
val entries: List[AFP.Entry] = entries_map.toList.map(_._2) |
69979 | 145 |
|
146 |
||
147 |
/* sessions */ |
|
148 |
||
149 |
val sessions_map: SortedMap[String, AFP.Entry] = |
|
150 |
(SortedMap.empty[String, AFP.Entry] /: entries)( |
|
151 |
{ case (m1, e) => (m1 /: e.sessions)({ case (m2, s) => m2 + (s -> e) }) }) |
|
152 |
||
66821 | 153 |
val sessions: List[String] = entries.flatMap(_.sessions) |
66824 | 154 |
|
67052 | 155 |
val sessions_structure: Sessions.Structure = |
67026 | 156 |
Sessions.load_structure(options, dirs = List(main_dir)). |
67025
961285f581e6
clarifified selection: always wrt. build_graph structure;
wenzelm
parents:
66861
diff
changeset
|
157 |
selection(Sessions.Selection(sessions = sessions.toList)) |
66821 | 158 |
|
159 |
||
66824 | 160 |
/* dependency graph */ |
66821 | 161 |
|
66824 | 162 |
private def sessions_deps(entry: AFP.Entry): List[String] = |
163 |
entry.sessions.flatMap(sessions_structure.imports_graph.imm_preds(_)).distinct.sorted |
|
164 |
||
66852
d20a668b394e
entries_graph requires acyclic graph, but lazy val allows forming the AFP object nonetheless;
wenzelm
parents:
66832
diff
changeset
|
165 |
lazy val entries_graph: Graph[String, Unit] = |
66821 | 166 |
{ |
167 |
val session_entries = |
|
66831
29ea2b900a05
tuned: each session has at most one defining entry;
wenzelm
parents:
66824
diff
changeset
|
168 |
(Map.empty[String, String] /: entries) { |
29ea2b900a05
tuned: each session has at most one defining entry;
wenzelm
parents:
66824
diff
changeset
|
169 |
case (m1, e) => (m1 /: e.sessions) { case (m2, s) => m2 + (s -> e.name) } |
66821 | 170 |
} |
66831
29ea2b900a05
tuned: each session has at most one defining entry;
wenzelm
parents:
66824
diff
changeset
|
171 |
(Graph.empty[String, Unit] /: entries) { case (g, entry) => |
29ea2b900a05
tuned: each session has at most one defining entry;
wenzelm
parents:
66824
diff
changeset
|
172 |
val e1 = entry.name |
66832 | 173 |
(g.default_node(e1, ()) /: sessions_deps(entry)) { case (g1, s) => |
174 |
(g1 /: session_entries.get(s).filterNot(_ == e1)) { case (g2, e2) => |
|
66852
d20a668b394e
entries_graph requires acyclic graph, but lazy val allows forming the AFP object nonetheless;
wenzelm
parents:
66832
diff
changeset
|
175 |
try { g2.default_node(e2, ()).add_edge_acyclic(e2, e1) } |
d20a668b394e
entries_graph requires acyclic graph, but lazy val allows forming the AFP object nonetheless;
wenzelm
parents:
66832
diff
changeset
|
176 |
catch { |
d20a668b394e
entries_graph requires acyclic graph, but lazy val allows forming the AFP object nonetheless;
wenzelm
parents:
66832
diff
changeset
|
177 |
case exn: Graph.Cycles[_] => |
d20a668b394e
entries_graph requires acyclic graph, but lazy val allows forming the AFP object nonetheless;
wenzelm
parents:
66832
diff
changeset
|
178 |
error(cat_lines(exn.cycles.map(cycle => |
d20a668b394e
entries_graph requires acyclic graph, but lazy val allows forming the AFP object nonetheless;
wenzelm
parents:
66832
diff
changeset
|
179 |
"Cyclic dependency of " + cycle.map(c => quote(c.toString)).mkString(" via ") + |
d20a668b394e
entries_graph requires acyclic graph, but lazy val allows forming the AFP object nonetheless;
wenzelm
parents:
66832
diff
changeset
|
180 |
" due to session " + quote(s)))) |
66832 | 181 |
} |
66821 | 182 |
} |
183 |
} |
|
66824 | 184 |
} |
66821 | 185 |
} |
186 |
||
66824 | 187 |
def entries_graph_display: Graph_Display.Graph = |
188 |
Graph_Display.make_graph(entries_graph) |
|
66823 | 189 |
|
66824 | 190 |
def entries_json_text: String = |
191 |
(for (entry <- entries.iterator) yield { |
|
192 |
val distrib_deps = sessions_deps(entry).filterNot(sessions.contains(_)) |
|
193 |
val afp_deps = entries_graph.imm_preds(entry.name).toList |
|
194 |
""" |
|
66821 | 195 |
{""" + JSON.Format(entry.name) + """: |
196 |
{"distrib_deps": """ + JSON.Format(distrib_deps) + """, |
|
197 |
"afp_deps": """ + JSON.Format(afp_deps) + """ |
|
198 |
} |
|
199 |
}""" |
|
66824 | 200 |
}).mkString("[", ", ", "\n]\n") |
66861
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
201 |
|
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
202 |
|
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
203 |
/* partition sessions */ |
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
204 |
|
67817 | 205 |
val force_partition1: List[String] = List("Category3", "HOL-ODE") |
67776 | 206 |
|
66861
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
207 |
def partition(n: Int): List[String] = |
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
208 |
n match { |
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
209 |
case 0 => Nil |
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
210 |
case 1 | 2 => |
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
211 |
val graph = sessions_structure.build_graph.restrict(sessions.toSet) |
67817 | 212 |
val force_part1 = |
213 |
graph.all_preds(graph.all_succs(force_partition1.filter(graph.defined(_)))).toSet |
|
214 |
val (part1, part2) = graph.keys.partition(a => force_part1(a) || graph.is_isolated(a)) |
|
215 |
if (n == 1) part1 else part2 |
|
66861
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
216 |
case _ => error("Bad AFP partition: " + n + " (should be 0, 1, 2)") |
f6676691ef8a
partition AFP sessions according to structure, which happens to cut it roughly into equal parts;
wenzelm
parents:
66854
diff
changeset
|
217 |
} |
66820 | 218 |
} |