1 package cz.cuni.amis.pogamut.sposh.executor;
2
3 import cz.cuni.amis.pogamut.sposh.engine.VariableContext;
4 import java.io.PrintStream;
5 import java.util.HashMap;
6
7
8
9
10
11
12
13
14
15
16 public class PrintWorkExecutor implements IWorkExecutor {
17
18
19
20
21 private PrintStream stream;
22
23
24
25 private HashMap<String, Boolean> map = new HashMap<String, Boolean>();
26
27
28
29
30
31
32
33
34 public PrintWorkExecutor(String[] succeed, String[] fail, PrintStream stream) {
35 this.stream = stream;
36
37 addPrimitives(succeed, Boolean.TRUE);
38 addPrimitives(fail, Boolean.FALSE);
39 }
40
41
42
43
44
45
46
47 public synchronized void addPrimitives(String[] primitives, Boolean value) {
48 for (String primtive : primitives) {
49 if (map.put(primtive, value) != null)
50 throw new IllegalArgumentException("Primitive \"" + primtive + "\" has already been specified.");
51 }
52 }
53
54
55
56
57 public PrintWorkExecutor(String[] succeed, String[] fail) {
58 this(succeed, fail, System.out);
59 }
60
61 @Override
62 public synchronized Object executePrimitive(String primitive, VariableContext ctx) {
63 Boolean value = map.get(primitive);
64
65 if (value == null) {
66 throw new IllegalArgumentException("Primitive \"" + primitive + "\" is not specified in the worker.");
67 }
68 stream.println(getClass().getSimpleName() + ": execute \"" + primitive + "\"" + ctx.toString() + " -> " + value);
69 return value;
70 }
71 }