diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java index 96f2cb74..c8931c28 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java @@ -36,14 +36,17 @@ public class FamilyOfGeneratedGenerics { 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; } /** @@ -72,7 +75,6 @@ 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) { @@ -95,7 +97,6 @@ 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) { @@ -122,57 +123,65 @@ 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 static 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 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 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 static List hasNoSupertypeForMethodTypes() { //TODO: - return cs_m; + return null; } - */ /** * nimm die Menge cs_cl aus cs_m raus */ -/* public static List methodTypesWithoutClassTypes() { //TODO: - return cs_m; + return null; } -*/ public static boolean checkForDuplicates(TPHConstraint constraint, List list) { List tempList = list; @@ -184,6 +193,32 @@ public class FamilyOfGeneratedGenerics { 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 aded8519..219e3fff 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 00000000..e1bc022b --- /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); + + } +}