| Conditions | 12 |
| Total Lines | 132 |
| Code Lines | 69 |
| Lines | 103 |
| Ratio | 78.03 % |
| 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.handleTick(long) 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; |
||
| 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()) { |
|
|
|
|||
| 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)) { |
|
| 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 | } |
||
| 259 |