-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoomEnv.java
93 lines (83 loc) · 2.76 KB
/
RoomEnv.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
/* ==============================
/ Locus - RoomEnv
/ Generated at 2015-03-09 02:19:34 -0300
/ ============================== */
import jason.asSyntax.*;
import jason.mas2j.*;
import jason.environment.*;
import java.util.logging.*;
import java.io.*;
import java.util.*;
public class RoomEnv extends Environment {
private Logger logger = Logger.getLogger("RoomEnv_logger");
private Map<String, Boolean> state = new HashMap<String, Boolean>();
private Map<String, String> agents = new HashMap<String, String>();
static final Literal literal0 = Literal.parseLiteral("locked(door)");
static final Literal literal1 = Literal.parseLiteral("~locked(door)");
/* Called before the MAS execution with the args informed in .mas2j */
@Override
public void init(String[] args) {
super.init(args);
/* Agent map with class */
// First argument must be the mas2j filename in order to map agents with their classes
// environment: TestEnv("ag-names.mas2j")
try {
jason.mas2j.parser.mas2j parser = new jason.mas2j.parser.mas2j(new FileInputStream(args[0]));
MAS2JProject project;
project = parser.mas();
for (AgentParameters ap : project.getAgents()) {
String agName = ap.name;
for (int cAg = 0; cAg < ap.getNbInstances(); cAg++) {
String numberedAg = agName;
if (ap.getNbInstances() > 1) {
numberedAg += (cAg + 1);
}
agents.put(numberedAg, ap.name);
}
}
System.out.println(agents);
} catch (jason.mas2j.parser.ParseException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
this.state.put("doorLocked", true);
addPercept(literal0);
}
/* Execute action at run-time */
@Override
public boolean executeAction(String agName, Structure action) {
/* Before actions */
clearPercepts();
/* Actions */
try {
logger.info(agName + " calls action " + action);
if(action.getFunctor().equals("lock")) {
if(this.agents.get(agName).equals("porter")) {
this.state.put("doorLocked", true);
}
} else if(action.getFunctor().equals("unlock")) {
if(this.agents.get(agName).equals("porter")) {
this.state.put("doorLocked", false);
}
} else {
logger.info("executing: " + action + ", but not implemented!");
}
} catch (Exception e) {
logger.log(Level.SEVERE, "error executing " + action + " for " + agName, e);
}
/* After actions */
if(this.state.get("doorLocked")) {
addPercept(literal0);
}
if(!this.state.get("doorLocked")) {
addPercept(literal1);
}
return true;
}
/* Called before the end of MAS execution */
@Override
public void stop() {
super.stop();
}
}