src/Pure/Thy/sessions.scala
author wenzelm
Tue Mar 15 23:16:15 2016 +0100 (2016-03-15 ago)
changeset 62632 cd991ba01ffd
parent 62631 c39614ddb80b
child 62633 e57416b649d5
permissions -rw-r--r--
clarified modules;
wenzelm@62631
     1
/*  Title:      Pure/Thy/sessions.scala
wenzelm@62631
     2
    Author:     Makarius
wenzelm@62631
     3
wenzelm@62631
     4
Session information.
wenzelm@62631
     5
*/
wenzelm@62631
     6
wenzelm@62631
     7
package isabelle
wenzelm@62631
     8
wenzelm@62631
     9
wenzelm@62631
    10
import scala.collection.SortedSet
wenzelm@62631
    11
import scala.collection.mutable
wenzelm@62631
    12
wenzelm@62631
    13
wenzelm@62631
    14
object Sessions
wenzelm@62631
    15
{
wenzelm@62631
    16
  /* info */
wenzelm@62631
    17
wenzelm@62631
    18
  val ROOT = Path.explode("ROOT")
wenzelm@62631
    19
  val ROOTS = Path.explode("ROOTS")
wenzelm@62631
    20
wenzelm@62631
    21
  def is_pure(name: String): Boolean = name == "Pure"
wenzelm@62631
    22
wenzelm@62631
    23
  sealed case class Info(
wenzelm@62631
    24
    chapter: String,
wenzelm@62631
    25
    select: Boolean,
wenzelm@62631
    26
    pos: Position.T,
wenzelm@62631
    27
    groups: List[String],
wenzelm@62631
    28
    dir: Path,
wenzelm@62631
    29
    parent: Option[String],
wenzelm@62631
    30
    description: String,
wenzelm@62631
    31
    options: Options,
wenzelm@62631
    32
    theories: List[(Boolean, Options, List[Path])],
wenzelm@62631
    33
    files: List[Path],
wenzelm@62631
    34
    document_files: List[(Path, Path)],
wenzelm@62631
    35
    meta_digest: SHA1.Digest)
wenzelm@62631
    36
  {
wenzelm@62631
    37
    def timeout: Time = Time.seconds(options.real("timeout") * options.real("timeout_scale"))
wenzelm@62631
    38
  }
wenzelm@62631
    39
wenzelm@62631
    40
wenzelm@62631
    41
  /* session tree */
wenzelm@62631
    42
wenzelm@62631
    43
  object Tree
wenzelm@62631
    44
  {
wenzelm@62631
    45
    def apply(infos: Seq[(String, Info)]): Tree =
wenzelm@62631
    46
    {
wenzelm@62631
    47
      val graph1 =
wenzelm@62631
    48
        (Graph.string[Info] /: infos) {
wenzelm@62631
    49
          case (graph, (name, info)) =>
wenzelm@62631
    50
            if (graph.defined(name))
wenzelm@62631
    51
              error("Duplicate session " + quote(name) + Position.here(info.pos) +
wenzelm@62631
    52
                Position.here(graph.get_node(name).pos))
wenzelm@62631
    53
            else graph.new_node(name, info)
wenzelm@62631
    54
        }
wenzelm@62631
    55
      val graph2 =
wenzelm@62631
    56
        (graph1 /: graph1.iterator) {
wenzelm@62631
    57
          case (graph, (name, (info, _))) =>
wenzelm@62631
    58
            info.parent match {
wenzelm@62631
    59
              case None => graph
wenzelm@62631
    60
              case Some(parent) =>
wenzelm@62631
    61
                if (!graph.defined(parent))
wenzelm@62631
    62
                  error("Bad parent session " + quote(parent) + " for " +
wenzelm@62631
    63
                    quote(name) + Position.here(info.pos))
wenzelm@62631
    64
wenzelm@62631
    65
                try { graph.add_edge_acyclic(parent, name) }
wenzelm@62631
    66
                catch {
wenzelm@62631
    67
                  case exn: Graph.Cycles[_] =>
wenzelm@62631
    68
                    error(cat_lines(exn.cycles.map(cycle =>
wenzelm@62631
    69
                      "Cyclic session dependency of " +
wenzelm@62631
    70
                        cycle.map(c => quote(c.toString)).mkString(" via "))) +
wenzelm@62631
    71
                          Position.here(info.pos))
wenzelm@62631
    72
                }
wenzelm@62631
    73
            }
wenzelm@62631
    74
        }
wenzelm@62631
    75
      new Tree(graph2)
wenzelm@62631
    76
    }
wenzelm@62631
    77
  }
wenzelm@62631
    78
wenzelm@62631
    79
  final class Tree private(val graph: Graph[String, Info])
wenzelm@62631
    80
    extends PartialFunction[String, Info]
wenzelm@62631
    81
  {
wenzelm@62631
    82
    def apply(name: String): Info = graph.get_node(name)
wenzelm@62631
    83
    def isDefinedAt(name: String): Boolean = graph.defined(name)
wenzelm@62631
    84
wenzelm@62631
    85
    def selection(
wenzelm@62631
    86
      requirements: Boolean = false,
wenzelm@62631
    87
      all_sessions: Boolean = false,
wenzelm@62631
    88
      exclude_session_groups: List[String] = Nil,
wenzelm@62631
    89
      exclude_sessions: List[String] = Nil,
wenzelm@62631
    90
      session_groups: List[String] = Nil,
wenzelm@62631
    91
      sessions: List[String] = Nil): (List[String], Tree) =
wenzelm@62631
    92
    {
wenzelm@62631
    93
      val bad_sessions =
wenzelm@62631
    94
        SortedSet((exclude_sessions ::: sessions).filterNot(isDefinedAt(_)): _*).toList
wenzelm@62631
    95
      if (bad_sessions.nonEmpty) error("Undefined session(s): " + commas_quote(bad_sessions))
wenzelm@62631
    96
wenzelm@62631
    97
      val excluded =
wenzelm@62631
    98
      {
wenzelm@62631
    99
        val exclude_group = exclude_session_groups.toSet
wenzelm@62631
   100
        val exclude_group_sessions =
wenzelm@62631
   101
          (for {
wenzelm@62631
   102
            (name, (info, _)) <- graph.iterator
wenzelm@62631
   103
            if apply(name).groups.exists(exclude_group)
wenzelm@62631
   104
          } yield name).toList
wenzelm@62631
   105
        graph.all_succs(exclude_group_sessions ::: exclude_sessions).toSet
wenzelm@62631
   106
      }
wenzelm@62631
   107
wenzelm@62631
   108
      val pre_selected =
wenzelm@62631
   109
      {
wenzelm@62631
   110
        if (all_sessions) graph.keys
wenzelm@62631
   111
        else {
wenzelm@62631
   112
          val select_group = session_groups.toSet
wenzelm@62631
   113
          val select = sessions.toSet
wenzelm@62631
   114
          (for {
wenzelm@62631
   115
            (name, (info, _)) <- graph.iterator
wenzelm@62631
   116
            if info.select || select(name) || apply(name).groups.exists(select_group)
wenzelm@62631
   117
          } yield name).toList
wenzelm@62631
   118
        }
wenzelm@62631
   119
      }.filterNot(excluded)
wenzelm@62631
   120
wenzelm@62631
   121
      val selected =
wenzelm@62631
   122
        if (requirements) (graph.all_preds(pre_selected).toSet -- pre_selected).toList
wenzelm@62631
   123
        else pre_selected
wenzelm@62631
   124
wenzelm@62631
   125
      val graph1 = graph.restrict(graph.all_preds(selected).toSet)
wenzelm@62631
   126
      (selected, new Tree(graph1))
wenzelm@62631
   127
    }
wenzelm@62631
   128
wenzelm@62631
   129
    def ancestors(name: String): List[String] =
wenzelm@62631
   130
      graph.all_preds(List(name)).tail.reverse
wenzelm@62631
   131
wenzelm@62631
   132
    def topological_order: List[(String, Info)] =
wenzelm@62631
   133
      graph.topological_order.map(name => (name, apply(name)))
wenzelm@62631
   134
wenzelm@62631
   135
    override def toString: String = graph.keys_iterator.mkString("Sessions.Tree(", ", ", ")")
wenzelm@62631
   136
  }
wenzelm@62631
   137
wenzelm@62631
   138
wenzelm@62631
   139
  /* parser */
wenzelm@62631
   140
wenzelm@62631
   141
  private val CHAPTER = "chapter"
wenzelm@62631
   142
  private val SESSION = "session"
wenzelm@62631
   143
  private val IN = "in"
wenzelm@62631
   144
  private val DESCRIPTION = "description"
wenzelm@62631
   145
  private val OPTIONS = "options"
wenzelm@62631
   146
  private val GLOBAL_THEORIES = "global_theories"
wenzelm@62631
   147
  private val THEORIES = "theories"
wenzelm@62631
   148
  private val FILES = "files"
wenzelm@62631
   149
  private val DOCUMENT_FILES = "document_files"
wenzelm@62631
   150
wenzelm@62631
   151
  lazy val root_syntax =
wenzelm@62631
   152
    Outer_Syntax.init() + "(" + ")" + "+" + "," + "=" + "[" + "]" +
wenzelm@62631
   153
      (CHAPTER, Keyword.THY_DECL) + (SESSION, Keyword.THY_DECL) +
wenzelm@62631
   154
      IN + DESCRIPTION + OPTIONS + GLOBAL_THEORIES + THEORIES + FILES + DOCUMENT_FILES
wenzelm@62631
   155
wenzelm@62631
   156
  object Parser extends Parse.Parser
wenzelm@62631
   157
  {
wenzelm@62631
   158
    private abstract class Entry
wenzelm@62631
   159
    private sealed case class Chapter(name: String) extends Entry
wenzelm@62631
   160
    private sealed case class Session_Entry(
wenzelm@62631
   161
      pos: Position.T,
wenzelm@62631
   162
      name: String,
wenzelm@62631
   163
      groups: List[String],
wenzelm@62631
   164
      path: String,
wenzelm@62631
   165
      parent: Option[String],
wenzelm@62631
   166
      description: String,
wenzelm@62631
   167
      options: List[Options.Spec],
wenzelm@62631
   168
      theories: List[(Boolean, List[Options.Spec], List[String])],
wenzelm@62631
   169
      files: List[String],
wenzelm@62631
   170
      document_files: List[(String, String)]) extends Entry
wenzelm@62631
   171
wenzelm@62631
   172
    private val chapter: Parser[Chapter] =
wenzelm@62631
   173
    {
wenzelm@62631
   174
      val chapter_name = atom("chapter name", _.is_name)
wenzelm@62631
   175
wenzelm@62631
   176
      command(CHAPTER) ~! chapter_name ^^ { case _ ~ a => Chapter(a) }
wenzelm@62631
   177
    }
wenzelm@62631
   178
wenzelm@62631
   179
    private val session_entry: Parser[Session_Entry] =
wenzelm@62631
   180
    {
wenzelm@62631
   181
      val session_name = atom("session name", _.is_name)
wenzelm@62631
   182
wenzelm@62631
   183
      val option =
wenzelm@62631
   184
        name ~ opt($$$("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
wenzelm@62631
   185
      val options = $$$("[") ~> rep1sep(option, $$$(",")) <~ $$$("]")
wenzelm@62631
   186
wenzelm@62631
   187
      val theories =
wenzelm@62631
   188
        ($$$(GLOBAL_THEORIES) | $$$(THEORIES)) ~!
wenzelm@62631
   189
          ((options | success(Nil)) ~ rep(theory_xname)) ^^
wenzelm@62631
   190
          { case x ~ (y ~ z) => (x == GLOBAL_THEORIES, y, z) }
wenzelm@62631
   191
wenzelm@62631
   192
      val document_files =
wenzelm@62631
   193
        $$$(DOCUMENT_FILES) ~!
wenzelm@62631
   194
          (($$$("(") ~! ($$$(IN) ~! (path ~ $$$(")"))) ^^
wenzelm@62631
   195
              { case _ ~ (_ ~ (x ~ _)) => x } | success("document")) ~
wenzelm@62631
   196
            rep1(path)) ^^ { case _ ~ (x ~ y) => y.map((x, _)) }
wenzelm@62631
   197
wenzelm@62631
   198
      command(SESSION) ~!
wenzelm@62631
   199
        (position(session_name) ~
wenzelm@62631
   200
          (($$$("(") ~! (rep1(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   201
          (($$$(IN) ~! path ^^ { case _ ~ x => x }) | success(".")) ~
wenzelm@62631
   202
          ($$$("=") ~!
wenzelm@62631
   203
            (opt(session_name ~! $$$("+") ^^ { case x ~ _ => x }) ~
wenzelm@62631
   204
              (($$$(DESCRIPTION) ~! text ^^ { case _ ~ x => x }) | success("")) ~
wenzelm@62631
   205
              (($$$(OPTIONS) ~! options ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   206
              rep1(theories) ~
wenzelm@62631
   207
              (($$$(FILES) ~! rep1(path) ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   208
              (rep(document_files) ^^ (x => x.flatten))))) ^^
wenzelm@62631
   209
        { case _ ~ ((a, pos) ~ b ~ c ~ (_ ~ (d ~ e ~ f ~ g ~ h ~ i))) =>
wenzelm@62631
   210
            Session_Entry(pos, a, b, c, d, e, f, g, h, i) }
wenzelm@62631
   211
    }
wenzelm@62631
   212
wenzelm@62631
   213
    def parse(options: Options, select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   214
    {
wenzelm@62631
   215
      def make_info(entry_chapter: String, entry: Session_Entry): (String, Info) =
wenzelm@62631
   216
      {
wenzelm@62631
   217
        try {
wenzelm@62631
   218
          val name = entry.name
wenzelm@62631
   219
wenzelm@62631
   220
          if (name == "") error("Bad session name")
wenzelm@62631
   221
          if (is_pure(name) && entry.parent.isDefined) error("Illegal parent session")
wenzelm@62631
   222
          if (!is_pure(name) && !entry.parent.isDefined) error("Missing parent session")
wenzelm@62631
   223
wenzelm@62631
   224
          val session_options = options ++ entry.options
wenzelm@62631
   225
wenzelm@62631
   226
          val theories =
wenzelm@62631
   227
            entry.theories.map({ case (global, opts, thys) =>
wenzelm@62631
   228
              (global, session_options ++ opts, thys.map(Path.explode(_))) })
wenzelm@62631
   229
          val files = entry.files.map(Path.explode(_))
wenzelm@62631
   230
          val document_files =
wenzelm@62631
   231
            entry.document_files.map({ case (s1, s2) => (Path.explode(s1), Path.explode(s2)) })
wenzelm@62631
   232
wenzelm@62631
   233
          val meta_digest =
wenzelm@62631
   234
            SHA1.digest((entry_chapter, name, entry.parent, entry.options,
wenzelm@62631
   235
              entry.theories, entry.files, entry.document_files).toString)
wenzelm@62631
   236
wenzelm@62631
   237
          val info =
wenzelm@62631
   238
            Info(entry_chapter, select, entry.pos, entry.groups, dir + Path.explode(entry.path),
wenzelm@62631
   239
              entry.parent, entry.description, session_options, theories, files,
wenzelm@62631
   240
              document_files, meta_digest)
wenzelm@62631
   241
wenzelm@62631
   242
          (name, info)
wenzelm@62631
   243
        }
wenzelm@62631
   244
        catch {
wenzelm@62631
   245
          case ERROR(msg) =>
wenzelm@62631
   246
            error(msg + "\nThe error(s) above occurred in session entry " +
wenzelm@62631
   247
              quote(entry.name) + Position.here(entry.pos))
wenzelm@62631
   248
        }
wenzelm@62631
   249
      }
wenzelm@62631
   250
wenzelm@62631
   251
      val root = dir + ROOT
wenzelm@62631
   252
      if (root.is_file) {
wenzelm@62631
   253
        val toks = Token.explode(root_syntax.keywords, File.read(root))
wenzelm@62631
   254
        val start = Token.Pos.file(root.implode)
wenzelm@62631
   255
wenzelm@62631
   256
        parse_all(rep(chapter | session_entry), Token.reader(toks, start)) match {
wenzelm@62631
   257
          case Success(result, _) =>
wenzelm@62631
   258
            var entry_chapter = "Unsorted"
wenzelm@62631
   259
            val infos = new mutable.ListBuffer[(String, Info)]
wenzelm@62631
   260
            result.foreach {
wenzelm@62631
   261
              case Chapter(name) => entry_chapter = name
wenzelm@62631
   262
              case entry: Session_Entry => infos += make_info(entry_chapter, entry)
wenzelm@62631
   263
            }
wenzelm@62631
   264
            infos.toList
wenzelm@62631
   265
          case bad => error(bad.toString)
wenzelm@62631
   266
        }
wenzelm@62631
   267
      }
wenzelm@62631
   268
      else Nil
wenzelm@62631
   269
    }
wenzelm@62631
   270
  }
wenzelm@62631
   271
wenzelm@62631
   272
wenzelm@62631
   273
  /* find sessions within certain directories */
wenzelm@62631
   274
wenzelm@62631
   275
  private def is_session_dir(dir: Path): Boolean =
wenzelm@62631
   276
    (dir + ROOT).is_file || (dir + ROOTS).is_file
wenzelm@62631
   277
wenzelm@62631
   278
  private def check_session_dir(dir: Path): Path =
wenzelm@62631
   279
    if (is_session_dir(dir)) dir
wenzelm@62631
   280
    else error("Bad session root directory: " + dir.toString)
wenzelm@62631
   281
wenzelm@62631
   282
  def find(options: Options, dirs: List[Path] = Nil, select_dirs: List[Path] = Nil): Tree =
wenzelm@62631
   283
  {
wenzelm@62631
   284
    def find_dir(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   285
      find_root(select, dir) ::: find_roots(select, dir)
wenzelm@62631
   286
wenzelm@62631
   287
    def find_root(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   288
      Parser.parse(options, select, dir)
wenzelm@62631
   289
wenzelm@62631
   290
    def find_roots(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   291
    {
wenzelm@62631
   292
      val roots = dir + ROOTS
wenzelm@62631
   293
      if (roots.is_file) {
wenzelm@62631
   294
        for {
wenzelm@62631
   295
          line <- split_lines(File.read(roots))
wenzelm@62631
   296
          if !(line == "" || line.startsWith("#"))
wenzelm@62631
   297
          dir1 =
wenzelm@62631
   298
            try { check_session_dir(dir + Path.explode(line)) }
wenzelm@62631
   299
            catch {
wenzelm@62631
   300
              case ERROR(msg) =>
wenzelm@62631
   301
                error(msg + "\nThe error(s) above occurred in session catalog " + roots.toString)
wenzelm@62631
   302
            }
wenzelm@62631
   303
          info <- find_dir(select, dir1)
wenzelm@62631
   304
        } yield info
wenzelm@62631
   305
      }
wenzelm@62631
   306
      else Nil
wenzelm@62631
   307
    }
wenzelm@62631
   308
wenzelm@62631
   309
    val default_dirs = Isabelle_System.components().filter(is_session_dir(_))
wenzelm@62631
   310
    dirs.foreach(check_session_dir(_))
wenzelm@62631
   311
    select_dirs.foreach(check_session_dir(_))
wenzelm@62631
   312
wenzelm@62631
   313
    Tree(
wenzelm@62631
   314
      for {
wenzelm@62631
   315
        (select, dir) <- (default_dirs ::: dirs).map((false, _)) ::: select_dirs.map((true, _))
wenzelm@62631
   316
        info <- find_dir(select, dir)
wenzelm@62631
   317
      } yield info)
wenzelm@62631
   318
  }
wenzelm@62632
   319
wenzelm@62632
   320
wenzelm@62632
   321
  /* persistent store */
wenzelm@62632
   322
wenzelm@62632
   323
  def log(name: String): Path = Path.basic("log") + Path.basic(name)
wenzelm@62632
   324
  def log_gz(name: String): Path = log(name).ext("gz")
wenzelm@62632
   325
wenzelm@62632
   326
  def store(system_mode: Boolean = false): Store = new Store(system_mode)
wenzelm@62632
   327
wenzelm@62632
   328
  class Store private [Sessions](system_mode: Boolean)
wenzelm@62632
   329
  {
wenzelm@62632
   330
    val output_dir: Path =
wenzelm@62632
   331
      if (system_mode) Path.explode("~~/heaps/$ML_IDENTIFIER")
wenzelm@62632
   332
      else Path.explode("$ISABELLE_OUTPUT")
wenzelm@62632
   333
wenzelm@62632
   334
    val browser_info: Path =
wenzelm@62632
   335
      if (system_mode) Path.explode("~~/browser_info")
wenzelm@62632
   336
      else Path.explode("$ISABELLE_BROWSER_INFO")
wenzelm@62632
   337
wenzelm@62632
   338
    private val input_dirs =
wenzelm@62632
   339
      if (system_mode) List(output_dir)
wenzelm@62632
   340
      else output_dir :: Isabelle_System.find_logics_dirs()
wenzelm@62632
   341
wenzelm@62632
   342
    def find(name: String): Option[(Path, Path)] =
wenzelm@62632
   343
      input_dirs.find(dir => (dir + log_gz(name)).is_file).map(dir =>
wenzelm@62632
   344
        (dir + Path.basic(name), dir + log_gz(name)))
wenzelm@62632
   345
wenzelm@62632
   346
    def find_log(name: String): Option[Path] = input_dirs.map(_ + log(name)).find(_.is_file)
wenzelm@62632
   347
    def find_log_gz(name: String): Option[Path] = input_dirs.map(_ + log_gz(name)).find(_.is_file)
wenzelm@62632
   348
wenzelm@62632
   349
    def prepare_output() { Isabelle_System.mkdirs(output_dir + Path.basic("log")) }
wenzelm@62632
   350
  }
wenzelm@62631
   351
}