Total Complexity | 52 |
Total Lines | 382 |
Duplicated Lines | 3.14 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like it.cnr.istc.pst.platinum.control.lang.Goal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package it.cnr.istc.pst.platinum.control.lang; |
||
22 | public class Goal implements Comparable<Goal>, Comparator<ExecutionNode> |
||
23 | { |
||
24 | private static AtomicLong GoalIdCounter = new AtomicLong(0); |
||
25 | private long id; |
||
26 | private long timestamp; |
||
27 | private AgentTaskDescription description; // goal and facts description |
||
28 | private GoalStatus status; |
||
29 | private SolutionPlan plan; // (current) solution plan for the goal |
||
30 | |||
31 | private List<Long> planningAttempts; // array counting planning attempts and keeping track of planning times |
||
32 | private List<Long> executionAttempts; // array counting execution attempts and keeping track of execution times |
||
33 | private List<Long> contingencyAttempts; // array counting contingency attempts and keeping track of their handling times |
||
34 | |||
35 | private List<PlanProtocolDescriptor> plans; // trace of generated plans |
||
36 | private ExecutionFailureCause failureCause; // goal execution failure cause |
||
37 | private Map<String, List<ExecutionNode>> trace; // execution trace |
||
38 | |||
39 | private boolean repaired; |
||
40 | private long executionTick; |
||
41 | //EDIT POINTER |
||
42 | private long uppaalTime; |
||
43 | private boolean existsStrategy; |
||
44 | private long managementStrategyTime; |
||
45 | private boolean isOutOfBounds; |
||
46 | private int maxOutOfBounds; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * |
||
51 | * @param description |
||
52 | */ |
||
53 | public Goal(AgentTaskDescription description) |
||
54 | { |
||
55 | this.id = GoalIdCounter.getAndIncrement(); |
||
56 | this.timestamp = System.currentTimeMillis(); |
||
57 | this.description = description; |
||
58 | this.status = null; |
||
59 | this.plan = null; |
||
60 | this.planningAttempts = new ArrayList<>(); |
||
61 | this.executionAttempts = new ArrayList<>(); |
||
62 | this.contingencyAttempts = new ArrayList<>(); |
||
63 | this.plans = new ArrayList<>(); |
||
64 | this.trace = new HashMap<>(); |
||
65 | this.failureCause = null; |
||
66 | this.repaired = false; |
||
67 | this.executionTick = 0; |
||
68 | //EDIT POINTER |
||
69 | this.uppaalTime = -1; |
||
70 | this.existsStrategy = false; |
||
71 | this.managementStrategyTime = -1; |
||
72 | this.isOutOfBounds = false; |
||
73 | this.maxOutOfBounds = -1; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * |
||
78 | * @param repaired |
||
79 | */ |
||
80 | public void setRepaired(boolean repaired) { |
||
81 | this.repaired = repaired; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * |
||
86 | */ |
||
87 | public void clearExecutionTrace() { |
||
88 | // clear trace |
||
89 | this.trace.clear(); |
||
90 | // clear failure cause |
||
91 | this.failureCause = null; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * |
||
96 | * @return |
||
97 | */ |
||
98 | public boolean isRepaired() { |
||
99 | return this.repaired; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * |
||
104 | * @return |
||
105 | */ |
||
106 | public long getExecutionTick() { |
||
107 | return executionTick; |
||
108 | } |
||
109 | |||
110 | public boolean isOutOfBounds() { |
||
111 | return isOutOfBounds; |
||
112 | } |
||
113 | |||
114 | public void setOutOfBounds(boolean isOutOfBounds) { |
||
115 | this.isOutOfBounds = isOutOfBounds; |
||
116 | } |
||
117 | |||
118 | public int getMaxOutOfBounds() { |
||
119 | return maxOutOfBounds; |
||
120 | } |
||
121 | |||
122 | public void setMaxOutOfBounds(int maxOutOfBounds) { |
||
123 | this.maxOutOfBounds = maxOutOfBounds; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * |
||
128 | * @param executionTick |
||
129 | */ |
||
130 | public void setExecutionTick(long executionTick) { |
||
131 | this.executionTick = executionTick; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * |
||
136 | * @param failureCause |
||
137 | */ |
||
138 | public void setFailureCause(ExecutionFailureCause failureCause) { |
||
139 | this.failureCause = failureCause; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * |
||
144 | * @return |
||
145 | */ |
||
146 | public ExecutionFailureCause getFailureCause() { |
||
147 | return failureCause; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * |
||
152 | * @param node |
||
153 | */ |
||
154 | public void addNodeToExecutionTrace(ExecutionNode node) { |
||
155 | if (!this.trace.containsKey(node.getComponent())) { |
||
156 | this.trace.put(node.getComponent(), new ArrayList<>()); |
||
157 | } |
||
158 | |||
159 | this.trace.get(node.getComponent()).add(node); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * |
||
164 | * @param name |
||
165 | */ |
||
166 | public List<ExecutionNode> getExecutionTraceByComponentName(String name) { |
||
167 | // list of executed nodes |
||
168 | List<ExecutionNode> list = new ArrayList<>(); |
||
169 | if (this.trace.containsKey(name)) { |
||
170 | // add nodes |
||
171 | list.addAll(this.trace.get(name)); |
||
172 | } |
||
173 | |||
174 | // sort the trace |
||
175 | Collections.sort(list, this); |
||
176 | // get the list |
||
177 | return list; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Add planning attempt time in milliseconds |
||
182 | * |
||
183 | * @param time |
||
184 | */ |
||
185 | public void addPlanningAttempt(long time) { |
||
186 | // add planning time |
||
187 | this.planningAttempts.add(time); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * |
||
192 | * @return |
||
193 | */ |
||
194 | public int getPlanningAttempts() { |
||
195 | return this.planningAttempts.size(); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get the total planning time in milliseconds |
||
200 | * |
||
201 | * @return |
||
202 | */ |
||
203 | public long getTotalPlanningTime() |
||
204 | { |
||
205 | // compute total planning time |
||
206 | long total = 0; |
||
207 | for (Long time : this.planningAttempts) { |
||
208 | total += time; |
||
209 | } |
||
210 | |||
211 | // get total planning time |
||
212 | return total; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Add execution attempt time in milliseconds |
||
217 | * |
||
218 | * @param time |
||
219 | */ |
||
220 | public void addExecutionAttempt(long time) { |
||
221 | // add execution time |
||
222 | this.executionAttempts.add(time); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * |
||
227 | * @return |
||
228 | */ |
||
229 | public int getExecutionAttempts() { |
||
230 | return this.executionAttempts.size(); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * |
||
235 | * @return |
||
236 | */ |
||
237 | public long getTotalExecutionTime() { |
||
238 | // compute total execution time |
||
239 | long total = 0; |
||
240 | for (Long time : this.executionAttempts) { |
||
241 | total += time; |
||
242 | } |
||
243 | |||
244 | // get total planning time |
||
245 | return total; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Add contingency handling time in milliseconds |
||
250 | * |
||
251 | * @param time |
||
252 | */ |
||
253 | public void addContingencyHandlingAttempt(long time) { |
||
254 | // add contingency time |
||
255 | this.contingencyAttempts.add(time); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * |
||
260 | * @return |
||
261 | */ |
||
262 | public int getContingencyHandlingAttempts() { |
||
263 | return this.contingencyAttempts.size(); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * |
||
268 | * @return |
||
269 | */ |
||
270 | public long getTotalContingencyHandlingTime() { |
||
271 | // compute total contingency handling time |
||
272 | long total = 0; |
||
273 | for (long time : this.contingencyAttempts) { |
||
274 | total += time; |
||
275 | } |
||
276 | |||
277 | // get total planning time |
||
278 | return total; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * |
||
283 | * @param plan |
||
284 | */ |
||
285 | public void setPlan(SolutionPlan plan) { |
||
286 | this.plan = plan; |
||
287 | // add exported plan |
||
288 | this.plans.add(plan.export()); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * |
||
293 | * @return |
||
294 | */ |
||
295 | public AgentTaskDescription getTaskDescription() { |
||
296 | return description; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * |
||
301 | * @return |
||
302 | */ |
||
303 | public SolutionPlan getPlan() { |
||
304 | return plan; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * |
||
309 | * @return |
||
310 | */ |
||
311 | public GoalStatus getStatus() { |
||
312 | return status; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * |
||
317 | * @param status |
||
318 | */ |
||
319 | public void setStatus(GoalStatus status) { |
||
320 | this.status = status; |
||
321 | } |
||
322 | |||
323 | //EDIT POINTER |
||
324 | |||
325 | public long getUppaalTime() { |
||
326 | return uppaalTime; |
||
327 | } |
||
328 | |||
329 | public void setUppaalTime(long uppaalTime) { |
||
330 | this.uppaalTime = uppaalTime; |
||
331 | } |
||
332 | |||
333 | public long getManagementStrategyTime() { |
||
334 | return managementStrategyTime; |
||
335 | } |
||
336 | |||
337 | public void setManagementStrategyTime(long managementStrategyTime) { |
||
338 | this.managementStrategyTime = managementStrategyTime; |
||
339 | } |
||
340 | |||
341 | public boolean isExistsStrategy() { |
||
342 | return existsStrategy; |
||
343 | } |
||
344 | |||
345 | public void setExistsStrategy(boolean existsStrategy) { |
||
346 | this.existsStrategy = existsStrategy; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * |
||
351 | */ |
||
352 | @Override |
||
353 | public int hashCode() { |
||
354 | final int prime = 31; |
||
355 | int result = 1; |
||
356 | result = prime * result + (int) (id ^ (id >>> 32)); |
||
357 | return result; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * |
||
362 | */ |
||
363 | View Code Duplication | @Override |
|
|
|||
364 | public boolean equals(Object obj) { |
||
365 | if (this == obj) |
||
366 | return true; |
||
367 | if (obj == null) |
||
368 | return false; |
||
369 | if (getClass() != obj.getClass()) |
||
370 | return false; |
||
371 | Goal other = (Goal) obj; |
||
372 | if (id != other.id) |
||
373 | return false; |
||
374 | return true; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * |
||
379 | */ |
||
380 | @Override |
||
381 | public int compareTo(Goal o) { |
||
382 | // chronological order |
||
383 | return this.timestamp < o.timestamp ? -1 : this.timestamp > o.timestamp ? 1 : 0; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Completely instantiated nodes are expected |
||
388 | */ |
||
389 | @Override |
||
390 | public int compare(ExecutionNode o1, ExecutionNode o2) { |
||
391 | // compare start times |
||
392 | TimePoint s1 = o1.getInterval().getStartTime(); |
||
393 | TimePoint s2 = o2.getInterval().getStartTime(); |
||
394 | // compare lower bounds |
||
395 | return s1.getLowerBound() < s2.getLowerBound() ? -1 : s1.getLowerBound() > s2.getLowerBound() ? 1 : 0; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * |
||
400 | */ |
||
401 | @Override |
||
402 | public String toString() { |
||
403 | return "[Goal id: " + this.id +", status: " + this.status + "]"; |
||
404 | } |
||
407 |