src/Pure/System/session.scala
author wenzelm
Sat May 22 20:59:55 2010 +0200 (2010-05-22)
changeset 37065 2a73253b5898
parent 37063 492bc98a8809
child 37129 4c83696b340e
permissions -rw-r--r--
separate event bus and dockable for raw output (stdout);
     1 /*  Title:      Pure/System/session.scala
     2     Author:     Makarius
     3 
     4 Isabelle session, potentially with running prover.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.actors.TIMEOUT
    11 import scala.actors.Actor._
    12 
    13 
    14 object Session
    15 {
    16   /* events */
    17 
    18   case object Global_Settings
    19 
    20 
    21   /* managed entities */
    22 
    23   type Entity_ID = String
    24 
    25   trait Entity
    26   {
    27     val id: Entity_ID
    28     def consume(session: Session, message: XML.Tree): Unit
    29   }
    30 }
    31 
    32 
    33 class Session(system: Isabelle_System)
    34 {
    35   /* pervasive event buses */
    36 
    37   val global_settings = new Event_Bus[Session.Global_Settings.type]
    38   val raw_results = new Event_Bus[Isabelle_Process.Result]
    39   val raw_output = new Event_Bus[Isabelle_Process.Result]
    40   val results = new Event_Bus[Command]
    41 
    42   val command_change = new Event_Bus[Command]
    43 
    44 
    45   /* unique ids */
    46 
    47   private var id_count: BigInt = 0
    48   def create_id(): Session.Entity_ID = synchronized { id_count += 1; "j" + id_count }
    49 
    50 
    51 
    52   /** main actor **/
    53 
    54   @volatile private var syntax = new Outer_Syntax(system.symbols)
    55   def current_syntax: Outer_Syntax = syntax
    56 
    57   @volatile private var entities = Map[Session.Entity_ID, Session.Entity]()
    58   def lookup_entity(id: Session.Entity_ID): Option[Session.Entity] = entities.get(id)
    59   def lookup_command(id: Session.Entity_ID): Option[Command] =
    60     lookup_entity(id) match {
    61       case Some(cmd: Command) => Some(cmd)
    62       case _ => None
    63     }
    64 
    65   private case class Start(timeout: Int, args: List[String])
    66   private case object Stop
    67   private case class Begin_Document(path: String)
    68 
    69   private val session_actor = actor {
    70 
    71     var prover: Isabelle_Process with Isar_Document = null
    72 
    73     def register(entity: Session.Entity) { entities += (entity.id -> entity) }
    74 
    75     var documents = Map[Isar_Document.Document_ID, Document]()
    76     def register_document(doc: Document) { documents += (doc.id -> doc) }
    77 
    78 
    79     /* document changes */
    80 
    81     def handle_change(change: Change)
    82     {
    83       require(change.parent.isDefined)
    84 
    85       val (changes, doc) = change.result.join
    86       val id_changes = changes map {
    87         case (c1, c2) =>
    88           (c1.map(_.id).getOrElse(""),
    89            c2 match {
    90               case None => None
    91               case Some(command) =>
    92                 if (!lookup_command(command.id).isDefined) {
    93                   register(command)
    94                   prover.define_command(command.id, system.symbols.encode(command.source))
    95                 }
    96                 Some(command.id)
    97             })
    98       }
    99       register_document(doc)
   100       prover.edit_document(change.parent.get.id, doc.id, id_changes)
   101     }
   102 
   103 
   104     /* prover results */
   105 
   106     def bad_result(result: Isabelle_Process.Result)
   107     {
   108       System.err.println("Ignoring prover result: " + result.message.toString)
   109     }
   110 
   111     def handle_result(result: Isabelle_Process.Result)
   112     {
   113       raw_results.event(result)
   114 
   115       val target_id: Option[Session.Entity_ID] = Position.get_id(result.props)
   116       val target: Option[Session.Entity] =
   117         target_id match {
   118           case None => None
   119           case Some(id) => lookup_entity(id)
   120         }
   121       if (target.isDefined) target.get.consume(this, result.message)
   122       else if (result.kind == Isabelle_Process.Kind.STATUS) {
   123         // global status message
   124         result.body match {
   125 
   126           // document state assignment
   127           case List(Isar_Document.Assign(edits)) if target_id.isDefined =>
   128             documents.get(target_id.get) match {
   129               case Some(doc) =>
   130                 val states =
   131                   for {
   132                     Isar_Document.Edit(cmd_id, state_id) <- edits
   133                     cmd <- lookup_command(cmd_id)
   134                   } yield {
   135                     val st = cmd.assign_state(state_id)
   136                     register(st)
   137                     (cmd, st)
   138                   }
   139                 doc.assign_states(states)
   140               case None => bad_result(result)
   141             }
   142 
   143           // keyword declarations
   144           case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
   145           case List(Keyword.Keyword_Decl(name)) => syntax += name
   146 
   147           case _ => if (!result.is_ready) bad_result(result)
   148         }
   149       }
   150       else if (result.kind == Isabelle_Process.Kind.EXIT)
   151         prover = null
   152       else if (result.is_raw)
   153         raw_output.event(result)
   154       else if (!result.is_system)   // FIXME syslog (!?)
   155         bad_result(result)
   156     }
   157 
   158 
   159     /* prover startup */
   160 
   161     def startup_error(): String =
   162     {
   163       val buf = new StringBuilder
   164       while (
   165         receiveWithin(0) {
   166           case result: Isabelle_Process.Result =>
   167             if (result.is_raw) {
   168               for (text <- XML.content(result.message))
   169                 buf.append(text)
   170             }
   171             true
   172           case TIMEOUT => false
   173         }) {}
   174       buf.toString
   175     }
   176 
   177     def prover_startup(timeout: Int): Option[String] =
   178     {
   179       receiveWithin(timeout) {
   180         case result: Isabelle_Process.Result
   181           if result.kind == Isabelle_Process.Kind.INIT =>
   182           while (receive {
   183             case result: Isabelle_Process.Result =>
   184               handle_result(result); !result.is_ready
   185             }) {}
   186           None
   187 
   188         case result: Isabelle_Process.Result
   189           if result.kind == Isabelle_Process.Kind.EXIT =>
   190           Some(startup_error())
   191 
   192         case TIMEOUT =>  // FIXME clarify
   193           prover.kill; Some(startup_error())
   194       }
   195     }
   196 
   197 
   198     /* main loop */
   199 
   200     val xml_cache = new XML.Cache(131071)
   201 
   202     loop {
   203       react {
   204         case Start(timeout, args) =>
   205           if (prover == null) {
   206             prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
   207             val origin = sender
   208             val opt_err = prover_startup(timeout)
   209             if (opt_err.isDefined) prover = null
   210             origin ! opt_err
   211           }
   212           else reply(None)
   213 
   214         case Stop =>  // FIXME clarify; synchronous
   215           if (prover != null) {
   216             prover.kill
   217             prover = null
   218           }
   219 
   220         case Begin_Document(path: String) if prover != null =>
   221           val id = create_id()
   222           val doc = Document.empty(id)
   223           register_document(doc)
   224           prover.begin_document(id, path)
   225           reply(doc)
   226 
   227         case change: Change if prover != null =>
   228           handle_change(change)
   229 
   230         case result: Isabelle_Process.Result =>
   231           handle_result(result.cache(xml_cache))
   232 
   233         case TIMEOUT =>  // FIXME clarify!
   234 
   235         case bad if prover != null =>
   236           System.err.println("session_actor: ignoring bad message " + bad)
   237       }
   238     }
   239   }
   240 
   241 
   242   /* main methods */
   243 
   244   def start(timeout: Int, args: List[String]): Option[String] =
   245     (session_actor !? Start(timeout, args)).asInstanceOf[Option[String]]
   246 
   247   def stop() { session_actor ! Stop }
   248   def input(change: Change) { session_actor ! change }
   249 
   250   def begin_document(path: String): Document =
   251     (session_actor !? Begin_Document(path)).asInstanceOf[Document]
   252 }