Passed
Push — master ( 8192a4...b9ceeb )
by Alessandro
07:30
created

getPriority()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
dl 0
loc 2
rs 10
1
package it.cnr.istc.pst.platinum.control.lang;
2
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.Comparator;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.concurrent.atomic.AtomicLong;
10
11
import it.cnr.istc.pst.platinum.ai.executive.lang.failure.ExecutionFailureCause;
12
import it.cnr.istc.pst.platinum.ai.executive.pdb.ExecutionNode;
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.ai.framework.time.tn.TimePoint;
16
17
/**
18
 * 
19
 * @author anacleto
20
 *
21
 */
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 priority; 								// goal priority
27
	private long timestamp;
28
	private AgentTaskDescription description;			// goal and facts description 
29
	private GoalStatus status;
30
	private SolutionPlan plan;							// (current) solution plan for the goal
31
	
32
	private List<Long> planningAttempts;				// array counting planning attempts and keeping track of planning times
33
	private List<Long> executionAttempts;				// array counting execution attempts and keeping track of execution times
34
	private List<Long> contingencyAttempts;				// array counting contingency attempts and keeping track of their handling times
35
36
	private List<PlanProtocolDescriptor> plans;			// trace of generated plans
37
	private ExecutionFailureCause failureCause;			// goal execution failure cause
38
	private Map<String, List<ExecutionNode>> trace;		// execution trace
39
	
40
	private boolean repaired;
41
	private long executionTick;
42
	//EDIT POINTER
43
	private long uppaalTime;
44
	private boolean existsStrategy;
45
	private long managementStrategyTime;
46
	private boolean isOutOfBounds;
47
	private int maxOutOfBounds;
48
	
49
	
50
	/**
51
	 * 
52
	 * @param description
53
	 */
54
	public Goal(AgentTaskDescription description) 
55
	{ 
56
		this.id = GoalIdCounter.getAndIncrement();
57
		this.timestamp = System.currentTimeMillis();
58
		this.description = description;
59
		this.status = null;
60
		this.plan = null;
61
		this.planningAttempts = new ArrayList<>();
62
		this.executionAttempts = new ArrayList<>();
63
		this.contingencyAttempts = new ArrayList<>();
64
		this.plans = new ArrayList<>();
65
		this.trace = new HashMap<>();
66
		this.failureCause = null;
67
		this.repaired = false;
68
		this.executionTick = 0;
69
		//EDIT POINTER
70
		this.uppaalTime = -1;
71
		this.existsStrategy = false;
72
		this.managementStrategyTime = -1;
73
		this.isOutOfBounds = false;
74
		this.maxOutOfBounds = -1;
75
	}
76
	
77
	/**
78
	 * 
79
	 * @param priority
80
	 */
81
	public void setPriority(long priority) {
82
		this.priority = priority;
83
	}
84
	
85
	/**
86
	 * 
87
	 * @return
88
	 */
89
	public long getPriority() {
90
		return priority;
91
	}
92
	
93
	/**
94
	 * 
95
	 * @param repaired
96
	 */
97
	public void setRepaired(boolean repaired) {
98
		this.repaired = repaired;
99
	}
100
	
101
	/**
102
	 * 
103
	 */
104
	public void clearExecutionTrace() {
105
		// clear trace
106
		this.trace.clear();
107
		// clear failure cause
108
		this.failureCause = null;
109
	}
110
	
111
	/**
112
	 * 
113
	 * @return
114
	 */
115
	public boolean isRepaired() {
116
		return this.repaired;
117
	}
118
	
119
	/**
120
	 * 
121
	 * @return
122
	 */
123
	public long getExecutionTick() {
124
		return executionTick;
125
	}
126
	
127
	public boolean isOutOfBounds() {
128
		return isOutOfBounds;
129
	}
130
131
	public void setOutOfBounds(boolean isOutOfBounds) {
132
		this.isOutOfBounds = isOutOfBounds;
133
	}
134
135
	public int getMaxOutOfBounds() {
136
		return maxOutOfBounds;
137
	}
138
139
	public void setMaxOutOfBounds(int maxOutOfBounds) {
140
		this.maxOutOfBounds = maxOutOfBounds;
141
	}
142
143
	/**
144
	 * 
145
	 * @param executionTick
146
	 */
147
	public void setExecutionTick(long executionTick) {
148
		this.executionTick = executionTick;
149
	}
150
	
151
	/**
152
	 * 
153
	 * @param failureCause
154
	 */
155
	public void setFailureCause(ExecutionFailureCause failureCause) {
156
		this.failureCause = failureCause;
157
	}
158
	
159
	/**
160
	 * 
161
	 * @return
162
	 */
163
	public ExecutionFailureCause getFailureCause() {
164
		return failureCause;
165
	}
166
	
167
	/**
168
	 * 
169
	 * @param node
170
	 */
171
	public void addNodeToExecutionTrace(ExecutionNode node) {
172
		if (!this.trace.containsKey(node.getComponent())) {
173
			this.trace.put(node.getComponent(), new ArrayList<>());
174
		}
175
		
176
		this.trace.get(node.getComponent()).add(node);
177
	}
178
	
179
	/**
180
	 * 
181
	 * @param name
182
	 */
183
	public List<ExecutionNode> getExecutionTraceByComponentName(String name) {
184
		// list of executed nodes
185
		List<ExecutionNode> list = new ArrayList<>();
186
		if (this.trace.containsKey(name)) {
187
			// add nodes
188
			list.addAll(this.trace.get(name));
189
		}
190
		
191
		// sort the trace
192
		Collections.sort(list, this);
193
		// get the list
194
		return list;
195
	}
196
	
197
	/**
198
	 * Add planning attempt time in milliseconds
199
	 * 
200
	 * @param time
201
	 */
