src/Pure/Admin/afp.scala
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (2 months ago)
changeset 69981 3dced198b9ec
parent 69980 f2e3adfd916f
child 69982 f150253cb201
permissions -rw-r--r--
more strict AFP properties;
     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 
    10 import java.time.LocalDate
    11 import scala.collection.immutable.SortedMap
    12 
    13 
    14 object AFP
    15 {
    16   val repos_source = "https://isabelle.sketis.net/repos/afp-devel"
    17 
    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 
    25   def init(options: Options, base_dir: Path = Path.explode("$AFP_BASE")): AFP =
    26     new AFP(options, base_dir)
    27 
    28 
    29   /* entries */
    30 
    31   def parse_date(s: String): Date =
    32   {
    33     val t = Date.Formatter.pattern("uuuu-MM-dd").parse(s)
    34     Date(LocalDate.from(t).atStartOfDay(Date.timezone_berlin))
    35   }
    36 
    37   sealed case class Entry(name: String, metadata: Properties.T, sessions: List[String])
    38   {
    39     def get(prop: String): Option[String] = Properties.get(metadata, prop)
    40     def get_string(prop: String): String = get(prop).getOrElse("")
    41     def get_strings(prop: String): List[String] =
    42       Library.space_explode(',', get_string(prop)).map(_.trim).filter(_.nonEmpty)
    43 
    44     def title: String = get_string("title")
    45     def authors: List[String] = get_strings("author")
    46     def date: Date =
    47       parse_date(get("date").getOrElse(error("Missing date for entry " + quote(name))))
    48     def topics: List[String] = get_strings("topic")
    49     def `abstract`: String = get_string("abstract").trim
    50     def maintainers: List[String] = get_strings("notify")
    51     def contributors: List[String] = get_strings("contributors")
    52     def license: String = get("license").getOrElse("BSD")
    53   }
    54 }
    55 
    56 class AFP private(options: Options, val base_dir: Path)
    57 {
    58   override def toString: String = base_dir.expand.toString
    59 
    60   val main_dir: Path = base_dir + Path.explode("thys")
    61 
    62 
    63   /* metadata */
    64 
    65   private val entry_metadata: Map[String, Properties.T] =
    66   {
    67     val metadata_file = base_dir + Path.explode("metadata/metadata")
    68 
    69     var result = Map.empty[String, Properties.T]
    70     var section = ""
    71     var props = List.empty[Properties.Entry]
    72 
    73     val Section = """^\[(\S+)\]\s*$""".r
    74     val Property = """^(\S+)\s*=(.*)$""".r
    75     val Extra_Line = """^\s+(.*)$""".r
    76     val Blank_Line = """^\s*$""".r
    77 
    78     def flush()
    79     {
    80       if (section != "") result += (section -> props.reverse.filter(p => p._2.nonEmpty))
    81       section = ""
    82       props = Nil
    83     }
    84 
    85     for ((line, i) <- split_lines(File.read(metadata_file)).zipWithIndex)
    86     {
    87       def err(msg: String): Nothing =
    88         error(msg + Position.here(Position.Line_File(i + 1, metadata_file.expand.implode)))
    89 
    90       line match {
    91         case Section(name) => flush(); section = name
    92         case Property(a, b) =>
    93           if (section == "") err("Property without a section")
    94           props = (a -> b.trim) :: props
    95         case Extra_Line(line) =>
    96           props match {
    97             case Nil => err("Extra line without a property")
    98             case (a, b) :: rest => props = (a, b + "\n" + line.trim) :: rest
    99           }
   100         case Blank_Line() =>
   101         case _ => err("Bad input")
   102       }
   103     }
   104 
   105     flush()
   106     result
   107   }
   108 
   109 
   110   /* entries */
   111 
   112   val entries_map: SortedMap[String, AFP.Entry] =
   113   {
   114     val entries =
   115       for (name <- Sessions.parse_roots(main_dir + Sessions.ROOTS)) yield {
   116         val metadata =
   117           entry_metadata.getOrElse(name, error("Entry without metadata: " + quote(name)))
   118         val sessions =
   119           Sessions.parse_root_entries(main_dir + Path.explode(name) + Sessions.ROOT).map(_.name)
   120         AFP.Entry(name, metadata, sessions)
   121       }
   122 
   123     val entries_map =
   124       (SortedMap.empty[String, AFP.Entry] /: entries)({ case (m, e) => m + (e.name -> e) })
   125 
   126     val extra_metadata =
   127       (for ((name, _) <- entry_metadata.iterator if !entries_map.isDefinedAt(name)) yield name).
   128         toList.sorted
   129     if (extra_metadata.nonEmpty)
   130       error("Meta data without entry: " + commas_quote(extra_metadata))
   131 
   132     entries_map
   133   }
   134 
   135   val entries: List[AFP.Entry] = entries_map.toList.map(_._2)
   136 
   137 
   138   /* sessions */
   139 
   140   val sessions_map: SortedMap[String, AFP.Entry] =
   141     (SortedMap.empty[String, AFP.Entry] /: entries)(
   142       { case (m1, e) => (m1 /: e.sessions)({ case (m2, s) => m2 + (s -> e) }) })
   143 
   144   val sessions: List[String] = entries.flatMap(_.sessions)
   145 
   146   val sessions_structure: Sessions.Structure =
   147     Sessions.load_structure(options, dirs = List(main_dir)).
   148       selection(Sessions.Selection(sessions = sessions.toList))
   149 
   150 
   151   /* dependency graph */
   152 
   153   private def sessions_deps(entry: AFP.Entry): List[String] =
   154     entry.sessions.flatMap(sessions_structure.imports_graph.imm_preds(_)).distinct.sorted
   155 
   156   lazy val entries_graph: Graph[String, Unit] =
   157   {
   158     val session_entries =
   159       (Map.empty[String, String] /: entries) {
   160         case (m1, e) => (m1 /: e.sessions) { case (m2, s) => m2 + (s -> e.name) }
   161       }
   162     (Graph.empty[String, Unit] /: entries) { case (g, entry) =>
   163       val e1 = entry.name
   164       (g.default_node(e1, ()) /: sessions_deps(entry)) { case (g1, s) =>
   165         (g1 /: session_entries.get(s).filterNot(_ == e1)) { case (g2, e2) =>
   166           try { g2.default_node(e2, ()).add_edge_acyclic(e2, e1) }
   167           catch {
   168             case exn: Graph.Cycles[_] =>
   169               error(cat_lines(exn.cycles.map(cycle =>
   170                 "Cyclic dependency of " + cycle.map(c => quote(c.toString)).mkString(" via ") +
   171                 " due to session " + quote(s))))
   172           }
   173         }
   174       }
   175     }
   176   }
   177 
   178   def entries_graph_display: Graph_Display.Graph =
   179     Graph_Display.make_graph(entries_graph)
   180 
   181   def entries_json_text: String =
   182     (for (entry <- entries.iterator) yield {
   183       val distrib_deps = sessions_deps(entry).filterNot(sessions.contains(_))
   184       val afp_deps = entries_graph.imm_preds(entry.name).toList
   185       """
   186  {""" + JSON.Format(entry.name) + """:
   187   {"distrib_deps": """ + JSON.Format(distrib_deps) + """,
   188    "afp_deps": """ + JSON.Format(afp_deps) + """
   189   }
   190  }"""
   191     }).mkString("[", ", ", "\n]\n")
   192 
   193 
   194   /* partition sessions */
   195 
   196   val force_partition1: List[String] = List("Category3", "HOL-ODE")
   197 
   198   def partition(n: Int): List[String] =
   199     n match {
   200       case 0 => Nil
   201       case 1 | 2 =>
   202         val graph = sessions_structure.build_graph.restrict(sessions.toSet)
   203         val force_part1 =
   204           graph.all_preds(graph.all_succs(force_partition1.filter(graph.defined(_)))).toSet
   205         val (part1, part2) = graph.keys.partition(a => force_part1(a) || graph.is_isolated(a))
   206         if (n == 1) part1 else part2
   207       case _ => error("Bad AFP partition: " + n + " (should be 0, 1, 2)")
   208     }
   209 }