Code Duplication    Length = 251-253 lines in 2 locations

src/main/java/it/cnr/istc/pst/platinum/ai/framework/microkernel/resolver/plan/PlanRefinementResolver.java 1 location

@@ 527-779 (lines=253) @@
524
	 * @param decision
525
	 * @return
526
	 */
527
	private boolean isPredicateUnificationFeasible(Decision goal, Decision decision) 
528
	{
529
		// feasibility flag
530
		boolean feasible = true;
531
		// first check if the decisions refer to the same values
532
		if (!decision.getValue().equals(goal.getValue()) && 
533
				decision.getComponent().equals(goal.getComponent())) {
534
			
535
			// not feasible unification
536
			feasible = false;
537
			debug("Not feasible predicate unification:\n"
538
					+ "- planning goal: " + goal + "\n"
539
					+ "- unification decision: " + decision + "\n"); 
540
		}
541
		else 
542
		{
543
			// list of committed parameter constraints
544
			Set<Relation> committed = new HashSet<>();
545
			// list of translated parameter relations - reference
546
			Set<ParameterRelation> translatedReferenceGoalRelations = new HashSet<>();
547
			// list of translated parameter relations - target
548
			Set<ParameterRelation> translatedTargetGoalRelations = new HashSet<>();
549
			
550
			// get goal component
551
			DomainComponent goalComp = goal.getComponent();
552
			// get all (pending) relation concerning the goal decision
553
			Set<Relation> pending = goalComp.getRelations(goal);
554
			// check relations
555
			for (Relation rel : pending)
556
			{
557
				// check parameter constraint type
558
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT))
559
				{
560
					// check relation type
561
					switch (rel.getType())
562
					{
563
						// bind parameter
564
						case BIND_PARAMETER: 
565
						{
566
							// the goal can be only the reference of the relation
567
							ParameterRelation pRel = (ParameterRelation) rel;
568
							
569
							// get relation reference parameter label
570
							String refParamLabel = pRel.getReferenceParameterLabel();
571
							// get label index
572
							int refParameterIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
573
							// get unification decision parameter label
574
							String label = decision.getParameterLabelByIndex(refParameterIndex);
575
576
							// update reference decision 
577
							pRel.setReference(decision);
578
							
579
							// update reference label of the relation 
580
							pRel.setReferenceParameterLabel(label);
581
							// add relation to the list of translated ones
582
							translatedReferenceGoalRelations.add(pRel);
583
						}
584
						break;
585
						
586
						case EQUAL_PARAMETER : 
587
						{
588
							// get parameter relation
589
							EqualParameterRelation eqRel = (EqualParameterRelation) rel;
590
							// check if the goal is the reference or the parameter constraint 
591
							if (eqRel.getReference().equals(goal))
592
							{
593
								// get relation reference parameter label
594
								String refParamLabel = eqRel.getReferenceParameterLabel();
595
								// get label index
596
								int refParameterIndex = eqRel.getReference().getParameterIndexByLabel(refParamLabel);
597
								// get unification decision parameter label
598
								String label = decision.getParameterLabelByIndex(refParameterIndex);
599
600
								// update reference decision 
601
								eqRel.setReference(decision);
602
								// update reference label of the relation 
603
								eqRel.setReferenceParameterLabel(label);
604
								// add relation to the list of translated ones
605
								translatedReferenceGoalRelations.add(eqRel);
606
							}
607
							else // the goal is the target of the relation 
608
							{
609
								// get relation reference parameter label
610
								String refParamLabel = eqRel.getTargetParameterLabel();
611
								// get label index
612
								int refParameterIndex = eqRel.getTarget().getParameterIndexByLabel(refParamLabel);
613
								// get unification decision parameter label
614
								String label = decision.getParameterLabelByIndex(refParameterIndex);
615
616
								// update reference decision 
617
								eqRel.setTarget(decision);
618
								// update reference label of the relation 
619
								eqRel.setTargetParameterLabel(label);
620
								// add relation to the list of translated ones
621
								translatedTargetGoalRelations.add(eqRel);
622
							}
623
						}
624
						break;
625
						
626
						case NOT_EQUAL_PARAMETER : 
627
						{
628
							// get parameter relation
629
							NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
630
							// check if the goal is the reference or the parameter constraint 
631
							if (neqRel.getReference().equals(goal))
632
							{
633
								// get relation reference parameter label
634
								String refParamLabel = neqRel.getReferenceParameterLabel();
635
								// get label index
636
								int refParameterIndex = neqRel.getReference().getParameterIndexByLabel(refParamLabel);
637
								// get unification decision parameter label
638
								String label = decision.getParameterLabelByIndex(refParameterIndex);
639
640
								// update reference decision 
641
								neqRel.setReference(decision);
642
								// update reference label of the relation 
643
								neqRel.setReferenceParameterLabel(label);
644
								// add relation to the list of translated ones
645
								translatedReferenceGoalRelations.add(neqRel);
646
							}
647
							else // the goal is the target of the relation 
648
							{
649
								// get relation reference parameter label
650
								String refParamLabel = neqRel.getTargetParameterLabel();
651
								// get label index
652
								int refParameterIndex = neqRel.getTarget().getParameterIndexByLabel(refParamLabel);
653
								// get unification decision parameter label
654
								String label = decision.getParameterLabelByIndex(refParameterIndex);
655
656
								// update reference decision 
657
								neqRel.setTarget(decision);
658
								// update reference label of the relation 
659
								neqRel.setTargetParameterLabel(label);
660
								// add relation to the list of translated ones
661
								translatedTargetGoalRelations.add(neqRel);
662
							}
663
						}
664
						break;
665
						
666
						
667
						default:
668
							// unknown parameter relation
669
							throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n");
670
					}
671
				}
672
			}
