| Conditions | 10 |
| Total Lines | 105 |
| Code Lines | 52 |
| Lines | 79 |
| Ratio | 75.24 % |
| 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.framework.microkernel.resolver.timeline.scheduling.TimelineSchedulingResolver.doComputeFlawSolutions(Flaw) 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.framework.microkernel.resolver.timeline.scheduling; |
||
| 193 | protected void doComputeFlawSolutions(Flaw flaw) |
||
| 194 | throws UnsolvableFlawException |
||
| 195 | { |
||
| 196 | // get detected conflict |
||
| 197 | BinaryDecisionConflict conflict = (BinaryDecisionConflict) flaw; |
||
| 198 | |||
| 199 | // check possible precedence constraints |
||
| 200 | Decision reference = conflict.getDecisions()[0]; |
||
| 201 | Decision target = conflict.getDecisions()[1]; |
||
| 202 | // create possible solutions |
||
| 203 | DecisionPrecedenceConstraint pc1 = new DecisionPrecedenceConstraint(conflict, reference, target, this.schedulingCost); |
||
| 204 | DecisionPrecedenceConstraint pc2 = new DecisionPrecedenceConstraint(conflict, target, reference, this.schedulingCost); |
||
| 205 | |||
| 206 | View Code Duplication | try |
|
|
|
|||
| 207 | { |
||
| 208 | // create relation reference -> target |
||
| 209 | BeforeRelation before = this.component.create(RelationType.BEFORE, reference, target); |
||
| 210 | // set bounds |
||
| 211 | before.setBound(new long[] { |
||
| 212 | 0, |
||
| 213 | this.component.getHorizon()}); |
||
| 214 | |||
| 215 | // add create relation |
||
| 216 | pc1.addCreatedRelation(before); |
||
| 217 | |||
| 218 | // activate relation |
||
| 219 | if (this.component.activate(before)) { |
||
| 220 | // add activated relations |
||
| 221 | pc1.addActivatedRelation(before); |
||
| 222 | } |
||
| 223 | |||
| 224 | // check consistency |
||
| 225 | this.tdb.verify(); |
||
| 226 | // add solution and deactivate relation |
||
| 227 | conflict.addSolution(pc1); |
||
| 228 | } |
||
| 229 | catch (RelationPropagationException | ConsistencyCheckException ex) { |
||
| 230 | // discard relation |
||
| 231 | debug("Unfeasible precedence constraint:\n" |
||
| 232 | + "\t- reference: " + reference + "\n" |
||
| 233 | + "\t- target: " + target + "\n"); |
||
| 234 | } |
||
| 235 | finally { |
||
| 236 | |||
| 237 | // deactivate relation |
||
| 238 | for (Relation rel : pc1.getActivatedRelations()) { |
||
| 239 | // deactivate relation |
||
| 240 | this.component.deactivate(rel); |
||
| 241 | } |
||
| 242 | |||
| 243 | for (Relation rel : pc1.getCreatedRelations()) { |
||
| 244 | // delete relation |
||
| 245 | this.component.delete(rel); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | |||
| 250 | View Code Duplication | try |
|
| 251 | { |
||
| 252 | // create relation reference -> target |
||
| 253 | BeforeRelation before = this.component.create(RelationType.BEFORE, target, reference); |
||
| 254 | // set bounds |
||
| 255 | before.setBound(new long[] { |
||
| 256 | 0, |
||
| 257 | this.component.getHorizon()}); |
||
| 258 | |||
| 259 | // add created relation |
||
| 260 | pc2.addCreatedRelation(before); |
||
| 261 | // check if relation is feasible |
||
| 262 | if (this.component.activate(before)) { |
||
| 263 | // add activated relation |
||
| 264 | pc2.addActivatedRelation(before); |
||
| 265 | } |
||
| 266 | |||
| 267 | // check consistency |
||
| 268 | this.tdb.verify(); |
||
| 269 | // add solution and deactivate relation |
||
| 270 | conflict.addSolution(pc2); |
||
| 271 | } |
||
| 272 | catch (RelationPropagationException | ConsistencyCheckException ex) { |
||
| 273 | // discard relation |
||
| 274 | debug("Unfeasible precedence constraint:\n" |
||
| 275 | + "\t- reference: " + target + "\n" |
||
| 276 | + "\t- target: " + reference + "\n"); |
||
| 277 | } |
||
| 278 | finally { |
||
| 279 | |||
| 280 | // deactivate relation |
||
| 281 | for (Relation rel : pc2.getActivatedRelations()) { |
||
| 282 | // deactivate relation |
||
| 283 | this.component.deactivate(rel); |
||
| 284 | } |
||
| 285 | |||
| 286 | for (Relation rel : pc2.getCreatedRelations()) { |
||
| 287 | // delete relation |
||
| 288 | this.component.delete(rel); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | |||
| 293 | // check if any solution has been found |
||
| 294 | if (conflict.getSolutions().isEmpty()) { |
||
| 295 | throw new UnsolvableFlawException("Unsolvable decision conflict on timeline:\n" |
||
| 296 | + "\t- component: " + this.component.getName() + "\n" |
||
| 297 | + "\t- decisions: " + conflict.getDecisions()[0] + ", " + conflict.getDecisions()[1] + "\n"); |
||
| 298 | } |
||
| 302 |