| Conditions | 14 |
| Total Lines | 94 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| 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.deliberative.solver.Solver.contextSwitch(SearchSpaceNode,SearchSpaceNode) 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.deliberative.solver; |
||
| 159 | protected void contextSwitch(SearchSpaceNode last, SearchSpaceNode extracted) |
||
| 160 | throws PlanRefinementException |
||
| 161 | { |
||
| 162 | // compare the two nodes |
||
| 163 | if (last != null) |
||
| 164 | { |
||
| 165 | // prepare a list of operators to retract |
||
| 166 | List<Operator> toRetract = new ArrayList<>(); |
||
| 167 | // prepare a list of operators to propagate |
||
| 168 | List<Operator> toPropagate = new ArrayList<>(); |
||
| 169 | |||
| 170 | // get the list of operators of the nodes |
||
| 171 | List<Operator> lastNodeOperators = last.getOperators(); |
||
| 172 | List<Operator> extractedNodeOperators = extracted.getOperators(); |
||
| 173 | |||
| 174 | // check min length between the two lists |
||
| 175 | int minLength = Math.min(lastNodeOperators.size(), extractedNodeOperators.size()); |
||
| 176 | // check potentially common operators |
||
| 177 | boolean common = true; |
||
| 178 | for (int i = 0; i < minLength; i++) { |
||
| 179 | |||
| 180 | // check common flag |
||
| 181 | if (common && !lastNodeOperators.get(i).equals(extractedNodeOperators.get(i))) { |
||
| 182 | common = false; |
||
| 183 | } |
||
| 184 | |||
| 185 | // check if no common operators have been found |
||
| 186 | if (!common) { |
||
| 187 | // add operator to the different lists |
||
| 188 | toRetract.add(lastNodeOperators.get(i)); |
||
| 189 | toPropagate.add(extractedNodeOperators.get(i)); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | // check other operators to retract |
||
| 194 | for (int i = minLength; i < lastNodeOperators.size(); i++) { |
||
| 195 | toRetract.add(lastNodeOperators.get(i)); |
||
| 196 | } |
||
| 197 | |||
| 198 | // check other operators to propagate |
||
| 199 | for (int i = minLength; i < extractedNodeOperators.size(); i++) { |
||
| 200 | toPropagate.add(extractedNodeOperators.get(i)); |
||
| 201 | } |
||
| 202 | |||
| 203 | |||
| 204 | // retract operators in reverse order |
||
| 205 | Collections.reverse(toRetract); |
||
| 206 | // retract all operators |
||
| 207 | for (Operator operator : toRetract) { |
||
| 208 | // retract operator |
||
| 209 | this.pdb.retract(operator); |
||
| 210 | } |
||
| 211 | |||
| 212 | // list of committed operators |
||
| 213 | List<Operator> committed = new ArrayList<>(); |
||
| 214 | try |
||
| 215 | { |
||
| 216 | // propagate operators in chronological order |
||
| 217 | for (Operator operator : toPropagate) { |
||
| 218 | // propagate operator |
||
| 219 | this.pdb.propagate(operator); |
||
| 220 | // add committed operator |
||
| 221 | committed.add(operator); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | catch (OperatorPropagationException ex) { |
||
| 225 | |||
| 226 | // retract committed operators in reverse order |
||
| 227 | Collections.reverse(committed); |
||
| 228 | for (Operator operator : committed) { |
||
| 229 | // retract operator |
||
| 230 | this.pdb.retract(operator); |
||
| 231 | } |
||
| 232 | |||
| 233 | // also restore retracted operators |
||
| 234 | Collections.reverse(toRetract); |
||
| 235 | for (Operator operator : toRetract) { |
||
| 236 | try { |
||
| 237 | // restore operator |
||
| 238 | this.pdb.propagate(operator); |
||
| 239 | } |
||
| 240 | catch (OperatorPropagationException exx) { |
||
| 241 | warning("[ContextSwitch] Error while restoring operators after failure:\n" |
||
| 242 | + "- message: " + ex.getMessage() + "\n"); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | // throw exception |
||
| 247 | throw new PlanRefinementException("Error while propagating node:\n" + extracted + "\n- message: " + ex.getMessage() + "\n"); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | else { |
||
| 251 | // simply propagate extracted node |
||
| 252 | this.propagate(extracted); |
||
| 253 | } |
||
| 330 |