673
				
674
			try
675
			{
676
				// activate translated relations
677
				for (Relation rel : translatedReferenceGoalRelations) {
678
					// check if can be activated
679
					if (rel.getReference().getComponent().activate(rel)) {
680
						// add relation to the committed list
681
						committed.add(rel);
682
					}
683
				}
684
				
685
				// activate translated relations
686
				for (Relation rel : translatedTargetGoalRelations) {
687
					// check if can be activated
688
					if (rel.getReference().getComponent().activate(rel)) {
689
						// add relation to the committed list
690
						committed.add(rel);
691
					}
692
				}
693
				
694
				// check parameter of the plan
695
				this.pdb.verify();
696
			}
697
			catch (ConsistencyCheckException | RelationPropagationException ex) {
698
				// not feasible 
699
				feasible = false;
700
				// not feasible unification
701
				debug("Not feasible predicate unification:\n"
702
						+ "- planning goal: " + goal + "\n"
703
						+ "- unification decision: " + decision + "\n");
704
			}
705
			finally 
706
			{
707
				// check committed relations
708
				for (Relation rel : committed) {
709
					// deactivate relation
710
					rel.getReference().getComponent().deactivate(rel);
711
				}
712
713
				
714
				// translated back parameter relations
715
				for (ParameterRelation rel : translatedReferenceGoalRelations)
716
				{
717
					// get relation reference parameter label
718
					String refParamLabel = rel.getReferenceParameterLabel();
719
					// get label index
720
					int pIndex = rel.getReference().getParameterIndexByLabel(refParamLabel);
721
					// get goal decision parameter label
722
					String label = goal.getParameterLabelByIndex(pIndex);
723
					
724
					// update relation
725
					rel.setReference(goal);
726
					rel.setReferenceParameterLabel(label);
727
				}
728
				
729
				// translated back parameter relations
730
				for (ParameterRelation rel : translatedTargetGoalRelations)
731
				{
732
					// check relation
733
					switch (rel.getType())
734
					{
735
						case EQUAL_PARAMETER : 
736
						{
737
							// get equal relation
738
							EqualParameterRelation eqRel = (EqualParameterRelation) rel;
739
							// get relation reference parameter label
740
							String tarParamLabel = eqRel.getTargetParameterLabel();
741
							// get label index
742
							int pIndex = eqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
743
							// get goal decision parameter label
744
							String label = goal.getParameterLabelByIndex(pIndex);
745
							
746
							// update relation
747
							eqRel.setTarget(goal);
748
							eqRel.setTargetParameterLabel(label);
749
						}
750
						break;
751
							
752
						case NOT_EQUAL_PARAMETER : 
753
						{
754
							// get equal relation
755
							NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
756
							// get relation reference parameter label
757
							String tarParamLabel = neqRel.getTargetParameterLabel();
758
							// get label index
759
							int pIndex = neqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
760
							// get goal decision parameter label
761
							String label = goal.getParameterLabelByIndex(pIndex);
762
							
763
							// update relation
764
							neqRel.setTarget(goal);
765
							neqRel.setTargetParameterLabel(label);
766
						}
767
						break;
768
						
769
						default:
770
							// unknown parameter relation
771
							throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n"); 
772
							
773
					}
774
				}
775
			}
776
		}
777
		
778
		// get feasibility flag
779
		return feasible;
