merged
authorwenzelm
Mon, 29 Dec 2008 15:16:25 +0100
changeset 29193 4410739f97a6
parent 29189 ee8572f3bb57 (current diff)
parent 29192 082ee2a01a6d (diff)
child 29194 53930d089f88
merged
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Pure/General/event_bus.scala	Mon Dec 29 15:16:25 2008 +0100
@@ -0,0 +1,23 @@
+/*  Title:      Pure/General/event_bus.scala
+    Author:     Makarius
+
+Generic event bus.
+*/
+
+package isabelle
+
+import scala.collection.mutable.ListBuffer
+
+
+class EventBus[Event] {
+  type Handler = Event => Unit
+  private val handlers = new ListBuffer[Handler]
+
+  def += (h: Handler) = synchronized { handlers += h }
+  def + (h: Handler) = { this += h; this }
+
+  def -= (h: Handler) = synchronized { handlers -= h }
+  def - (h: Handler) = { this -= h; this }
+
+  def event(e: Event) = synchronized { handlers.toList } foreach (h => h(e))
+}
--- a/src/Pure/IsaMakefile	Mon Dec 29 13:23:53 2008 +0100
+++ b/src/Pure/IsaMakefile	Mon Dec 29 15:16:25 2008 +0100
@@ -121,10 +121,11 @@
 
 ## Scala material
 
-SCALA_FILES = General/markup.scala General/position.scala		\
-  General/symbol.scala General/xml.scala General/yxml.scala		\
-  Isar/isar.scala Thy/thy_header.scala Tools/isabelle_process.scala	\
-  Tools/isabelle_syntax.scala Tools/isabelle_system.scala
+SCALA_FILES = General/event_bus.scala General/markup.scala	\
+  General/position.scala General/symbol.scala General/xml.scala	\
+  General/yxml.scala Isar/isar.scala Thy/thy_header.scala	\
+  Tools/isabelle_process.scala Tools/isabelle_syntax.scala	\
+  Tools/isabelle_system.scala
 
 
 SCALA_TARGET = $(ISABELLE_HOME)/lib/classes/Pure.jar
--- a/src/Pure/Isar/isar.scala	Mon Dec 29 13:23:53 2008 +0100
+++ b/src/Pure/Isar/isar.scala	Mon Dec 29 15:16:25 2008 +0100
@@ -9,8 +9,9 @@
 import java.util.Properties
 
 
-class Isar(isabelle_system: IsabelleSystem, args: String*) extends
-    IsabelleProcess(isabelle_system, args: _*) {
+class Isar(isabelle_system: IsabelleSystem, results: EventBus[IsabelleProcess.Result], args: String*)
+  extends IsabelleProcess(isabelle_system, results, args: _*)
+{
 
   /* basic editor commands */
 
--- a/src/Pure/Tools/isabelle_process.scala	Mon Dec 29 13:23:53 2008 +0100
+++ b/src/Pure/Tools/isabelle_process.scala	Mon Dec 29 15:16:25 2008 +0100
@@ -71,22 +71,24 @@
       if (props == null) kind.toString + " [[" + res + "]]"
       else kind.toString + " " + props.toString + " [[" + res + "]]"
     }
-    def is_raw() = Kind.is_raw(kind)
-    def is_control() = Kind.is_control(kind)
-    def is_system() = Kind.is_system(kind)
+    def is_raw = Kind.is_raw(kind)
+    def is_control = Kind.is_control(kind)
+    def is_system = Kind.is_system(kind)
   }
 
 }
 
 
-class IsabelleProcess(isabelle_system: IsabelleSystem, args: String*) {
-
+class IsabelleProcess(isabelle_system: IsabelleSystem,
+  results: EventBus[IsabelleProcess.Result], args: String*)
+{
   import IsabelleProcess._
+  
 
+  /* demo constructor */
 
-  /* alternative constructors */
-
-  def this(args: String*) = this(new IsabelleSystem, args: _*)
+  def this(args: String*) =
+    this(new IsabelleSystem, new EventBus[IsabelleProcess.Result] + (Console.println(_)), args: _*)
 
 
   /* process information */
@@ -100,21 +102,31 @@
 
   /* results */
 
-  private val results = new LinkedBlockingQueue[Result]
+  private val result_queue = new LinkedBlockingQueue[Result]
 
   private def put_result(kind: Kind.Value, props: Properties, result: String) {
     if (kind == Kind.INIT && props != null) {
       pid = props.getProperty(Markup.PID)
       the_session = props.getProperty(Markup.SESSION)
     }
-    results.put(new Result(kind, props, result))
+    result_queue.put(new Result(kind, props, result))
   }
 
-  def get_result() = results.take
+  private class ResultThread extends Thread("isabelle: results") {
+    override def run() = {
+      var finished = false
+      while (!finished) {
+        val result =
+          try { result_queue.take }
+          catch { case _: NullPointerException => null }
 
-  def try_result() = {
-    val res = results.poll
-    if (res != null) Some(res) else None
+        if (result != null) {
+          results.event(result)  // FIXME try/catch (!??)
+          if (result.kind == Kind.EXIT) finished = true
+        }
+        else finished = true
+      }
+    }
   }
 
 
@@ -334,6 +346,7 @@
   }
 
 
+
   /** main **/
 
   {
@@ -346,7 +359,13 @@
     }
 
 
-    /* message fifo */
+    /* results */
+
+    val result_thread = new ResultThread
+    result_thread.start
+    
+
+    /* messages */
 
     val message_fifo = isabelle_system.mk_fifo()
     def rm_fifo() = isabelle_system.rm_fifo(message_fifo)
@@ -388,5 +407,4 @@
     }.start
 
   }
-
 }