src/Pure/PIDE/session.scala
author wenzelm
Thu Apr 24 11:01:14 2014 +0200 (2014-04-24)
changeset 56691 ad5d7461b370
parent 56690 69b31dc7256e
child 56704 c2f0ddd14747
permissions -rw-r--r--
tuned signature, in accordance to ML version;
     1 /*  Title:      Pure/PIDE/session.scala
     2     Author:     Makarius
     3     Options:    :folding=explicit:collapseFolds=1:
     4 
     5 PIDE editor session, potentially with running prover process.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import java.util.{Timer, TimerTask}
    12 
    13 import scala.collection.mutable
    14 import scala.collection.immutable.Queue
    15 import scala.actors.TIMEOUT
    16 import scala.actors.Actor._
    17 
    18 
    19 object Session
    20 {
    21   /* change */
    22 
    23   sealed case class Change(
    24     previous: Document.Version,
    25     doc_blobs: Document.Blobs,
    26     syntax_changed: Boolean,
    27     deps_changed: Boolean,
    28     doc_edits: List[Document.Edit_Command],
    29     version: Document.Version)
    30 
    31 
    32   /* events */
    33 
    34   //{{{
    35   case class Statistics(props: Properties.T)
    36   case class Global_Options(options: Options)
    37   case object Caret_Focus
    38   case class Raw_Edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
    39   case class Dialog_Result(id: Document_ID.Generic, serial: Long, result: String)
    40   case class Commands_Changed(
    41     assignment: Boolean, nodes: Set[Document.Node.Name], commands: Set[Command])
    42 
    43   sealed abstract class Phase
    44   case object Inactive extends Phase
    45   case object Startup extends Phase  // transient
    46   case object Failed extends Phase
    47   case object Ready extends Phase
    48   case object Shutdown extends Phase  // transient
    49   //}}}
    50 
    51 
    52   /* protocol handlers */
    53 
    54   abstract class Protocol_Handler
    55   {
    56     def stop(prover: Prover): Unit = {}
    57     val functions: Map[String, (Prover, Prover.Protocol_Output) => Boolean]
    58   }
    59 
    60   class Protocol_Handlers(
    61     handlers: Map[String, Session.Protocol_Handler] = Map.empty,
    62     functions: Map[String, Prover.Protocol_Output => Boolean] = Map.empty)
    63   {
    64     def get(name: String): Option[Protocol_Handler] = handlers.get(name)
    65 
    66     def add(prover: Prover, name: String): Protocol_Handlers =
    67     {
    68       val (handlers1, functions1) =
    69         handlers.get(name) match {
    70           case Some(old_handler) =>
    71             System.err.println("Redefining protocol handler: " + name)
    72             old_handler.stop(prover)
    73             (handlers - name, functions -- old_handler.functions.keys)
    74           case None => (handlers, functions)
    75         }
    76 
    77       val (handlers2, functions2) =
    78         try {
    79           val new_handler = Class.forName(name).newInstance.asInstanceOf[Protocol_Handler]
    80           val new_functions =
    81             for ((a, f) <- new_handler.functions.toList) yield
    82               (a, (msg: Prover.Protocol_Output) => f(prover, msg))
    83 
    84           val dups = for ((a, _) <- new_functions if functions1.isDefinedAt(a)) yield a
    85           if (!dups.isEmpty) error("Duplicate protocol functions: " + commas_quote(dups))
    86 
    87           (handlers1 + (name -> new_handler), functions1 ++ new_functions)
    88         }
    89         catch {
    90           case exn: Throwable =>
    91             System.err.println(Exn.error_message(
    92               "Failed to initialize protocol handler: " + quote(name) + "\n" + Exn.message(exn)))
    93             (handlers1, functions1)
    94         }
    95 
    96       new Protocol_Handlers(handlers2, functions2)
    97     }
    98 
    99     def invoke(msg: Prover.Protocol_Output): Boolean =
   100       msg.properties match {
   101         case Markup.Function(a) if functions.isDefinedAt(a) =>
   102           try { functions(a)(msg) }
   103           catch {
   104             case exn: Throwable =>
   105               System.err.println(Exn.error_message(
   106                 "Failed invocation of protocol function: " + quote(a) + "\n" + Exn.message(exn)))
   107             false
   108           }
   109         case _ => false
   110       }
   111 
   112     def stop(prover: Prover): Protocol_Handlers =
   113     {
   114       for ((_, handler) <- handlers) handler.stop(prover)
   115       new Protocol_Handlers()
   116     }
   117   }
   118 }
   119 
   120 
   121 class Session(val resources: Resources)
   122 {
   123   /* global flags */
   124 
   125   @volatile var timing: Boolean = false
   126   @volatile var verbose: Boolean = false
   127 
   128 
   129   /* tuning parameters */
   130 
   131   def output_delay: Time = Time.seconds(0.1)  // prover output (markup, common messages)
   132   def message_delay: Time = Time.seconds(0.1)  // prover input/output messages
   133   def prune_delay: Time = Time.seconds(60.0)  // prune history -- delete old versions
   134   def prune_size: Int = 0  // size of retained history
   135   def syslog_limit: Int = 100
   136   def reparse_limit: Int = 0
   137 
   138 
   139   /* pervasive event buses */
   140 
   141   val statistics = new Event_Bus[Session.Statistics]
   142   val global_options = new Event_Bus[Session.Global_Options]
   143   val caret_focus = new Event_Bus[Session.Caret_Focus.type]
   144   val raw_edits = new Event_Bus[Session.Raw_Edits]
   145   val commands_changed = new Event_Bus[Session.Commands_Changed]
   146   val phase_changed = new Event_Bus[Session.Phase]
   147   val syslog_messages = new Event_Bus[Prover.Output]
   148   val raw_output_messages = new Event_Bus[Prover.Output]
   149   val all_messages = new Event_Bus[Prover.Message]  // potential bottle-neck
   150   val trace_events = new Event_Bus[Simplifier_Trace.Event.type]
   151 
   152 
   153   /** buffered command changes (delay_first discipline) **/
   154 
   155   //{{{
   156   private case object Stop
   157 
   158   private val (_, commands_changed_buffer) =
   159     Simple_Thread.actor("commands_changed_buffer", daemon = true)
   160   {
   161     var finished = false
   162     while (!finished) {
   163       receive {
   164         case Stop => finished = true; reply(())
   165         case changed: Session.Commands_Changed => commands_changed.event(changed)
   166         case bad => System.err.println("commands_changed_buffer: ignoring bad message " + bad)
   167       }
   168     }
   169   }
   170   //}}}
   171 
   172 
   173   /** pipelined change parsing **/
   174 
   175   //{{{
   176   private case class Text_Edits(
   177     previous: Future[Document.Version],
   178     doc_blobs: Document.Blobs,
   179     text_edits: List[Document.Edit_Text],
   180     version_result: Promise[Document.Version])
   181 
   182   private val (_, change_parser) = Simple_Thread.actor("change_parser", daemon = true)
   183   {
   184     var finished = false
   185     while (!finished) {
   186       receive {
   187         case Stop => finished = true; reply(())
   188 
   189         case Text_Edits(previous, doc_blobs, text_edits, version_result) =>
   190           val prev = previous.get_finished
   191           val change =
   192             Timing.timeit("parse_change", timing) {
   193               resources.parse_change(reparse_limit, prev, doc_blobs, text_edits)
   194             }
   195           version_result.fulfill(change.version)
   196           sender ! change
   197 
   198         case bad => System.err.println("change_parser: ignoring bad message " + bad)
   199       }
   200     }
   201   }
   202   //}}}
   203 
   204 
   205 
   206   /** main protocol actor **/
   207 
   208   /* global state */
   209 
   210   private val syslog = Synchronized(Queue.empty[XML.Elem])
   211   def current_syslog(): String = cat_lines(syslog.value.iterator.map(XML.content))
   212 
   213   @volatile private var _phase: Session.Phase = Session.Inactive
   214   private def phase_=(new_phase: Session.Phase)
   215   {
   216     _phase = new_phase
   217     phase_changed.event(new_phase)
   218   }
   219   def phase = _phase
   220   def is_ready: Boolean = phase == Session.Ready
   221 
   222   private val global_state = Synchronized(Document.State.init)
   223   def current_state(): Document.State = global_state.value
   224 
   225   def recent_syntax(): Prover.Syntax =
   226   {
   227     val version = current_state().recent_finished.version.get_finished
   228     version.syntax getOrElse resources.base_syntax
   229   }
   230 
   231   def snapshot(name: Document.Node.Name = Document.Node.Name.empty,
   232       pending_edits: List[Text.Edit] = Nil): Document.Snapshot =
   233     global_state.value.snapshot(name, pending_edits)
   234 
   235 
   236   /* protocol handlers */
   237 
   238   @volatile private var _protocol_handlers = new Session.Protocol_Handlers()
   239 
   240   def protocol_handler(name: String): Option[Session.Protocol_Handler] =
   241     _protocol_handlers.get(name)
   242 
   243 
   244   /* theory files */
   245 
   246   def header_edit(name: Document.Node.Name, header: Document.Node.Header): Document.Edit_Text =
   247   {
   248     val header1 =
   249       if (resources.loaded_theories(name.theory))
   250         header.error("Cannot update finished theory " + quote(name.theory))
   251       else header
   252     (name, Document.Node.Deps(header1))
   253   }
   254 
   255 
   256   /* actor messages */
   257 
   258   private case class Start(name: String, args: List[String])
   259   private case class Cancel_Exec(exec_id: Document_ID.Exec)
   260   private case class Protocol_Command(name: String, args: List[String])
   261   private case class Messages(msgs: List[Prover.Message])
   262   private case class Update_Options(options: Options)
   263 
   264   private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
   265   {
   266     val this_actor = self
   267 
   268     var prune_next = Time.now() + prune_delay
   269 
   270 
   271     /* buffered prover messages */
   272 
   273     object receiver
   274     {
   275       private var buffer = new mutable.ListBuffer[Prover.Message]
   276 
   277       private def flush(): Unit = synchronized {
   278         if (!buffer.isEmpty) {
   279           val msgs = buffer.toList
   280           this_actor ! Messages(msgs)
   281           buffer = new mutable.ListBuffer[Prover.Message]
   282         }
   283       }
   284       def invoke(msg: Prover.Message): Unit = synchronized {
   285         msg match {
   286           case _: Prover.Input =>
   287             buffer += msg
   288           case output: Prover.Protocol_Output if output.properties == Markup.Flush =>
   289             flush()
   290           case output: Prover.Output =>
   291             buffer += msg
   292             if (output.is_syslog)
   293               syslog.change(queue =>
   294                 {
   295                   val queue1 = queue.enqueue(output.message)
   296                   if (queue1.length > syslog_limit) queue1.dequeue._2 else queue1
   297                 })
   298         }
   299       }
   300 
   301       private val timer = new Timer("session_actor.receiver", true)
   302       timer.schedule(new TimerTask { def run = flush }, message_delay.ms, message_delay.ms)
   303 
   304       def cancel() { timer.cancel() }
   305     }
   306 
   307     var prover: Option[Prover] = None
   308 
   309 
   310     /* delayed command changes */
   311 
   312     object delay_commands_changed
   313     {
   314       private var changed_assignment: Boolean = false
   315       private var changed_nodes: Set[Document.Node.Name] = Set.empty
   316       private var changed_commands: Set[Command] = Set.empty
   317 
   318       private var flush_time: Option[Time] = None
   319 
   320       def flush_timeout: Time =
   321         flush_time match {
   322           case None => Time.seconds(5.0)
   323           case Some(time) => (time - Time.now()) max Time.zero
   324         }
   325 
   326       def flush()
   327       {
   328         if (changed_assignment || !changed_nodes.isEmpty || !changed_commands.isEmpty)
   329           commands_changed_buffer !
   330             Session.Commands_Changed(changed_assignment, changed_nodes, changed_commands)
   331         changed_assignment = false
   332         changed_nodes = Set.empty
   333         changed_commands = Set.empty
   334         flush_time = None
   335       }
   336 
   337       def invoke(assign: Boolean, commands: List[Command])
   338       {
   339         changed_assignment |= assign
   340         for (command <- commands) {
   341           changed_nodes += command.node_name
   342           changed_commands += command
   343         }
   344         val now = Time.now()
   345         flush_time match {
   346           case None => flush_time = Some(now + output_delay)
   347           case Some(time) => if (now >= time) flush()
   348         }
   349       }
   350     }
   351 
   352 
   353     /* raw edits */
   354 
   355     def handle_raw_edits(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
   356     //{{{
   357     {
   358       prover.get.discontinue_execution()
   359 
   360       val previous = global_state.value.history.tip.version
   361       val version = Future.promise[Document.Version]
   362       val change = global_state.change_result(_.continue_history(previous, edits, version))
   363 
   364       raw_edits.event(Session.Raw_Edits(doc_blobs, edits))
   365       change_parser ! Text_Edits(previous, doc_blobs, edits, version)
   366     }
   367     //}}}
   368 
   369 
   370     /* resulting changes */
   371 
   372     def handle_change(change: Session.Change)
   373     //{{{
   374     {
   375       def id_command(command: Command)
   376       {
   377         for {
   378           digest <- command.blobs_digests
   379           if !global_state.value.defined_blob(digest)
   380         } {
   381           change.doc_blobs.get(digest) match {
   382             case Some(blob) =>
   383               global_state.change(_.define_blob(digest))
   384               prover.get.define_blob(digest, blob.bytes)
   385             case None =>
   386               System.err.println("Missing blob for SHA1 digest " + digest)
   387           }
   388         }
   389 
   390         if (!global_state.value.defined_command(command.id)) {
   391           global_state.change(_.define_command(command))
   392           prover.get.define_command(command)
   393         }
   394       }
   395       change.doc_edits foreach {
   396         case (_, edit) =>
   397           edit foreach { case (c1, c2) => c1 foreach id_command; c2 foreach id_command }
   398       }
   399 
   400       val assignment = global_state.value.the_assignment(change.previous).check_finished
   401       global_state.change(_.define_version(change.version, assignment))
   402       prover.get.update(change.previous.id, change.version.id, change.doc_edits)
   403       resources.commit(change)
   404     }
   405     //}}}
   406 
   407 
   408     /* prover output */
   409 
   410     def handle_output(output: Prover.Output)
   411     //{{{
   412     {
   413       def bad_output()
   414       {
   415         if (verbose)
   416           System.err.println("Ignoring prover output: " + output.message.toString)
   417       }
   418 
   419       def accumulate(state_id: Document_ID.Generic, message: XML.Elem)
   420       {
   421         try {
   422           val st = global_state.change_result(_.accumulate(state_id, message))
   423           delay_commands_changed.invoke(false, List(st.command))
   424         }
   425         catch {
   426           case _: Document.State.Fail => bad_output()
   427         }
   428       }
   429 
   430       output match {
   431         case msg: Prover.Protocol_Output =>
   432           val handled = _protocol_handlers.invoke(msg)
   433           if (!handled) {
   434             msg.properties match {
   435               case Markup.Protocol_Handler(name) =>
   436                 _protocol_handlers = _protocol_handlers.add(prover.get, name)
   437 
   438               case Protocol.Command_Timing(state_id, timing) =>
   439                 val message = XML.elem(Markup.STATUS, List(XML.Elem(Markup.Timing(timing), Nil)))
   440                 accumulate(state_id, prover.get.xml_cache.elem(message))
   441 
   442               case Markup.Assign_Update =>
   443                 msg.text match {
   444                   case Protocol.Assign_Update(id, update) =>
   445                     try {
   446                       val cmds = global_state.change_result(_.assign(id, update))
   447                       delay_commands_changed.invoke(true, cmds)
   448                     }
   449                     catch { case _: Document.State.Fail => bad_output() }
   450                   case _ => bad_output()
   451                 }
   452                 // FIXME separate timeout event/message!?
   453                 if (prover.isDefined && Time.now() > prune_next) {
   454                   val old_versions = global_state.change_result(_.prune_history(prune_size))
   455                   if (!old_versions.isEmpty) prover.get.remove_versions(old_versions)
   456                   prune_next = Time.now() + prune_delay
   457                 }
   458 
   459               case Markup.Removed_Versions =>
   460                 msg.text match {
   461                   case Protocol.Removed(removed) =>
   462                     try {
   463                       global_state.change(_.removed_versions(removed))
   464                     }
   465                     catch { case _: Document.State.Fail => bad_output() }
   466                   case _ => bad_output()
   467                 }
   468 
   469               case Markup.ML_Statistics(props) =>
   470                 statistics.event(Session.Statistics(props))
   471 
   472               case Markup.Task_Statistics(props) =>
   473                 // FIXME
   474 
   475               case _ => bad_output()
   476             }
   477           }
   478         case _ =>
   479           output.properties match {
   480             case Position.Id(state_id) =>
   481               accumulate(state_id, output.message)
   482 
   483             case _ if output.is_init =>
   484               phase = Session.Ready
   485 
   486             case Markup.Return_Code(rc) if output.is_exit =>
   487               if (rc == 0) phase = Session.Inactive
   488               else phase = Session.Failed
   489 
   490             case _ => raw_output_messages.event(output)
   491           }
   492         }
   493     }
   494     //}}}
   495 
   496 
   497     /* main loop */
   498 
   499     //{{{
   500     var finished = false
   501     while (!finished) {
   502       receiveWithin(delay_commands_changed.flush_timeout.ms) {
   503         case TIMEOUT => delay_commands_changed.flush()
   504 
   505         case Start(name, args) if prover.isEmpty =>
   506           if (phase == Session.Inactive || phase == Session.Failed) {
   507             phase = Session.Startup
   508             prover = Some(resources.start_prover(receiver.invoke _, name, args))
   509           }
   510 
   511         case Stop =>
   512           if (phase == Session.Ready) {
   513             _protocol_handlers = _protocol_handlers.stop(prover.get)
   514             global_state.change(_ => Document.State.init)  // FIXME event bus!?
   515             phase = Session.Shutdown
   516             prover.get.terminate
   517             prover = None
   518             phase = Session.Inactive
   519           }
   520           finished = true
   521           receiver.cancel()
   522           reply(())
   523 
   524         case Update_Options(options) =>
   525           if (prover.isDefined && is_ready) {
   526             prover.get.options(options)
   527             handle_raw_edits(Document.Blobs.empty, Nil)
   528           }
   529           global_options.event(Session.Global_Options(options))
   530           reply(())
   531 
   532         case Cancel_Exec(exec_id) if prover.isDefined =>
   533           prover.get.cancel_exec(exec_id)
   534 
   535         case Session.Raw_Edits(doc_blobs, edits) if prover.isDefined =>
   536           handle_raw_edits(doc_blobs, edits)
   537           reply(())
   538 
   539         case Session.Dialog_Result(id, serial, result) if prover.isDefined =>
   540           prover.get.dialog_result(serial, result)
   541           handle_output(new Prover.Output(Protocol.Dialog_Result(id, serial, result)))
   542 
   543         case Protocol_Command(name, args) if prover.isDefined =>
   544           prover.get.protocol_command(name, args:_*)
   545 
   546         case Messages(msgs) =>
   547           msgs foreach {
   548             case input: Prover.Input =>
   549               all_messages.event(input)
   550 
   551             case output: Prover.Output =>
   552               if (output.is_stdout || output.is_stderr) raw_output_messages.event(output)
   553               else handle_output(output)
   554               if (output.is_syslog) syslog_messages.event(output)
   555               all_messages.event(output)
   556           }
   557 
   558         case change: Session.Change
   559         if prover.isDefined && global_state.value.is_assigned(change.previous) =>
   560           handle_change(change)
   561 
   562         case bad if !bad.isInstanceOf[Session.Change] =>
   563           System.err.println("session_actor: ignoring bad message " + bad)
   564       }
   565     }
   566     //}}}
   567   }
   568 
   569 
   570   /* actions */
   571 
   572   def start(name: String, args: List[String])
   573   {
   574     session_actor ! Start(name, args)
   575   }
   576 
   577   def stop()
   578   {
   579     commands_changed_buffer !? Stop
   580     change_parser !? Stop
   581     session_actor !? Stop
   582   }
   583 
   584   def protocol_command(name: String, args: String*)
   585   { session_actor ! Protocol_Command(name, args.toList) }
   586 
   587   def cancel_exec(exec_id: Document_ID.Exec) { session_actor ! Cancel_Exec(exec_id) }
   588 
   589   def update(doc_blobs: Document.Blobs, edits: List[Document.Edit_Text])
   590   { if (!edits.isEmpty) session_actor !? Session.Raw_Edits(doc_blobs, edits) }
   591 
   592   def update_options(options: Options)
   593   { session_actor !? Update_Options(options) }
   594 
   595   def dialog_result(id: Document_ID.Generic, serial: Long, result: String)
   596   { session_actor ! Session.Dialog_Result(id, serial, result) }
   597 }