-
Notifications
You must be signed in to change notification settings - Fork 0
/
Room.java
277 lines (230 loc) · 9.8 KB
/
Room.java
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import jason.asSyntax.*;
import jason.environment.Environment;
import jason.environment.grid.Location;
import java.util.logging.Logger;
public class Room extends Environment {
//General actions associated to the Patient
public static final Literal takeMedicinal = Literal.parseLiteral("take(medicinal)");
public static final Literal sipWater = Literal.parseLiteral("sipWater");
public static final Literal giveMoney = Literal.parseLiteral("giveMoney(Amount)");
public static final Literal hasMedicinal = Literal.parseLiteral("hasMed(patient,medicinal)");
//Actions associated to the Nurse 1
public static final Literal goToCabinet1 = Literal.parseLiteral("goTo(robotNurse1,cabinet)");
public static final Literal goToPatient = Literal.parseLiteral("goTo(robotNurse1,patient)");
public static final Literal goToStartNurse1 = Literal.parseLiteral("goTo(robotNurse1,start)");
public static final Literal openCabinet = Literal.parseLiteral("open(cabinet)");
public static final Literal closeCabinet = Literal.parseLiteral("close(cabinet)");
public static final Literal giveMedicinal = Literal.parseLiteral("give(medicinal)");
public static final Literal avoidObst = Literal.parseLiteral("avoidObst(robotNurse1)");
//Actions associated to the Nurse 2
public static final Literal goToStart = Literal.parseLiteral("goTo(robotNurse2,start)");
public static final Literal goToDoor = Literal.parseLiteral("goTo(robotNurse2,door)");
public static final Literal goToCabinet2 = Literal.parseLiteral("goTo(robotNurse2,cabinet)");
//Actions associated to the Robot Emergency
public static final Literal goToPhone = Literal.parseLiteral("goTo(robotEmergency,phone)");
public static final Literal goToStartEmerg = Literal.parseLiteral("goTo(robotEmergency,start)");
public static final Literal callEmerg = Literal.parseLiteral("call");
public static final Literal endCallEmerg = Literal.parseLiteral("endCall");
public static final Literal untrustAgent = Literal.parseLiteral("untrust");
public static final Literal endUntrustAgent = Literal.parseLiteral("untrustEnd");
//Actions associated to the Payment Manager
public static final Literal goToDoorManager = Literal.parseLiteral("goTo(paymentManager,door)");
public static final Literal goToStartManager = Literal.parseLiteral("goTo(paymentManager,start)");
public static final Literal goToPatientManager = Literal.parseLiteral("goTo(paymentManager,patient)");
static Logger logger = Logger.getLogger(Room.class.getName());
RoomModel model;
@Override
public void init(String[] args) {
model = new RoomModel();
// the GUI starts only if we execute the MAS environment with "on" parameter
if (args.length == 1 && args[0].equals("on")) {
RoomView view = new RoomView(model);
model.setView(view);
}
updatePercepts();
}
void updatePercepts() {
clearPercepts("robotNurse1");
clearPercepts("paymentManager");
clearPercepts("robotNurse2");
clearPercepts("patient");
clearPercepts("robotEmergency");
Location locNurse1 = model.getAgPos(0); //id 0 is the nurse1
Location locEmerg= model.getAgPos(1); //id 1 is the emergency robot
Location locNurse2 = model.getAgPos(2); //id 2 is the nurse2
Location locPayManager = model.getAgPos(3); //id 3 is the payment manager
if (locNurse1.equals(model.locCabinet)) {
addPercept("robotNurse1", goToCabinet1);
}
if (locNurse1.equals(model.locPatient)) {
addPercept("robotNurse1", goToPatient);
}
if (locNurse1.equals(model.locNurse1)) {
addPercept("robotNurse1", goToStartNurse1);
}
if (locNurse2.equals(model.locDoor)) {
addPercept("robotNurse2", goToDoor);
}
if (locNurse2.equals(model.locCabinet)) {
addPercept("robotNurse2", goToCabinet2);
}
if (locNurse2.equals(model.locNurse2)) {
addPercept("robotNurse2", goToStart);
}
if(locEmerg.equals(model.locPhone)){
addPercept("robotEmergency", goToPhone);
}
if(locEmerg.equals(model.locRobotEmerg)){
addPercept("robotEmergency", goToStartEmerg);
}
if(locPayManager.equals(model.locDoor)){
addPercept("paymentManager", goToDoorManager);
}
if(locPayManager.equals(model.locPayManager)){
addPercept("paymentManager", goToStartManager);
}
if(locPayManager.equals(model.locPatient)){
addPercept("paymentManager", goToPatientManager);
}
if (model.isCabinetOpen) {
addPercept("robotNurse1", Literal.parseLiteral("stock(medicinal,"+model.availableMeds+")"));
}
if (model.hasMoney) {
addPercept("paymentManager", Literal.parseLiteral("hasMoney(money,"+model.money+")"));
addPercept("paymentManager", Literal.parseLiteral("hasMoney(money,"+model.money+","+model.price+")"));
}
if (model.sipCount > 0) {
addPercept("robotNurse1", hasMedicinal);
addPercept("patient", hasMedicinal);
}
if (model.hasObstacle) {
addPercept("robotNurse1", Literal.parseLiteral("hasObstacle("+model.mark+")"));
System.out.println("ciao room true");
}
if (model.hasObstacle == false) {
addPercept("robotNurse1", Literal.parseLiteral("hasObstacle("+model.mark+")"));
System.out.println("ciao room false");
}
}
@Override
public boolean executeAction(String ag, Structure action) {
System.out.println("["+ag+"] doing: "+action);
boolean result = false;
if (action.equals(openCabinet)) {
result = model.openCabinet();
} else if (action.equals(closeCabinet)) {
result = model.closeCabinet();
} else if (action.getFunctor().equals("move")) {
String agent = action.getTerm(0).toString();
String loc = action.getTerm(1).toString();
Location dest = null;
switch(agent) {
case "robotNurse2":
if (loc.equals("cabinet")) {
dest = model.locCabinet;
} else if (loc.equals("door")) {
dest = model.locDoor;
} else if (loc.equals("start")) {
dest = model.locNurse2;
}
try {
result = model.moveNurse2(dest);
} catch (Exception e) {
logger.info("Failed to move Nurse2: "+e);
}
break;
case "robotNurse1":
if (loc.equals("cabinet")) {
dest = model.locCabinet;
} else if (loc.equals("patient")) {
dest = model.locPatient;
}else if (loc.equals("start")) {
dest = model.locNurse1;
}
try {
result = model.moveNurse1(dest);
} catch (Exception e) {
logger.info("Failed to move Nurse1: "+e);
}
break;
case "robotEmergency":
if (loc.equals("phone")) {
dest = model.locPhone;
} else if (loc.equals("start")) {
dest = model.locRobotEmerg;
}
try {
result = model.moveEmerg(dest);
} catch (Exception e) {
logger.info("Failed to move robotEmergency: "+e);
}
break;
case "paymentManager":
if (loc.equals("door")) {
dest = model.locDoor;
} else if (loc.equals("start")) {
dest = model.locPayManager;
} else if (loc.equals("patient")) {
dest = model.locPatient;
}
try {
result = model.movePayManager(dest);
} catch (Exception e) {
logger.info("Failed to move paymentManager: "+e);
}
break;
}
}
else if (action.equals(avoidObst)) {
String agent = action.getTerm(0).toString();
Location dest = new Location (model.table.x - 2, model.table.y);
result = model.avoidObst(dest);
}
else if(action.equals(callEmerg)){
result = model.callEmerg();
} else if(action.equals(endCallEmerg)){
result = model.endCallEmerg();
}else if(action.equals(untrustAgent)){
result = model.untrustAg();
} else if(action.equals(endUntrustAgent)){
result = model.endUntrustAg();
} else if (action.equals(takeMedicinal)) {
result = model.takeMedicinal();
} else if (action.equals(giveMedicinal)) {
result = model.giveMedicinal();
} else if (action.equals(sipWater)) {
result = model.sipWater();
} else if (action.getFunctor().equals("deliver")) {
try {
Thread.sleep(3000);
result = model.addMedicinal( (int)((NumberTerm)action.getTerm(1)).solve());
} catch (Exception e) {
logger.info("Deliver of new medicinals is failed: "+e);
}
} else if (action.getFunctor().equals("pay")) {
try {
Thread.sleep(2000);
result = model.pay( (int)((NumberTerm)action.getTerm(0)).solve());
} catch (Exception e) {
logger.info("Payement for new medicinals is failed: "+e);
}
}
else if (action.getFunctor().equals("giveMoney")) {
try {
Thread.sleep(2000);
result = model.giveMoney( (int)((NumberTerm)action.getTerm(0)).solve());
} catch (Exception e) {
logger.info("Give money to payment manager is failed: "+e);
}
} else {
logger.info("Failed to execute action "+action);
}
if (result) {
updatePercepts();
try {
Thread.sleep(200);
} catch (Exception e) {}
}
return result;
}
}