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