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