View Javadoc

1   package cz.cuni.amis.pogamut.sposh.engine;
2   
3   import cz.cuni.amis.pogamut.sposh.elements.Result;
4   import cz.cuni.amis.pogamut.sposh.elements.Sense;
5   import cz.cuni.amis.pogamut.sposh.executor.IWorkExecutor;
6   import java.util.logging.Logger;
7   
8   /**
9    * This is used to execute single primitive, like in DE or C.
10   * @author Honza
11   */
12  class PrimitiveExecutor extends AbstractExecutor implements ElementExecutor {
13      final private Sense.SenseCall actionCall;
14      final private FireResult.Type successResult;
15      final private FireResult.Type failResult;
16  
17      /**
18       * Create a primitive executor that will call a specified action
19       * within context of parameters. Always return SURFACE, whether the primitive fails or not.
20       *
21       * @param actionCall actionCall to primitive
22       * @param ctx Context of this primitive. Shoudl differ from parent, but isn't necessary
23       * @param log logger to record actions of this executor, can be null
24       */
25      PrimitiveExecutor(Sense.SenseCall actionCall, VariableContext ctx, Logger log) {
26          this(actionCall, FireResult.Type.SURFACE, FireResult.Type.SURFACE, ctx, log);
27      }
28  
29      /**
30       * Create a primitive executor that will call a specified action
31       * within context of parameters
32       * 
33       * @param actionCall actionCall to primitive
34       * @param successResult what to return
35       * @param failResult
36       * @param ctx Context of this primitive. Shoudl differ from parent, but isn't necessary
37       * @param log logger to record actions of this executor, can be null
38       */
39      PrimitiveExecutor(
40              Sense.SenseCall actionCall,
41              FireResult.Type successResult,
42              FireResult.Type failResult,
43              VariableContext ctx,
44              Logger log) {
45          super(ctx, log);
46          this.actionCall = actionCall;
47          this.successResult = successResult;
48          this.failResult = failResult;
49      }
50  
51  
52      /**
53       * Fire the action and return FireResult(false), so don't continue
54       * the execution.
55       * @param workExecuter
56       * @return failResult if result of primitive is empty or false, true otherwise
57       */
58      @Override
59      public FireResult fire(IWorkExecutor workExecuter) {
60          Object result = workExecuter.executePrimitive(actionCall.getName(), ctx);
61          if (Result.isFalse(result)) {
62              return new FireResult(failResult);
63          }
64          return new FireResult(successResult);
65      }
66  
67      @Override
68      public String toString() {
69          return this.getClass().getSimpleName() + "[" + actionCall.getName() + "]";
70      }
71  
72      @Override
73      public TriggerResult getTriggerResult() {
74          // XXX: Maybe null, I don;t have a consistent strategy for this
75          return new TriggerResult(true);
76      }
77  }