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 final PoshPlan plan;
21 private final Competence competence;
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.plan = plan;
44 this.competence = competence;
45 this.goalExecutor = new SenseListExecutor<Competence>(competencePath, ctx, log);
46
47 int choiceId = 0;
48 for (CompetenceElement ce : competence.getChildDataNodes()) {
49 LapPath choicePath = competencePath.concat(LapType.COMPETENCE_ELEMENT, choiceId++);
50 CEExecutor choiceExecutor = new CEExecutor(plan, competence, ce, choicePath, ctx, log);
51
52 ceExecutors.add(choiceExecutor);
53 }
54 }
55
56
57
58
59
60
61
62 public boolean isGoalSatisfied(IWorkExecutor workExecutor) {
63 if (goalExecutor == null) {
64 return false;
65 }
66 return goalExecutor.fire(workExecutor, false).wasSuccess();
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81 @Override
82 public FireResult fire(IWorkExecutor workExecuter) {
83 engineLog.pathReached(path);
84
85
86 TriggerResult goalResult = goalExecutor.fire(workExecuter, false);
87 if (goalResult.wasSuccess()) {
88 return new FireResult(FireResult.Type.FULFILLED);
89 }
90
91 for (CEExecutor ceExecutor : ceExecutors) {
92 TriggerResult triggerResult = ceExecutor.isReady(workExecuter);
93 if (triggerResult.wasSuccess()) {
94 StackElement stackElement = new StackElement(CompetenceElement.class, ceExecutor.getName(), ceExecutor);
95 return new FireResult(FireResult.Type.FOLLOW, stackElement);
96 }
97 }
98
99 return new FireResult(FireResult.Type.FAILED);
100 }
101 }