| Conditions | 22 |
| Total Lines | 203 |
| Code Lines | 95 |
| Lines | 53 |
| Ratio | 26.11 % |
| 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.behavior.planning.TimelineBehaviorPlanningResolver.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.behavior.planning; |
||
| 330 | @Override |
||
| 331 | protected void doComputeFlawSolutions(Flaw flaw) |
||
| 332 | throws UnsolvableFlawException |
||
| 333 | { |
||
| 334 | // get the gap |
||
| 335 | Gap gap = (Gap) flaw; |
||
| 336 | // check gap type |
||
| 337 | switch (gap.getGapType()) |
||
| 338 | { |
||
| 339 | // incomplete time-line |
||
| 340 | case INCOMPLETE_TIMELINE : |
||
| 341 | { |
||
| 342 | // check all (acyclic) paths among tokens |
||
| 343 | List<ValuePath> paths = this.component.getPaths( |
||
| 344 | gap.getLeftDecision().getValue(), |
||
| 345 | gap.getRightDecision().getValue()); |
||
| 346 | |||
| 347 | // check found solutions |
||
| 348 | if (paths.isEmpty()) { |
||
| 349 | // not gap completion found between the two tokens |
||
| 350 | throw new UnsolvableFlawException("Not gap completion found:\n" |
||
| 351 | + "- gap: " + gap + "\n"); |
||
| 352 | } |
||
| 353 | else |
||
| 354 | { |
||
| 355 | // compute a solution for each possible value path |
||
| 356 | for (ValuePath path : paths) |
||
| 357 | { |
||
| 358 | // get steps |
||
| 359 | List<ComponentValue> steps = path.getSteps(); |
||
| 360 | // remove the source and destination values from the path |
||
| 361 | steps.remove(0); |
||
| 362 | steps.remove(steps.size() - 1); |
||
| 363 | // gap solution |
||
| 364 | GapCompletion solution = new GapCompletion(gap, steps, this.cost); |
||
| 365 | |||
| 366 | try |
||
| 367 | { |
||
| 368 | // check solution feasibility |
||
| 369 | if (solution.getPath().isEmpty()) |
||
| 370 | { |
||
| 371 | // direct token transition between active decisions |
||
| 372 | Decision reference = solution.getLeftDecision(); |
||
| 373 | Decision target = solution.getRightDecision(); |
||
| 374 | |||
| 375 | // create meets constraint to enforce the desired transition |
||
| 376 | MeetsRelation meets = this.component.create(RelationType.MEETS, reference, target); |
||
| 377 | // add created relation |
||
| 378 | solution.addCreatedRelation(meets); |
||
| 379 | // propagate relation and activate it if possible |
||
| 380 | if (this.component.activate(meets)) { |
||
| 381 | // add activated relation to solution |
||
| 382 | solution.addActivatedRelation(meets); |
||
| 383 | } |
||
| 384 | |||
| 385 | // create parameter relations |
||
| 386 | Set<Relation> pRels = this.createParameterRelations(reference, target); |
||
| 387 | // add created relation |
||
| 388 | solution.addCreatedRelation(meets); |
||
| 389 | // propagate relation |
||
| 390 | for (Relation pRel : pRels) { |
||
| 391 | // activate relation if possible |
||
| 392 | if (this.component.activate(pRel)) { |
||
| 393 | // add activated relation to solution |
||
| 394 | solution.addActivatedRelation(pRel); |
||
| 395 | } |
||
| 396 | } |
||
| 397 | } |
||
| 398 | else |
||
| 399 | { |
||
| 400 | // create the list of the decisions |
||
| 401 | List<Decision> transition = new ArrayList<>(); |
||
| 402 | // add the gap-left decision |
||
| 403 | transition.add(solution.getLeftDecision()); |
||
| 404 | |||
| 405 | // intermediate values and related relations can be activated since no synchronization is entailed |
||
| 406 | View Code Duplication | for (ComponentValue value : solution.getPath()) |
|
| 407 | { |
||
| 408 | // create parameters' labels |
||
| 409 | String[] labels = new String[value.getNumberOfParameterPlaceHolders()]; |
||
| 410 | for (int index = 0; index < labels.length; index++) { |
||
| 411 | // set parameter label |
||
| 412 | labels[index] = "label-" + index; |
||
| 413 | } |
||
| 414 | |||
| 415 | // create pending decision |
||
| 416 | Decision dec = this.component.create(value, labels); |
||
| 417 | // these decisions can be set as mandatory expansion |
||
| 418 | dec.setMandatoryExpansion(); |
||
| 419 | // add created decision to transition |
||
| 420 | transition.add(dec); |
||
| 421 | // add pending decision |
||
| 422 | solution.addCreatedDecision(dec); |
||
| 423 | |||
| 424 | // activate the decision if possible |
||
| 425 | if (!value.isComplex()) { |
||
| 426 | |||
| 427 | // activated decision |
||
| 428 | Set<Relation> list = this.component.activate(dec); |
||
| 429 | // add activated decisions |
||
| 430 | solution.addActivatedDecision(dec); |
||
| 431 | // add activated relations |
||
| 432 | solution.addActivatedRelations(list); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | // add the gap-right decision |
||
| 437 | transition.add(solution.getRightDecision()); |
||
| 438 | |||
| 439 | // create relations needed to enforce the transition |
||
| 440 | View Code Duplication | for (int index = 0; index < transition.size() - 1; index++) |
|
| 441 | { |
||
| 442 | // get adjacent decisions |
||
| 443 | Decision reference = transition.get(index); |
||
| 444 | Decision target = transition.get(index + 1); |
||
| 445 | |||
| 446 | // create pending relation |
||
| 447 | MeetsRelation meets = this.component.create(RelationType.MEETS, reference, target); |
||
| 448 | // add created relation |
||
| 449 | solution.addCreatedRelation(meets); |
||
| 450 | // activate relation if possible |
||
| 451 | if (this.component.activate(meets)) { |
||
| 452 | // add to activated relations |
||
| 453 | solution.addActivatedRelation(meets); |
||
| 454 | } |
||
| 455 | |||
| 456 | // create parameter relations |
||
| 457 | Set<Relation> pRels = this.createParameterRelations(reference, target); |
||
| 458 | // check relations |
||
| 459 | for (Relation prel : pRels) { |
||
| 460 | // add relation to solution |
||
| 461 | solution.addCreatedRelation(prel); |
||
| 462 | // activate relation if possible |
||
| 463 | if (this.component.activate(prel)) { |
||
| 464 | // add to activated relations |
||
| 465 | solution.addActivatedRelation(prel); |
||
| 466 | } |
||
| 467 | } |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | // check feasibility |
||
| 472 | this.tdb.verify(); |
||
| 473 | this.pdb.verify(); |
||
| 474 | |||
| 475 | // add solution to the flaw since everything is feasible |
||
| 476 | gap.addSolution(solution); |
||
| 477 | // print gap information |
||
| 478 | debug("Gap found on component " + this.component.getName() + ":\n" |
||
| 479 | + "- gap distance: [dmin= " + gap.getDmin() + ", dmax= " + gap.getDmax() + "] \n" |
||
| 480 | + "- left-side decision: " + gap.getLeftDecision() + "\n" |
||
| 481 | + "- right-side decision: " + gap.getRightDecision() + "\n" |
||
| 482 | + "- solution path: " + path + "\n"); |
||
| 483 | } |
||
| 484 | catch (RelationPropagationException | TransitionNotFoundException | DecisionPropagationException | ConsistencyCheckException ex) { |
||
| 485 | // discard this path as not temporally feasible |
||
| 486 | warning("Unfeasible transition path for timelime behavior completion:\n" |
||
| 487 | + "- gap distance: [dmin= " + gap.getDmin() +", " + gap.getDmax() + "]\n" |
||
| 488 | + "- path: " + path + "\n"); |
||
| 489 | // + "- path duration: [dmin= " + tranMinDuration + ", " + tranMaxDuration + "]"); |
||
| 490 | } |
||
| 491 | finally { |
||
| 492 | |||
| 493 | // deactivate relations |
||
| 494 | for (Relation rel : solution.getActivatedRelations()) { |
||
| 495 | this.component.deactivate(rel); |
||
| 496 | } |
||
| 497 | |||
| 498 | // delete relation |
||
| 499 | for (Relation rel : solution.getCreatedRelations()) { |
||
| 500 | this.component.delete(rel); |
||
| 501 | } |
||
| 502 | |||
| 503 | // deactivate decision |
||
| 504 | for (Decision dec : solution.getActivatedDecisions()) { |
||
| 505 | this.component.deactivate(dec); |
||
| 506 | } |
||
| 507 | |||
| 508 | // free decision |
||
| 509 | for (Decision dec : solution.getCreatedDecisions()) { |
||
| 510 | this.component.free(dec); |
||
| 511 | } |
||
| 512 | } |
||
| 513 | } |
||
| 514 | } |
||
| 515 | } |
||
| 516 | break; |
||
| 517 | |||
| 518 | // semantic connection missing |
||
| 519 | case SEMANTIC_CONNECTION : { |
||
| 520 | |||
| 521 | // direct connection between decisions |
||
| 522 | GapCompletion solution = new GapCompletion(gap, new ArrayList<ComponentValue>(), this.cost); |
||
| 523 | // add gap solution |
||
| 524 | gap.addSolution(solution); |
||
| 525 | } |
||
| 526 | break; |
||
| 527 | } |
||
| 528 | |||
| 529 | // check if solvable |
||
| 530 | if (!gap.isSolvable()) { |
||
| 531 | throw new UnsolvableFlawException("Unsolvable gap found on component " + this.component.getName() + ":" |
||
| 532 | + "\n- gap: " + flaw + "\n"); |
||
| 533 | } |
||
| 617 |