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