| Conditions | 10 |
| Total Lines | 79 |
| Code Lines | 38 |
| Lines | 79 |
| Ratio | 100 % |
| Changes | 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.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; |
||
| 167 | View Code Duplication | @Override |
|
| 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 | } |
||
| 252 |