1
|
|
|
package it.cnr.istc.pst.platinum.control.acting; |
2
|
|
|
|
3
|
|
|
import java.util.HashSet; |
4
|
|
|
import java.util.Set; |
5
|
|
|
|
6
|
|
|
import it.cnr.istc.pst.platinum.ai.executive.Executive; |
7
|
|
|
import it.cnr.istc.pst.platinum.ai.executive.ExecutiveBuilder; |
8
|
|
|
import it.cnr.istc.pst.platinum.ai.executive.lang.ex.ExecutionException; |
9
|
|
|
import it.cnr.istc.pst.platinum.ai.executive.lang.ex.ExecutionPreparationException; |
10
|
|
|
import it.cnr.istc.pst.platinum.ai.executive.lang.failure.ExecutionFailureCause; |
11
|
|
|
import it.cnr.istc.pst.platinum.ai.executive.pdb.ExecutionNode; |
12
|
|
|
import it.cnr.istc.pst.platinum.ai.executive.pdb.ExecutionNodeStatus; |
13
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.plan.SolutionPlan; |
14
|
|
|
import it.cnr.istc.pst.platinum.ai.framework.protocol.lang.PlanProtocolDescriptor; |
15
|
|
|
import it.cnr.istc.pst.platinum.control.lang.Goal; |
16
|
|
|
import it.cnr.istc.pst.platinum.control.lang.GoalStatus; |
17
|
|
|
import it.cnr.istc.pst.platinum.control.lang.TokenDescription; |
18
|
|
|
import it.cnr.istc.pst.platinum.control.lang.ex.PlatformException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @author alessandro |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
public class ExecutiveProcess implements Runnable { |
26
|
|
|
|
27
|
|
|
private GoalOrientedActingAgent agent; |
28
|
|
|
private Class<? extends Executive> eClass; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @param eClass |
33
|
|
|
* @param agent |
34
|
|
|
*/ |
35
|
|
|
protected ExecutiveProcess(Class<? extends Executive> eClass, GoalOrientedActingAgent agent) { |
36
|
|
|
this.agent = agent; |
37
|
|
|
this.eClass = eClass; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
@Override |
44
|
|
|
public void run() { |
45
|
|
|
|
46
|
|
|
boolean running = true; |
47
|
|
|
while(running) { |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
|
51
|
|
|
// take a goal to plan for |
52
|
|
|
Goal goal = this.agent.waitGoal(GoalStatus.COMMITTED); |
53
|
|
|
System.out.println("executing goal ...\n" + goal + "\n"); |
54
|
|
|
// execute extracted goal |
55
|
|
|
int code = this.agent.execute(goal); |
56
|
|
|
|
57
|
|
|
// check execution code |
58
|
|
|
switch (code) { |
59
|
|
|
|
60
|
|
|
// success |
61
|
|
|
case 1 : { |
62
|
|
|
|
63
|
|
|
// goal successfully executed |
64
|
|
|
this.agent.finish(goal); |
65
|
|
|
} |
66
|
|
|
break; |
67
|
|
|
|
68
|
|
|
// execution failure |
69
|
|
|
case 2 : { |
70
|
|
|
|
71
|
|
|
// goal execution suspended due to some failure |
72
|
|
|
this.agent.suspend(goal); |
73
|
|
|
} |
74
|
|
|
break; |
75
|
|
|
|
76
|
|
|
// execution error |
77
|
|
|
case 3 : { |
78
|
|
|
|
79
|
|
|
// goal execution error due to some major failure |
80
|
|
|
this.agent.abort(goal); |
81
|
|
|
} |
82
|
|
|
break; |
83
|
|
|
|
84
|
|
|
default : { |
85
|
|
|
|
86
|
|
|
// unknown execution code |
87
|
|
|
System.err.println("Unknown Execution code :\n" |
88
|
|
|
+ "\t- goal: " + goal + "\n" |
89
|
|
|
+ "\t- code: " + code + "\n"); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
} catch (InterruptedException ex) { |
|
|
|
|
95
|
|
|
running = false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
* @param goal |
103
|
|
|
* @throws InterruptedException |
104
|
|
|
* @throws ExecutionException |
105
|
|
|
* @throws ExecutionPreparationException |
106
|
|
|
* @throws PlatformException |
107
|
|
|
*/ |
108
|
|
|
protected void doHandle(Goal goal) |
109
|
|
|
throws InterruptedException, ExecutionException, ExecutionPreparationException, PlatformException { |
110
|
|
|
|
111
|
|
|
// get solution plan |
112
|
|
|
SolutionPlan plan = goal.getPlan(); |
113
|
|
|
// build executive |
114
|
|
|
Executive exec = ExecutiveBuilder.createAndSet(this.eClass, 0, plan.getHorizon()); |
115
|
|
|
// export plan |
116
|
|
|
PlanProtocolDescriptor desc = plan.export(); |
117
|
|
|
System.out.println("\n\nREADY TO EXECUTE PLAN:\n" + desc + "\n\n"); |
118
|
|
|
// set the executive according to the plan being executed |
119
|
|
|
exec.initialize(desc); |
120
|
|
|
|
121
|
|
|
// bind simulator if any |
122
|
|
|
if (this.agent.proxy != null) { |
123
|
|
|
// bind simulator |
124
|
|
|
exec.link(this.agent.proxy); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
try { |
129
|
|
|
|
130
|
|
|
// run the executive starting at a given tick |
131
|
|
|
boolean complete = exec.execute(goal.getExecutionTick(), goal); |
132
|
|
|
// check execution result |
133
|
|
|
if (!complete) { |
134
|
|
|
|
135
|
|
|
// get failure cause |
136
|
|
|
ExecutionFailureCause cause = exec.getFailureCause(); |
137
|
|
|
// set failure cause |
138
|
|
|
goal.setFailureCause(cause); |
139
|
|
|
// set repaired |
140
|
|
|
goal.setRepaired(false); |
141
|
|
|
// set goal interruption tick |
142
|
|
|
goal.setExecutionTick(cause.getInterruptionTick()); |
143
|
|
|
// set execution trace by taking into account executed nodes |
144
|
|
|
for (ExecutionNode node : exec.getNodes(ExecutionNodeStatus.EXECUTED)) { |
145
|
|
|
// add the node to the goal execution trace |
146
|
|
|
goal.addNodeToExecutionTrace(node); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// get the name of of goal components |
150
|
|
|
Set<String> components = new HashSet<>(); |
151
|
|
|
for (TokenDescription t : goal.getTaskDescription().getGoals()) { |
152
|
|
|
components.add(t.getComponent()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// set execution trace by taking into account also (virtual) nodes in-execution |
156
|
|
|
for (ExecutionNode node : exec.getNodes(ExecutionNodeStatus.IN_EXECUTION)) { |
157
|
|
|
// do not consider nodes belonging to "goal component" |
158
|
|
|
if (!components.contains(node.getComponent())) { |
159
|
|
|
// add the node to the goal execution trace |
160
|
|
|
goal.addNodeToExecutionTrace(node); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// throw exception |
165
|
|
|
throw new ExecutionException("Execution failure... try to repair the plan through replanning... \n" |
166
|
|
|
+ "\t- cause: " + cause + "\n", cause); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
} finally { |
170
|
|
|
|
171
|
|
|
// stop simulator if any |
172
|
|
|
if (this.agent.proxy != null) { |
173
|
|
|
// unlink from simulator |
174
|
|
|
exec.unlink(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|