mild overhaul of Interpreter; now capable of performing MergeSort
[fleet.git] / src / edu / berkeley / fleet / interpreter / InterpreterShip.java
index 37c10cd..85358fa 100644 (file)
@@ -1,21 +1,23 @@
+
 package edu.berkeley.fleet.interpreter;
 import edu.berkeley.fleet.api.*;
 import edu.berkeley.fleet.two.*;
 import java.util.*;
 import java.io.*;
 
-/** a ship, which belongs to a fleet and which may have many ports */
+/** a ship, which belongs to a fleet and which may have many docks */
 abstract class InterpreterShip extends FleetTwoShip {
         
+    LinkedHashMap<String,InterpreterDock> docks = new LinkedHashMap<String,InterpreterDock>();
+
     /** You should instantiate a bunch of Inboxes and Outboxes in your constructor */
-    public InterpreterShip(Interpreter fleet, String name, ShipDescription sd) {
+    public InterpreterShip(Interpreter fleet, ShipDescription sd) {
         super(fleet, sd);
     }
 
-    private HashMap<String,InterpreterDock> ports = new HashMap<String,InterpreterDock>();
-
-    public Iterator<Dock> iterator() { return (Iterator<Dock>)(Object)ports.values().iterator(); }
-    public Interpreter        getInterpreter() { return (Interpreter)getFleet(); }
+    public Iterator<Dock> iterator() {
+        return (Iterator<Dock>)(Object)docks.values().iterator();
+    }
 
     /**
      *  Override this method, check inboxes for the data you need, and
@@ -25,16 +27,39 @@ abstract class InterpreterShip extends FleetTwoShip {
     public abstract void service();
 
     public final void _service() {
-        for(InterpreterDock p : ports.values()) p.service();
+
+        // service all the docks
+        for(InterpreterDock p : docks.values()) p.service();
+
+        // flushing logic (must come between dock servicing and subclass)
+        boolean someflushing = false;
+        boolean allflushing = true;
+        boolean someempty = false;
+        for(InterpreterDock d : docks.values()) {
+            if (!d.isInputDock()) continue;
+            if (d.flushing) someflushing = true;
+            else allflushing = false;
+            if (!d.flushing && !d.dataReadyForShip) someempty = true;
+        }
+        if (allflushing && someflushing) {
+            for(InterpreterDock d : docks.values())
+                if (d.isInputDock())
+                    d.flushing = false;
+            return;
+        } else if (someflushing && !someempty) {
+            for(InterpreterDock d : docks.values())
+                if (d.isInputDock() && !d.flushing && d.dataReadyForShip)
+                    d.dataReadyForShip = false;
+        }
+
+        // now pass control to the subclass
         service();
     }
 
-    protected void addDock(String name, InterpreterDock port) {
-        ports.put(name, port);
+    public void reset() {
+        for(InterpreterDock p : docks.values())
+            p.reset();
     }
 
-    public void shutdown() {
-        for(InterpreterDock p : ports.values())
-            p.shutdown();
-    }
+    public Interpreter getInterpreter() { return (Interpreter)getFleet(); }
 }