diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java index 7f86e9359..fd5ffcaed 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java @@ -3,7 +3,7 @@ package de.dhbwstuttgart.bytecode.gGenericsAli; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; public class ClassConstraint extends TPHConstraint { - private TPHConstraint constraint; + //private TPHConstraint constraint; public ClassConstraint(String left, String right, Relation rel) { super(left, right, rel); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java index fa626c48e..c8931c28b 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java @@ -15,28 +15,38 @@ public class FamilyOfGeneratedGenerics { public static List getClassConstraints(List cs, HashMap posOfTphs) { //Inputparameter List constraintsSet weg List cs_cl = new ArrayList<>(); - System.out.println("0: " + cs_cl); List classConstraints1 = typeOfANodeOfAField(cs, cs_cl, posOfTphs); - cs_cl.addAll(classConstraints1); - System.out.println("1: " + cs_cl); + for (ClassConstraint cons: classConstraints1) { + if (!checkForDuplicates(cons, cs_cl)) { + cs_cl.add(cons); + } + } List classConstraints2 = transitiveSubtypeForClassTypes(cs, cs_cl); // in Klammer classConstraints1 oder constraintsSet? beides eher - cs_cl.addAll(classConstraints2); - System.out.println("2: " + cs_cl); + for (ClassConstraint cons: classConstraints2) { + if (!checkForDuplicates(cons, cs_cl)) { + cs_cl.add(cons); + } + } List classConstraints3 = hasNoSupertypeForClassTypes(cs, cs_cl, posOfTphs); - cs_cl.addAll(classConstraints3); - System.out.println("3: " + cs_cl); - + for (ClassConstraint cons: classConstraints3) { + if (!checkForDuplicates(cons, cs_cl)) { + cs_cl.add(cons); + } + } return cs_cl; } - public static List getMethodConstraints(List constraintsSet) { + public static List getMethodConstraints(List cs, HashMap posOfTphs) { //TODO: Regeln List cs_m = new ArrayList<>(); -// for(TPHConstraint cons: constraintsSet){ -// -// cs_m = -// } - return null; + List methodConstraints1 = typeOfTheMethodInClSigma(cs, cs_m, posOfTphs); + for (MethodConstraint cons: methodConstraints1) { + if (!checkForDuplicates(cons, cs_m)) { + cs_m.add(cons); + } + } + List methodConstraints2 = firstTransitiveSubtypeForMethodTypes(); + return cs_m; } /** @@ -49,7 +59,10 @@ public class FamilyOfGeneratedGenerics { if(posOfTphs.containsKey(allCons.getLeft()) && allCons.getRight()!=null && allCons.getRel()==Relation.EXTENDS) { for(String tph: posOfTphs.keySet()) { if(tph == allCons.getLeft() && posOfTphs.get(tph) == PositionFinder.Position.FIELD) { - tempCC.add(new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel())); + ClassConstraint consToAdd = new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()); + if (!checkForDuplicates(consToAdd, tempCC)) { + tempCC.add(consToAdd); + } } } } @@ -62,13 +75,15 @@ public class FamilyOfGeneratedGenerics { * {T' <. T'' | \exists T: (T <. T') \in cs_cl, (T' <. T'') \in cs } */ public static List transitiveSubtypeForClassTypes(List allConstraints, List cs_cl) { - //TODO: List tempCC= new ArrayList<>(); for(ClassConstraint cCons: cs_cl) { if(cCons.getLeft() != null && cCons.getRel()==Relation.EXTENDS) { for(TPHConstraint allCons: allConstraints) { if(cCons.getRight() == allCons.getLeft() && allCons.getRight() != null && allCons.getRel()==Relation.EXTENDS){ - tempCC.add(new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel())); + ClassConstraint consToAdd = new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()); + if (!checkForDuplicates(consToAdd, tempCC)) { + tempCC.add(consToAdd); + } } } } @@ -82,18 +97,19 @@ public class FamilyOfGeneratedGenerics { * or (\exists T~: (T~ <. T) \in cs_cl)) and (\existsnot T': T <. T') \in cs)} */ public static List hasNoSupertypeForClassTypes(List allConstraints, List cs_cl, HashMap posOfTphs) { - //TODO: List tempCC= new ArrayList<>(); for(TPHConstraint allCons: allConstraints) { for(ClassConstraint cCons: cs_cl) { for(String tph: posOfTphs.keySet()) { - System.out.println("cCons: " + cCons); if( (posOfTphs.get(tph) == PositionFinder.Position.FIELD) || - (posOfTphs.containsKey(cCons.getLeft()) && cCons.getRight() != null) && - allCons.getRight() == null && - allCons.getRel()==Relation.EXTENDS && cCons.getRel()==Relation.EXTENDS) { - System.out.println("C3: " + allCons); - tempCC.add(new ClassConstraint(allCons.getLeft(), "Object", Relation.EXTENDS)); + (posOfTphs.containsKey(cCons.getRight()) && cCons.getLeft() != null) && + (allCons.getLeft() == tph && allCons.getRight() == null) && + allCons.getRel()==Relation.EXTENDS && cCons.getRel()==Relation.EXTENDS && + cCons.getRight() == tph && allCons.getLeft() == tph) { + ClassConstraint consToAdd = new ClassConstraint(cCons.getRight(), "Object", Relation.EXTENDS); + if (!checkForDuplicates(consToAdd, tempCC)){ + tempCC.add(consToAdd); + } } } } @@ -107,57 +123,101 @@ public class FamilyOfGeneratedGenerics { /** * Def. FGG: erste Zeile von cs_m * {T < .T' | T is a type variable in a type of the method/constructor m in cl_\sigma, (T <. T') \in cs} - *//* + */ - public List typeOfTheMethodInClSigma() { // cl_\sigma?? + public static List typeOfTheMethodInClSigma(List allConstraints, List cs_m, HashMap posOfTphs) { // cl_\sigma?? //TODO: - return cs_m; + List tempCC= new ArrayList<>(); + for(TPHConstraint allCons: allConstraints){ + if(posOfTphs.containsKey(allCons.getLeft()) && allCons.getRight()!=null && allCons.getRel()==Relation.EXTENDS) { + for(String tph: posOfTphs.keySet()) { + if(tph == allCons.getLeft() && (posOfTphs.get(tph) == PositionFinder.Position.METHOD || posOfTphs.get(tph) == PositionFinder.Position.CONSTRUCTOR)) { + MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()); + if (!checkForDuplicates(consToAdd, tempCC)) { + tempCC.add(consToAdd); + } + } + } + } + } + return tempCC; } - */ + /** * Def. FGG: zweite Zeile von cs_m * {R' <. S | (R <. R'), (S <. S') \in cs_m and (R',S) is in the transitive closure of cs} - *//* + */ - public List firstTransitiveSubtypeForMethodTypes() { //transitive closure of cs + public static List firstTransitiveSubtypeForMethodTypes() { //transitive closure of cs //TODO: - return cs_m; + return null; } - */ /** * Def. FGG: dritte Zeile von cs_m * {R' <. S | (R <. R') \in cs_m, (S <. S') \in cs_cl and (R',S) is in the transitive closure of cs} - *//* + */ - public List secondTransitiveSubtypeForMethodTypes() { + public static List secondTransitiveSubtypeForMethodTypes() { //TODO: - return cs_m; + return null; } - */ /** * Def. FGG: vierte Zeile von cs_m * {T <. Object | (T is a type variable in a type of a node of the method/constructor m in cl_\sigma), * (\existsnot T': T <. T') \in cs)} - *//* + */ - public List hasNoSupertypeForMethodTypes() { + public static List hasNoSupertypeForMethodTypes() { //TODO: - return cs_m; + return null; } - */ /** * nimm die Menge cs_cl aus cs_m raus - *//* - - public List methodTypesWithoutClassTypes() { + */ + public static List methodTypesWithoutClassTypes() { //TODO: - return cs_m; + return null; } -*/ + + public static boolean checkForDuplicates(TPHConstraint constraint, List list) { + List tempList = list; + for (TPHConstraint tphC: tempList) { + return (constraint.getLeft() == tphC.getLeft() && + constraint.getRight() == tphC.getRight() && + constraint.getRel() == tphC.getRel()); //constraint already in ArrayList if true + } + return false; + } + + public static List buildTransitiveClosure(List list) { + List iterList = list; + List runList = list; + List tcList = new ArrayList<>(); + boolean addedConToList = false; + for (TPHConstraint cons: iterList) { + for (TPHConstraint cons2: runList) { + if(cons.getRight() == cons2.getLeft()) { + for (TPHConstraint tcCons: tcList) { + if (!checkForDuplicates(tcCons,tcList)) { + tcList.add(new TPHConstraint(cons.getLeft(), cons2.getRight(), Relation.EXTENDS)); //Duplikate? dürfte nicht sein -> checken + addedConToList = true; + if (addedConToList) { + list.add(new TPHConstraint(cons.getLeft(), cons2.getRight(), Relation.EXTENDS)); + buildTransitiveClosure(list); + } + } + //TODO: über aktualisierte Liste laufen wegen Updates -> Rekursion? + } + } + } + } + return tcList; + } + diff --git a/src/test/java/insertGenerics/TestExample42.java b/src/test/java/insertGenerics/TestExample42.java index aded85198..219e3fffa 100644 --- a/src/test/java/insertGenerics/TestExample42.java +++ b/src/test/java/insertGenerics/TestExample42.java @@ -4,6 +4,7 @@ import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; import de.dhbwstuttgart.bytecode.gGenericsAli.ClassConstraint; import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics; +import de.dhbwstuttgart.bytecode.gGenericsAli.MethodConstraint; import de.dhbwstuttgart.bytecode.gGenericsAli.PositionFinder; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -42,6 +43,10 @@ public class TestExample42 { @Test public void genericTest() { List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs()); - System.out.println(classConstraints); + System.out.println("ClassConstraints: " + classConstraints); + List methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),fillPosOfTphs()); + System.out.println("MethodConstraints: " + methodConstraints); + + List testCons; } } diff --git a/src/test/java/insertGenerics/TestTransitiveClosure.java b/src/test/java/insertGenerics/TestTransitiveClosure.java new file mode 100644 index 000000000..e1bc022b1 --- /dev/null +++ b/src/test/java/insertGenerics/TestTransitiveClosure.java @@ -0,0 +1,32 @@ +package insertGenerics; + +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; +import de.dhbwstuttgart.bytecode.gGenericsAli.ClassConstraint; +import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics; +import de.dhbwstuttgart.bytecode.gGenericsAli.MethodConstraint; +import de.dhbwstuttgart.bytecode.gGenericsAli.PositionFinder; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class TestTransitiveClosure { + public List fillList() { + List list = new ArrayList<>(); + list.add(new TPHConstraint("A", "B", Relation.EXTENDS)); + list.add(new TPHConstraint("B", "C", Relation.EXTENDS)); + list.add(new TPHConstraint("C", "D", Relation.EXTENDS)); + + return list; + } + + @Test + public void genericTest() { + List testCons = FamilyOfGeneratedGenerics.buildTransitiveClosure(fillList()); + System.out.println(testCons); + + } +}