src/Pure/PIDE/batch_session.scala
author wenzelm
Wed Jan 14 17:24:55 2015 +0100 (2015-01-14)
changeset 59367 6193bbbbe564
parent 59366 e94df7f6b608
child 59369 7090199d3f78
permissions -rw-r--r--
more type-safe handler interface;
proper progress for Build.Handler;
wenzelm@59362
     1
/*  Title:      Pure/Tools/batch_session.scala
wenzelm@59362
     2
    Author:     Makarius
wenzelm@59362
     3
wenzelm@59362
     4
PIDE session in batch mode.
wenzelm@59362
     5
*/
wenzelm@59362
     6
wenzelm@59362
     7
package isabelle
wenzelm@59362
     8
wenzelm@59362
     9
wenzelm@59362
    10
import isabelle._
wenzelm@59362
    11
wenzelm@59362
    12
wenzelm@59362
    13
object Batch_Session
wenzelm@59362
    14
{
wenzelm@59362
    15
  def batch_session(
wenzelm@59362
    16
    options: Options,
wenzelm@59362
    17
    verbose: Boolean = false,
wenzelm@59362
    18
    dirs: List[Path] = Nil,
wenzelm@59366
    19
    session: String): Boolean =
wenzelm@59362
    20
  {
wenzelm@59362
    21
    val (_, session_tree) =
wenzelm@59362
    22
      Build.find_sessions(options, dirs).selection(false, false, Nil, List(session))
wenzelm@59362
    23
    val session_info = session_tree(session)
wenzelm@59362
    24
    val parent_session =
wenzelm@59362
    25
      session_info.parent getOrElse error("No parent session for " + quote(session))
wenzelm@59362
    26
wenzelm@59362
    27
    if (Build.build(options, new Build.Console_Progress(verbose),
wenzelm@59362
    28
        verbose = verbose, build_heap = true,
wenzelm@59362
    29
        dirs = dirs, sessions = List(parent_session)) != 0)
wenzelm@59362
    30
      new RuntimeException
wenzelm@59362
    31
wenzelm@59362
    32
    val deps = Build.dependencies(Build.Ignore_Progress, false, verbose, false, session_tree)
wenzelm@59362
    33
    val resources =
wenzelm@59362
    34
    {
wenzelm@59362
    35
      val content = deps(parent_session)
wenzelm@59362
    36
      new Resources(content.loaded_theories, content.known_theories, content.syntax)
wenzelm@59362
    37
    }
wenzelm@59362
    38
wenzelm@59362
    39
    val progress = new Build.Console_Progress(verbose)
wenzelm@59366
    40
wenzelm@59366
    41
    val session_result = Future.promise[Unit]
wenzelm@59366
    42
    @volatile var build_theories_result: Option[Promise[Boolean]] = None
wenzelm@59362
    43
wenzelm@59362
    44
    val prover_session = new Session(resources)
wenzelm@59362
    45
wenzelm@59367
    46
    val handler = new Build.Handler(progress, session)
wenzelm@59367
    47
wenzelm@59362
    48
    prover_session.phase_changed +=
wenzelm@59362
    49
      Session.Consumer[Session.Phase](getClass.getName) {
wenzelm@59362
    50
        case Session.Ready =>
wenzelm@59367
    51
          prover_session.add_protocol_handler(handler)
wenzelm@59362
    52
          val master_dir = session_info.dir
wenzelm@59364
    53
          val theories = session_info.theories.map({ case (_, opts, thys) => (opts, thys) })
wenzelm@59366
    54
          build_theories_result =
wenzelm@59366
    55
            Some(Build.build_theories(prover_session, master_dir, theories))
wenzelm@59362
    56
        case Session.Inactive | Session.Failed =>
wenzelm@59366
    57
          session_result.fulfill_result(Exn.Exn(ERROR("Prover process terminated")))
wenzelm@59362
    58
        case Session.Shutdown =>
wenzelm@59366
    59
          session_result.fulfill(())
wenzelm@59362
    60
        case _ =>
wenzelm@59362
    61
      }
wenzelm@59362
    62
wenzelm@59362
    63
    prover_session.start("Isabelle", List("-r", "-q", parent_session))
wenzelm@59362
    64
wenzelm@59366
    65
    session_result.join
wenzelm@59366
    66
    build_theories_result match {
wenzelm@59366
    67
      case None => false
wenzelm@59366
    68
      case Some(promise) => promise.join
wenzelm@59366
    69
    }
wenzelm@59362
    70
  }
wenzelm@59362
    71
}
wenzelm@59362
    72