summaryrefslogtreecommitdiff
path: root/java/EAC/Configuration.java
blob: 426a8ba2a5e63bdd05ff31502aa7239af5ad3bca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
 *  Title:        Configuration
 *  Description:  A container of Event Analysis Configurator primitives,
 *                capable of executing interactions among those primitives
 *                one "tick" at a time.
 */
package EAC;
import java.awt.*;
import java.lang.*;

public class Configuration extends Thread {

  // highest simulation clock value
  public final long MAX_SIM_CLOCK = 1000000;

  // maximum number of primitives in a configuration
  public final int MAX_PRIMITIVES = 100;

  // execution modes
  public final int INITIAL = 0;
  public final int RUNNING = 1;
  public final int STOPPED = 2;

  // current mode
  protected int mode = INITIAL;

  // simulation "clock"
  protected long simulation_time;

  // one simulation time unit (msec)
  public int tick; // will be set by runtime parameter

  // number of missed execution deadlines
  protected int missed_deadlines;

  // simulated resource pool
  protected ResourcePool rp;

  // event queue
  protected Queue eventQ;

  // input and output areas
  protected TextField inputArea;
  protected Label reportArea;

  // collection of primitives
  private Primitive primitive[];

  // number of primitives currently stored
  private int primitive_count;

  public Configuration(TextField i, Label r) {
    inputArea = i;
    reportArea = r;
    eventQ = new Queue();
    clear();
    mode = INITIAL;
  } /* constructor */

  public void clear() {
    primitive_count = 0;
    simulation_time = 0;
    missed_deadlines = 0;
    rp = new ResourcePool();
    primitive = new Primitive[MAX_PRIMITIVES];
    eventQ.clear();
  } /* clear */

  public void restart() {
    int i;

    simulation_time = 0;
    missed_deadlines = 0;
    rp.release(rp.usage());
    eventQ.clear();
    for (i = 0; i < primitive_count; i++)
      primitive[i].restart();

  } /* restart */

  public void setTick(int t) {
    tick = t;
  } /* setTick */

  public void addLabel(Primitive p) {
    reportArea.setText("Need a label");
  } /* addLabel */

  public int getPrimitiveCount() {
    return primitive_count;
  } /* getPrimitiveCount */

  public Primitive getPrimitive(int n) {
    return primitive[n];
  } /* getPrimitive */

  protected void setPrimitive(int n, Primitive p) {
    primitive[n] = p;
  } /* setPrimitive */

  public int addPrimitive(Primitive p) throws TooManyPrimitivesException {
    if (primitive_count == MAX_PRIMITIVES)
      throw new TooManyPrimitivesException("ERROR: Too many primitives");

    primitive[primitive_count++] = p;
    return primitive_count;
  } /* addPrimitive */

  public boolean deletePrimitive(int i) {
    if (primitive[i] == null)
      return false;
    else {
      primitive[i] = null;
      return true;
    }
  } /* deletePrimitive */

  public int leftSideOverlaps(Primitive p) {
    int i;

    for (i = 0; i < primitive_count; i++) {
      if (primitive[i] != null) {
        if ((primitive[i].contains(p.upperLeft())) ||
            (primitive[i].contains(p.lowerLeft())) ||
            (p.contains(primitive[i].lowerLeft())) ||
            (p.contains(primitive[i].upperLeft()))) {
          return i;
        } /* if */
      } /* if */
    } /* for */

    // no overlap detected
    return -1;
  } /* leftSideOverlaps */

  public int rightSideOverlaps(Primitive p) {
    int i;

    for (i = 0; i < primitive_count; i++) {
      if (primitive[i] != null) {
        if ((primitive[i].contains(p.lowerRight())) ||
            (primitive[i].contains(p.upperRight())) ||
            (p.contains(primitive[i].upperRight())) ||
            (p.contains(primitive[i].lowerRight()))) {
          return i;
        } /* if */
      } /* if */
    } /* for */

    // no overlap detected
    return -1;
  } /* rightSideOverlaps */

  public Primitive primitiveContaining (Point p) {
    int i;

    for (i = 0; i < primitive_count; i++)
      if (primitive[i].contains(p))
        return primitive[i];

    return null;
  } /* primitiveContaining */

  public void draw() {
    int i;

    for (i = 0; i < primitive_count; i++)
      if (primitive[i] != null)
        try {
          primitive[i].draw();
        } catch (BoundsException be) {
          // can't happen
        }
  } /* draw */

  public void eventEnqueue(Primitive p, long t) {
    eventQ.enqueue(p,t);
  } /* eventEnqueue */

  public long getTime() {
    return simulation_time;
  } /* getTime */

  public void run() {
    Primitive nextP;

    while (true) {
      simulation_time += 1;
      simulation_time %= MAX_SIM_CLOCK;
      try {
        reportArea.setText("Simulation Time: " +
                         java.lang.Long.toString(simulation_time) +
                         "    Resource Consumption: " +
                         java.lang.Integer.toString(rp.usage()) +
                         "%   Missed Deadlines: " +
                         java.lang.Integer.toString(missed_deadlines) +
                         "    Next Event: " +
                         java.lang.Long.toString(eventQ.frontTime()));

        // wake up all primitives who've set alarms for current time
        while (simulation_time == eventQ.frontTime()) {
          nextP = eventQ.dequeue();
          nextP.wakeup(simulation_time);
        } /* while */
      } catch (EmptyQueueException eqe) {
        System.out.println("Event queue empty.  We're done?");
      } catch (ConnectionException ce) {
        System.out.println("Connection exception in run");
      }

      try {
        sleep(tick);
      }
      catch (InterruptedException ie) {
        System.out.println("Interrupted");
      }
    } /* outer while */
  } /* run */

}