Passed
Push — master ( 634a3f...1f3b93 )
by Alessandro
05:38
created

it.cnr.istc.pst.platinum.executive.dc.DCMonitor   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 232
Duplicated Lines 78.45 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 112
dl 182
loc 232
rs 10
c 1
b 0
f 0
wmc 23

3 Methods

Rating   Name   Duplication   Size   Complexity  
C handleExecutionFailure(long,ExecutionFailureCause) 79 79 10
A DCMonitor() 0 2 1
D handleTick(long) 103 132 12

How to fix   Duplicated Code   

Duplicated Code

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:

1
package it.cnr.istc.pst.platinum.executive.dc;
2
3
import it.cnr.istc.pst.platinum.ai.executive.lang.ExecutionFeedback;
4
import it.cnr.istc.pst.platinum.ai.executive.lang.ex.ExecutionException;
5
import it.cnr.istc.pst.platinum.ai.executive.lang.ex.NodeExecutionErrorException;
6
import it.cnr.istc.pst.platinum.ai.executive.lang.ex.NodeObservationException;
7
import it.cnr.istc.pst.platinum.ai.executive.lang.failure.ExecutionFailureCause;
8
import it.cnr.istc.pst.platinum.ai.executive.lang.failure.NodeDurationOverflow;
9
import it.cnr.istc.pst.platinum.ai.executive.lang.failure.NodeExecutionError;
10
import it.cnr.istc.pst.platinum.ai.executive.lang.failure.NodeStartOverflow;
11
import it.cnr.istc.pst.platinum.ai.executive.monitor.Monitor;
12
import it.cnr.istc.pst.platinum.ai.executive.pdb.ControllabilityType;
13
import it.cnr.istc.pst.platinum.ai.executive.pdb.ExecutionNode;
14
import it.cnr.istc.pst.platinum.ai.executive.pdb.ExecutionNodeStatus;
15
import it.cnr.istc.pst.platinum.ai.framework.time.ex.TemporalConstraintPropagationException;
16
import it.cnr.istc.pst.platinum.control.lang.ex.PlatformException;
17
18
/**
19
 * 
20
 */
