-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCentrale.java
303 lines (253 loc) · 10.8 KB
/
Centrale.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Environment code for project centrale_nucleaire.mas2j
import jason.asSyntax.*;
import jason.environment.*;
import jason.asSyntax.parser.*;
import javax.swing.*;
import java.util.logging.*;
import java.awt.*;
import javax.swing.border.*;
public class Centrale extends Environment {
private Logger logger = Logger.getLogger("centrale_nucleaire.mas2j."+Centrale.class.getName());
private CentraleGUI gui = new CentraleGUI();
private Object model = new Object();
// The world in which we'll be in
static boolean[][] grid = new boolean [4][4];
// Initial position of the robot
private int pos_squirrel_x = 0;
private int pos_squirrel_y = 0;
private int nbMat = 0;
private static int goal_x, goal_y, goal_xx, goal_yy;
boolean isEmptyCentrale = false;
// The constant terms used for perception
private static final Literal lPos1 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(1));
private static final Literal lPos2 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(2));
private static final Literal lPos3 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(3));
private static final Literal lPos4 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(4));
private static final Literal lPos5 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(5));
private static final Literal lPos6 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(6));
private static final Literal lPos7 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(7));
private static final Literal lPos8 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(8));
private static final Literal lPos9 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(9));
private static final Literal lPos10 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(10));
private static final Literal lPos11 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(11));
private static final Literal lPos12 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(12));
private static final Literal lPos13 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(13));
private static final Literal lPos14 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(14));
private static final Literal lPos15 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(15));
private static final Literal lPos16 = ASSyntax.createLiteral("posRobot", ASSyntax.createNumber(16));
private static final Literal lMaterial = ASSyntax.createLiteral("material"); // it means we perceive there is a material
private static final Literal lNotMaterial = ASSyntax.createLiteral("noMaterial");
private static final Literal lAllDone = ASSyntax.createLiteral("everythingControlled");
private static final Literal lEmptyMatrix = ASSyntax.createLiteral("matrixEmpty");
// Constructor
public Centrale() {
System.out.println("Initializing goals...");
// coordinates of first material
goal_x = 2;
goal_y = 1;
// coordinates of second material
goal_xx = 3;
goal_yy = 2;
/* We define in which position there will be a material (which our robot will have to take the state) in our world
we suppose we are given this data by the human supervisor */
grid[goal_x][goal_y] = true;
grid[goal_xx][goal_yy] = true;
createPercept();
}
public static boolean areAllFalse(boolean[][] array)
{
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
// at the first that is true
if(array[i][j] == true)
return false;
}
}
return true;
}
// All of our actions will be listed there
@Override
public boolean executeAction(String agName, Structure action) {
logger.info("SquiRobot doing : ->"+action);
try {
// 750ms between each action taken
Thread.sleep(700);
} catch (Exception e) {
//e.printStackTrace();
}
// The environment change based on actions taken
if (action.getFunctor().equals("take")) {
if (grid[pos_squirrel_x][pos_squirrel_y]) {
grid[pos_squirrel_x][pos_squirrel_y] = false;
// the material doesn't move, but we suppose we won't have to take it again atm, so false.
nbMat += 1;
logger.info("The state of the material has been taken.");
} else {
logger.info("Not a material there");
Toolkit.getDefaultToolkit().beep();
}
} else if (action.getFunctor().equals("print")) {
//System.out.println("SquirrelRobot saying: " +action.getTerm(0)); // print the 0th argument of print function
} else if (action.getFunctor().equals("puttingIntoMM")) {
//System.out.println("TEST:" +action.getTerm(0));
/*pos_goal_one = ASSyntax.termToObject(action.getTerm(0));
po*s_goal_two = ASSyntax.termToObject(action.getTerm(0));*/
// lpos = 1 + (4 * pos_squirrel_x ) + pos_squirrel_y // as we might optmize later on
}
else if (action.getFunctor().equals("moving")) {
// At the first material's state recovered, we check which one it is and we define our new goal position
if (nbMat == 1 && grid[goal_x][goal_y] == false){
goal_x = goal_xx;
goal_y = goal_yy;
} else if (nbMat == 1 && grid[goal_xx][goal_yy] == false){
goal_xx = goal_x;
goal_yy = goal_y;
// All materials recovered, return where you entered, and go out
} else if (nbMat == 2){
goal_x = 0;
goal_y = 0;
}
if (pos_squirrel_x < goal_x) {
pos_squirrel_x++;
logger.info("new coordinates: [x="+pos_squirrel_x+",y="+pos_squirrel_y+"]");
}
else if(pos_squirrel_y < goal_y) {
pos_squirrel_y++;
logger.info("new coordinates: [x="+pos_squirrel_x+",y="+pos_squirrel_y+"]");
}
else if(pos_squirrel_x > goal_x){
pos_squirrel_x--;
logger.info("new coordinates: [x="+pos_squirrel_x+",y="+pos_squirrel_y+"]");
}
else if(pos_squirrel_y > goal_y){
pos_squirrel_y--;
logger.info("new coordinates: [x="+pos_squirrel_x+",y="+pos_squirrel_y+"]");
}
}
// I come back to the begining of the grid
else if (action.getFunctor().equals("returnBegining")){
//if (pos_squirrel_x == 3 && pos_squirrel_y == 3) {
// If we are in one of the goals position and we have taken all the materials state then we're allowed to go out!
if ( (pos_squirrel_x == goal_x && pos_squirrel_y == goal_y && nbMat == 2) | (pos_squirrel_x == goal_xx && pos_squirrel_y == goal_yy && nbMat == 2) ) {
pos_squirrel_x = 0;
pos_squirrel_y = 0;
}
}
else if (action.getFunctor().equals("leave")){
logger.info("Nobody in the Nuclear Power Plant anymore.");
}
else {
logger.info("The action "+action+" is not implemented!");
return false;
}
createPercept(); // update agents perception for the new world state
gui.paint();
return true;
}
// the functor is an atom
/** Called before the end of MAS execution */
@Override
public void stop() {
super.stop();
gui.setVisible(false);
}
private void createPercept() {
clearPercepts();
// First line of the grid
if (pos_squirrel_x == 0 && pos_squirrel_y == 0){
addPercept(lPos1);
} else if (pos_squirrel_x == 0 && pos_squirrel_y == 1) {
addPercept(lPos2);
} else if (pos_squirrel_x == 0 && pos_squirrel_y == 2) {
addPercept(lPos3);
} else if (pos_squirrel_x == 0 && pos_squirrel_y == 3) {
addPercept(lPos4);
}
// Second line
if (pos_squirrel_x == 1 && pos_squirrel_y == 0){
addPercept(lPos5);
} else if (pos_squirrel_x == 1 && pos_squirrel_y == 1) {
addPercept(lPos6);
} else if (pos_squirrel_x == 1 && pos_squirrel_y == 2) {
addPercept(lPos7);
} else if (pos_squirrel_x == 1 && pos_squirrel_y == 3) {
addPercept(lPos8);
}
// Third line
if (pos_squirrel_x == 2 && pos_squirrel_y == 0){
addPercept(lPos9);
} else if (pos_squirrel_x == 2 && pos_squirrel_y == 1) {
addPercept(lPos10);
} else if (pos_squirrel_x == 2 && pos_squirrel_y == 2) {
addPercept(lPos11);
} else if (pos_squirrel_x == 2 && pos_squirrel_y == 3) {
addPercept(lPos12);
}
// Last and fourth line
if (pos_squirrel_x == 3 && pos_squirrel_y == 0){
addPercept(lPos13);
} else if (pos_squirrel_x == 3 && pos_squirrel_y == 1) {
addPercept(lPos14);
} else if (pos_squirrel_x == 3 && pos_squirrel_y == 2) {
addPercept(lPos15);
} else if (pos_squirrel_x == 3 && pos_squirrel_y == 3) {
addPercept(lPos16);
}
// if there is a material (position of the grid equal to true)
if (grid[pos_squirrel_x][pos_squirrel_y]) {
addPercept(lMaterial);
logger.info("there is a material");
} else {
addPercept(lNotMaterial);
logger.info("there is no material");
}
isEmptyCentrale = areAllFalse(grid);
if (isEmptyCentrale) {
addPercept(lEmptyMatrix);
logger.info("-----------> !! Centrale is empty !! Every material state has been taken. <-----------");
}
/*if ( (pos_squirrel_x == goal_x && pos_squirrel_y == goal_y && nbMat == 2) | (pos_squirrel_x == goal_xx && pos_squirrel_y == goal_yy && nbMat == 2) ) {
addPercept(lAllDone);
logger.info("Work is done. I'm going out.");
//logger.info("cpt="+nbMat);
}*/
} // end createPercept
public class CentraleGUI extends JFrame {
JLabel[][] labels;
// Constructor
public CentraleGUI() {
super("Squirrel Robot");
labels = new JLabel[grid.length][grid.length];
getContentPane().setLayout(new GridLayout(labels.length, labels.length));
for (int j = 0; j < labels.length; j++) {
for (int i = 0; i < labels.length; i++) {
labels[i][j] = new JLabel();
labels[i][j].setBackground(Color.CYAN);
labels[i][j].setOpaque(true);
labels[i][j].setPreferredSize(new Dimension(360,360));
labels[i][j].setHorizontalAlignment(JLabel.CENTER);
labels[i][j].setBorder(new EtchedBorder());
getContentPane().add(labels[i][j]);
}
}
pack();
setVisible(true);
paint();
}
void paint() {
for (int i = 0; i < labels.length; i++) {
for (int j = 0; j < labels.length; j++) {
String square = "<html><center>";
if (pos_squirrel_x == i && pos_squirrel_y == j) {
square += "<font color=\"purple\" size=7><b>SquiBot</b> <br></font>";
}
if (grid[i][j]) {
square += "<font color=\"red\" size=5>material</font>";
}
square += "</center></html>";
labels[i][j].setText(square);
}
}
}
} // end CentraleGUI class
}