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