src/Pure/Thy/sessions.scala
author wenzelm
Wed May 03 15:53:23 2017 +0200 (2017-05-03)
changeset 65699 9f74d9aa0bdf
parent 65698 38139b2067cf
child 65740 83388f09e9ab
permissions -rw-r--r--
tuned signature;
wenzelm@62631
     1
/*  Title:      Pure/Thy/sessions.scala
wenzelm@62631
     2
    Author:     Makarius
wenzelm@62631
     3
wenzelm@65430
     4
Cumulative session information.
wenzelm@62631
     5
*/
wenzelm@62631
     6
wenzelm@62631
     7
package isabelle
wenzelm@62631
     8
wenzelm@65461
     9
import java.io.{File => JFile}
wenzelm@62704
    10
import java.nio.ByteBuffer
wenzelm@62704
    11
import java.nio.channels.FileChannel
wenzelm@62704
    12
import java.nio.file.StandardOpenOption
wenzelm@65320
    13
import java.sql.PreparedStatement
wenzelm@62631
    14
wenzelm@62631
    15
import scala.collection.SortedSet
wenzelm@62631
    16
import scala.collection.mutable
wenzelm@62631
    17
wenzelm@62631
    18
wenzelm@62631
    19
object Sessions
wenzelm@62631
    20
{
wenzelm@65360
    21
  /* base info and source dependencies */
wenzelm@62883
    22
wenzelm@65445
    23
  val DRAFT = "Draft"
wenzelm@65445
    24
wenzelm@65360
    25
  def is_pure(name: String): Boolean = name == Thy_Header.PURE
wenzelm@64856
    26
wenzelm@65495
    27
  object Known
wenzelm@64856
    28
  {
wenzelm@65495
    29
    val empty: Known = Known()
wenzelm@65360
    30
wenzelm@65495
    31
    def make(local_dir: Path, bases: List[Base], theories: List[Document.Node.Name]): Known =
wenzelm@65427
    32
    {
wenzelm@65463
    33
      def bases_iterator(local: Boolean) =
wenzelm@65463
    34
        for {
wenzelm@65463
    35
          base <- bases.iterator
wenzelm@65499
    36
          (_, name) <- (if (local) base.known.theories_local else base.known.theories).iterator
wenzelm@65463
    37
        } yield name
wenzelm@65463
    38
wenzelm@65463
    39
      def local_theories_iterator =
wenzelm@65463
    40
      {
wenzelm@65463
    41
        val local_path = local_dir.file.getCanonicalFile.toPath
wenzelm@65463
    42
        theories.iterator.filter(name =>
wenzelm@65463
    43
          Path.explode(name.node).file.getCanonicalFile.toPath.startsWith(local_path))
wenzelm@65463
    44
      }
wenzelm@65427
    45
wenzelm@65461
    46
      val known_theories =
wenzelm@65463
    47
        (Map.empty[String, Document.Node.Name] /: (bases_iterator(false) ++ theories.iterator))({
wenzelm@65461
    48
          case (known, name) =>
wenzelm@65461
    49
            known.get(name.theory) match {
wenzelm@65461
    50
              case Some(name1) if name != name1 =>
wenzelm@65461
    51
                error("Duplicate theory " + quote(name.node) + " vs. " + quote(name1.node))
wenzelm@65461
    52
              case _ => known + (name.theory -> name)
wenzelm@65461
    53
            }
wenzelm@65461
    54
          })
wenzelm@65463
    55
      val known_theories_local =
wenzelm@65463
    56
        (Map.empty[String, Document.Node.Name] /:
wenzelm@65463
    57
            (bases_iterator(true) ++ local_theories_iterator))({
wenzelm@65463
    58
          case (known, name) => known + (name.theory -> name)
wenzelm@65463
    59
        })
wenzelm@65461
    60
      val known_files =
wenzelm@65463
    61
        (Map.empty[JFile, List[Document.Node.Name]] /:
wenzelm@65463
    62
            (bases_iterator(true) ++ bases_iterator(false) ++ theories.iterator))({
wenzelm@65461
    63
          case (known, name) =>
wenzelm@65461
    64
            val file = Path.explode(name.node).file.getCanonicalFile
wenzelm@65463
    65
            val theories1 = known.getOrElse(file, Nil)
wenzelm@65463
    66
            if (theories1.exists(name1 => name.node == name1.node && name.theory == name1.theory))
wenzelm@65461
    67
              known
wenzelm@65463
    68
            else known + (file -> (name :: theories1))
wenzelm@65427
    69
        })
wenzelm@65495
    70
      Known(known_theories, known_theories_local,
wenzelm@65463
    71
        known_files.iterator.map(p => (p._1, p._2.reverse)).toMap)
wenzelm@65427
    72
    }
wenzelm@64856
    73
  }
wenzelm@64856
    74
wenzelm@65495
    75
  sealed case class Known(
wenzelm@65499
    76
    theories: Map[String, Document.Node.Name] = Map.empty,
wenzelm@65499
    77
    theories_local: Map[String, Document.Node.Name] = Map.empty,
wenzelm@65499
    78
    files: Map[JFile, List[Document.Node.Name]] = Map.empty)
wenzelm@65355
    79
  {
wenzelm@65495
    80
    def platform_path: Known =
wenzelm@65499
    81
      copy(theories = for ((a, b) <- theories) yield (a, b.map(File.platform_path(_))),
wenzelm@65499
    82
        theories_local = for ((a, b) <- theories_local) yield (a, b.map(File.platform_path(_))),
wenzelm@65499
    83
        files = for ((a, b) <- files) yield (a, b.map(c => c.map(File.platform_path(_)))))
wenzelm@65524
    84
wenzelm@65524
    85
    def get_file(file: JFile): Option[Document.Node.Name] =
wenzelm@65524
    86
      files.getOrElse(file.getCanonicalFile, Nil).headOption
wenzelm@65495
    87
  }
wenzelm@65495
    88
wenzelm@65495
    89
  object Base
wenzelm@65495
    90
  {
wenzelm@65495
    91
    def pure(options: Options): Base = session_base(options, Thy_Header.PURE)
wenzelm@65495
    92
wenzelm@65495
    93
    def bootstrap(global_theories: Map[String, String]): Base =
wenzelm@65495
    94
      Base(
wenzelm@65495
    95
        global_theories = global_theories,
wenzelm@65495
    96
        keywords = Thy_Header.bootstrap_header,
wenzelm@65495
    97
        syntax = Thy_Header.bootstrap_syntax)
wenzelm@65495
    98
  }
wenzelm@65495
    99
wenzelm@65495
   100
  sealed case class Base(
wenzelm@65540
   101
    imports: Option[Base] = None,
wenzelm@65495
   102
    global_theories: Map[String, String] = Map.empty,
wenzelm@65495
   103
    loaded_theories: Map[String, String] = Map.empty,
wenzelm@65495
   104
    known: Known = Known.empty,
wenzelm@65495
   105
    keywords: Thy_Header.Keywords = Nil,
wenzelm@65495
   106
    syntax: Outer_Syntax = Outer_Syntax.empty,
wenzelm@65495
   107
    sources: List[(Path, SHA1.Digest)] = Nil,
wenzelm@65495
   108
    session_graph: Graph_Display.Graph = Graph_Display.empty_graph)
wenzelm@65495
   109
  {
wenzelm@65540
   110
    def get_imports: Base = imports getOrElse Base.bootstrap(global_theories)
wenzelm@65540
   111
wenzelm@65495
   112
    def platform_path: Base = copy(known = known.platform_path)
wenzelm@65463
   113
wenzelm@65355
   114
    def loaded_theory(name: Document.Node.Name): Boolean =
wenzelm@65429
   115
      loaded_theories.isDefinedAt(name.theory)
wenzelm@65432
   116
wenzelm@65498
   117
    def known_theory(name: String): Option[Document.Node.Name] =
wenzelm@65499
   118
      known.theories.get(name)
wenzelm@65498
   119
wenzelm@65432
   120
    def dest_known_theories: List[(String, String)] =
wenzelm@65499
   121
      for ((theory, node_name) <- known.theories.toList)
wenzelm@65432
   122
        yield (theory, node_name.node)
wenzelm@65355
   123
  }
wenzelm@64856
   124
wenzelm@65495
   125
  sealed case class Deps(session_bases: Map[String, Base], all_known: Known)
wenzelm@65251
   126
  {
wenzelm@65463
   127
    def is_empty: Boolean = session_bases.isEmpty
wenzelm@65463
   128
    def apply(name: String): Base = session_bases(name)
wenzelm@65463
   129
    def sources(name: String): List[SHA1.Digest] = session_bases(name).sources.map(_._2)
wenzelm@65251
   130
  }
wenzelm@64856
   131
wenzelm@65415
   132
  def deps(sessions: T,
wenzelm@65251
   133
      progress: Progress = No_Progress,
wenzelm@65251
   134
      inlined_files: Boolean = false,
wenzelm@65251
   135
      verbose: Boolean = false,
wenzelm@65251
   136
      list_files: Boolean = false,
wenzelm@65251
   137
      check_keywords: Set[String] = Set.empty,
wenzelm@65463
   138
      global_theories: Map[String, String] = Map.empty,
wenzelm@65495
   139
      all_known: Boolean = false): Deps =
wenzelm@65372
   140
  {
wenzelm@65463
   141
    val session_bases =
wenzelm@65463
   142
      (Map.empty[String, Base] /: sessions.imports_topological_order)({
wenzelm@65519
   143
        case (session_bases, info) =>
wenzelm@65463
   144
          if (progress.stopped) throw Exn.Interrupt()
wenzelm@65251
   145
wenzelm@65463
   146
          try {
wenzelm@65496
   147
            val parent_base: Sessions.Base =
wenzelm@65463
   148
              info.parent match {
wenzelm@65475
   149
                case None => Base.bootstrap(global_theories)
wenzelm@65463
   150
                case Some(parent) => session_bases(parent)
wenzelm@65463
   151
              }
wenzelm@65496
   152
            val imports_base: Sessions.Base =
wenzelm@65496
   153
              parent_base.copy(known =
wenzelm@65496
   154
                Known.make(info.dir, parent_base :: info.imports.map(session_bases(_)), Nil))
wenzelm@65496
   155
wenzelm@65532
   156
            val resources = new Resources(imports_base)
wenzelm@65251
   157
wenzelm@65463
   158
            if (verbose || list_files) {
wenzelm@65463
   159
              val groups =
wenzelm@65463
   160
                if (info.groups.isEmpty) ""
wenzelm@65463
   161
                else info.groups.mkString(" (", " ", ")")
wenzelm@65519
   162
              progress.echo("Session " + info.chapter + "/" + info.name + groups)
wenzelm@65463
   163
            }
wenzelm@65251
   164
wenzelm@65463
   165
            val thy_deps =
wenzelm@65463
   166
            {
wenzelm@65463
   167
              val root_theories =
wenzelm@65463
   168
                info.theories.flatMap({ case (_, thys) =>
wenzelm@65517
   169
                  thys.map({ case (thy, pos) =>
wenzelm@65528
   170
                    (resources.import_name(info.theory_qualifier, info.dir.implode, thy), pos) })
wenzelm@65463
   171
                })
wenzelm@65463
   172
              val thy_deps = resources.thy_info.dependencies(root_theories)
wenzelm@65251
   173
wenzelm@65463
   174
              thy_deps.errors match {
wenzelm@65463
   175
                case Nil => thy_deps
wenzelm@65463
   176
                case errs => error(cat_lines(errs))
wenzelm@65463
   177
              }
wenzelm@65251
   178
            }
wenzelm@65251
   179
wenzelm@65463
   180
            val syntax = thy_deps.syntax
wenzelm@65251
   181
wenzelm@65463
   182
            val theory_files = thy_deps.deps.map(dep => Path.explode(dep.name.node))
wenzelm@65463
   183
            val loaded_files =
wenzelm@65463
   184
              if (inlined_files) {
wenzelm@65463
   185
                val pure_files =
wenzelm@65519
   186
                  if (is_pure(info.name)) {
wenzelm@65463
   187
                    val roots = Thy_Header.ml_roots.map(p => info.dir + Path.explode(p._1))
wenzelm@65463
   188
                    val files =
wenzelm@65463
   189
                      roots.flatMap(root => resources.loaded_files(syntax, File.read(root))).
wenzelm@65463
   190
                        map(file => info.dir + Path.explode(file))
wenzelm@65463
   191
                    roots ::: files
wenzelm@65463
   192
                  }
wenzelm@65463
   193
                  else Nil
wenzelm@65463
   194
                pure_files ::: thy_deps.loaded_files
wenzelm@65463
   195
              }
wenzelm@65463
   196
              else Nil
wenzelm@65251
   197
wenzelm@65463
   198
            val all_files =
wenzelm@65463
   199
              (theory_files ::: loaded_files :::
wenzelm@65463
   200
                info.files.map(file => info.dir + file) :::
wenzelm@65463
   201
                info.document_files.map(file => info.dir + file._1 + file._2)).map(_.expand)
wenzelm@65463
   202
wenzelm@65463
   203
            if (list_files)
wenzelm@65463
   204
              progress.echo(cat_lines(all_files.map(_.implode).sorted.map("  " + _)))
wenzelm@65463
   205
wenzelm@65463
   206
            if (check_keywords.nonEmpty)
wenzelm@65463
   207
              Check_Keywords.check_keywords(progress, syntax.keywords, check_keywords, theory_files)
wenzelm@65251
   208
wenzelm@65507
   209
            val session_graph: Graph_Display.Graph =
wenzelm@65507
   210
            {
wenzelm@65507
   211
              def session_node(name: String): Graph_Display.Node =
wenzelm@65507
   212
                Graph_Display.Node("[" + name + "]", "session." + name)
wenzelm@65507
   213
wenzelm@65507
   214
              def node(name: Document.Node.Name): Graph_Display.Node =
wenzelm@65507
   215
              {
wenzelm@65528
   216
                val qualifier = resources.theory_qualifier(name)
wenzelm@65528
   217
                if (qualifier == info.theory_qualifier)
wenzelm@65507
   218
                  Graph_Display.Node(name.theory_base_name, "theory." + name.theory)
wenzelm@65528
   219
                else session_node(qualifier)
wenzelm@65507
   220
              }
wenzelm@65507
   221
wenzelm@65507
   222
              val imports_subgraph =
wenzelm@65507
   223
                sessions.imports_graph.restrict(
wenzelm@65507
   224
                  sessions.imports_graph.all_preds(info.parent.toList ::: info.imports).toSet)
wenzelm@65507
   225
wenzelm@65507
   226
              val graph0 =
wenzelm@65507
   227
                (Graph_Display.empty_graph /: imports_subgraph.topological_order)(
wenzelm@65507
   228
                  { case (g, session) =>
wenzelm@65507
   229
                      val a = session_node(session)
wenzelm@65507
   230
                      val bs = imports_subgraph.imm_preds(session).toList.map(session_node(_))
wenzelm@65507
   231
                      ((g /: (a :: bs))(_.default_node(_, Nil)) /: bs)(_.add_edge(_, a)) })
wenzelm@65507
   232
wenzelm@65507
   233
              (graph0 /: thy_deps.deps)(
wenzelm@65507
   234
                { case (g, dep) =>
wenzelm@65507
   235
                    val a = node(dep.name)
wenzelm@65507
   236
                    val bs =
wenzelm@65507
   237
                      dep.header.imports.map({ case (name, _) => node(name) }).
wenzelm@65507
   238
                        filterNot(_ == a)
wenzelm@65507
   239
                    ((g /: (a :: bs))(_.default_node(_, Nil)) /: bs)(_.add_edge(_, a)) })
wenzelm@65507
   240
            }
wenzelm@65507
   241
wenzelm@65463
   242
            val base =
wenzelm@65540
   243
              Base(imports = Some(imports_base),
wenzelm@65540
   244
                global_theories = global_theories,
wenzelm@65463
   245
                loaded_theories = thy_deps.loaded_theories,
wenzelm@65496
   246
                known = Known.make(info.dir, List(imports_base), thy_deps.deps.map(_.name)),
wenzelm@65463
   247
                keywords = thy_deps.keywords,
wenzelm@65463
   248
                syntax = syntax,
wenzelm@65463
   249
                sources = all_files.map(p => (p, SHA1.digest(p.file))),
wenzelm@65507
   250
                session_graph = session_graph)
wenzelm@65251
   251
wenzelm@65519
   252
            session_bases + (info.name -> base)
wenzelm@65463
   253
          }
wenzelm@65463
   254
          catch {
wenzelm@65463
   255
            case ERROR(msg) =>
wenzelm@65463
   256
              cat_error(msg, "The error(s) above occurred in session " +
wenzelm@65519
   257
                quote(info.name) + Position.here(info.pos))
wenzelm@65463
   258
          }
wenzelm@65463
   259
      })
wenzelm@65251
   260
wenzelm@65496
   261
    Deps(session_bases,
wenzelm@65495
   262
      if (all_known) Known.make(Path.current, session_bases.toList.map(_._2), Nil)
wenzelm@65496
   263
      else Known.empty)
wenzelm@65372
   264
  }
wenzelm@65251
   265
wenzelm@65428
   266
  def session_base(
wenzelm@65428
   267
    options: Options,
wenzelm@65428
   268
    session: String,
wenzelm@65428
   269
    dirs: List[Path] = Nil,
wenzelm@65495
   270
    all_known: Boolean = false): Base =
wenzelm@65251
   271
  {
wenzelm@65415
   272
    val full_sessions = load(options, dirs = dirs)
wenzelm@65428
   273
    val global_theories = full_sessions.global_theories
wenzelm@65428
   274
    val selected_sessions = full_sessions.selection(Selection(sessions = List(session)))._2
wenzelm@65372
   275
wenzelm@65495
   276
    if (all_known) {
wenzelm@65560
   277
      val deps = Sessions.deps(full_sessions, global_theories = global_theories, all_known = true)
wenzelm@65495
   278
      deps(session).copy(known = deps.all_known)
wenzelm@65428
   279
    }
wenzelm@65428
   280
    else
wenzelm@65428
   281
      deps(selected_sessions, global_theories = global_theories)(session)
wenzelm@65251
   282
  }
wenzelm@65251
   283
wenzelm@65251
   284
wenzelm@65415
   285
  /* cumulative session info */
wenzelm@62631
   286
wenzelm@62631
   287
  sealed case class Info(
wenzelm@65519
   288
    name: String,
wenzelm@62631
   289
    chapter: String,
wenzelm@62631
   290
    select: Boolean,
wenzelm@62631
   291
    pos: Position.T,
wenzelm@62631
   292
    groups: List[String],
wenzelm@62631
   293
    dir: Path,
wenzelm@62631
   294
    parent: Option[String],
wenzelm@62631
   295
    description: String,
wenzelm@62631
   296
    options: Options,
wenzelm@65420
   297
    imports: List[String],
wenzelm@65517
   298
    theories: List[(Options, List[(String, Position.T)])],
wenzelm@65374
   299
    global_theories: List[String],
wenzelm@62631
   300
    files: List[Path],
wenzelm@62631
   301
    document_files: List[(Path, Path)],
wenzelm@62631
   302
    meta_digest: SHA1.Digest)
wenzelm@62631
   303
  {
wenzelm@62631
   304
    def timeout: Time = Time.seconds(options.real("timeout") * options.real("timeout_scale"))
wenzelm@65456
   305
wenzelm@65520
   306
    def theory_qualifier: String =
wenzelm@65456
   307
      options.string("theory_qualifier") match {
wenzelm@65520
   308
        case "" => name
wenzelm@65457
   309
        case qualifier => qualifier
wenzelm@65456
   310
      }
wenzelm@62631
   311
  }
wenzelm@62631
   312
wenzelm@65419
   313
  object Selection
wenzelm@65419
   314
  {
wenzelm@65422
   315
    val empty: Selection = Selection()
wenzelm@65525
   316
    val all: Selection = Selection(all_sessions = true)
wenzelm@65419
   317
  }
wenzelm@65419
   318
wenzelm@65419
   319
  sealed case class Selection(
wenzelm@65419
   320
    requirements: Boolean = false,
wenzelm@65419
   321
    all_sessions: Boolean = false,
wenzelm@65419
   322
    exclude_session_groups: List[String] = Nil,
wenzelm@65419
   323
    exclude_sessions: List[String] = Nil,
wenzelm@65419
   324
    session_groups: List[String] = Nil,
wenzelm@65419
   325
    sessions: List[String] = Nil)
wenzelm@65419
   326
  {
wenzelm@65422
   327
    def + (other: Selection): Selection =
wenzelm@65422
   328
      Selection(
wenzelm@65422
   329
        requirements = requirements || other.requirements,
wenzelm@65422
   330
        all_sessions = all_sessions || other.all_sessions,
wenzelm@65422
   331
        exclude_session_groups = exclude_session_groups ::: other.exclude_session_groups,
wenzelm@65422
   332
        exclude_sessions = exclude_sessions ::: other.exclude_sessions,
wenzelm@65422
   333
        session_groups = session_groups ::: other.session_groups,
wenzelm@65422
   334
        sessions = sessions ::: other.sessions)
wenzelm@65422
   335
wenzelm@65419
   336
    def apply(graph: Graph[String, Info]): (List[String], Graph[String, Info]) =
wenzelm@65419
   337
    {
wenzelm@65419
   338
      val bad_sessions =
wenzelm@65419
   339
        SortedSet((exclude_sessions ::: sessions).filterNot(graph.defined(_)): _*).toList
wenzelm@65419
   340
      if (bad_sessions.nonEmpty) error("Undefined session(s): " + commas_quote(bad_sessions))
wenzelm@65419
   341
wenzelm@65419
   342
      val excluded =
wenzelm@65419
   343
      {
wenzelm@65419
   344
        val exclude_group = exclude_session_groups.toSet
wenzelm@65419
   345
        val exclude_group_sessions =
wenzelm@65419
   346
          (for {
wenzelm@65419
   347
            (name, (info, _)) <- graph.iterator
wenzelm@65419
   348
            if graph.get_node(name).groups.exists(exclude_group)
wenzelm@65419
   349
          } yield name).toList
wenzelm@65419
   350
        graph.all_succs(exclude_group_sessions ::: exclude_sessions).toSet
wenzelm@65419
   351
      }
wenzelm@65419
   352
wenzelm@65419
   353
      val pre_selected =
wenzelm@65419
   354
      {
wenzelm@65419
   355
        if (all_sessions) graph.keys
wenzelm@65419
   356
        else {
wenzelm@65419
   357
          val select_group = session_groups.toSet
wenzelm@65419
   358
          val select = sessions.toSet
wenzelm@65419
   359
          (for {
wenzelm@65419
   360
            (name, (info, _)) <- graph.iterator
wenzelm@65419
   361
            if info.select || select(name) || graph.get_node(name).groups.exists(select_group)
wenzelm@65419
   362
          } yield name).toList
wenzelm@65419
   363
        }
wenzelm@65419
   364
      }.filterNot(excluded)
wenzelm@65419
   365
wenzelm@65419
   366
      val selected =
wenzelm@65419
   367
        if (requirements) (graph.all_preds(pre_selected).toSet -- pre_selected).toList
wenzelm@65419
   368
        else pre_selected
wenzelm@65419
   369
wenzelm@65419
   370
      (selected, graph.restrict(graph.all_preds(selected).toSet))
wenzelm@65419
   371
    }
wenzelm@65419
   372
  }
wenzelm@65419
   373
wenzelm@65415
   374
  def make(infos: Traversable[(String, Info)]): T =
wenzelm@62631
   375
  {
wenzelm@65420
   376
    def add_edges(graph: Graph[String, Info], kind: String, edges: Info => Traversable[String])
wenzelm@65420
   377
      : Graph[String, Info] =
wenzelm@65420
   378
    {
wenzelm@65420
   379
      def add_edge(pos: Position.T, name: String, g: Graph[String, Info], parent: String) =
wenzelm@65420
   380
      {
wenzelm@65420
   381
        if (!g.defined(parent))
wenzelm@65420
   382
          error("Bad " + kind + " session " + quote(parent) + " for " +
wenzelm@65420
   383
            quote(name) + Position.here(pos))
wenzelm@65420
   384
wenzelm@65420
   385
        try { g.add_edge_acyclic(parent, name) }
wenzelm@65420
   386
        catch {
wenzelm@65420
   387
          case exn: Graph.Cycles[_] =>
wenzelm@65420
   388
            error(cat_lines(exn.cycles.map(cycle =>
wenzelm@65420
   389
              "Cyclic session dependency of " +
wenzelm@65420
   390
                cycle.map(c => quote(c.toString)).mkString(" via "))) + Position.here(pos))
wenzelm@65420
   391
        }
wenzelm@65420
   392
      }
wenzelm@65420
   393
      (graph /: graph.iterator) {
wenzelm@65420
   394
        case (g, (name, (info, _))) => (g /: edges(info))(add_edge(info.pos, name, _, _))
wenzelm@65420
   395
      }
wenzelm@65420
   396
    }
wenzelm@65420
   397
wenzelm@65420
   398
    val graph0 =
wenzelm@65415
   399
      (Graph.string[Info] /: infos) {
wenzelm@65415
   400
        case (graph, (name, info)) =>
wenzelm@65415
   401
          if (graph.defined(name))
wenzelm@65415
   402
            error("Duplicate session " + quote(name) + Position.here(info.pos) +
wenzelm@65415
   403
              Position.here(graph.get_node(name).pos))
wenzelm@65415
   404
          else graph.new_node(name, info)
wenzelm@65415
   405
      }
wenzelm@65420
   406
    val graph1 = add_edges(graph0, "parent", _.parent)
wenzelm@65420
   407
    val graph2 = add_edges(graph1, "imports", _.imports)
wenzelm@62631
   408
wenzelm@65420
   409
    new T(graph1, graph2)
wenzelm@62631
   410
  }
wenzelm@62631
   411
wenzelm@65420
   412
  final class T private[Sessions](
wenzelm@65420
   413
      val build_graph: Graph[String, Info],
wenzelm@65420
   414
      val imports_graph: Graph[String, Info])
wenzelm@62631
   415
  {
wenzelm@65420
   416
    def apply(name: String): Info = imports_graph.get_node(name)
wenzelm@65420
   417
    def get(name: String): Option[Info] =
wenzelm@65420
   418
      if (imports_graph.defined(name)) Some(imports_graph.get_node(name)) else None
wenzelm@62631
   419
wenzelm@65457
   420
    def global_theories: Map[String, String] =
wenzelm@65490
   421
      (Thy_Header.bootstrap_global_theories.toMap /:
wenzelm@65424
   422
        (for {
wenzelm@65519
   423
          (_, (info, _)) <- imports_graph.iterator
wenzelm@65519
   424
          thy <- info.global_theories.iterator }
wenzelm@65519
   425
         yield (thy, info)))({
wenzelm@65519
   426
            case (global, (thy, info)) =>
wenzelm@65520
   427
              val qualifier = info.theory_qualifier
wenzelm@65457
   428
              global.get(thy) match {
wenzelm@65457
   429
                case Some(qualifier1) if qualifier != qualifier1 =>
wenzelm@65457
   430
                  error("Duplicate global theory " + quote(thy) + Position.here(info.pos))
wenzelm@65457
   431
                case _ => global + (thy -> qualifier)
wenzelm@65457
   432
              }
wenzelm@65457
   433
          })
wenzelm@65372
   434
wenzelm@65419
   435
    def selection(select: Selection): (List[String], T) =
wenzelm@62631
   436
    {
wenzelm@65420
   437
      val (_, build_graph1) = select(build_graph)
wenzelm@65420
   438
      val (selected, imports_graph1) = select(imports_graph)
wenzelm@65420
   439
      (selected, new T(build_graph1, imports_graph1))
wenzelm@62631
   440
    }
wenzelm@62631
   441
wenzelm@65420
   442
    def build_ancestors(name: String): List[String] =
wenzelm@65420
   443
      build_graph.all_preds(List(name)).tail.reverse
wenzelm@65420
   444
wenzelm@65420
   445
    def build_descendants(names: List[String]): List[String] =
wenzelm@65420
   446
      build_graph.all_succs(names)
wenzelm@62631
   447
wenzelm@65519
   448
    def build_topological_order: List[Info] =
wenzelm@65519
   449
      build_graph.topological_order.map(apply(_))
wenzelm@62631
   450
wenzelm@65566
   451
    def imports_ancestors(name: String): List[String] =
wenzelm@65566
   452
      imports_graph.all_preds(List(name)).tail.reverse
wenzelm@65566
   453
wenzelm@65519
   454
    def imports_topological_order: List[Info] =
wenzelm@65519
   455
      imports_graph.topological_order.map(apply(_))
wenzelm@65420
   456
wenzelm@65420
   457
    override def toString: String =
wenzelm@65420
   458
      imports_graph.keys_iterator.mkString("Sessions.T(", ", ", ")")
wenzelm@62631
   459
  }
wenzelm@62631
   460
wenzelm@62631
   461
wenzelm@62631
   462
  /* parser */
wenzelm@62631
   463
wenzelm@62864
   464
  val ROOT = Path.explode("ROOT")
wenzelm@62864
   465
  val ROOTS = Path.explode("ROOTS")
wenzelm@62864
   466
wenzelm@62631
   467
  private val CHAPTER = "chapter"
wenzelm@62631
   468
  private val SESSION = "session"
wenzelm@62631
   469
  private val IN = "in"
wenzelm@62631
   470
  private val DESCRIPTION = "description"
wenzelm@62631
   471
  private val OPTIONS = "options"
wenzelm@65420
   472
  private val SESSIONS = "sessions"
wenzelm@62631
   473
  private val THEORIES = "theories"
wenzelm@65374
   474
  private val GLOBAL = "global"
wenzelm@62631
   475
  private val FILES = "files"
wenzelm@62631
   476
  private val DOCUMENT_FILES = "document_files"
wenzelm@62631
   477
wenzelm@62631
   478
  lazy val root_syntax =
wenzelm@65374
   479
    Outer_Syntax.init() + "(" + ")" + "+" + "," + "=" + "[" + "]" + GLOBAL + IN +
wenzelm@63443
   480
      (CHAPTER, Keyword.THY_DECL) +
wenzelm@63443
   481
      (SESSION, Keyword.THY_DECL) +
wenzelm@63443
   482
      (DESCRIPTION, Keyword.QUASI_COMMAND) +
wenzelm@63443
   483
      (OPTIONS, Keyword.QUASI_COMMAND) +
wenzelm@65420
   484
      (SESSIONS, Keyword.QUASI_COMMAND) +
wenzelm@63443
   485
      (THEORIES, Keyword.QUASI_COMMAND) +
wenzelm@63443
   486
      (FILES, Keyword.QUASI_COMMAND) +
wenzelm@63443
   487
      (DOCUMENT_FILES, Keyword.QUASI_COMMAND)
wenzelm@62631
   488
wenzelm@62968
   489
  private object Parser extends Parse.Parser with Options.Parser
wenzelm@62631
   490
  {
wenzelm@62631
   491
    private abstract class Entry
wenzelm@62631
   492
    private sealed case class Chapter(name: String) extends Entry
wenzelm@62631
   493
    private sealed case class Session_Entry(
wenzelm@62631
   494
      pos: Position.T,
wenzelm@62631
   495
      name: String,
wenzelm@62631
   496
      groups: List[String],
wenzelm@62631
   497
      path: String,
wenzelm@62631
   498
      parent: Option[String],
wenzelm@62631
   499
      description: String,
wenzelm@62631
   500
      options: List[Options.Spec],
wenzelm@65420
   501
      imports: List[String],
wenzelm@65517
   502
      theories: List[(List[Options.Spec], List[((String, Position.T), Boolean)])],
wenzelm@62631
   503
      files: List[String],
wenzelm@62631
   504
      document_files: List[(String, String)]) extends Entry
wenzelm@65580
   505
    {
wenzelm@65580
   506
      def theories_no_position: List[(List[Options.Spec], List[(String, Boolean)])] =
wenzelm@65580
   507
        theories.map({ case (a, b) => (a, b.map({ case ((c, _), d) => (c, d) })) })
wenzelm@65580
   508
    }
wenzelm@62631
   509
wenzelm@62631
   510
    private val chapter: Parser[Chapter] =
wenzelm@62631
   511
    {
wenzelm@62631
   512
      val chapter_name = atom("chapter name", _.is_name)
wenzelm@62631
   513
wenzelm@62631
   514
      command(CHAPTER) ~! chapter_name ^^ { case _ ~ a => Chapter(a) }
wenzelm@62631
   515
    }
wenzelm@62631
   516
wenzelm@62631
   517
    private val session_entry: Parser[Session_Entry] =
wenzelm@62631
   518
    {
wenzelm@62631
   519
      val session_name = atom("session name", _.is_name)
wenzelm@62631
   520
wenzelm@62631
   521
      val option =
wenzelm@62968
   522
        option_name ~ opt($$$("=") ~! option_value ^^
wenzelm@62968
   523
          { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
wenzelm@62631
   524
      val options = $$$("[") ~> rep1sep(option, $$$(",")) <~ $$$("]")
wenzelm@62631
   525
wenzelm@65374
   526
      val global =
wenzelm@65374
   527
        ($$$("(") ~! $$$(GLOBAL) ~ $$$(")")) ^^ { case _ => true } | success(false)
wenzelm@65374
   528
wenzelm@65374
   529
      val theory_entry =
wenzelm@65517
   530
        position(theory_name) ~ global ^^ { case x ~ y => (x, y) }
wenzelm@65374
   531
wenzelm@62631
   532
      val theories =
wenzelm@65374
   533
        $$$(THEORIES) ~!
wenzelm@65374
   534
          ((options | success(Nil)) ~ rep(theory_entry)) ^^
wenzelm@65374
   535
          { case _ ~ (x ~ y) => (x, y) }
wenzelm@62631
   536
wenzelm@62631
   537
      val document_files =
wenzelm@62631
   538
        $$$(DOCUMENT_FILES) ~!
wenzelm@62631
   539
          (($$$("(") ~! ($$$(IN) ~! (path ~ $$$(")"))) ^^
wenzelm@62631
   540
              { case _ ~ (_ ~ (x ~ _)) => x } | success("document")) ~
wenzelm@62631
   541
            rep1(path)) ^^ { case _ ~ (x ~ y) => y.map((x, _)) }
wenzelm@62631
   542
wenzelm@62631
   543
      command(SESSION) ~!
wenzelm@62631
   544
        (position(session_name) ~
wenzelm@62631
   545
          (($$$("(") ~! (rep1(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   546
          (($$$(IN) ~! path ^^ { case _ ~ x => x }) | success(".")) ~
wenzelm@62631
   547
          ($$$("=") ~!
wenzelm@62631
   548
            (opt(session_name ~! $$$("+") ^^ { case x ~ _ => x }) ~
wenzelm@62631
   549
              (($$$(DESCRIPTION) ~! text ^^ { case _ ~ x => x }) | success("")) ~
wenzelm@62631
   550
              (($$$(OPTIONS) ~! options ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@65420
   551
              (($$$(SESSIONS) ~! rep(session_name)  ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   552
              rep1(theories) ~
wenzelm@62631
   553
              (($$$(FILES) ~! rep1(path) ^^ { case _ ~ x => x }) | success(Nil)) ~
wenzelm@62631
   554
              (rep(document_files) ^^ (x => x.flatten))))) ^^
wenzelm@65420
   555
        { case _ ~ ((a, pos) ~ b ~ c ~ (_ ~ (d ~ e ~ f ~ g ~ h ~ i ~ j))) =>
wenzelm@65420
   556
            Session_Entry(pos, a, b, c, d, e, f, g, h, i, j) }
wenzelm@62631
   557
    }
wenzelm@62631
   558
wenzelm@62631
   559
    def parse(options: Options, select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   560
    {
wenzelm@62631
   561
      def make_info(entry_chapter: String, entry: Session_Entry): (String, Info) =
wenzelm@62631
   562
      {
wenzelm@62631
   563
        try {
wenzelm@62631
   564
          val name = entry.name
wenzelm@62631
   565
wenzelm@65445
   566
          if (name == "" || name == DRAFT) error("Bad session name")
wenzelm@65360
   567
          if (is_pure(name) && entry.parent.isDefined) error("Illegal parent session")
wenzelm@65360
   568
          if (!is_pure(name) && !entry.parent.isDefined) error("Missing parent session")
wenzelm@62631
   569
wenzelm@62631
   570
          val session_options = options ++ entry.options
wenzelm@62631
   571
wenzelm@62631
   572
          val theories =
wenzelm@65392
   573
            entry.theories.map({ case (opts, thys) => (session_options ++ opts, thys.map(_._1)) })
wenzelm@65374
   574
wenzelm@65374
   575
          val global_theories =
wenzelm@65517
   576
            for { (_, thys) <- entry.theories; ((thy, pos), global) <- thys if global }
wenzelm@65374
   577
            yield {
wenzelm@65374
   578
              val thy_name = Path.explode(thy).expand.base.implode
wenzelm@65374
   579
              if (Long_Name.is_qualified(thy_name))
wenzelm@65517
   580
                error("Bad qualified name for global theory " +
wenzelm@65517
   581
                  quote(thy_name) + Position.here(pos))
wenzelm@65374
   582
              else thy_name
wenzelm@65374
   583
            }
wenzelm@65374
   584
wenzelm@62631
   585
          val files = entry.files.map(Path.explode(_))
wenzelm@62631
   586
          val document_files =
wenzelm@62631
   587
            entry.document_files.map({ case (s1, s2) => (Path.explode(s1), Path.explode(s2)) })
wenzelm@62631
   588
wenzelm@62631
   589
          val meta_digest =
wenzelm@65580
   590
            SHA1.digest((entry_chapter, name, entry.parent, entry.options, entry.imports,
wenzelm@65580
   591
              entry.theories_no_position, entry.files, entry.document_files).toString)
wenzelm@62631
   592
wenzelm@62631
   593
          val info =
wenzelm@65519
   594
            Info(name, entry_chapter, select, entry.pos, entry.groups,
wenzelm@65519
   595
              dir + Path.explode(entry.path), entry.parent, entry.description, session_options,
wenzelm@65519
   596
              entry.imports, theories, global_theories, files, document_files, meta_digest)
wenzelm@62631
   597
wenzelm@62631
   598
          (name, info)
wenzelm@62631
   599
        }
wenzelm@62631
   600
        catch {
wenzelm@62631
   601
          case ERROR(msg) =>
wenzelm@62631
   602
            error(msg + "\nThe error(s) above occurred in session entry " +
wenzelm@62631
   603
              quote(entry.name) + Position.here(entry.pos))
wenzelm@62631
   604
        }
wenzelm@62631
   605
      }
wenzelm@62631
   606
wenzelm@62631
   607
      val root = dir + ROOT
wenzelm@62631
   608
      if (root.is_file) {
wenzelm@62631
   609
        val toks = Token.explode(root_syntax.keywords, File.read(root))
wenzelm@62631
   610
        val start = Token.Pos.file(root.implode)
wenzelm@62631
   611
wenzelm@62631
   612
        parse_all(rep(chapter | session_entry), Token.reader(toks, start)) match {
wenzelm@62631
   613
          case Success(result, _) =>
wenzelm@62631
   614
            var entry_chapter = "Unsorted"
wenzelm@62631
   615
            val infos = new mutable.ListBuffer[(String, Info)]
wenzelm@62631
   616
            result.foreach {
wenzelm@62631
   617
              case Chapter(name) => entry_chapter = name
wenzelm@62631
   618
              case entry: Session_Entry => infos += make_info(entry_chapter, entry)
wenzelm@62631
   619
            }
wenzelm@62631
   620
            infos.toList
wenzelm@62631
   621
          case bad => error(bad.toString)
wenzelm@62631
   622
        }
wenzelm@62631
   623
      }
wenzelm@62631
   624
      else Nil
wenzelm@62631
   625
    }
wenzelm@62631
   626
  }
wenzelm@62631
   627
wenzelm@62631
   628
wenzelm@62635
   629
  /* load sessions from certain directories */
wenzelm@62631
   630
wenzelm@62631
   631
  private def is_session_dir(dir: Path): Boolean =
wenzelm@62631
   632
    (dir + ROOT).is_file || (dir + ROOTS).is_file
wenzelm@62631
   633
wenzelm@62631
   634
  private def check_session_dir(dir: Path): Path =
wenzelm@65468
   635
    if (is_session_dir(dir)) File.pwd() + dir.expand
wenzelm@62631
   636
    else error("Bad session root directory: " + dir.toString)
wenzelm@62631
   637
wenzelm@65561
   638
  def directories(dirs: List[Path], select_dirs: List[Path]): List[(Boolean, Path)] =
wenzelm@65561
   639
  {
wenzelm@65561
   640
    val default_dirs = Isabelle_System.components().filter(is_session_dir(_))
wenzelm@65561
   641
    (default_dirs ::: dirs).map((false, _)) ::: select_dirs.map((true, _))
wenzelm@65561
   642
  }
wenzelm@65561
   643
wenzelm@65415
   644
  def load(options: Options, dirs: List[Path] = Nil, select_dirs: List[Path] = Nil): T =
wenzelm@62631
   645
  {
wenzelm@62635
   646
    def load_dir(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62635
   647
      load_root(select, dir) ::: load_roots(select, dir)
wenzelm@62631
   648
wenzelm@62635
   649
    def load_root(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   650
      Parser.parse(options, select, dir)
wenzelm@62631
   651
wenzelm@62635
   652
    def load_roots(select: Boolean, dir: Path): List[(String, Info)] =
wenzelm@62631
   653
    {
wenzelm@62631
   654
      val roots = dir + ROOTS
wenzelm@62631
   655
      if (roots.is_file) {
wenzelm@62631
   656
        for {
wenzelm@62631
   657
          line <- split_lines(File.read(roots))
wenzelm@62631
   658
          if !(line == "" || line.startsWith("#"))
wenzelm@62631
   659
          dir1 =
wenzelm@62631
   660
            try { check_session_dir(dir + Path.explode(line)) }
wenzelm@62631
   661
            catch {
wenzelm@62631
   662
              case ERROR(msg) =>
wenzelm@62631
   663
                error(msg + "\nThe error(s) above occurred in session catalog " + roots.toString)
wenzelm@62631
   664
            }
wenzelm@62635
   665
          info <- load_dir(select, dir1)
wenzelm@62631
   666
        } yield info
wenzelm@62631
   667
      }
wenzelm@62631
   668
      else Nil
wenzelm@62631
   669
    }
wenzelm@62631
   670
wenzelm@65415
   671
    make(
wenzelm@62631
   672
      for {
wenzelm@65561
   673
        (select, dir) <- directories(dirs, select_dirs)
wenzelm@65468
   674
        info <- load_dir(select, check_session_dir(dir))
wenzelm@62631
   675
      } yield info)
wenzelm@62631
   676
  }
wenzelm@62632
   677
wenzelm@62632
   678
wenzelm@62637
   679
wenzelm@62704
   680
  /** heap file with SHA1 digest **/
wenzelm@62704
   681
wenzelm@62704
   682
  private val sha1_prefix = "SHA1:"
wenzelm@62704
   683
wenzelm@62704
   684
  def read_heap_digest(heap: Path): Option[String] =
wenzelm@62704
   685
  {
wenzelm@62704
   686
    if (heap.is_file) {
wenzelm@62704
   687
      val file = FileChannel.open(heap.file.toPath, StandardOpenOption.READ)
wenzelm@62704
   688
      try {
wenzelm@62704
   689
        val len = file.size
wenzelm@62704
   690
        val n = sha1_prefix.length + SHA1.digest_length
wenzelm@62704
   691
        if (len >= n) {
wenzelm@62704
   692
          file.position(len - n)
wenzelm@62704
   693
wenzelm@62704
   694
          val buf = ByteBuffer.allocate(n)
wenzelm@62704
   695
          var i = 0
wenzelm@62704
   696
          var m = 0
wenzelm@62704
   697
          do {
wenzelm@62704
   698
            m = file.read(buf)
wenzelm@62704
   699
            if (m != -1) i += m
wenzelm@62704
   700
          }
wenzelm@62704
   701
          while (m != -1 && n > i)
wenzelm@62704
   702
wenzelm@62704
   703
          if (i == n) {
wenzelm@62704
   704
            val prefix = new String(buf.array(), 0, sha1_prefix.length, UTF8.charset)
wenzelm@62704
   705
            val s = new String(buf.array(), sha1_prefix.length, SHA1.digest_length, UTF8.charset)
wenzelm@62704
   706
            if (prefix == sha1_prefix) Some(s) else None
wenzelm@62704
   707
          }
wenzelm@62704
   708
          else None
wenzelm@62704
   709
        }
wenzelm@62704
   710
        else None
wenzelm@62704
   711
      }
wenzelm@62704
   712
      finally { file.close }
wenzelm@62704
   713
    }
wenzelm@62704
   714
    else None
wenzelm@62704
   715
  }
wenzelm@62704
   716
wenzelm@62704
   717
  def write_heap_digest(heap: Path): String =
wenzelm@62704
   718
    read_heap_digest(heap) match {
wenzelm@62704
   719
      case None =>
wenzelm@62704
   720
        val s = SHA1.digest(heap).rep
wenzelm@62704
   721
        File.append(heap, sha1_prefix + s)
wenzelm@62704
   722
        s
wenzelm@62704
   723
      case Some(s) => s
wenzelm@62704
   724
    }
wenzelm@62704
   725
wenzelm@62704
   726
wenzelm@62704
   727
wenzelm@62637
   728
  /** persistent store **/
wenzelm@62632
   729
wenzelm@65296
   730
  object Session_Info
wenzelm@65296
   731
  {
wenzelm@65326
   732
    val session_name = SQL.Column.string("session_name", primary_key = true)
wenzelm@65326
   733
wenzelm@65296
   734
    // Build_Log.Session_Info
wenzelm@65296
   735
    val session_timing = SQL.Column.bytes("session_timing")
wenzelm@65296
   736
    val command_timings = SQL.Column.bytes("command_timings")
wenzelm@65296
   737
    val ml_statistics = SQL.Column.bytes("ml_statistics")
wenzelm@65296
   738
    val task_statistics = SQL.Column.bytes("task_statistics")
wenzelm@65296
   739
    val build_log_columns =
wenzelm@65296
   740
      List(session_name, session_timing, command_timings, ml_statistics, task_statistics)
wenzelm@65296
   741
wenzelm@65296
   742
    // Build.Session_Info
wenzelm@65296
   743
    val sources = SQL.Column.string("sources")
wenzelm@65296
   744
    val input_heaps = SQL.Column.string("input_heaps")
wenzelm@65296
   745
    val output_heap = SQL.Column.string("output_heap")
wenzelm@65296
   746
    val return_code = SQL.Column.int("return_code")
wenzelm@65296
   747
    val build_columns = List(sources, input_heaps, output_heap, return_code)
wenzelm@65296
   748
wenzelm@65296
   749
    val table = SQL.Table("isabelle_session_info", build_log_columns ::: build_columns)
wenzelm@65296
   750
  }
wenzelm@65296
   751
wenzelm@62632
   752
  def store(system_mode: Boolean = false): Store = new Store(system_mode)
wenzelm@62632
   753
wenzelm@65592
   754
  class Store private[Sessions](system_mode: Boolean) extends Properties.Store
wenzelm@62632
   755
  {
wenzelm@65278
   756
    /* file names */
wenzelm@65278
   757
wenzelm@65281
   758
    def database(name: String): Path = Path.basic("log") + Path.basic(name).ext("db")
wenzelm@65278
   759
    def log(name: String): Path = Path.basic("log") + Path.basic(name)
wenzelm@65278
   760
    def log_gz(name: String): Path = log(name).ext("gz")
wenzelm@65278
   761
wenzelm@65278
   762
wenzelm@65286
   763
    /* SQL database content */
wenzelm@65283
   764
wenzelm@65320
   765
    def read_bytes(db: SQL.Database, name: String, column: SQL.Column): Bytes =
wenzelm@65698
   766
      db.using_statement(Session_Info.table.select(List(column),
wenzelm@65699
   767
        Session_Info.session_name.where_equal(name)))(stmt =>
wenzelm@65291
   768
      {
wenzelm@65291
   769
        val rs = stmt.executeQuery
wenzelm@65324
   770
        if (!rs.next) Bytes.empty else db.bytes(rs, column)
wenzelm@65291
   771
      })
wenzelm@65285
   772
wenzelm@65320
   773
    def read_properties(db: SQL.Database, name: String, column: SQL.Column): List[Properties.T] =
wenzelm@65320
   774
      uncompress_properties(read_bytes(db, name, column))
wenzelm@65286
   775
wenzelm@65283
   776
wenzelm@62637
   777
    /* output */
wenzelm@62632
   778
wenzelm@62632
   779
    val browser_info: Path =
wenzelm@62632
   780
      if (system_mode) Path.explode("~~/browser_info")
wenzelm@62632
   781
      else Path.explode("$ISABELLE_BROWSER_INFO")
wenzelm@62632
   782
wenzelm@62637
   783
    val output_dir: Path =
wenzelm@62637
   784
      if (system_mode) Path.explode("~~/heaps/$ML_IDENTIFIER")
wenzelm@62637
   785
      else Path.explode("$ISABELLE_OUTPUT")
wenzelm@62637
   786
wenzelm@65298
   787
    override def toString: String = "Store(output_dir = " + output_dir.expand + ")"
wenzelm@65298
   788
wenzelm@62637
   789
    def prepare_output() { Isabelle_System.mkdirs(output_dir + Path.basic("log")) }
wenzelm@62637
   790
wenzelm@62637
   791
wenzelm@62637
   792
    /* input */
wenzelm@62637
   793
wenzelm@62637
   794
    private val input_dirs =
wenzelm@62632
   795
      if (system_mode) List(output_dir)
wenzelm@62633
   796
      else {
wenzelm@62633
   797
        val ml_ident = Path.explode("$ML_IDENTIFIER").expand
wenzelm@62633
   798
        output_dir :: Path.split(Isabelle_System.getenv_strict("ISABELLE_PATH")).map(_ + ml_ident)
wenzelm@62633
   799
      }
wenzelm@62632
   800
wenzelm@65291
   801
    def find_database_heap(name: String): Option[(Path, Option[String])] =
wenzelm@65291
   802
      input_dirs.find(dir => (dir + database(name)).is_file).map(dir =>
wenzelm@65291
   803
        (dir + database(name), read_heap_digest(dir + Path.basic(name))))
wenzelm@62632
   804
wenzelm@65281
   805
    def find_database(name: String): Option[Path] =
wenzelm@65281
   806
      input_dirs.map(_ + database(name)).find(_.is_file)
wenzelm@65281
   807
wenzelm@62637
   808
    def heap(name: String): Path =
wenzelm@65288
   809
      input_dirs.map(_ + Path.basic(name)).find(_.is_file) getOrElse
wenzelm@62637
   810
        error("Unknown logic " + quote(name) + " -- no heap file found in:\n" +
wenzelm@62769
   811
          cat_lines(input_dirs.map(dir => "  " + dir.expand.implode)))
wenzelm@65287
   812
wenzelm@65287
   813
wenzelm@65296
   814
    /* session info */
wenzelm@65281
   815
wenzelm@65296
   816
    def write_session_info(
wenzelm@65318
   817
      db: SQL.Database,
wenzelm@65320
   818
      name: String,
wenzelm@65318
   819
      build_log: Build_Log.Session_Info,
wenzelm@65318
   820
      build: Build.Session_Info)
wenzelm@65281
   821
    {
wenzelm@65281
   822
      db.transaction {
wenzelm@65296
   823
        db.create_table(Session_Info.table)
wenzelm@65698
   824
        db.using_statement(
wenzelm@65699
   825
          Session_Info.table.delete(Session_Info.session_name.where_equal(name)))(_.execute)
wenzelm@65698
   826
        db.using_statement(Session_Info.table.insert())(stmt =>
wenzelm@65281
   827
        {
wenzelm@65320
   828
          db.set_string(stmt, 1, name)
wenzelm@65296
   829
          db.set_bytes(stmt, 2, encode_properties(build_log.session_timing))
wenzelm@65296
   830
          db.set_bytes(stmt, 3, compress_properties(build_log.command_timings))
wenzelm@65296
   831
          db.set_bytes(stmt, 4, compress_properties(build_log.ml_statistics))
wenzelm@65296
   832
          db.set_bytes(stmt, 5, compress_properties(build_log.task_statistics))
wenzelm@65284
   833
          db.set_string(stmt, 6, cat_lines(build.sources))
wenzelm@65284
   834
          db.set_string(stmt, 7, cat_lines(build.input_heaps))
wenzelm@65291
   835
          db.set_string(stmt, 8, build.output_heap getOrElse "")
wenzelm@65283
   836
          db.set_int(stmt, 9, build.return_code)
wenzelm@65281
   837
          stmt.execute()
wenzelm@65281
   838
        })
wenzelm@65281
   839
      }
wenzelm@65281
   840
    }
wenzelm@65281
   841
wenzelm@65320
   842
    def read_session_timing(db: SQL.Database, name: String): Properties.T =
wenzelm@65320
   843
      decode_properties(read_bytes(db, name, Session_Info.session_timing))
wenzelm@65286
   844
wenzelm@65320
   845
    def read_command_timings(db: SQL.Database, name: String): List[Properties.T] =
wenzelm@65320
   846
      read_properties(db, name, Session_Info.command_timings)
wenzelm@65286
   847
wenzelm@65320
   848
    def read_ml_statistics(db: SQL.Database, name: String): List[Properties.T] =
wenzelm@65320
   849
      read_properties(db, name, Session_Info.ml_statistics)
wenzelm@65286
   850
wenzelm@65320
   851
    def read_task_statistics(db: SQL.Database, name: String): List[Properties.T] =
wenzelm@65320
   852
      read_properties(db, name, Session_Info.task_statistics)
wenzelm@65286
   853
wenzelm@65320
   854
    def read_build_log(db: SQL.Database, name: String,
wenzelm@65291
   855
      command_timings: Boolean = false,
wenzelm@65291
   856
      ml_statistics: Boolean = false,
wenzelm@65291
   857
      task_statistics: Boolean = false): Build_Log.Session_Info =
wenzelm@65291
   858
    {
wenzelm@65291
   859
      Build_Log.Session_Info(
wenzelm@65320
   860
        session_timing = read_session_timing(db, name),
wenzelm@65320
   861
        command_timings = if (command_timings) read_command_timings(db, name) else Nil,
wenzelm@65320
   862
        ml_statistics = if (ml_statistics) read_ml_statistics(db, name) else Nil,
wenzelm@65320
   863
        task_statistics = if (task_statistics) read_task_statistics(db, name) else Nil)
wenzelm@65291
   864
    }
wenzelm@65285
   865
wenzelm@65320
   866
    def read_build(db: SQL.Database, name: String): Option[Build.Session_Info] =
wenzelm@65698
   867
      db.using_statement(Session_Info.table.select(Session_Info.build_columns,
wenzelm@65699
   868
        Session_Info.session_name.where_equal(name)))(stmt =>
wenzelm@65285
   869
      {
wenzelm@65285
   870
        val rs = stmt.executeQuery
wenzelm@65285
   871
        if (!rs.next) None
wenzelm@65285
   872
        else {
wenzelm@65285
   873
          Some(
wenzelm@65281
   874
            Build.Session_Info(
wenzelm@65324
   875
              split_lines(db.string(rs, Session_Info.sources)),
wenzelm@65324
   876
              split_lines(db.string(rs, Session_Info.input_heaps)),
wenzelm@65324
   877
              db.string(rs, Session_Info.output_heap) match { case "" => None case s => Some(s) },
wenzelm@65324
   878
              db.int(rs, Session_Info.return_code)))
wenzelm@65281
   879
        }
wenzelm@65281
   880
      })
wenzelm@65281
   881
  }
wenzelm@62631
   882
}