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