src/Pure/System/session.scala
changeset 34871 e596a0b71f3c
parent 34859 f986d84dd44b
child 35013 f3d491658893
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/System/session.scala	Mon Jan 11 23:00:05 2010 +0100
     1.3 @@ -0,0 +1,250 @@
     1.4 +/*
     1.5 + * Isabelle session, potentially with running prover
     1.6 + *
     1.7 + * @author Makarius
     1.8 + */
     1.9 +
    1.10 +package isabelle
    1.11 +
    1.12 +
    1.13 +import scala.actors.TIMEOUT
    1.14 +import scala.actors.Actor._
    1.15 +
    1.16 +
    1.17 +object Session
    1.18 +{
    1.19 +  /* events */
    1.20 +
    1.21 +  case object Global_Settings
    1.22 +
    1.23 +
    1.24 +  /* managed entities */
    1.25 +
    1.26 +  type Entity_ID = String
    1.27 +
    1.28 +  trait Entity
    1.29 +  {
    1.30 +    val id: Entity_ID
    1.31 +    def consume(session: Session, message: XML.Tree): Unit
    1.32 +  }
    1.33 +}
    1.34 +
    1.35 +
    1.36 +class Session(system: Isabelle_System)
    1.37 +{
    1.38 +  /* pervasive event buses */
    1.39 +
    1.40 +  val global_settings = new Event_Bus[Session.Global_Settings.type]
    1.41 +  val raw_results = new Event_Bus[Isabelle_Process.Result]
    1.42 +  val results = new Event_Bus[Command]
    1.43 +
    1.44 +  val command_change = new Event_Bus[Command]
    1.45 +
    1.46 +
    1.47 +  /* unique ids */
    1.48 +
    1.49 +  private var id_count: BigInt = 0
    1.50 +  def create_id(): Session.Entity_ID = synchronized { id_count += 1; "j" + id_count }
    1.51 +
    1.52 +
    1.53 +
    1.54 +  /** main actor **/
    1.55 +
    1.56 +  @volatile private var syntax = new Outer_Syntax(system.symbols)
    1.57 +  def current_syntax: Outer_Syntax = syntax
    1.58 +
    1.59 +  @volatile private var entities = Map[Session.Entity_ID, Session.Entity]()
    1.60 +  def lookup_entity(id: Session.Entity_ID): Option[Session.Entity] = entities.get(id)
    1.61 +  def lookup_command(id: Session.Entity_ID): Option[Command] =
    1.62 +    lookup_entity(id) match {
    1.63 +      case Some(cmd: Command) => Some(cmd)
    1.64 +      case _ => None
    1.65 +    }
    1.66 +
    1.67 +  private case class Start(timeout: Int, args: List[String])
    1.68 +  private case object Stop
    1.69 +  private case class Begin_Document(path: String)
    1.70 +
    1.71 +  private val session_actor = actor {
    1.72 +
    1.73 +    var prover: Isabelle_Process with Isar_Document = null
    1.74 +
    1.75 +    def register(entity: Session.Entity) { entities += (entity.id -> entity) }
    1.76 +
    1.77 +    var documents = Map[Isar_Document.Document_ID, Document]()
    1.78 +    def register_document(doc: Document) { documents += (doc.id -> doc) }
    1.79 +
    1.80 +
    1.81 +    /* document changes */
    1.82 +
    1.83 +    def handle_change(change: Change)
    1.84 +    {
    1.85 +      require(change.parent.isDefined)
    1.86 +
    1.87 +      val (changes, doc) = change.result.join
    1.88 +      val id_changes = changes map {
    1.89 +        case (c1, c2) =>
    1.90 +          (c1.map(_.id).getOrElse(""),
    1.91 +           c2 match {
    1.92 +              case None => None
    1.93 +              case Some(command) =>
    1.94 +                if (!lookup_command(command.id).isDefined) {
    1.95 +                  register(command)
    1.96 +                  prover.define_command(command.id, system.symbols.encode(command.source))
    1.97 +                }
    1.98 +                Some(command.id)
    1.99 +            })
   1.100 +      }
   1.101 +      register_document(doc)
   1.102 +      prover.edit_document(change.parent.get.id, doc.id, id_changes)
   1.103 +    }
   1.104 +
   1.105 +
   1.106 +    /* prover results */
   1.107 +
   1.108 +    def bad_result(result: Isabelle_Process.Result)
   1.109 +    {
   1.110 +      System.err.println("Ignoring prover result: " + result)
   1.111 +    }
   1.112 +
   1.113 +    def handle_result(result: Isabelle_Process.Result)
   1.114 +    {
   1.115 +      raw_results.event(result)
   1.116 +
   1.117 +      val target_id: Option[Session.Entity_ID] = Position.get_id(result.props)
   1.118 +      val target: Option[Session.Entity] =
   1.119 +        target_id match {
   1.120 +          case None => None
   1.121 +          case Some(id) => lookup_entity(id)
   1.122 +        }
   1.123 +      if (target.isDefined) target.get.consume(this, result.message)
   1.124 +      else if (result.kind == Isabelle_Process.Kind.STATUS) {
   1.125 +        // global status message
   1.126 +        result.body match {
   1.127 +
   1.128 +          // document state assigment
   1.129 +          case List(XML.Elem(Markup.ASSIGN, _, edits)) if target_id.isDefined =>
   1.130 +            documents.get(target_id.get) match {
   1.131 +              case Some(doc) =>
   1.132 +                val states =
   1.133 +                  for {
   1.134 +                    XML.Elem(Markup.EDIT, (Markup.ID, cmd_id) :: (Markup.STATE, state_id) :: _, _)
   1.135 +                      <- edits
   1.136 +                    cmd <- lookup_command(cmd_id)
   1.137 +                  } yield {
   1.138 +                    val st = cmd.assign_state(state_id)
   1.139 +                    register(st)
   1.140 +                    (cmd, st)
   1.141 +                  }
   1.142 +                doc.assign_states(states)
   1.143 +              case None => bad_result(result)
   1.144 +            }
   1.145 +
   1.146 +          // command and keyword declarations
   1.147 +          case List(XML.Elem(Markup.COMMAND_DECL, (Markup.NAME, name) :: (Markup.KIND, kind) :: _, _)) =>
   1.148 +            syntax += (name, kind)
   1.149 +          case List(XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _)) =>
   1.150 +            syntax += name
   1.151 +
   1.152 +          case _ => if (!result.is_ready) bad_result(result)
   1.153 +        }
   1.154 +      }
   1.155 +      else if (result.kind == Isabelle_Process.Kind.EXIT)
   1.156 +        prover = null
   1.157 +      else if (result.kind != Isabelle_Process.Kind.STDIN && !result.is_raw)
   1.158 +        bad_result(result)
   1.159 +    }
   1.160 +
   1.161 +
   1.162 +    /* prover startup */
   1.163 +
   1.164 +    def startup_error(): String =
   1.165 +    {
   1.166 +      val buf = new StringBuilder
   1.167 +      while (
   1.168 +        receiveWithin(0) {
   1.169 +          case result: Isabelle_Process.Result =>
   1.170 +            if (result.is_raw) {
   1.171 +              for (text <- XML.content(result.message))
   1.172 +                buf.append(text)
   1.173 +            }
   1.174 +            true
   1.175 +          case TIMEOUT => false
   1.176 +        }) {}
   1.177 +      buf.toString
   1.178 +    }
   1.179 +
   1.180 +    def prover_startup(timeout: Int): Option[String] =
   1.181 +    {
   1.182 +      receiveWithin(timeout) {
   1.183 +        case result: Isabelle_Process.Result
   1.184 +          if result.kind == Isabelle_Process.Kind.INIT =>
   1.185 +          while (receive {
   1.186 +            case result: Isabelle_Process.Result =>
   1.187 +              handle_result(result); !result.is_ready
   1.188 +            }) {}
   1.189 +          None
   1.190 +
   1.191 +        case result: Isabelle_Process.Result
   1.192 +          if result.kind == Isabelle_Process.Kind.EXIT =>
   1.193 +          Some(startup_error())
   1.194 +
   1.195 +        case TIMEOUT =>  // FIXME clarify
   1.196 +          prover.kill; Some(startup_error())
   1.197 +      }
   1.198 +    }
   1.199 +
   1.200 +
   1.201 +    /* main loop */
   1.202 +
   1.203 +    val xml_cache = new XML.Cache(131071)
   1.204 +
   1.205 +    loop {
   1.206 +      react {
   1.207 +        case Start(timeout, args) =>
   1.208 +          if (prover == null) {
   1.209 +            prover = new Isabelle_Process(system, self, args:_*) with Isar_Document
   1.210 +            val origin = sender
   1.211 +            val opt_err = prover_startup(timeout)
   1.212 +            if (opt_err.isDefined) prover = null
   1.213 +            origin ! opt_err
   1.214 +          }
   1.215 +          else reply(None)
   1.216 +
   1.217 +        case Stop =>  // FIXME clarify; synchronous
   1.218 +          if (prover != null) {
   1.219 +            prover.kill
   1.220 +            prover = null
   1.221 +          }
   1.222 +
   1.223 +        case Begin_Document(path: String) if prover != null =>
   1.224 +          val id = create_id()
   1.225 +          val doc = Document.empty(id)
   1.226 +          register_document(doc)
   1.227 +          prover.begin_document(id, path)
   1.228 +          reply(doc)
   1.229 +
   1.230 +        case change: Change if prover != null =>
   1.231 +          handle_change(change)
   1.232 +
   1.233 +        case result: Isabelle_Process.Result =>
   1.234 +          handle_result(result.cache(xml_cache))
   1.235 +
   1.236 +        case bad if prover != null =>
   1.237 +          System.err.println("session_actor: ignoring bad message " + bad)
   1.238 +      }
   1.239 +    }
   1.240 +  }
   1.241 +
   1.242 +
   1.243 +  /* main methods */
   1.244 +
   1.245 +  def start(timeout: Int, args: List[String]): Option[String] =
   1.246 +    (session_actor !? Start(timeout, args)).asInstanceOf[Option[String]]
   1.247 +
   1.248 +  def stop() { session_actor ! Stop }
   1.249 +  def input(change: Change) { session_actor ! change }
   1.250 +
   1.251 +  def begin_document(path: String): Document =
   1.252 +    (session_actor !? Begin_Document(path)).asInstanceOf[Document]
   1.253 +}