overhaul of interpreter, update ships to match; "make test" works now
[fleet.git] / src / edu / berkeley / fleet / interpreter / InterpreterShip.java
1 package edu.berkeley.fleet.interpreter;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.two.*;
4 import java.util.*;
5 import java.io.*;
6
7 /** a ship, which belongs to a fleet and which may have many docks */
8 abstract class InterpreterShip extends FleetTwoShip {
9         
10     LinkedHashMap<String,InterpreterDock> docks = new LinkedHashMap<String,InterpreterDock>();
11
12     /** You should instantiate a bunch of Inboxes and Outboxes in your constructor */
13     public InterpreterShip(Interpreter fleet, ShipDescription sd) {
14         super(fleet, sd);
15     }
16
17     public Iterator<Dock> iterator() {
18         return (Iterator<Dock>)(Object)docks.values().iterator();
19     }
20
21     /**
22      *  Override this method, check inboxes for the data you need, and
23      *  if you find it, deposit results in an outbox; we'll take care
24      *  of the rest.
25      */
26     public abstract void service();
27
28     public final void _service() {
29
30         // service all the docks
31         for(InterpreterDock p : docks.values()) p.service();
32
33         // flushing logic (must come between dock servicing and subclass)
34         boolean someflushing = false;
35         boolean allflushing = true;
36         for(InterpreterDock d : docks.values()) {
37             if (!d.isInputDock()) continue;
38             if (d.flushing) someflushing = true;
39             else allflushing = false;
40         }
41         if (allflushing) {
42             for(InterpreterDock d : docks.values())
43                 if (d.isInputDock())
44                     d.flushing = false;
45             return;
46         } else if (someflushing) {
47             for(InterpreterDock d : docks.values())
48                 if (d.isInputDock() && !d.flushing && d.dataReadyForShip)
49                     d.dataReadyForShip = false;
50         }
51
52         // now pass control to the subclass
53         service();
54     }
55
56     public void reset() {
57         for(InterpreterDock p : docks.values())
58             p.reset();
59     }
60
61     public Interpreter getInterpreter() { return (Interpreter)getFleet(); }
62 }