src/Pure/System/session.scala
author wenzelm
Wed May 05 22:23:45 2010 +0200 (2010-05-05)
changeset 36676 ac7961d42ac3
parent 35013 f3d491658893
child 36682 3f989067f87d
permissions -rw-r--r--
some rearrangement of Scala sources;
     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(XML.Elem(Markup.ASSIGN, _, edits)) if target_id.isDefined =>
   127             documents.get(target_id.get) match {
   128               case Some(doc) =>
   129                 val states =
   130                   for {
   131                     XML.Elem(Markup.EDIT, (Markup.ID, cmd_id) :: (Markup.STATE, 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           // command and keyword declarations
   143           case List(XML.Elem(Markup.COMMAND_DECL, (Markup.NAME, name) :: (Markup.KIND, kind) :: _, _)) =>
   144             syntax += (name, kind)
   145           case List(XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _)) =>
   146             syntax += name
   147 
   148           case _ => if (!result.is_ready) bad_result(result)
   149         }
   150       }
   151       else if (result.kind == Isabelle_Process.Kind.EXIT)
   152         prover = null
   153       else if (result.kind != Isabelle_Process.Kind.STDIN && !result.is_raw)
   154         bad_result(result)
   155     }
   156 
   157 
   158     /* prover startup */
   159 
   160     def startup_error(): String =
   161     {
   162       val buf = new StringBuilder
   163       while (
   164         receiveWithin(0) {
   165           case result: Isabelle_Process.Result =>
   166             if (result.is_raw) {
   167               for (text <- XML.content(result.message))
   168                 buf.append(text)
   169             }
   170             true
   171           case TIMEOUT => false
   172         }) {}
   173       buf.toString
   174     }
   175 
   176     def prover_startup(timeout: Int): Option[String] =
   177     {
   178       receiveWithin(timeout) {
   179         case result: Isabelle_Process.Result
   180           if result.kind == Isabelle_Process.Kind.INIT =>
   181           while (receive {
   182             case result: Isabelle_Process.Result =>
   183               handle_result(result); !result.is_ready
   184             }) {}
   185           None
   186 
   187         case result: Isabelle_Process.Result
   188           if result.kind == Isabelle_Process.Kind.EXIT =>
   189           Some(startup_error())
   190 
   191         case TIMEOUT =>  // FIXME clarify
   192           prover.kill; Some(startup_error())
   193       }
   194     }
   195 
   196 
   197     /* main loop */
   198 
   199     val xml_cache = new XML.Cache(131071)
   200 
   201     loop {
   202       react {
   203         case Start(timeout, args) =>
   204           if (prover == null) {
   205             prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
   206             val origin = sender
   207             val opt_err = prover_startup(timeout)
   208             if (opt_err.isDefined) prover = null
   209             origin ! opt_err
   210           }
   211           else reply(None)
   212 
   213         case Stop =>  // FIXME clarify; synchronous
   214           if (prover != null) {
   215             prover.kill
   216             prover = null
   217           }
   218 
   219         case Begin_Document(path: String) if prover != null =>
   220           val id = create_id()
   221           val doc = Document.empty(id)
   222           register_document(doc)
   223           prover.begin_document(id, path)
   224           reply(doc)
   225 
   226         case change: Change if prover != null =>
   227           handle_change(change)
   228 
   229         case result: Isabelle_Process.Result =>
   230           handle_result(result.cache(xml_cache))
   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 }