Conditions | 12 |
Total Lines | 123 |
Code Lines | 64 |
Lines | 104 |
Ratio | 84.55 % |
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.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.ai.executive.monitor; |
||
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()) { |
|
|
|||
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)) { |
|
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 | } |
||
252 |