src/Pure/System/session.scala
changeset 53054 8365d7fca3de
parent 52931 ac6648c0c0fb
child 54442 c39972ddd672
equal deleted inserted replaced
53053:6a3320758f0d 53054:8365d7fca3de
    51 
    51 
    52   class Protocol_Handlers(
    52   class Protocol_Handlers(
    53     handlers: Map[String, Session.Protocol_Handler] = Map.empty,
    53     handlers: Map[String, Session.Protocol_Handler] = Map.empty,
    54     functions: Map[String, Isabelle_Process.Output => Boolean] = Map.empty)
    54     functions: Map[String, Isabelle_Process.Output => Boolean] = Map.empty)
    55   {
    55   {
       
    56     def get(name: String): Option[Protocol_Handler] = handlers.get(name)
       
    57 
    56     def add(prover: Prover, name: String): Protocol_Handlers =
    58     def add(prover: Prover, name: String): Protocol_Handlers =
    57     {
    59     {
    58       val (handlers1, functions1) =
    60       val (handlers1, functions1) =
    59         handlers.get(name) match {
    61         handlers.get(name) match {
    60           case Some(old_handler) =>
    62           case Some(old_handler) =>
   221   def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
   223   def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
   222       pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
   224       pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
   223     global_state().snapshot(name, pending_edits)
   225     global_state().snapshot(name, pending_edits)
   224 
   226 
   225 
   227 
       
   228   /* protocol handlers */
       
   229 
       
   230   @volatile private var _protocol_handlers = new Session.Protocol_Handlers()
       
   231 
       
   232   def protocol_handler(name: String): Option[Session.Protocol_Handler] =
       
   233     _protocol_handlers.get(name)
       
   234 
       
   235 
   226   /* theory files */
   236   /* theory files */
   227 
   237 
   228   def header_edit(name: Document.Node.Name, header: Document.Node.Header): Document.Edit_Text =
   238   def header_edit(name: Document.Node.Name, header: Document.Node.Header): Document.Edit_Text =
   229   {
   239   {
   230     val header1 =
   240     val header1 =
   241   private case class Cancel_Exec(exec_id: Document_ID.Exec)
   251   private case class Cancel_Exec(exec_id: Document_ID.Exec)
   242   private case class Change(
   252   private case class Change(
   243     doc_edits: List[Document.Edit_Command],
   253     doc_edits: List[Document.Edit_Command],
   244     previous: Document.Version,
   254     previous: Document.Version,
   245     version: Document.Version)
   255     version: Document.Version)
       
   256   private case class Protocol_Command(name: String, args: List[String])
   246   private case class Messages(msgs: List[Isabelle_Process.Message])
   257   private case class Messages(msgs: List[Isabelle_Process.Message])
   247   private case class Update_Options(options: Options)
   258   private case class Update_Options(options: Options)
   248 
   259 
   249   private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
   260   private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
   250   {
   261   {
   251     val this_actor = self
   262     val this_actor = self
   252 
       
   253     var protocol_handlers = new Session.Protocol_Handlers()
       
   254 
   263 
   255     var prune_next = System.currentTimeMillis() + prune_delay.ms
   264     var prune_next = System.currentTimeMillis() + prune_delay.ms
   256 
   265 
   257 
   266 
   258     /* buffered prover messages */
   267     /* buffered prover messages */
   404           case _: Document.State.Fail => bad_output()
   413           case _: Document.State.Fail => bad_output()
   405         }
   414         }
   406       }
   415       }
   407 
   416 
   408       if (output.is_protocol) {
   417       if (output.is_protocol) {
   409         val handled = protocol_handlers.invoke(output)
   418         val handled = _protocol_handlers.invoke(output)
   410         if (!handled) {
   419         if (!handled) {
   411           output.properties match {
   420           output.properties match {
   412             case Markup.Protocol_Handler(name) =>
   421             case Markup.Protocol_Handler(name) =>
   413               protocol_handlers = protocol_handlers.add(prover.get, name)
   422               _protocol_handlers = _protocol_handlers.add(prover.get, name)
   414 
   423 
   415             case Protocol.Command_Timing(state_id, timing) =>
   424             case Protocol.Command_Timing(state_id, timing) =>
   416               val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil)))
   425               val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil)))
   417               accumulate(state_id, prover.get.xml_cache.elem(message))
   426               accumulate(state_id, prover.get.xml_cache.elem(message))
   418 
   427 
   486             prover = Some(new Isabelle_Process(receiver.invoke _, args) with Protocol)
   495             prover = Some(new Isabelle_Process(receiver.invoke _, args) with Protocol)
   487           }
   496           }
   488 
   497 
   489         case Stop =>
   498         case Stop =>
   490           if (phase == Session.Ready) {
   499           if (phase == Session.Ready) {
   491             protocol_handlers = protocol_handlers.stop(prover.get)
   500             _protocol_handlers = _protocol_handlers.stop(prover.get)
   492             global_state >> (_ => Document.State.init)  // FIXME event bus!?
   501             global_state >> (_ => Document.State.init)  // FIXME event bus!?
   493             phase = Session.Shutdown
   502             phase = Session.Shutdown
   494             prover.get.terminate
   503             prover.get.terminate
   495             prover = None
   504             prover = None
   496             phase = Session.Inactive
   505             phase = Session.Inactive
   516 
   525 
   517         case Session.Dialog_Result(id, serial, result) if prover.isDefined =>
   526         case Session.Dialog_Result(id, serial, result) if prover.isDefined =>
   518           prover.get.dialog_result(serial, result)
   527           prover.get.dialog_result(serial, result)
   519           handle_output(new Isabelle_Process.Output(Protocol.Dialog_Result(id, serial, result)))
   528           handle_output(new Isabelle_Process.Output(Protocol.Dialog_Result(id, serial, result)))
   520 
   529 
       
   530         case Protocol_Command(name, args) if prover.isDefined =>
       
   531           prover.get.protocol_command(name, args:_*)
       
   532 
   521         case Messages(msgs) =>
   533         case Messages(msgs) =>
   522           msgs foreach {
   534           msgs foreach {
   523             case input: Isabelle_Process.Input =>
   535             case input: Isabelle_Process.Input =>
   524               all_messages.event(input)
   536               all_messages.event(input)
   525 
   537 
   554     commands_changed_buffer !? Stop
   566     commands_changed_buffer !? Stop
   555     change_parser !? Stop
   567     change_parser !? Stop
   556     session_actor !? Stop
   568     session_actor !? Stop
   557   }
   569   }
   558 
   570 
       
   571   def protocol_command(name: String, args: String*)
       
   572   { session_actor ! Protocol_Command(name, args.toList) }
       
   573 
   559   def cancel_exec(exec_id: Document_ID.Exec) { session_actor ! Cancel_Exec(exec_id) }
   574   def cancel_exec(exec_id: Document_ID.Exec) { session_actor ! Cancel_Exec(exec_id) }
   560 
   575 
   561   def update(edits: List[Document.Edit_Text])
   576   def update(edits: List[Document.Edit_Text])
   562   { if (!edits.isEmpty) session_actor !? Session.Raw_Edits(edits) }
   577   { if (!edits.isEmpty) session_actor !? Session.Raw_Edits(edits) }
   563 
   578