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