1 package cz.cuni.amis.pogamut.sposh.elements;
2
3 import cz.cuni.amis.pogamut.sposh.exceptions.DuplicateNameException;
4 import cz.cuni.amis.pogamut.sposh.exceptions.FubarException;
5 import cz.cuni.amis.pogamut.sposh.executor.DoNothing;
6 import cz.cuni.amis.pogamut.sposh.executor.Succeed;
7 import java.io.StringReader;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.Collections;
11 import java.util.LinkedList;
12 import java.util.List;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
15
16
17
18
19
20
21
22
23
24 public class LapElementsFactory {
25
26
27
28
29
30 public static String DEFAULT_ACTION = DoNothing.class.getName();
31
32
33
34
35 public static String DEFAULT_TRIGGER_SENSE = Succeed.class.getName();
36 public static String DEFAULT_DRIVE_COLLECTION_NAME = "life";
37
38
39
40
41
42
43
44
45
46 public static Competence createCompetence(String name, String... choices) throws DuplicateNameException {
47 List<CompetenceElement> elements = new ArrayList<CompetenceElement>(choices.length);
48
49 for (String choice : choices) {
50 elements.add(LapElementsFactory.createCompetenceElement(choice));
51 }
52
53 return new Competence(name, new FormalParameters(), elements);
54 }
55
56
57
58
59
60
61 public static DriveElement createDriveElement(String driveName) {
62 return new DriveElement(driveName, Arrays.asList(new Sense(DEFAULT_TRIGGER_SENSE)), new PrimitiveCall(DEFAULT_ACTION), Freq.INFINITE, "");
63 }
64
65
66
67
68
69
70
71 public static DriveElement createDriveElementNoTriggers(String name) {
72 return new DriveElement(name, Collections.<Sense>emptyList(), new PrimitiveCall(DEFAULT_ACTION), Freq.INFINITE, "");
73 }
74
75
76
77
78
79
80
81 public static CompetenceElement createCompetenceElement(String name) {
82 return new CompetenceElement(name, Collections.<Sense>emptyList(), new PrimitiveCall(DEFAULT_ACTION), CompetenceElement.INFINITE_RETRIES, "");
83 }
84
85
86
87
88
89
90
91 public static Sense createSense(String name) {
92 return new Sense(name);
93 }
94
95
96
97
98 public static TriggeredAction createAction() {
99 return new TriggeredAction(DEFAULT_ACTION);
100 }
101
102
103
104
105
106
107
108 public static TriggeredAction createAction(String name) {
109 return new TriggeredAction(name);
110 }
111
112
113
114
115 public static TriggeredAction createAction(PrimitiveCall call) {
116 return new TriggeredAction(new PrimitiveCall(call));
117 }
118
119
120
121
122
123
124
125
126 public static ActionPattern createActionPattern(String name) {
127 return new ActionPattern(name, new FormalParameters(), Arrays.asList(new TriggeredAction(DEFAULT_ACTION)), "");
128 }
129
130
131
132
133
134
135
136
137
138 public static ActionPattern createActionPattern(String name, TriggeredAction... actions) {
139 return new ActionPattern(name, new FormalParameters(), Arrays.asList(actions), "");
140 }
141
142
143
144
145
146
147
148
149 public static ActionPattern createActionPattern(String name, FormalParameters params) {
150 return new ActionPattern(name, params, Collections.<TriggeredAction>emptyList(), "");
151 }
152
153
154
155
156
157
158
159
160
161 public static ActionPattern createActionPattern(String name, FormalParameters params, List<TriggeredAction> actions) {
162 return new ActionPattern(name, params, actions, "");
163 }
164
165
166
167
168
169
170
171
172 public static ActionPattern createActionPattern(ActionPattern ap) {
173
174 String serializedActionPattern = ap.toString();
175 try {
176 return new PoshParser(new StringReader(serializedActionPattern.substring(serializedActionPattern.indexOf('(') + 1))).actionPattern();
177 } catch (ParseException ex) {
178 String msg = "Tried to serialize and deserialize action pattern as copy constructor: " + serializedActionPattern;
179 Logger.getLogger(LapElementsFactory.class.getName()).log(Level.SEVERE, msg, ex);
180 throw new FubarException(msg, ex);
181 }
182 }
183
184
185
186
187
188
189
190
191 public static Competence createCompetence(Competence c) {
192
193 String serializedCompetence = c.toString();
194 try {
195 return new PoshParser(new StringReader(serializedCompetence.substring(serializedCompetence.indexOf('(') + 1))).competence();
196 } catch (ParseException ex) {
197 String msg = "Tried to serialize and deserialize competence as copy constructor: " + serializedCompetence;
198 Logger.getLogger(LapElementsFactory.class.getName()).log(Level.SEVERE, msg, ex);
199 throw new FubarException(msg, ex);
200 }
201 }
202
203 public static DriveElement createDriveElement(DriveElement drive) {
204
205 String serializedDrive = drive.toString();
206 try {
207 return new PoshParser(new StringReader(serializedDrive)).driveElement(new FormalParameters());
208 } catch (ParseException ex) {
209 String msg = "Tried to serialize and deserialize drive as copy constructor: " + serializedDrive;
210 Logger.getLogger(LapElementsFactory.class.getName()).log(Level.SEVERE, msg, ex);
211 throw new FubarException(msg, ex);
212 }
213 }
214
215
216
217
218
219
220
221 public static Sense createSense(Sense sense) {
222
223 String serializedSense = sense.toString();
224 try {
225 return new PoshParser(new StringReader(serializedSense)).fullSense(new FormalParameters());
226 } catch (ParseException ex) {
227 String msg = "Tried to serialize and deserialize sense as copy constructor: " + serializedSense;
228 Logger.getLogger(LapElementsFactory.class.getName()).log(Level.SEVERE, msg, ex);
229 throw new FubarException(msg, ex);
230 }
231 }
232
233
234
235
236
237
238
239 public static DriveCollection createDriveCollection() {
240
241 return new DriveCollection("placeholder");
242 }
243
244
245
246
247
248
249
250
251 public static DriveCollection createDriveCollection(String driveCollectionName) {
252 return new DriveCollection(driveCollectionName);
253 }
254
255 public static CompetenceElement createCompetenceElement(String name, List<Sense> triggerSenses, String actionName) {
256 return new CompetenceElement(name, triggerSenses, new PrimitiveCall(actionName), CompetenceElement.INFINITE_RETRIES, "");
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270 public static DriveElement createDriveElement(String name, List<Sense> triggerSenses, PrimitiveCall actionCall, Freq freq, String comment) {
271 return new DriveElement(name, triggerSenses, actionCall, freq, comment);
272 }
273
274
275
276
277
278
279
280 public static CompetenceElement createCompetenceElement(DriveElement drive) {
281 return new CompetenceElement(
282 drive.getName(),
283 copySenses(drive.getTrigger()),
284 new PrimitiveCall(drive.getAction().getActionCall()),
285 CompetenceElement.INFINITE_RETRIES,
286 drive.getComment());
287 }
288
289
290
291
292
293
294
295 public static CompetenceElement createCompetenceElement(CompetenceElement choice) {
296 return new CompetenceElement(
297 choice.getName(),
298 copySenses(choice.getTrigger()),
299 new PrimitiveCall(choice.getAction().getActionCall()),
300 choice.getRetries(),
301 choice.getComment());
302 }
303
304
305
306
307
308
309
310
311 public static DriveElement createDriveElement(CompetenceElement choice) {
312 return new DriveElement(
313 choice.getName(),
314 copySenses(choice.getTrigger()),
315 new PrimitiveCall(choice.getAction().getActionCall()),
316 Freq.INFINITE,
317 choice.getComment());
318 }
319
320
321
322
323 private static <OWNER extends PoshElement> List<Sense> copySenses(Trigger<OWNER> trigger) {
324 List<Sense> senses = new LinkedList<Sense>();
325 for (Sense sense : trigger) {
326 senses.add(createSense(sense));
327 }
328 return senses;
329 }
330
331
332
333
334
335
336
337 public static PoshPlan createPlan(String dcName) {
338 return new PoshPlan(dcName);
339 }
340
341
342
343
344
345 static PoshPlan createPlan() {
346 return createPlan(DEFAULT_DRIVE_COLLECTION_NAME);
347 }
348
349
350
351
352
353
354
355
356 public static TriggeredAction createAction(TriggeredAction original) {
357 return new TriggeredAction(new PrimitiveCall(original.getActionCall()));
358 }
359 }