src/Pure/System/session.scala
author wenzelm
Sat, 25 Sep 2010 13:57:34 +0200
changeset 39963 625a3bc4417b
parent 39961 f4da0428dc78
child 39966 7c351c1c0624
permissions -rw-r--r--
tuned signature;
     1 /*  Title:      Pure/System/session.scala
     2     Author:     Makarius
     3     Options:    :folding=explicit:collapseFolds=1:
     4 
     5 Isabelle session, potentially with running prover.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import scala.actors.TIMEOUT
    12 import scala.actors.Actor
    13 import scala.actors.Actor._
    14 
    15 
    16 object Session
    17 {
    18   /* events */
    19 
    20   case object Global_Settings
    21   case object Perspective
    22   case object Assignment
    23   case class Commands_Changed(set: Set[Command])
    24 
    25   sealed abstract class Phase
    26   case object Inactive extends Phase
    27   case object Startup extends Phase
    28   case object Ready extends Phase
    29   case object Shutdown extends Phase
    30 }
    31 
    32 
    33 class Session(system: Isabelle_System)
    34 {
    35   /* real time parameters */  // FIXME properties or settings (!?)
    36 
    37   // user input (e.g. text edits, cursor movement)
    38   val input_delay = 300
    39 
    40   // prover output (markup, common messages)
    41   val output_delay = 100
    42 
    43   // GUI layout updates
    44   val update_delay = 500
    45 
    46 
    47   /* pervasive event buses */
    48 
    49   val phase_changed = new Event_Bus[(Session.Phase, Session.Phase)]
    50   val global_settings = new Event_Bus[Session.Global_Settings.type]
    51   val raw_messages = new Event_Bus[Isabelle_Process.Result]
    52   val commands_changed = new Event_Bus[Session.Commands_Changed]
    53   val perspective = new Event_Bus[Session.Perspective.type]
    54   val assignments = new Event_Bus[Session.Assignment.type]
    55 
    56 
    57   /* unique ids */
    58 
    59   private var id_count: Document.ID = 0
    60   def new_id(): Document.ID = synchronized {
    61     require(id_count > java.lang.Long.MIN_VALUE)
    62     id_count -= 1
    63     id_count
    64   }
    65 
    66 
    67 
    68   /** buffered command changes (delay_first discipline) **/
    69 
    70   private case object Stop
    71 
    72   private val (_, command_change_buffer) =
    73     Simple_Thread.actor("command_change_buffer", daemon = true)
    74   //{{{
    75   {
    76     import scala.compat.Platform.currentTime
    77 
    78     var changed: Set[Command] = Set()
    79     var flush_time: Option[Long] = None
    80 
    81     def flush_timeout: Long =
    82       flush_time match {
    83         case None => 5000L
    84         case Some(time) => (time - currentTime) max 0
    85       }
    86 
    87     def flush()
    88     {
    89       if (!changed.isEmpty) commands_changed.event(Session.Commands_Changed(changed))
    90       changed = Set()
    91       flush_time = None
    92     }
    93 
    94     def invoke()
    95     {
    96       val now = currentTime
    97       flush_time match {
    98         case None => flush_time = Some(now + output_delay)
    99         case Some(time) => if (now >= time) flush()
   100       }
   101     }
   102 
   103     var finished = false
   104     while (!finished) {
   105       receiveWithin(flush_timeout) {
   106         case command: Command => changed += command; invoke()
   107         case TIMEOUT => flush()
   108         case Stop => finished = true; reply(())
   109         case bad => System.err.println("command_change_buffer: ignoring bad message " + bad)
   110       }
   111     }
   112   }
   113   //}}}
   114 
   115 
   116 
   117   /** main protocol actor **/
   118 
   119   @volatile private var syntax = new Outer_Syntax(system.symbols)
   120   def current_syntax(): Outer_Syntax = syntax
   121 
   122   @volatile private var reverse_syslog = List[XML.Elem]()
   123   def syslog(): String = reverse_syslog.reverse.map(msg => XML.content(msg).mkString).mkString("\n")
   124 
   125   @volatile private var _phase: Session.Phase = Session.Inactive
   126   def phase = _phase
   127   private def phase_=(new_phase: Session.Phase)
   128   {
   129     val old_phase = _phase
   130     _phase = new_phase
   131     phase_changed.event(old_phase, new_phase)
   132   }
   133 
   134   private val global_state = new Volatile(Document.State.init)
   135   def current_state(): Document.State = global_state.peek()
   136 
   137   private case object Interrupt_Prover
   138   private case class Edit_Version(edits: List[Document.Node_Text_Edit])
   139   private case class Start(timeout: Int, args: List[String])
   140 
   141   private val (_, session_actor) = Simple_Thread.actor("session_actor", daemon = true)
   142   {
   143     var prover: Isabelle_Process with Isar_Document = null
   144 
   145 
   146     /* document changes */
   147 
   148     def handle_change(change: Document.Change)
   149     //{{{
   150     {
   151       val previous = change.previous.get_finished
   152       val (node_edits, version) = change.result.get_finished
   153 
   154       var former_assignment = global_state.peek().the_assignment(previous).get_finished
   155       for {
   156         (name, Some(cmd_edits)) <- node_edits
   157         (prev, None) <- cmd_edits
   158         removed <- previous.nodes(name).commands.get_after(prev)
   159       } former_assignment -= removed
   160 
   161       val id_edits =
   162         node_edits map {
   163           case (name, None) => (name, None)
   164           case (name, Some(cmd_edits)) =>
   165             val ids =
   166               cmd_edits map {
   167                 case (c1, c2) =>
   168                   val id1 = c1.map(_.id)
   169                   val id2 =
   170                     c2 match {
   171                       case None => None
   172                       case Some(command) =>
   173                         if (global_state.peek().lookup_command(command.id).isEmpty) {
   174                           global_state.change(_.define_command(command))
   175                           prover.define_command(command.id, system.symbols.encode(command.source))
   176                         }
   177                         Some(command.id)
   178                     }
   179                   (id1, id2)
   180               }
   181             (name -> Some(ids))
   182         }
   183       global_state.change(_.define_version(version, former_assignment))
   184       prover.edit_version(previous.id, version.id, id_edits)
   185     }
   186     //}}}
   187 
   188 
   189     /* prover results */
   190 
   191     def bad_result(result: Isabelle_Process.Result)
   192     {
   193       System.err.println("Ignoring prover result: " + result.message.toString)
   194     }
   195 
   196     def handle_result(result: Isabelle_Process.Result)
   197     //{{{
   198     {
   199       raw_messages.event(result)
   200 
   201       result.properties match {
   202         case Position.Id(state_id) =>
   203           try {
   204             val st = global_state.change_yield(_.accumulate(state_id, result.message))
   205             command_change_buffer ! st.command
   206           }
   207           catch { case _: Document.State.Fail => bad_result(result) }
   208         case _ =>
   209           if (result.is_syslog) {
   210             reverse_syslog ::= result.message
   211             if (result.is_ready) phase = Session.Ready
   212             else if (result.is_exit) phase = Session.Inactive
   213           }
   214           else if (result.is_stdout) { }
   215           else if (result.is_status) {
   216             result.body match {
   217               case List(Isar_Document.Assign(id, edits)) =>
   218                 try {
   219                   val cmds: List[Command] = global_state.change_yield(_.assign(id, edits))
   220                   for (cmd <- cmds) command_change_buffer ! cmd
   221                   assignments.event(Session.Assignment)
   222                 }
   223                 catch { case _: Document.State.Fail => bad_result(result) }
   224               case List(Keyword.Command_Decl(name, kind)) => syntax += (name, kind)
   225               case List(Keyword.Keyword_Decl(name)) => syntax += name
   226               case _ => bad_result(result)
   227             }
   228           }
   229           else bad_result(result)
   230         }
   231     }
   232     //}}}
   233 
   234 
   235     /* main loop */
   236 
   237     var finished = false
   238     while (!finished) {
   239       receive {
   240         case Interrupt_Prover =>
   241           if (prover != null) prover.interrupt
   242 
   243         case Edit_Version(edits) if prover != null =>
   244           val previous = global_state.peek().history.tip.version
   245           val result = Future.fork { Thy_Syntax.text_edits(Session.this, previous.join, edits) }
   246           val change = global_state.change_yield(_.extend_history(previous, edits, result))
   247 
   248           val this_actor = self
   249           change.version.map(_ => {
   250             assignments.await { global_state.peek().is_assigned(previous.get_finished) }
   251             this_actor ! change })
   252 
   253           reply(())
   254 
   255         case change: Document.Change if prover != null => handle_change(change)
   256 
   257         case result: Isabelle_Process.Result => handle_result(result)
   258 
   259         case Start(timeout, args) if prover == null =>
   260           phase = Session.Startup
   261           prover = new Isabelle_Process(system, timeout, self, args:_*) with Isar_Document
   262 
   263         case Stop if phase == Session.Ready =>
   264           global_state.change(_ => Document.State.init)  // FIXME event bus!?
   265           phase = Session.Shutdown
   266           prover.terminate
   267           phase = Session.Inactive
   268           finished = true
   269           reply(())
   270 
   271         case bad if prover != null =>
   272           System.err.println("session_actor: ignoring bad message " + bad)
   273       }
   274     }
   275   }
   276 
   277 
   278 
   279   /** main methods **/
   280 
   281   def start(timeout: Int, args: List[String]) { session_actor ! Start(timeout, args) }
   282 
   283   def stop() { command_change_buffer !? Stop; session_actor !? Stop }
   284 
   285   def interrupt() { session_actor ! Interrupt_Prover }
   286 
   287   def edit_version(edits: List[Document.Node_Text_Edit]) { session_actor !? Edit_Version(edits) }
   288 
   289   def snapshot(name: String, pending_edits: List[Text.Edit]): Document.Snapshot =
   290     global_state.peek().snapshot(name, pending_edits)
   291 }