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
13
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
27
28
29
30
31
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
54
55
56
57
58 private StackElement createActionExecutor(PrimitiveCall actionCall) {
59 LapPath choiceActionPath = createChoiceActionPath();
60 return getElement(plan, actionCall, choiceActionPath);
61 }
62
63
64
65
66
67
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
82
83
84
85
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
104
105 String getName() {
106 return name;
107 }
108 }