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.engine.FireResult.Type;
9 import cz.cuni.amis.pogamut.sposh.executor.IWorkExecutor;
10 import java.util.ArrayList;
11 import java.util.List;
12
13
14
15
16
17
18 final class CExecutor extends AbstractExecutor implements ElementExecutor {
19
20 private boolean executorWasEvalued = false;
21
22
23
24
25 @Deprecated
26 private final SenseListExecutor goalExecutor;
27 private final List<CEExecutor> ceExecutors = new ArrayList<CEExecutor>();
28
29
30
31
32
33
34
35
36
37
38 public CExecutor(PoshPlan plan, Competence competence, LapPath competencePath, VariableContext ctx, EngineLog log) {
39 super(competencePath, ctx, log);
40
41 assert competencePath.traversePath(plan) == competence;
42
43 this.goalExecutor = new SenseListExecutor<Competence>(competencePath, ctx, log);
44
45 int choiceId = 0;
46 for (CompetenceElement ce : competence.getChildDataNodes()) {
47 LapPath choicePath = competencePath.concat(LapType.COMPETENCE_ELEMENT, choiceId++);
48 CEExecutor choiceExecutor = new CEExecutor(plan, competence, ce, choicePath, ctx, log);
49
50 ceExecutors.add(choiceExecutor);
51 }
52 }
53
54
55
56
57
58
59
60 public boolean isGoalSatisfied(IWorkExecutor workExecutor) {
61 if (goalExecutor == null) {
62 return false;
63 }
64 return goalExecutor.fire(workExecutor, false).wasSuccess();
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78
79 @Override
80 public FireResult fire(IWorkExecutor workExecuter) {
81 engineLog.pathReached(path);
82
83 if (executorWasEvalued) {
84 return new FireResult(Type.SURFACE_CONTINUE);
85 }
86 executorWasEvalued = true;
87
88
89
90 TriggerResult goalResult = goalExecutor.fire(workExecuter, false);
91 if (goalResult.wasSuccess()) {
92 return new FireResult(FireResult.Type.FULFILLED);
93 }
94
95 for (CEExecutor ceExecutor : ceExecutors) {
96 TriggerResult triggerResult = ceExecutor.isReady(workExecuter);
97 if (triggerResult.wasSuccess()) {
98 StackElement stackElement = new StackElement(CompetenceElement.class, ceExecutor.getName(), ceExecutor);
99 return new FireResult(FireResult.Type.FOLLOW, stackElement);
100 }
101 }
102
103 return new FireResult(FireResult.Type.FAILED);
104 }
105 }