View Javadoc

1   package cz.cuni.amis.pogamut.sposh.engine;
2   
3   import cz.cuni.amis.pogamut.sposh.elements.Competence;
4   import cz.cuni.amis.pogamut.sposh.elements.CompetenceElement;
5   import cz.cuni.amis.pogamut.sposh.elements.LapPath;
6   import cz.cuni.amis.pogamut.sposh.elements.LapType;
7   import cz.cuni.amis.pogamut.sposh.elements.PoshPlan;
8   import cz.cuni.amis.pogamut.sposh.elements.PrimitiveCall;
9   import cz.cuni.amis.pogamut.sposh.executor.IWorkExecutor;
10  
11  /**
12   * Executor for CE. 
13   * @author Honza
14   */
15  class CEExecutor extends AbstractExecutor implements ElementExecutor {
16      private final PoshPlan plan;
17      private final Competence competence;
18      private final CompetenceElement choice;
19      private PrimitiveCall actionCall;
20      private String name;
21      private SenseListExecutor<CompetenceElement> trigger;
22      private int retries = 0;
23      private int maxRetries;
24      
25      /**
26       * Create competence executor.
27       * @param plan plan that will be used to resolve primitives
28       * @param choice competence element that is going to be executed
29       * @param choicePath Path to the choice.
30       * @param ctx variable context of competence element
31       * @param log logger to record actions of this executor
32       */
33      CEExecutor(PoshPlan plan, Competence competence, CompetenceElement choice, LapPath choicePath, VariableContext ctx, EngineLog log) {
34          super(choicePath, ctx, log);
35  
36          assert choicePath.traversePath(plan) == choice;
37          assert choicePath.subpath(0, choicePath.length() - 1).traversePath(plan) == competence;
38          
39          this.plan = plan;
40          this.competence = competence;
41          this.choice = choice;
42          this.name = choice.getName();
43          this.trigger = new SenseListExecutor<CompetenceElement>(choice.getTrigger(), choicePath, ctx, log);
44          this.maxRetries = choice.getRetries();
45          this.actionCall = choice.getAction().getActionCall();
46      }
47  
48      private LapPath createChoiceActionPath() {
49          return path.concat(LapType.ACTION, 0);
50      }
51      
52      /**
53       * FIXME: I should probably make single method and join methods in CEExecutor, APExecutor and DEExecutor
54       * @param plan
55       * @param actionCall
56       * @return
57       */
58      private StackElement createActionExecutor(PrimitiveCall actionCall) {
59          LapPath choiceActionPath = createChoiceActionPath();
60      	return getElement(plan, actionCall, choiceActionPath); 
61      }
62  
63      /**
64       * Can this executor be executed? Are all preconditions (triggers and 
65       * retries) OK?
66       * @param workExecuter
67       * @return
68       */
69      TriggerResult isReady(IWorkExecutor workExecuter) {
70          engineLog.fine("isReady? " + retries + "/" + maxRetries);
71          TriggerResult result;
72          if (maxRetries == CompetenceElement.INFINITE_RETRIES || retries < maxRetries) {
73              result = trigger.fire(workExecuter, true);
74          } else {
75              result = new TriggerResult(false);
76          }
77          return result;
78      }
79  
80      /**
81       * How should this behave:
82       *  - if called from above, return new FOLLOW element for the action
83       *  - if the action was already finished, return
84       * @param workExecuter
85       * @return
86       */
87      @Override
88      public FireResult fire(IWorkExecutor workExecuter) {
89          engineLog.pathReached(path);
90          if (actionCalled) {
91              actionCalled = false;
92              return new FireResult(FireResult.Type.SURFACE_CONTINUE);
93          }
94  
95          retries++;
96          actionCalled = true;
97          return new FireResult(FireResult.Type.FOLLOW, createActionExecutor(actionCall));
98      }
99  
100     private boolean actionCalled = false;
101 
102     /**
103      * @return Get name of this CE
104      */
105     String getName() {
106         return name;
107     }
108 }