1 package cz.cuni.amis.pogamut.sposh.engine;
2
3 import cz.cuni.amis.pogamut.sposh.elements.ActionPattern;
4 import cz.cuni.amis.pogamut.sposh.elements.Adopt;
5 import cz.cuni.amis.pogamut.sposh.elements.Competence;
6 import cz.cuni.amis.pogamut.sposh.elements.LapPath;
7 import cz.cuni.amis.pogamut.sposh.elements.LapType;
8 import cz.cuni.amis.pogamut.sposh.elements.PoshPlan;
9 import cz.cuni.amis.pogamut.sposh.elements.PrimitiveCall;
10 import cz.cuni.amis.pogamut.sposh.elements.TriggeredAction;
11 import cz.cuni.amis.pogamut.sposh.exceptions.FubarException;
12
13
14
15
16
17 abstract class AbstractExecutor {
18
19
20
21 protected final LapPath path;
22
23
24
25 protected final VariableContext ctx;
26
27
28
29 protected final EngineLog engineLog;
30
31
32
33
34
35
36
37 protected AbstractExecutor(LapPath path, VariableContext ctx, EngineLog engineLog) {
38 this.path = path;
39 this.ctx = ctx;
40 this.engineLog = engineLog;
41 }
42
43 public final LapPath getPath() {
44 return path;
45 }
46
47 private StackElement<ADExecutor> getElementAD(PoshPlan plan, PrimitiveCall adaptCall, LapPath referencePath) {
48 String adoptName = adaptCall.getName();
49 Adopt adopt = plan.getAD(adoptName);
50 if (adopt == null) {
51 return null;
52 }
53 int adoptId = plan.getAdoptId(adopt);
54
55 assert plan.getAdopts().get(adoptId) == adopt;
56
57 LapPath adoptPath = referencePath.concat(LapType.ADOPT, adoptId);
58 VariableContext adoptContext = new VariableContext(ctx, adaptCall.getParameters(), adopt.getParameters());
59 ADExecutor adoptExecutor = new ADExecutor(plan, adopt, adoptPath, adoptContext, engineLog);
60
61 return new StackElement<ADExecutor>(Adopt.class, adoptName, adoptExecutor);
62 }
63
64 private StackElement<APExecutor> getElementAP(PoshPlan plan, PrimitiveCall actionPatternCall, LapPath referencePath) {
65 String actionPatternName = actionPatternCall.getName();
66 ActionPattern actionPattern = plan.getAP(actionPatternName);
67 if (actionPattern == null) {
68 return null;
69 }
70 int actionPatternId = plan.getActionPatternId(actionPattern);
71
72 assert plan.getActionPatterns().get(actionPatternId) == actionPattern;
73
74 LapPath actionPatternPath = referencePath.concat(LapType.ACTION_PATTERN, actionPatternId);
75 VariableContext actionPatternContext = new VariableContext(ctx, actionPatternCall.getParameters(), actionPattern.getParameters());
76 APExecutor actionPatternExecutor = new APExecutor(plan, actionPattern, FireResult.Type.SURFACE, actionPatternPath, actionPatternContext, engineLog);
77
78 return new StackElement<APExecutor>(ActionPattern.class, actionPatternName, actionPatternExecutor);
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92 private StackElement<CExecutor> getElementC(PoshPlan plan, PrimitiveCall competenceCall, LapPath referencePath) {
93 String competenceName = competenceCall.getName();
94 Competence competence = plan.getC(competenceName);
95 if (competence == null) {
96 return null;
97 }
98 int competenceId = plan.getCompetenceId(competence);
99
100 assert plan.getCompetences().get(competenceId) == competence;
101
102 LapPath competencePath = referencePath.concat(LapType.COMPETENCE, competenceId);
103 VariableContext competenceContext = new VariableContext(ctx, competenceCall.getParameters(), competence.getParameters());
104 CExecutor competenceExecutor = new CExecutor(plan, competence, competencePath, competenceContext, engineLog);
105
106 return new StackElement<CExecutor>(Competence.class, competenceName, competenceExecutor);
107 }
108
109
110
111
112
113
114
115
116
117 private StackElement<ActionExecutor> getElementAction(
118 PoshPlan plan,
119 PrimitiveCall actionCall,
120 LapPath referencePath,
121 Runnable finishedResultCallback,
122 Runnable runningResultCallback,
123 Runnable runningOnceResultCallback,
124 Runnable failedResultCallback)
125 {
126 String actionName = actionCall.getName();
127 ActionExecutor actionExecutor = new ActionExecutor(actionCall,
128 finishedResultCallback, runningResultCallback, runningOnceResultCallback, failedResultCallback,
129 referencePath, new VariableContext(ctx, actionCall.getParameters()), engineLog);
130
131 return new StackElement<ActionExecutor>(TriggeredAction.class, actionName, actionExecutor);
132 }
133
134 protected StackElement getElement(PoshPlan plan, PrimitiveCall actionCall, LapPath referencePath) {
135 return getElement(plan, actionCall, referencePath,
136 null, null, null, null);
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 protected StackElement getElement(PoshPlan plan, PrimitiveCall actionCall, LapPath referencePath,
161 Runnable finishedResultCallback,
162 Runnable runningResultCallback,
163 Runnable runningOnceCallback,
164 Runnable failedResultCallback
165 ) {
166 StackElement stackElement = getElementC(plan, actionCall, referencePath);
167
168 if (stackElement == null) {
169 stackElement = getElementAP(plan, actionCall, referencePath);
170 if (stackElement == null) {
171 stackElement = getElementAD(plan, actionCall, referencePath);
172 if (stackElement == null) {
173 stackElement = getElementAction(plan, actionCall, referencePath,
174 finishedResultCallback,
175 runningResultCallback,
176 runningOnceCallback,
177 failedResultCallback
178 );
179 }
180 }
181 }
182
183 return stackElement;
184 }
185 }