1 package cz.cuni.amis.pogamut.sposh.executor;
2
3 import cz.cuni.amis.pogamut.sposh.engine.VariableContext;
4 import java.text.MessageFormat;
5 import java.util.HashMap;
6 import java.util.logging.Logger;
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 public class StateWorkExecutor implements IWorkExecutor {
39
40
41
42
43 protected final HashMap<String, IAction> actions = new HashMap<String, IAction>();
44
45
46
47 protected final HashMap<String, ISense> senses = new HashMap<String, ISense>();
48
49
50
51 protected String currentActionName;
52
53
54
55
56
57
58 protected String currentVariableContext;
59
60
61
62 protected Logger log;
63
64 public StateWorkExecutor() {
65 this.log = Logger.getLogger(getClass().getSimpleName());
66 }
67
68 public StateWorkExecutor(Logger log) {
69 this.log = log;
70 }
71
72
73
74
75
76 public Logger getLogger() {
77 return log;
78 }
79
80
81
82
83
84
85 public synchronized boolean isNameUsed(String name) {
86 return isSense(name) || isAction(name);
87 }
88
89
90
91
92
93 protected boolean isAction(String name) {
94 return actions.containsKey(name);
95 }
96
97
98
99
100
101 protected boolean isSense(String name) {
102 return senses.containsKey(name);
103 }
104
105
106
107
108
109
110
111 public synchronized void addAction(String name, IAction action) {
112 if (isNameUsed(name)) {
113 throw new IllegalArgumentException("Primtive with name \"" + name + "\" is already present in executor.");
114 }
115 actions.put(name, action);
116 }
117
118
119
120
121
122
123 public synchronized void addSense(String name, ISense sense) {
124 if (isNameUsed(name)) {
125 throw new IllegalArgumentException("Primtive with name \"" + name + "\" is already present in executor.");
126 }
127 senses.put(name, sense);
128 }
129
130
131
132
133
134 public void addAction(IAction action) {
135 throw new UnsupportedOperationException("Not supported yet.");
136 }
137
138
139
140
141
142 public void addSense(ISense sense) {
143 throw new UnsupportedOperationException("Not supported yet.");
144 }
145
146 private String getSenseName(ISense sense, String primitiveName) {
147 PrimitiveInfo info = sense.getClass().getAnnotation(PrimitiveInfo.class);
148 if (info == null) return primitiveName;
149 if (info.name() == null) return primitiveName;
150 return info.name();
151 }
152
153 private String getActionName(IAction sense, String primitiveName) {
154 PrimitiveInfo info = sense.getClass().getAnnotation(PrimitiveInfo.class);
155 if (info == null) return primitiveName;
156 if (info.name() == null) return primitiveName;
157 return info.name();
158 }
159
160 @Override
161 public synchronized Object executeSense(String primitive, VariableContext ctx) {
162 if (!isSense(primitive)) {
163 throw new IllegalArgumentException("Sense \"" + primitive + "\" is not specified in the worker.");
164 }
165
166 ISense current = senses.get(primitive);
167 Object result = current.query(ctx);
168
169 log.info(MessageFormat.format("Query: {0}({1}) = {2}", getSenseName(current, primitive), ctx, result));
170
171 return result;
172 }
173
174 @Override
175 public synchronized ActionResult executeAction(String actionToExecuteName, VariableContext ctx) {
176 if (!isAction(actionToExecuteName)) {
177 throw new IllegalArgumentException("Action \"" + actionToExecuteName + "\" is not specified in the worker.");
178 }
179
180
181 IAction actionToExecute = actions.get(actionToExecuteName);
182 String variableContextToExecute = ctx.toString();
183
184 IAction currentAction = actions.get(currentActionName);
185
186
187 if (currentAction == null) {
188
189
190 currentActionName = actionToExecuteName;
191 currentVariableContext = ctx.toString();
192 currentAction = actionToExecute;
193
194
195 log.info(MessageFormat.format("Action: {0}.init({1})", getActionName(currentAction, currentActionName), ctx));
196 currentAction.init(ctx);
197 } else
198 if ((currentAction != null && actionToExecute != currentAction) ||
199 !variableContextToExecute.equals(currentVariableContext)) {
200
201
202 log.info(MessageFormat.format("Action: {0}.done({1})", getActionName(currentAction, currentActionName), ctx));
203 currentAction.done(ctx);
204
205
206 currentActionName = actionToExecuteName;
207 currentVariableContext = variableContextToExecute;
208 currentAction = actionToExecute;
209
210
211 log.info(MessageFormat.format("Action: {0}.init({1})", getActionName(currentAction, currentActionName), ctx));
212 currentAction.init(ctx);
213 }
214
215
216 ActionResult result = currentAction.run(ctx);
217 log.info(MessageFormat.format("Action: {0}.run({1}) = {2}", getActionName(currentAction, currentActionName), ctx, result));
218
219
220 if (result == ActionResult.FINISHED || result == ActionResult.RUNNING_ONCE) {
221
222
223 log.info(MessageFormat.format("Action: {0}.done({1})", getActionName(currentAction, currentActionName), ctx));
224 currentAction.done(ctx);
225
226 currentAction = null;
227 currentActionName = null;
228 currentVariableContext = null;
229 }
230
231 return result;
232 }
233 }