-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assistant.jr
106 lines (87 loc) · 2.51 KB
/
Assistant.jr
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
import edu.ucdavis.jr.*;
import java.util.ArrayList;
import java.util.ArrayDeque;
import java.util.Collection;
public class Assistant {
private ArrayList<Table> tables;
private ArrayDeque<ServingArea.Glass> glasses = new ArrayDeque<ServingArea.Glass>();
private ArrayDeque<ServingArea.Cup> cups = new ArrayDeque<ServingArea.Cup>();
private Clock clock;
private ServingArea sa;
private op void alarm ();
private cap void () pubIsEmpty;
private cap void () assistantDone;
public Assistant(Collection<Table> tables, Clock clock, ServingArea sa, cap void () pubIsEmpty, cap void () assistantDone) {
this.tables = new ArrayList<Table>(tables);
this.clock = clock;
this.sa = sa;
this.assistantDone = assistantDone;
this.pubIsEmpty = pubIsEmpty;
}
/* Main loop
* Cleans tables sequentially. Checks if pub is empty
*/
process work {
int nTables = Constants.N_OF_TABLES;
int i = 0;
while (true) {
cleanTable(i);
i = (i + 1) % Constants.N_OF_TABLES;
send clock.setAlarm(alarm, Constants.REST);
receive alarm();
inni void pubIsEmpty() {
break;
}
[] else {
;
}
}
doLastRounds();
System.out.println("Assistant: Finished, I go home.");
send assistantDone();
}
public void doLastRounds() {
for (int i = 0; i < tables.size(); i++) {
cleanTable(i);
}
}
public void cleanTable(int i) {
System.out.println("Cleaning table number " + i);
Table t = tables.get(i);
// Get all glasses from the table
ServingArea.Glass glass;
glass = t.getGlass();
while (glass != null) {
glasses.push(glass);
glass = t.getGlass();
}
// Get all cups from the table
ServingArea.Cup cup;
cup = t.getCup();
while (cup != null) {
cups.push(cup);
cup = t.getCup();
}
// Go wash the glasses
for (ServingArea.Glass g : glasses) {
send clock.setAlarm(alarm, Constants.WASH);
receive alarm();
}
// Wash the cups
for (ServingArea.Cup c : cups) {
send clock.setAlarm(alarm, Constants.WASH);
receive alarm();
}
// Replace into cupboard
while (glasses.peek() != null){
call sa.replaceGlass(glasses.poll());
send clock.setAlarm(alarm, Constants.REPLACE);
receive alarm();
}
while (cups.peek() != null){
call sa.replaceCup(cups.poll());
send clock.setAlarm(alarm, Constants.REPLACE);
receive alarm();
}
}
}