780
	}
781
	
782
	/**

src/main/java/it/cnr/istc/pst/platinum/ai/framework/microkernel/resolver/plan/TimelineAwarePlanRefinementResolver.java 1 location

@@ 532-782 (lines=251) @@
529
	 * @param decision
530
	 * @return
531
	 */
532
	private boolean isPredicateUnificationFeasible(Decision goal, Decision decision) {
533
		
534
		// feasibility flag
535
		boolean feasible = true;
536
		// first check if the decisions refer to the same values
537
		if (!decision.getValue().equals(goal.getValue()) && 
538
				decision.getComponent().equals(goal.getComponent())) {
539
			
540
			// not feasible unification
541
			feasible = false;
542
			debug("Not feasible predicate unification:\n"
543
					+ "- planning goal: " + goal + "\n"
544
					+ "- unification decision: " + decision + "\n"); 
545
		} else {
546
			
547
			// list of committed parameter constraints
548
			Set<Relation> committed = new HashSet<>();
549
			// list of translated parameter relations - reference
550
			Set<ParameterRelation> translatedReferenceGoalRelations = new HashSet<>();
551
			// list of translated parameter relations - target
552
			Set<ParameterRelation> translatedTargetGoalRelations = new HashSet<>();
553
			
554
			// get goal component
555
			DomainComponent goalComp = goal.getComponent();
556
			// get all (pending) relation concerning the goal decision
557
			Set<Relation> pending = goalComp.getRelations(goal);
558
			// check relations
559
			for (Relation rel : pending)
560
			{
561
				// check parameter constraint type
562
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT))
563
				{
564
					// check relation type
565
					switch (rel.getType())
566
					{
567
						// bind parameter
568
						case BIND_PARAMETER: 
569
						{
570
							// the goal can be only the reference of the relation
571
							ParameterRelation pRel = (ParameterRelation) rel;
572
							
573
							// get relation reference parameter label
574
							String refParamLabel = pRel.getReferenceParameterLabel();
575
							// get label index
576
							int refParameterIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
577
							// get unification decision parameter label
578
							String label = decision.getParameterLabelByIndex(refParameterIndex);
579
580
							// update reference decision 
581
							pRel.setReference(decision);
582
							
583
							// update reference label of the relation 
584
							pRel.setReferenceParameterLabel(label);
585
							// add relation to the list of translated ones
586
							translatedReferenceGoalRelations.add(pRel);
587
						}
588
						break;
589
						
590
						case EQUAL_PARAMETER : 
591
						{
592
							// get parameter relation
593
							EqualParameterRelation eqRel = (EqualParameterRelation) rel;
594
							// check if the goal is the reference or the parameter constraint 
595
							if (eqRel.getReference().equals(goal))
596
							{
597
								// get relation reference parameter label
598
								String refParamLabel = eqRel.getReferenceParameterLabel();
599
								// get label index
600
								int refParameterIndex = eqRel.getReference().getParameterIndexByLabel(refParamLabel);
601
								// get unification decision parameter label
602
								String label = decision.getParameterLabelByIndex(refParameterIndex);
603
604
								// update reference decision 
605
								eqRel.setReference(decision);
606
								// update reference label of the relation 
607
								eqRel.setReferenceParameterLabel(label);
608
								// add relation to the list of translated ones
609
								translatedReferenceGoalRelations.add(eqRel);
610
							}
611
							else // the goal is the target of the relation 
612
							{
613
								// get relation reference parameter label
614
								String refParamLabel = eqRel.getTargetParameterLabel();
615
								// get label index
616
								int refParameterIndex = eqRel.getTarget().getParameterIndexByLabel(refParamLabel);
617
								// get unification decision parameter label
618
								String label = decision.getParameterLabelByIndex(refParameterIndex);
619
620
								// update reference decision 
621
								eqRel.setTarget(decision);
622
								// update reference label of the relation 
623
								eqRel.setTargetParameterLabel(label);
624
								// add relation to the list of translated ones
625
								translatedTargetGoalRelations.add(eqRel);
626
							}
627
						}
628
						break;
629
						
630
						case NOT_EQUAL_PARAMETER : 
631
						{
632
							// get parameter relation
633
							NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
634
							// check if the goal is the reference or the parameter constraint 
635
							if (neqRel.getReference().equals(goal))
636
							{
637
								// get relation reference parameter label
638
								String refParamLabel = neqRel.getReferenceParameterLabel();
639
								// get label index
640
								int refParameterIndex = neqRel.getReference().getParameterIndexByLabel(refParamLabel);
641
								// get unification decision parameter label
642
								String label = decision.getParameterLabelByIndex(refParameterIndex);
643
644
								// update reference decision 
645
								neqRel.setReference(decision);
646
								// update reference label of the relation 
647
								neqRel.setReferenceParameterLabel(label);
648
								// add relation to the list of translated ones
649
								translatedReferenceGoalRelations.add(neqRel);
650
							}
651
							else // the goal is the target of the relation 
652
							{
653
								// get relation reference parameter label
654
								String refParamLabel = neqRel.getTargetParameterLabel();
655
								// get label index
656
								int refParameterIndex = neqRel.getTarget().getParameterIndexByLabel(refParamLabel);
657
								// get unification decision parameter label
658
								String label = decision.getParameterLabelByIndex(refParameterIndex);
659
660
								// update reference decision 
661
								neqRel.setTarget(decision);
662
								// update reference label of the relation 
663
								neqRel.setTargetParameterLabel(label);
664
								// add relation to the list of translated ones
665
								translatedTargetGoalRelations.add(neqRel);
666
							}
667
						}
668
						break;
669
						
670
						
671
						default:
672
							// unknown parameter relation
673
							throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n");
674
					}
675
				}
676
			}
