View Javadoc

1   package cz.cuni.amis.pogamut.sposh.engine;
2   
3   import cz.cuni.amis.pogamut.sposh.executor.ActionResult;
4   import cz.cuni.amis.pogamut.sposh.executor.IWorkExecutor;
5   import java.util.Collections;
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   /**
10   *
11   * @author Honza
12   */
13  public class TestWorkExecutor implements IWorkExecutor {
14  
15      private Map<String, ITestPrimitive> primitivesMap = new HashMap<String, ITestPrimitive>();
16      private Map<String, ITestPrimitive> primitivesMapUm = Collections.unmodifiableMap(primitivesMap);
17  
18      public TestWorkExecutor(ITestPrimitive[] primitives) {
19          for (ITestPrimitive primitive : primitives) {
20              if (primitivesMap.put(primitive.getName(), primitive) != null) {
21                  throw new IllegalArgumentException("Primitive with name \"" + primitive.getName() + "\" was specified twice.");
22              }
23          }
24      }
25  
26      /**
27       * Get all primitives in this test executor.
28       *
29       * @return Unmodifiable map of all primitives.
30       */
31      public Map<String, ITestPrimitive> getPrimitives() {
32          return primitivesMapUm;
33      }
34  
35      private Object executePrimitive(String name, VariableContext ctx) {
36          ITestPrimitive primitive = primitivesMap.get(name);
37          if (primitive == null) {
38              throw new IllegalArgumentException("Primitive \"" + name + "\" is not defined.");
39          }
40          return primitive.work(ctx);
41      }
42  
43  	@Override
44  	public ActionResult executeAction(String actionName, VariableContext ctx) {
45  		Object result = executePrimitive(actionName, ctx);
46  		if (result instanceof ActionResult) return (ActionResult)result;
47  		return ActionResult.FINISHED;
48  	}
49  
50  	@Override
51  	public Object executeSense(String senseName, VariableContext ctx) {
52  		return executePrimitive(senseName, ctx);
53  	}
54  }