Conditions | 10 |
Total Lines | 79 |
Code Lines | 40 |
Lines | 79 |
Ratio | 100 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
Complex classes like it.cnr.istc.pst.platinum.executive.dc.DCMonitor.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.executive.dc; |
||
174 | View Code Duplication | @Override |
|
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 | } |
||
259 |