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

handleExecutionFailure(long,ExecutionFailureCause)   C

Complexity

Conditions 10

Size

Total Lines 79
Code Lines 38

Duplication

Lines 79
Ratio 100 %

Importance

Changes 0
Metric Value
cc 10
eloc 38
dl 79
loc 79
rs 5.9999
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

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