21
public class DCMonitor extends Monitor<DCExecutive> 
22
{
23
	/**
24
	 * 
25
	 * @param exec
26
	 */
27
	protected DCMonitor() {
28
		super();
29
	}
30
	
31
	/**
32
	 * 
33
	 */
34
	@Override
35
	public void handleTick(long tick) 
36
			throws ExecutionException, PlatformException
37
	{
38
		// convert tick to tau
39
		long tau = this.executive.convertTickToTau(tick);
40
		// check received feedbacks
41
//		List<ExecutionFeedback> feedbacks = this.getObservations();
42
//		// manage uncontrollable tokens of the plan according to the feedbacks
43
//		for (ExecutionFeedback feedback : feedbacks) {
44
			
45
		while (this.hasObservations()) {
46
			
47
			// get next feedback
48
			ExecutionFeedback feedback = this.next();
49
			// get node 
50
			ExecutionNode node = feedback.getNode();
51
			// execution feedback
52
			debug("[DCMonitor] [tick: " + tick + "][{tau: " +  tau + "] -> Execution feedback:\n"
53
					+ "\t- type: " + feedback.getType() + "\n"
54
					+ "\t- node: " + feedback.getNode() + "\n");
55
			
56
			
57
			// check feedback type
58 View Code Duplication
			switch (feedback.getType()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
59
			
60
				case PARTIALLY_CONTROLLABLE_TOKEN_COMPLETE : 
61
				case UNCONTROLLABLE_TOKEN_COMPLETE : 
62
				{
63
					// compute node duration of the token in execution 
64
					long duration = Math.max(1, tau - node.getStart()[0]);
65
					try {
66
						
67
						// schedule token duration
68
						this.executive.scheduleTokenDuration(node, duration);
69
						// update node state
70
						this.executive.updateNode(node, ExecutionNodeStatus.EXECUTED);
71
						info("[DCMonitor] [tick: " + tick + "] [tau: " +  tau + "] -> Observed token execution with duration " + duration + " \n"
72
								+ "\t- node: " + node.getGroundSignature() + " (" + node + ")\n");
73
					}
74
					catch (TemporalConstraintPropagationException ex) {
75
						
76
						// set token as in failure
77
						this.executive.updateNode(node, ExecutionNodeStatus.FAILURE);
78
						// create execution failure message
79
						ExecutionFailureCause cause = new NodeDurationOverflow(tick, node, tau);
80
						// throw execution exception
81
						throw new NodeObservationException(
82
								"Observed duration time does not comply with the expected one:\n"
83
								+ "\t- duration: " + duration + "\n"
84
								+ "\t- node: " + node + "\n", 
85
								cause);
86
					}
87
				}
88
				break;
89
				
90
				case UNCONTROLLABLE_TOKEN_START : 
91
				{
92
					try
93
					{
94
						// schedule the start of uncontrollable token
95
						this.executive.scheduleUncontrollableTokenStart(node, tau);
96
						info("{Monitor} {tick: " + tick + "} {tau: " + tau + "} -> Observed token execution start at time " + tau + "\n"
97
								+ "\t- node: " + node.getGroundSignature() + " (" + node + ")\n");
98
					}
99
					catch (TemporalConstraintPropagationException ex) {
100
						
101
						// the node can be considered as in execution
102
						this.executive.updateNode(node, ExecutionNodeStatus.FAILURE);
103
						// create execution cause
104
						ExecutionFailureCause cause = new NodeStartOverflow(tick, node, tau);
105
						// throw execution exception
106
						throw new NodeObservationException(
107
								"The observed start time of the token does not comply with the expected one:\n"
108
								+ "\t- start: " + tau + "\n"
109
								+ "\t- node: " + node + "\n", 
110
								cause);
111
					}
112
				}
113
				break;
114
				
115
				case TOKEN_EXECUTION_FAILURE : {
116
					
117
					// update node status
118
					this.executive.updateNode(node, ExecutionNodeStatus.FAILURE);
119
					// execution failure
120
					ExecutionFailureCause cause = new NodeExecutionError(tick, node); 
121
					// throw execution exception
122
					throw new NodeExecutionErrorException(
123
							"Node execution error:\n\t- node: " + node + "\n", 
124
							cause);
125
				}
126
			}
127
		}
128
		
129
		// manage controllable tokens of the plan
130 View Code Duplication
		for (ExecutionNode node : this.executive.getNodes(ExecutionNodeStatus.IN_EXECUTION)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
131
			
132
			// check node controllability 
133
			if (node.getControllabilityType().equals(ControllabilityType.CONTROLLABLE)) {
134
				
135
				// check end conditions
136
				if (this.executive.canEnd(node)) {
137
					
138
					// check node schedule
139
					this.executive.checkSchedule(node);
140
					// check expected schedule
141
					if (tau >= node.getEnd()[0]) {
142
						
143
						// compute (controllable) execution duration
144
						long duration = Math.max(1, tau - node.getStart()[0]);
145
						// send stop signal to the platform
146
						this.executive.sendStopCommandSignalToPlatform(node);
147
						// set node as executed
148
						this.executive.updateNode(node, ExecutionNodeStatus.EXECUTED);
149
						// token scheduled
150
						info("{Monitor} {tick: " + tick + "} {tau: " + tau + "} -> Scheduling duration for controllable token\n"
151
								+ "\t- duration: " + duration + "\n"
152
								+ "\t- node: " + node.getGroundSignature() + " (" + node + ")\n");
153
					}
154
					else {
155
						
156
						// wait - not ready for dispatching
157
						debug("{Monitor} {tick: " + tick + "} {tau: " + tau + "} -> End conditions satisifed but node schedule not ready for ending\n"
158
								+ "\t- node: " + node.getGroundSignature() + " (" + node + ")\n");
159
					}
160
				}
161
				else {
162
					
163
					// print a message in debug mode
164
					debug("{Monitor} {tick: " + tick + "} {tau: " + tau + "} -> End execution conditions not satisfied yet\n"
165
							+ "\t- node: " + node.getGroundSignature() + " (" + node + ")\n");
166
				}
167
			}
168
		}
169
	}
170
	
171
	/**
172
	 * 
173
	 */
174 View Code Duplication
	@Override
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
175
	public void handleExecutionFailure(long tick, ExecutionFailureCause cause) 
176
			throws PlatformException
177
	{
178
		// convert tick to tau
179
		long tau = this.executive.convertTickToTau(tick);
180
		// manage uncontrollable tokens of the plan according to the feedbacks
181
		while (this.hasObservations())
182
		{
183
			// get next observation
184
			ExecutionFeedback feedback = this.next();
185
			// get node 
186
			ExecutionNode node = feedback.getNode();
187
			// check node schedule
188
			this.executive.checkSchedule(node);
189
			// check execution result
190
			switch (feedback.getType()) {
191
			
192
				case PARTIALLY_CONTROLLABLE_TOKEN_COMPLETE : 
193
				case UNCONTROLLABLE_TOKEN_COMPLETE : {
194
					
195
					// compute node duration of the token in execution 
196
					long duration = Math.max(1, tau - node.getStart()[0]);
197
					// the node can be considered as executed
198
					this.executive.updateNode(node, ExecutionNodeStatus.FAILURE);
199
					// add repair information
200
					cause.addRepairInfo(node, duration);
201
					// info message
202
					info("{Monitor} {tick: " + tick + "} {tau: " +  tau + "} {FAILURE-HANDLING} -> Observed token execution with duration " + duration + " \n"
203
							+ "\t- node: " + node.getGroundSignature() + " (" + node + ")\n");
204
				}
205
				break;
206
				
207
				case UNCONTROLLABLE_TOKEN_START : {
208
		
209
					// update node status
210
					this.executive.updateNode(node, ExecutionNodeStatus.FAILURE);
211
					info("{Monitor} {tick: " + tick + "} {tau: " + tau + "} {FAILURE-HANDLING} -> Observed token execution start at time " + tau + "\n"
212
							+ "\t- node: " + node.getGroundSignature() + " (" + node + ")\n");
213
				}
214
				break;
215
				
216
				case TOKEN_EXECUTION_FAILURE : {
217
					
218
					// the node can be considered as executed
219
					this.executive.updateNode(node, ExecutionNodeStatus.FAILURE);
220
					info("{Monitor} {tick: " + tick + "} {tau: " + tau + "} {FAILURE-HANDLING} -> Observed execution failure at time " + tau + "\n"
221
							+ "\t- node: " + node.getGroundSignature() + " (" + node + ")\n");
222
				}
223
			}
224
		}
225
		
226
		// manage controllable tokens of the plan
227
		for (ExecutionNode node : this.executive.getNodes(ExecutionNodeStatus.IN_EXECUTION)) {
228
			
229
			// check node controllability 
230
			if (node.getControllabilityType().equals(ControllabilityType.CONTROLLABLE)) {
231
				
232
				// check end conditions
233
				if (this.executive.canEnd(node)) {
234
					
235
					// check schedule
236
					this.executive.checkSchedule(node);
237
					// check expected schedule
238
					if (tau >= node.getEnd()[0]) {
239
						
240
						// the node can be considered as executed
241
						this.executive.updateNode(node, ExecutionNodeStatus.FAILURE);
242
						// send stop command
243
						this.executive.sendStopCommandSignalToPlatform(node);
244
						// info message
245
						debug("{Monitor} {tick: " + tick + "} {tau: " + tau + "} {FAILURE-HANDLING} -> Stopping execution of controllable token\n"
246
								+ "\t- node: " + node.getGroundSignature() + " (" + node + ")\n");
247
						
248
					} else {
249
						
250
						// info message
251
						debug("{Monitor} {tick: " + tick + "} {tau: " + tau + "} {FAILURE-HANDLING} -> Waiting stop condition for controllable token\n"
252
								+ "\t- node: " + node.getGroundSignature() + " (" + node + ")\n");
253
					}
254
				}
255
			}
256
		}
257
	}
258
}
259