202
	public void addPlanningAttempt(long time) {
203
		// add planning time
204
		this.planningAttempts.add(time);
205
	}
206
	
207
	/**
208
	 * 
209
	 * @return
210
	 */
211
	public int getPlanningAttempts() {
212
		return this.planningAttempts.size();
213
	}
214
	
215
	/**
216
	 * Get the total planning time in milliseconds
217
	 * 
218
	 * @return
219
	 */
220
	public long getTotalPlanningTime() 
221
	{
222
		// compute total planning time 
223
		long total = 0;
224
		for (Long time : this.planningAttempts) {
225
			total += time;
226
		}
227
		
228
		// get total planning time
229
		return total;
230
	}
231
	
232
	/**
233
	 * Add execution attempt time in milliseconds
234
	 * 
235
	 * @param time
236
	 */
237
	public void addExecutionAttempt(long time) {
238
		// add execution time 
239
		this.executionAttempts.add(time);
240
	}
241
	
242
	/**
243
	 * 
244
	 * @return
245
	 */
246
	public int getExecutionAttempts() {
247
		return this.executionAttempts.size();
248
	}
249
	
250
	/**
251
	 * 
252
	 * @return
253
	 */
254
	public long getTotalExecutionTime() {
255
		// compute total execution time 
256
		long total = 0;
257
		for (Long time : this.executionAttempts) {
258
			total += time;
259
		}
260
		
261
		// get total planning time
262
		return total;
263
	}
264
	
265
	/**
266
	 * Add contingency handling time in milliseconds
267
	 * 
268
	 * @param time
269
	 */
270
	public void addContingencyHandlingAttempt(long time) {
271
		// add contingency time
272
		this.contingencyAttempts.add(time);
273
	}
274
	
275
	/**
276
	 * 
277
	 * @return
278
	 */
279
	public int getContingencyHandlingAttempts() {
280
		return this.contingencyAttempts.size();
281
	}
282
	
283
	/**
284
	 * 
285
	 * @return
286
	 */
287
	public long getTotalContingencyHandlingTime() {
288
		// compute total contingency handling time 
289
		long total = 0;
290
		for (long time : this.contingencyAttempts) {
291
			total += time;
292
		}
293
		
294
		// get total planning time
295
		return total;
296
	}
297
	
298
	/**
299
	 * 
300
	 * @param plan
301
	 */
302
	public void setPlan(SolutionPlan plan) {
303
		this.plan = plan;
304
		// add exported plan 
305
		this.plans.add(plan.export());
306
	}
307
	
308
	/**
309
	 * 
310
	 * @return
311
	 */
312
	public AgentTaskDescription getTaskDescription() {
313
		return description;
314
	}
315
	
316
	/**
317
	 * 
318
	 * @return
319
	 */
320
	public SolutionPlan getPlan() {
321
		return plan;
322
	}
323
	
324
	/**
325
	 * 
326
	 * @return
327
	 */
328
	public GoalStatus getStatus() {
329
		return status;
330
	}
331
	
332
	/**
333
	 * 
334
	 * @param status
335
	 */
336
	public void setStatus(GoalStatus status) {
337
		this.status = status;
338
	}
339
	
340
	//EDIT POINTER
341
	
342
	public long getUppaalTime() {
343
		return uppaalTime;
344
	}
345
346
	public void setUppaalTime(long uppaalTime) {
347
		this.uppaalTime = uppaalTime;
348
	}
349
350
	public long getManagementStrategyTime() {
351
		return managementStrategyTime;
352
	}
353
354
	public void setManagementStrategyTime(long managementStrategyTime) {
355
		this.managementStrategyTime = managementStrategyTime;
356
	}
357
	
358
	public boolean isExistsStrategy() {
359
		return existsStrategy;
360
	}
361
362
	public void setExistsStrategy(boolean existsStrategy) {
363
		this.existsStrategy = existsStrategy;
364
	}
365
366
	/**
367
	 * 
368
	 */
369
	@Override
370
	public int hashCode() {
371
		final int prime = 31;
372
		int result = 1;
373
		result = prime * result + (int) (id ^ (id >>> 32));
374
		return result;
375
	}
376
377
	/**
378
	 * 
379
	 */
380 View Code Duplication
	@Override
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
381
	public boolean equals(Object obj) {
382
		if (this == obj)
383
			return true;
384
		if (obj == null)
385
			return false;
386
		if (getClass() != obj.getClass())
387
			return false;
388
		Goal other = (Goal) obj;
389
		if (id != other.id)
390
			return false;
391
		return true;
392
	}
393
	
394
	/**
395
	 * 
396
	 */
397
	@Override
398
	public int compareTo(Goal o) {
399
		
400
		// compare priority and time
401
		return this.priority > o.priority ? -1 : 
402
			this.priority == o.priority && this.timestamp < o.timestamp ? -1 : 
403
				this.priority == o.priority && this.timestamp == o.timestamp ? 0 : 1;
404
	}
405
	
406
	/**
407
	 * Completely instantiated nodes are expected
408
	 */
409
	@Override
410
	public int compare(ExecutionNode o1, ExecutionNode o2) {
411
		
412
		// compare start times
413
		TimePoint s1 = o1.getInterval().getStartTime();
414
		TimePoint s2 = o2.getInterval().getStartTime();
415
		// compare lower bounds
416
		return s1.getLowerBound() < s2.getLowerBound() ? -1 : s1.getLowerBound() > s2.getLowerBound() ? 1 : 0;
417
	}
418
	
419
	/**
420
	 * 
421
	 */
422
	@Override
423
	public String toString() {
424
		return "[Goal id: " + this.id +", priority: " + this.priority + ",  status: " + this.status + "]";
425
	}
426
427
}
428