Conditions | 24 |
Total Lines | 219 |
Code Lines | 95 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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.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; |
||
329 | @Override |
||
330 | protected void doComputeFlawSolutions(Flaw flaw) |
||
331 | throws UnsolvableFlawException { |
||
332 | |||
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 | // extract the direct predecessors of the right decision |
||
343 | List<ComponentValue> predecessors = this.component.getDirectPredecessors(gap.getRightDecision().getValue()); |
||
344 | // create an alternative solution for each feasible predecessor |
||
345 | for (ComponentValue pred : predecessors) { |
||
346 | |||
347 | // check if direct transition |
||
348 | if (pred.equals(gap.getLeftDecision().getValue())) { |
||
349 | |||
350 | // direct transition from left to right |
||
351 | GapCompletion solution = new GapCompletion(gap, new ArrayList<>(), this.cost); |
||
352 | // list of created and activated decisions |
||
353 | List<Decision> dCreated = new ArrayList<>(); |
||
354 | List<Decision> dActivated = new ArrayList<>(); |
||
355 | // list of created and activated relations |
||
356 | List<Relation> rCreated = new ArrayList<>(); |
||
357 | List<Relation> rActivated = new ArrayList<>(); |
||
358 | |||
359 | try { |
||
360 | |||
361 | // direct token transition between active decisions |
||
362 | Decision reference = solution.getLeftDecision(); |
||
363 | Decision target = solution.getRightDecision(); |
||
364 | |||
365 | // create meets constraint to enforce the desired transition |
||
366 | MeetsRelation meets = this.component.create(RelationType.MEETS, reference, target); |
||
367 | // add created relation |
||
368 | rCreated.add(meets); |
||
369 | // propagate relation and activate it if possible |
||
370 | if (this.component.activate(meets)) { |
||
371 | // add activated relation to solution |
||
372 | rActivated.add(meets); |
||
373 | } |
||
374 | |||
375 | // create parameter relations |
||
376 | Set<Relation> pRels = this.createParameterRelations(reference, target); |
||
377 | // add created relation |
||
378 | rCreated.add(meets); |
||
379 | // propagate relation |
||
380 | for (Relation pRel : pRels) { |
||
381 | // activate relation if possible |
||
382 | if (this.component.activate(pRel)) { |
||
383 | // add activated relation to solution |
||
384 | rActivated.add(pRel); |
||
385 | } |
||
386 | } |
||
387 | |||
388 | // the solution is feasible |
||
389 | this.tdb.verify(); |
||
390 | this.pdb.verify(); |
||
391 | |||
392 | // valid solution |
||
393 | gap.addSolution(solution); |
||
394 | |||
395 | } catch (RelationPropagationException | TransitionNotFoundException | ConsistencyCheckException ex) { |
||
396 | // discard this path as not temporally feasible |
||
397 | warning("Unfeasible direct transition for timelime behavior completion:\n" |
||
398 | + "- gap distance: [dmin= " + gap.getDmin() +", " + gap.getDmax() + "]"); |
||
399 | } finally { |
||
400 | |||
401 | // deactivate relations |
||
402 | for (Relation rel : rActivated) { |
||
403 | this.component.deactivate(rel); |
||
404 | } |
||
405 | |||
406 | // delete relation |
||
407 | for (Relation rel : rCreated) { |
||
408 | this.component.delete(rel); |
||
409 | } |
||
410 | |||
411 | // deactivate decision |
||
412 | for (Decision dec : dActivated) { |
||
413 | this.component.deactivate(dec); |
||
414 | } |
||
415 | |||
416 | // free and then delete decision |
||
417 | for (Decision dec : dCreated) { |
||
418 | // free decision |
||
419 | this.component.free(dec); |
||
420 | // completely remove decision reference |
||
421 | this.component.delete(dec); |
||
422 | } |
||
423 | } |
||
424 | |||
425 | } else { |
||
426 | |||
427 | |||
428 | // set (partial) steps to complete the gap |
||
429 | List<ComponentValue> steps = new ArrayList<>(); |
||
430 | steps.add(pred); |
||
431 | |||
432 | // transition from left to right through the "step" value |
||
433 | GapCompletion solution = new GapCompletion(gap, steps, this.cost); |
||
434 | // list of created and activated decisions |
||
435 | List<Decision> dCreated = new ArrayList<>(); |
||
436 | List<Decision> dActivated = new ArrayList<>(); |
||
437 | // list of created and activated relations |
||
438 | List<Relation> rCreated = new ArrayList<>(); |
||
439 | List<Relation> rActivated = new ArrayList<>(); |
||
440 | |||
441 | try { |
||
442 | |||
443 | |||
444 | // create parameters' labels |
||
445 | String[] labels = new String[pred.getNumberOfParameterPlaceHolders()]; |
||
446 | for (int index = 0; index < labels.length; index++) { |
||
447 | // set parameter label |
||
448 | labels[index] = "label-" + index; |
||
449 | } |
||
450 | |||
451 | // create pending decision |
||
452 | Decision dec = this.component.create(pred, labels); |
||
453 | // these decisions can be set as mandatory expansion |
||
454 | dec.setMandatoryExpansion(); |
||
455 | // add pending decision |
||
456 | dCreated.add(dec); |
||
457 | |||
458 | // activate the decision if possible |
||
459 | if (!pred.isComplex()) { |
||
460 | // activated decision |
||
461 | Set<Relation> list = this.component.activate(dec); |
||
462 | // add activated decisions |
||
463 | dActivated.add(dec); |
||
464 | // add activated relations |
||
465 | rActivated.addAll(list); |
||
466 | } |
||
467 | |||
468 | // create pending relation |
||
469 | MeetsRelation meets = this.component.create(RelationType.MEETS, dec, gap.getRightDecision()); |
||
470 | // add created relation |
||
471 | rCreated.add(meets); |
||
472 | // activate relation if possible |
||
473 | if (this.component.activate(meets)) { |
||
474 | // add to activated relations |
||
475 | rActivated.add(meets); |
||
476 | } |
||
477 | |||
478 | // create parameter relations |
||
479 | Set<Relation> pRels = this.createParameterRelations(dec, gap.getRightDecision()); |
||
480 | // check relations |
||
481 | for (Relation prel : pRels) { |
||
482 | // add relation to solution |
||
483 | rCreated.add(prel); |
||
484 | // activate relation if possible |
||
485 | if (this.component.activate(prel)) { |
||
486 | // add to activated relations |
||
487 | rCreated.add(prel); |
||
488 | } |
||
489 | } |
||
490 | |||
491 | // the solution is feasible |
||
492 | this.tdb.verify(); |
||
493 | this.pdb.verify(); |
||
494 | |||
495 | // valid (partial) solution |
||
496 | gap.addSolution(solution); |
||
497 | |||
498 | } catch (RelationPropagationException | TransitionNotFoundException | DecisionPropagationException | ConsistencyCheckException ex) { |
||
499 | // discard this path as not temporally feasible |
||
500 | warning("Unfeasible (partial) transition path for timelime behavior completion:\n" |
||
501 | + "- gap distance: [dmin= " + gap.getDmin() +", " + gap.getDmax() + "]\n" |
||
502 | + "- intermediate: " + pred + "\n"); |
||
503 | } finally { |
||
504 | |||
505 | // deactivate relations |
||
506 | for (Relation rel : rActivated) { |
||
507 | this.component.deactivate(rel); |
||
508 | } |
||
509 | |||
510 | // delete relation |
||
511 | for (Relation rel : rCreated) { |
||
512 | this.component.delete(rel); |
||
513 | } |
||
514 | |||
515 | // deactivate decision |
||
516 | for (Decision dec : dActivated) { |
||
517 | this.component.deactivate(dec); |
||
518 | } |
||
519 | |||
520 | // free and then delete decision |
||
521 | for (Decision dec : dCreated) { |
||
522 | // free decision |
||
523 | this.component.free(dec); |
||
524 | // completely remove decision reference |
||
525 | this.component.delete(dec); |
||
526 | } |
||
527 | } |
||
528 | } |
||
529 | } |
||
530 | } |
||
531 | break; |
||
532 | |||
533 | // semantic connection missing |
||
534 | case SEMANTIC_CONNECTION : { |
||
535 | |||
536 | // direct connection between decisions |
||
537 | GapCompletion solution = new GapCompletion(gap, new ArrayList<ComponentValue>(), this.cost); |
||
538 | // add gap solution |
||
539 | gap.addSolution(solution); |
||
540 | } |
||
541 | break; |
||
542 | } |
||
543 | |||
544 | // check if solvable |
||
545 | if (!gap.isSolvable()) { |
||
546 | throw new UnsolvableFlawException("Unsolvable gap found on component " + this.component.getName() + ":" |
||
547 | + "\n- gap: " + flaw + "\n"); |
||
548 | } |
||
628 |