View Javadoc

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    * Very simple executor for plans that will print name of primitive, that is supposed
9    * to execute, to specified stream.
10   * <p/>
11   * There are two lists of primitives, one will always return true, other will always return false.
12   * Both will be printed. Primitives that are not in either of lists will cause exception
13   * {@link IllegalArgumentException}.
14   * @author Honza
15   */
16  public class PrintWorkExecutor implements IWorkExecutor {
17  
18      /**
19       * Stream into which we will write called primitives.
20       */
21      private PrintStream stream;
22      /**
23       * Map that stores return value of primitives.
24       */
25      private HashMap<String, Boolean> map = new HashMap<String, Boolean>();
26  
27      /**
28       * Create worker that will print name of primitives into specified stream.
29       * @param succeed list of non-zero length strings with names of primitives returning true
30       * @param fail list of non-zero length strings with names of primitives returning false
31       * @param stream stream to write into
32       * @throws IllegalArgumentException If some primitive is specified twice,
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       * Add primitives to the map with specified return value. 
43       * @param primitives list of primitives we want to add.
44       * @param value what value should all primitives return
45       * @throws IllegalArgumentException If some primitive is specified twice,
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       * Create worker that will print name of primitives into {@link System#out}.
56       */
57      public PrintWorkExecutor(String[] succeed, String[] fail) {
58          this(succeed, fail, System.out);
59      }
60      
61      private Object executePrimitive(String primitive, VariableContext ctx) {
62          Boolean value = map.get(primitive);
63          // no mapping for specified primitive
64          if (value == null) {
65              throw new IllegalArgumentException("Primitive \"" + primitive + "\" is not specified in the worker.");
66          }
67          stream.println(getClass().getSimpleName() + ": execute \"" + primitive + "\"" + ctx.toString() + " -> " + value);
68          return value;
69      }
70      
71      @Override
72      public synchronized Object executeSense(String primitive, VariableContext ctx) {
73          return executePrimitive(primitive, ctx);
74      }
75      
76      @Override
77      public synchronized ActionResult executeAction(String primitive, VariableContext ctx) {
78      	Object result = executePrimitive(primitive, ctx);
79      	if (result instanceof ActionResult) return (ActionResult)result;
80          return ActionResult.FINISHED;
81      }
82      
83  }