677
				
678
			try {
679
				
680
				// activate translated relations
681
				for (Relation rel : translatedReferenceGoalRelations) {
682
					// check if can be activated
683
					if (rel.getReference().getComponent().activate(rel)) {
684
						// add relation to the committed list
685
						committed.add(rel);
686
					}
687
				}
688
				
689
				// activate translated relations
690
				for (Relation rel : translatedTargetGoalRelations) {
691
					// check if can be activated
692
					if (rel.getReference().getComponent().activate(rel)) {
693
						// add relation to the committed list
694
						committed.add(rel);
695
					}
696
				}
697
				
698
				// check parameter of the plan
699
				this.pdb.verify();
700
			}
701
			catch (ConsistencyCheckException | RelationPropagationException ex) {
702
				// not feasible 
703
				feasible = false;
704
				// not feasible unification
705
				debug("Not feasible predicate unification:\n"
706
						+ "- planning goal: " + goal + "\n"
707
						+ "- unification decision: " + decision + "\n");
708
			
709
			} finally  {
710
				
711
				// check committed relations
712
				for (Relation rel : committed) {
713
					// deactivate relation
714
					rel.getReference().getComponent().deactivate(rel);
715
				}
716
				
717
				// translated back parameter relations
718
				for (ParameterRelation rel : translatedReferenceGoalRelations) {
719
					
720
					// get relation reference parameter label
721
					String refParamLabel = rel.getReferenceParameterLabel();
722
					// get label index
723
					int pIndex = rel.getReference().getParameterIndexByLabel(refParamLabel);
724
					// get goal decision parameter label
725
					String label = goal.getParameterLabelByIndex(pIndex);
726
					
727
					// update relation
728
					rel.setReference(goal);
729
					rel.setReferenceParameterLabel(label);
730
				}
731
				
732
				// translated back parameter relations
733
				for (ParameterRelation rel : translatedTargetGoalRelations)
734
				{
735
					// check relation
736
					switch (rel.getType())
737
					{
738
						case EQUAL_PARAMETER : 
739
						{
740
							// get equal relation
741
							EqualParameterRelation eqRel = (EqualParameterRelation) rel;
742
							// get relation reference parameter label
743
							String tarParamLabel = eqRel.getTargetParameterLabel();
744
							// get label index
745
							int pIndex = eqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
746
							// get goal decision parameter label
747
							String label = goal.getParameterLabelByIndex(pIndex);
748
							
749
							// update relation
750
							eqRel.setTarget(goal);
751
							eqRel.setTargetParameterLabel(label);
752
						}
753
						break;
754
							
755
						case NOT_EQUAL_PARAMETER : 
756
						{
757
							// get equal relation
758
							NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
759
							// get relation reference parameter label
760
							String tarParamLabel = neqRel.getTargetParameterLabel();
761
							// get label index
762
							int pIndex = neqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
763
							// get goal decision parameter label
764
							String label = goal.getParameterLabelByIndex(pIndex);
765
							
766
							// update relation
767
							neqRel.setTarget(goal);
768
							neqRel.setTargetParameterLabel(label);
769
						}
770
						break;
771
						
772
						default:
773
							// unknown parameter relation
774
							throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n"); 
775
							
776
					}
777
				}
778
			}
779
		}
780
		
781
		// get feasibility flag
782
		return feasible;
783
	}
784
	
785
	/**