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