src/Pure/Thy/sessions.scala
author wenzelm
Wed, 06 Apr 2016 11:37:37 +0200
changeset 62883 b04e9fe29223
parent 62864 2d5959cf3c1a
child 62902 3c0f53eae166
permissions -rw-r--r--
clarified ML bootstrap;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/sessions.scala
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     3
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     4
Session information.
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     5
*/
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     6
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
     8
62704
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
     9
import java.nio.ByteBuffer
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
    10
import java.nio.channels.FileChannel
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
    11
import java.nio.file.StandardOpenOption
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    12
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    13
import scala.collection.SortedSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    14
import scala.collection.mutable
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    15
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    16
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    17
object Sessions
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    18
{
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    19
  /* Pure */
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    20
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    21
  def pure_name(name: String): Boolean = name == "Pure"
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    22
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    23
  val pure_roots: List[Path] = List("ROOT0.ML", "ROOT.ML", "ROOT1.ML").map(Path.explode(_))
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    24
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    25
  def pure_files(dir: Path): List[Path] =
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    26
    (pure_roots ::: pure_roots.flatMap(root => ML_Root.read_files(dir + root))).map(dir + _)
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    27
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    28
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
    29
  /* info */
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    30
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    31
  sealed case class Info(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    32
    chapter: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    33
    select: Boolean,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    34
    pos: Position.T,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    35
    groups: List[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    36
    dir: Path,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    37
    parent: Option[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    38
    description: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    39
    options: Options,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    40
    theories: List[(Boolean, Options, List[Path])],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    41
    files: List[Path],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    42
    document_files: List[(Path, Path)],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    43
    meta_digest: SHA1.Digest)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    44
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    45
    def timeout: Time = Time.seconds(options.real("timeout") * options.real("timeout_scale"))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    46
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    47
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    48
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    49
  /* session tree */
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    50
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    51
  object Tree
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    52
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    53
    def apply(infos: Seq[(String, Info)]): Tree =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    54
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    55
      val graph1 =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    56
        (Graph.string[Info] /: infos) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    57
          case (graph, (name, info)) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    58
            if (graph.defined(name))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    59
              error("Duplicate session " + quote(name) + Position.here(info.pos) +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    60
                Position.here(graph.get_node(name).pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    61
            else graph.new_node(name, info)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    62
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    63
      val graph2 =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    64
        (graph1 /: graph1.iterator) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    65
          case (graph, (name, (info, _))) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    66
            info.parent match {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    67
              case None => graph
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    68
              case Some(parent) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    69
                if (!graph.defined(parent))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    70
                  error("Bad parent session " + quote(parent) + " for " +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    71
                    quote(name) + Position.here(info.pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    72
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    73
                try { graph.add_edge_acyclic(parent, name) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    74
                catch {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    75
                  case exn: Graph.Cycles[_] =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    76
                    error(cat_lines(exn.cycles.map(cycle =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    77
                      "Cyclic session dependency of " +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    78
                        cycle.map(c => quote(c.toString)).mkString(" via "))) +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    79
                          Position.here(info.pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    80
                }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    81
            }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    82
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    83
      new Tree(graph2)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    84
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    85
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    86
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    87
  final class Tree private(val graph: Graph[String, Info])
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    88
    extends PartialFunction[String, Info]
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    89
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    90
    def apply(name: String): Info = graph.get_node(name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    91
    def isDefinedAt(name: String): Boolean = graph.defined(name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    92
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    93
    def selection(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    94
      requirements: Boolean = false,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    95
      all_sessions: Boolean = false,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    96
      exclude_session_groups: List[String] = Nil,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    97
      exclude_sessions: List[String] = Nil,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    98
      session_groups: List[String] = Nil,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
    99
      sessions: List[String] = Nil): (List[String], Tree) =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   100
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   101
      val bad_sessions =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   102
        SortedSet((exclude_sessions ::: sessions).filterNot(isDefinedAt(_)): _*).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   103
      if (bad_sessions.nonEmpty) error("Undefined session(s): " + commas_quote(bad_sessions))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   104
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   105
      val excluded =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   106
      {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   107
        val exclude_group = exclude_session_groups.toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   108
        val exclude_group_sessions =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   109
          (for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   110
            (name, (info, _)) <- graph.iterator
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   111
            if apply(name).groups.exists(exclude_group)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   112
          } yield name).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   113
        graph.all_succs(exclude_group_sessions ::: exclude_sessions).toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   114
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   115
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   116
      val pre_selected =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   117
      {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   118
        if (all_sessions) graph.keys
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   119
        else {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   120
          val select_group = session_groups.toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   121
          val select = sessions.toSet
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   122
          (for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   123
            (name, (info, _)) <- graph.iterator
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   124
            if info.select || select(name) || apply(name).groups.exists(select_group)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   125
          } yield name).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   126
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   127
      }.filterNot(excluded)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   128
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   129
      val selected =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   130
        if (requirements) (graph.all_preds(pre_selected).toSet -- pre_selected).toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   131
        else pre_selected
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   132
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   133
      val graph1 = graph.restrict(graph.all_preds(selected).toSet)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   134
      (selected, new Tree(graph1))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   135
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   136
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   137
    def ancestors(name: String): List[String] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   138
      graph.all_preds(List(name)).tail.reverse
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   139
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   140
    def topological_order: List[(String, Info)] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   141
      graph.topological_order.map(name => (name, apply(name)))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   142
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   143
    override def toString: String = graph.keys_iterator.mkString("Sessions.Tree(", ", ", ")")
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   144
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   145
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   146
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   147
  /* parser */
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   148
62864
wenzelm
parents: 62769
diff changeset
   149
  val ROOT = Path.explode("ROOT")
wenzelm
parents: 62769
diff changeset
   150
  val ROOTS = Path.explode("ROOTS")
wenzelm
parents: 62769
diff changeset
   151
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   152
  private val CHAPTER = "chapter"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   153
  private val SESSION = "session"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   154
  private val IN = "in"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   155
  private val DESCRIPTION = "description"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   156
  private val OPTIONS = "options"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   157
  private val GLOBAL_THEORIES = "global_theories"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   158
  private val THEORIES = "theories"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   159
  private val FILES = "files"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   160
  private val DOCUMENT_FILES = "document_files"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   161
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   162
  lazy val root_syntax =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   163
    Outer_Syntax.init() + "(" + ")" + "+" + "," + "=" + "[" + "]" +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   164
      (CHAPTER, Keyword.THY_DECL) + (SESSION, Keyword.THY_DECL) +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   165
      IN + DESCRIPTION + OPTIONS + GLOBAL_THEORIES + THEORIES + FILES + DOCUMENT_FILES
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   166
62864
wenzelm
parents: 62769
diff changeset
   167
  private object Parser extends Parse.Parser
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   168
  {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   169
    private abstract class Entry
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   170
    private sealed case class Chapter(name: String) extends Entry
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   171
    private sealed case class Session_Entry(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   172
      pos: Position.T,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   173
      name: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   174
      groups: List[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   175
      path: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   176
      parent: Option[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   177
      description: String,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   178
      options: List[Options.Spec],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   179
      theories: List[(Boolean, List[Options.Spec], List[String])],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   180
      files: List[String],
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   181
      document_files: List[(String, String)]) extends Entry
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   182
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   183
    private val chapter: Parser[Chapter] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   184
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   185
      val chapter_name = atom("chapter name", _.is_name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   186
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   187
      command(CHAPTER) ~! chapter_name ^^ { case _ ~ a => Chapter(a) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   188
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   189
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   190
    private val session_entry: Parser[Session_Entry] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   191
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   192
      val session_name = atom("session name", _.is_name)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   193
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   194
      val option =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   195
        name ~ opt($$$("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   196
      val options = $$$("[") ~> rep1sep(option, $$$(",")) <~ $$$("]")
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   197
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   198
      val theories =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   199
        ($$$(GLOBAL_THEORIES) | $$$(THEORIES)) ~!
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   200
          ((options | success(Nil)) ~ rep(theory_xname)) ^^
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   201
          { case x ~ (y ~ z) => (x == GLOBAL_THEORIES, y, z) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   202
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   203
      val document_files =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   204
        $$$(DOCUMENT_FILES) ~!
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   205
          (($$$("(") ~! ($$$(IN) ~! (path ~ $$$(")"))) ^^
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   206
              { case _ ~ (_ ~ (x ~ _)) => x } | success("document")) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   207
            rep1(path)) ^^ { case _ ~ (x ~ y) => y.map((x, _)) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   208
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   209
      command(SESSION) ~!
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   210
        (position(session_name) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   211
          (($$$("(") ~! (rep1(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | success(Nil)) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   212
          (($$$(IN) ~! path ^^ { case _ ~ x => x }) | success(".")) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   213
          ($$$("=") ~!
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   214
            (opt(session_name ~! $$$("+") ^^ { case x ~ _ => x }) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   215
              (($$$(DESCRIPTION) ~! text ^^ { case _ ~ x => x }) | success("")) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   216
              (($$$(OPTIONS) ~! options ^^ { case _ ~ x => x }) | success(Nil)) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   217
              rep1(theories) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   218
              (($$$(FILES) ~! rep1(path) ^^ { case _ ~ x => x }) | success(Nil)) ~
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   219
              (rep(document_files) ^^ (x => x.flatten))))) ^^
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   220
        { case _ ~ ((a, pos) ~ b ~ c ~ (_ ~ (d ~ e ~ f ~ g ~ h ~ i))) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   221
            Session_Entry(pos, a, b, c, d, e, f, g, h, i) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   222
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   223
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   224
    def parse(options: Options, select: Boolean, dir: Path): List[(String, Info)] =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   225
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   226
      def make_info(entry_chapter: String, entry: Session_Entry): (String, Info) =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   227
      {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   228
        try {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   229
          val name = entry.name
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   230
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   231
          if (name == "") error("Bad session name")
62883
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
   232
          if (pure_name(name) && entry.parent.isDefined) error("Illegal parent session")
b04e9fe29223 clarified ML bootstrap;
wenzelm
parents: 62864
diff changeset
   233
          if (!pure_name(name) && !entry.parent.isDefined) error("Missing parent session")
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   234
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   235
          val session_options = options ++ entry.options
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   236
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   237
          val theories =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   238
            entry.theories.map({ case (global, opts, thys) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   239
              (global, session_options ++ opts, thys.map(Path.explode(_))) })
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   240
          val files = entry.files.map(Path.explode(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   241
          val document_files =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   242
            entry.document_files.map({ case (s1, s2) => (Path.explode(s1), Path.explode(s2)) })
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   243
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   244
          val meta_digest =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   245
            SHA1.digest((entry_chapter, name, entry.parent, entry.options,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   246
              entry.theories, entry.files, entry.document_files).toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   247
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   248
          val info =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   249
            Info(entry_chapter, select, entry.pos, entry.groups, dir + Path.explode(entry.path),
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   250
              entry.parent, entry.description, session_options, theories, files,
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   251
              document_files, meta_digest)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   252
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   253
          (name, info)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   254
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   255
        catch {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   256
          case ERROR(msg) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   257
            error(msg + "\nThe error(s) above occurred in session entry " +
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   258
              quote(entry.name) + Position.here(entry.pos))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   259
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   260
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   261
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   262
      val root = dir + ROOT
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   263
      if (root.is_file) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   264
        val toks = Token.explode(root_syntax.keywords, File.read(root))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   265
        val start = Token.Pos.file(root.implode)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   266
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   267
        parse_all(rep(chapter | session_entry), Token.reader(toks, start)) match {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   268
          case Success(result, _) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   269
            var entry_chapter = "Unsorted"
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   270
            val infos = new mutable.ListBuffer[(String, Info)]
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   271
            result.foreach {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   272
              case Chapter(name) => entry_chapter = name
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   273
              case entry: Session_Entry => infos += make_info(entry_chapter, entry)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   274
            }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   275
            infos.toList
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   276
          case bad => error(bad.toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   277
        }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   278
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   279
      else Nil
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   280
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   281
  }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   282
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   283
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   284
  /* load sessions from certain directories */
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   285
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   286
  private def is_session_dir(dir: Path): Boolean =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   287
    (dir + ROOT).is_file || (dir + ROOTS).is_file
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   288
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   289
  private def check_session_dir(dir: Path): Path =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   290
    if (is_session_dir(dir)) dir
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   291
    else error("Bad session root directory: " + dir.toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   292
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   293
  def load(options: Options, dirs: List[Path] = Nil, select_dirs: List[Path] = Nil): Tree =
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   294
  {
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   295
    def load_dir(select: Boolean, dir: Path): List[(String, Info)] =
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   296
      load_root(select, dir) ::: load_roots(select, dir)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   297
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   298
    def load_root(select: Boolean, dir: Path): List[(String, Info)] =
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   299
      Parser.parse(options, select, dir)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   300
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   301
    def load_roots(select: Boolean, dir: Path): List[(String, Info)] =
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   302
    {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   303
      val roots = dir + ROOTS
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   304
      if (roots.is_file) {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   305
        for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   306
          line <- split_lines(File.read(roots))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   307
          if !(line == "" || line.startsWith("#"))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   308
          dir1 =
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   309
            try { check_session_dir(dir + Path.explode(line)) }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   310
            catch {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   311
              case ERROR(msg) =>
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   312
                error(msg + "\nThe error(s) above occurred in session catalog " + roots.toString)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   313
            }
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   314
          info <- load_dir(select, dir1)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   315
        } yield info
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   316
      }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   317
      else Nil
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   318
    }
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   319
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   320
    val default_dirs = Isabelle_System.components().filter(is_session_dir(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   321
    dirs.foreach(check_session_dir(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   322
    select_dirs.foreach(check_session_dir(_))
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   323
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   324
    Tree(
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   325
      for {
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   326
        (select, dir) <- (default_dirs ::: dirs).map((false, _)) ::: select_dirs.map((true, _))
62635
4854a38061de tuned signature;
wenzelm
parents: 62633
diff changeset
   327
        info <- load_dir(select, dir)
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   328
      } yield info)
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   329
  }
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   330
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   331
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   332
62704
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   333
  /** heap file with SHA1 digest **/
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   334
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   335
  private val sha1_prefix = "SHA1:"
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   336
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   337
  def read_heap_digest(heap: Path): Option[String] =
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   338
  {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   339
    if (heap.is_file) {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   340
      val file = FileChannel.open(heap.file.toPath, StandardOpenOption.READ)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   341
      try {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   342
        val len = file.size
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   343
        val n = sha1_prefix.length + SHA1.digest_length
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   344
        if (len >= n) {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   345
          file.position(len - n)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   346
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   347
          val buf = ByteBuffer.allocate(n)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   348
          var i = 0
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   349
          var m = 0
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   350
          do {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   351
            m = file.read(buf)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   352
            if (m != -1) i += m
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   353
          }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   354
          while (m != -1 && n > i)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   355
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   356
          if (i == n) {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   357
            val prefix = new String(buf.array(), 0, sha1_prefix.length, UTF8.charset)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   358
            val s = new String(buf.array(), sha1_prefix.length, SHA1.digest_length, UTF8.charset)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   359
            if (prefix == sha1_prefix) Some(s) else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   360
          }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   361
          else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   362
        }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   363
        else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   364
      }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   365
      finally { file.close }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   366
    }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   367
    else None
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   368
  }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   369
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   370
  def write_heap_digest(heap: Path): String =
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   371
    read_heap_digest(heap) match {
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   372
      case None =>
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   373
        val s = SHA1.digest(heap).rep
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   374
        File.append(heap, sha1_prefix + s)
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   375
        s
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   376
      case Some(s) => s
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   377
    }
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   378
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   379
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   380
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   381
  /** persistent store **/
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   382
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   383
  def log(name: String): Path = Path.basic("log") + Path.basic(name)
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   384
  def log_gz(name: String): Path = log(name).ext("gz")
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   385
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   386
  def store(system_mode: Boolean = false): Store = new Store(system_mode)
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   387
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   388
  class Store private [Sessions](system_mode: Boolean)
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   389
  {
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   390
    /* output */
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   391
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   392
    val browser_info: Path =
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   393
      if (system_mode) Path.explode("~~/browser_info")
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   394
      else Path.explode("$ISABELLE_BROWSER_INFO")
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   395
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   396
    val output_dir: Path =
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   397
      if (system_mode) Path.explode("~~/heaps/$ML_IDENTIFIER")
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   398
      else Path.explode("$ISABELLE_OUTPUT")
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   399
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   400
    def prepare_output() { Isabelle_System.mkdirs(output_dir + Path.basic("log")) }
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   401
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   402
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   403
    /* input */
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   404
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   405
    private val input_dirs =
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   406
      if (system_mode) List(output_dir)
62633
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   407
      else {
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   408
        val ml_ident = Path.explode("$ML_IDENTIFIER").expand
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   409
        output_dir :: Path.split(Isabelle_System.getenv_strict("ISABELLE_PATH")).map(_ + ml_ident)
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   410
      }
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   411
62636
e676ae9f1bf6 clarified signature;
wenzelm
parents: 62635
diff changeset
   412
    def find(name: String): Option[(Path, Option[String])] =
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   413
      input_dirs.find(dir => (dir + log_gz(name)).is_file).map(dir =>
62704
478b49f0d726 proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents: 62637
diff changeset
   414
        (dir + log_gz(name), read_heap_digest(dir + Path.basic(name))))
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   415
62633
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   416
    def find_log(name: String): Option[Path] =
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   417
      input_dirs.map(_ + log(name)).find(_.is_file)
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   418
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   419
    def find_log_gz(name: String): Option[Path] =
e57416b649d5 find heaps uniformly via Sessions.Store;
wenzelm
parents: 62632
diff changeset
   420
      input_dirs.map(_ + log_gz(name)).find(_.is_file)
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   421
62637
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   422
    def find_heap(name: String): Option[Path] =
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   423
      input_dirs.map(_ + Path.basic(name)).find(_.is_file)
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   424
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   425
    def heap(name: String): Path =
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   426
      find_heap(name) getOrElse
0189fe0f6452 support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents: 62636
diff changeset
   427
        error("Unknown logic " + quote(name) + " -- no heap file found in:\n" +
62769
146945b9e83c tuned message;
wenzelm
parents: 62704
diff changeset
   428
          cat_lines(input_dirs.map(dir => "  " + dir.expand.implode)))
62632
cd991ba01ffd clarified modules;
wenzelm
parents: 62631
diff changeset
   429
  }
62631
c39614ddb80b clarified modules;
wenzelm
parents:
diff changeset
   430
}