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