Compare commits

..

1 Commits

Author SHA1 Message Date
AluAli
88175f8b48 modified: src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java
modified:   src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java
	modified:   src/test/java/insertGenerics/TestExample42.java
2021-01-08 15:44:07 +01:00
15 changed files with 141 additions and 289 deletions

View File

@@ -118,14 +118,6 @@ http://maven.apache.org/maven-v4_0_0.xsd">
</descriptorRefs> </descriptorRefs>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
<repositories> <repositories>

View File

@@ -16,13 +16,15 @@ public class FamilyOfGeneratedGenerics {
public HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTPHs = new HashMap<>(); public HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTPHs = new HashMap<>();
public List<ClassConstraint> classConstraints = new ArrayList<>(); public List<ClassConstraint> classConstraints = new ArrayList<>();
public List<MethodConstraint> methodConstraints = new ArrayList<>(); public List<MethodConstraint> methodConstraints = new ArrayList<>();
public HashMap<MethodConstraint, String> methodsWithPosition = new HashMap<>();
public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) { public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) {
this.allConstraints = tphExtractor.allCons; this.allConstraints = tphExtractor.allCons;
this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph); this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph);
this.classConstraints = getClassConstraints(allConstraints,posOfTPHs); this.classConstraints = getClassConstraints(allConstraints,posOfTPHs);
// this.methodConstraints = // this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs);
} }
public static List<ClassConstraint> getClassConstraints(List<TPHConstraint> cs, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { //Inputparameter List<TPHConstraint> constraintsSet weg public static List<ClassConstraint> getClassConstraints(List<TPHConstraint> cs, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { //Inputparameter List<TPHConstraint> constraintsSet weg
@@ -48,38 +50,72 @@ public class FamilyOfGeneratedGenerics {
return cs_cl; return cs_cl;
} }
public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs, List<ClassConstraint> cs_cl, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { // public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs, List<ClassConstraint> cs_cl, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
// //TODO: Regeln
// List<MethodConstraint> cs_m = new ArrayList<>();
// List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs);
// for (MethodConstraint cons: methodConstraints1) {
// if (!checkForDuplicates(cons, cs_m)) {
// cs_m.add(cons);
// }
// }
// List<MethodConstraint> methodConstraints2 = firstTransitiveSubtypeForMethodTypes(cs, cs_m);
// for (MethodConstraint cons: methodConstraints2) {
// if (!checkForDuplicates(cons, cs_m)) {
// cs_m.add(cons);
// }
// }
// List<MethodConstraint> methodConstraints3 = secondTransitiveSubtypeForMethodTypes(cs, cs_cl, cs_m);
// for (MethodConstraint cons: methodConstraints3) {
// if (!checkForDuplicates(cons, cs_m)) {
// cs_m.add(cons);
// }
// }
// List<MethodConstraint> methodConstraints4 = hasNoSupertypeForMethodTypes(cs, posOfTphs);
// for (MethodConstraint cons: methodConstraints4) {
// if (!checkForDuplicates(cons, cs_m)) {
// cs_m.add(cons);
// }
// }
// List<MethodConstraint> methodConstraints5 = methodTypesWithoutClassTypes(cs_cl, cs_m);
// cs_m = methodConstraints5;
// return cs_m;
// }
public static HashMap<MethodConstraint, String> getMethodConstraints(List<TPHConstraint> cs, List<ClassConstraint> cs_cl, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
//TODO: Regeln //TODO: Regeln
List<MethodConstraint> cs_m = new ArrayList<>(); HashMap<MethodConstraint, String> cs_m = new HashMap<>();
List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs); HashMap<MethodConstraint, String> methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs);
for (MethodConstraint cons: methodConstraints1) { for (MethodConstraint cons: methodConstraints1.keySet()) {
if (!checkForDuplicates(cons, cs_m)) { if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) {
cs_m.put(cons,);
}
}
HashMap<MethodConstraint, String> methodConstraints2 = firstTransitiveSubtypeForMethodTypes(cs, cs_m);
for (MethodConstraint cons: methodConstraints2.keySet()) {
if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) {
cs_m.add(cons); cs_m.add(cons);
} }
} }
List<MethodConstraint> methodConstraints2 = firstTransitiveSubtypeForMethodTypes(cs, cs_m); HashMap<MethodConstraint, String> methodConstraints3 = secondTransitiveSubtypeForMethodTypes(cs, cs_cl, cs_m);
for (MethodConstraint cons: methodConstraints2) { for (MethodConstraint cons: methodConstraints3.keySet()) {
if (!checkForDuplicates(cons, cs_m)) { if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) {
cs_m.add(cons); cs_m.add(cons);
} }
} }
List<MethodConstraint> methodConstraints3 = secondTransitiveSubtypeForMethodTypes(cs, cs_cl, cs_m); HashMap<MethodConstraint, String> methodConstraints4 = hasNoSupertypeForMethodTypes(cs, posOfTphs);
for (MethodConstraint cons: methodConstraints3) { for (MethodConstraint cons: methodConstraints4.keySet()) {
if (!checkForDuplicates(cons, cs_m)) { if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) {
cs_m.add(cons); cs_m.add(cons);
} }
} }
List<MethodConstraint> methodConstraints4 = hasNoSupertypeForMethodTypes(cs, posOfTphs); HashMap<MethodConstraint, String> methodConstraints5 = methodTypesWithoutClassTypes(cs_cl, cs_m);
for (MethodConstraint cons: methodConstraints4) {
if (!checkForDuplicates(cons, cs_m)) {
cs_m.add(cons);
}
}
List<MethodConstraint> methodConstraints5 = methodTypesWithoutClassTypes(cs_cl, cs_m);
cs_m = methodConstraints5; cs_m = methodConstraints5;
return cs_m; return cs_m;
} }
/** /**
* Def. FGG: erste Zeile von cs_cl * Def. FGG: erste Zeile von cs_cl
* {T < .T' | T is a type variable in a type of a node of a field} * {T < .T' | T is a type variable in a type of a node of a field}
@@ -155,16 +191,16 @@ public class FamilyOfGeneratedGenerics {
* Def. FGG: erste Zeile von cs_m * 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} * {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<MethodConstraint> typeOfTheMethodInClSigma(List<TPHConstraint> allConstraints, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { // cl_\sigma?? public static HashMap<MethodConstraint, String> typeOfTheMethodInClSigma(List<TPHConstraint> allConstraints, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { // cl_\sigma??
//TODO: //TODO:
List<MethodConstraint> tempMC= new ArrayList<>(); HashMap<MethodConstraint, String> tempMC= new HashMap<>();
for(TPHConstraint allCons: allConstraints){ for(TPHConstraint allCons: allConstraints){
if(posOfTphs.containsKey(allCons.getLeft()) && allCons.getRight()!=null && allCons.getRel()==Relation.EXTENDS) { if(posOfTphs.containsKey(allCons.getLeft()) && allCons.getRight()!=null && allCons.getRel()==Relation.EXTENDS) {
for(String tph: posOfTphs.keySet()) { for(String tph: posOfTphs.keySet()) {
if(tph == allCons.getLeft() && (posOfTphs.get(tph).fst == PositionFinder.Position.METHOD || posOfTphs.get(tph).fst == PositionFinder.Position.CONSTRUCTOR)) { if(tph == allCons.getLeft() && (posOfTphs.get(tph).fst == PositionFinder.Position.METHOD || posOfTphs.get(tph).fst == PositionFinder.Position.CONSTRUCTOR)) {
MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()); MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel());
if (!checkForDuplicates(consToAdd, tempMC)) { if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
tempMC.add(consToAdd); tempMC.put(consToAdd, posOfTphs.get(tph).snd);
} }
} }
} }
@@ -178,19 +214,19 @@ public class FamilyOfGeneratedGenerics {
* Def. FGG: zweite Zeile von cs_m * 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} * {R' <. S | (R <. R'), (S <. S') \in cs_m and (R',S) is in the transitive closure of cs}
*/ */
public static List<MethodConstraint> firstTransitiveSubtypeForMethodTypes(List<TPHConstraint> allConstraints, List<MethodConstraint> cs_m) { //transitive closure of cs public static HashMap<MethodConstraint, String> firstTransitiveSubtypeForMethodTypes(List<TPHConstraint> allConstraints, HashMap<MethodConstraint, String> cs_m, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { //transitive closure of cs
//TODO: //TODO:
List<MethodConstraint> tempMC= new ArrayList<>(); HashMap<MethodConstraint, String> tempMC= new HashMap<>();
List<TPHConstraint> tcOfCs = buildTransitiveClosure(allConstraints); List<TPHConstraint> tcOfCs = buildTransitiveClosure(allConstraints);
for(MethodConstraint mC1 : cs_m) { //(R <. R') for(MethodConstraint mC1 : cs_m.keySet()) { //(R <. R')
for(MethodConstraint mC2 : cs_m) { //(S <. S') for(MethodConstraint mC2 : cs_m.keySet()) { //(S <. S')
String lSide = mC1.getRight(); //R' String lSide = mC1.getRight(); //R'
String rSide = mC2.getLeft(); //S String rSide = mC2.getLeft(); //S
for(TPHConstraint tphC : tcOfCs) { for(TPHConstraint tphC : tcOfCs) {
if(tphC.getLeft().equals(lSide)&&tphC.getRight().equals(rSide)) { //is it (R',S) if(tphC.getLeft().equals(lSide)&&tphC.getRight().equals(rSide)) { //is it (R',S)
MethodConstraint consToAdd = new MethodConstraint(lSide, rSide, tphC.getRel()); //create (R'<.S) MethodConstraint consToAdd = new MethodConstraint(lSide, rSide, tphC.getRel()); //create (R'<.S)
if (!checkForDuplicates(consToAdd, tempMC)) { if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
tempMC.add(consToAdd); tempMC.put(consToAdd, posOfTphs.get());
} }
} }
} }
@@ -203,20 +239,20 @@ public class FamilyOfGeneratedGenerics {
* Def. FGG: dritte Zeile von cs_m * 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} * {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<MethodConstraint> secondTransitiveSubtypeForMethodTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl, List<MethodConstraint> cs_m) { public static HashMap<MethodConstraint, String> secondTransitiveSubtypeForMethodTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl, HashMap<MethodConstraint, String> cs_m, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
//TODO: //TODO:
List<MethodConstraint> tempMC= new ArrayList<>(); HashMap<MethodConstraint, String> tempMC= new HashMap<>();
List<TPHConstraint> tcOfCs = buildTransitiveClosure(allConstraints); List<TPHConstraint> tcOfCs = buildTransitiveClosure(allConstraints);
for(ClassConstraint cC : cs_cl) { for(ClassConstraint cC : cs_cl) {
for(MethodConstraint mC : cs_m) { for(MethodConstraint mC : cs_m.keySet()) {
String leftSide = mC.getRight(); String leftSide = mC.getRight();
String rightSide = cC.getLeft(); String rightSide = cC.getLeft();
for(TPHConstraint tphC : tcOfCs) { for(TPHConstraint tphC : tcOfCs) {
if(tphC.getLeft().equals(leftSide)&&tphC.getRight().equals(rightSide)) { if(tphC.getLeft().equals(leftSide)&&tphC.getRight().equals(rightSide)) {
MethodConstraint consToAdd = new MethodConstraint(tphC.getLeft(), tphC.getRight(), tphC.getRel()); MethodConstraint consToAdd = new MethodConstraint(tphC.getLeft(), tphC.getRight(), tphC.getRel());
if (!checkForDuplicates(consToAdd, tempMC)) { if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
tempMC.add(consToAdd); tempMC.put(consToAdd, posOfTphs.get());
System.out.println(consToAdd); // System.out.println(consToAdd);
} }
} }
@@ -231,16 +267,16 @@ public class FamilyOfGeneratedGenerics {
* {T <. Object | (T is a type variable in a type of a node of the method/constructor m in cl_\sigma), * {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)} * (\existsnot T': T <. T') \in cs)}
*/ */
public static List<MethodConstraint> hasNoSupertypeForMethodTypes(List<TPHConstraint> allConstraints, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { public static HashMap<MethodConstraint, String> hasNoSupertypeForMethodTypes(List<TPHConstraint> allConstraints, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
//TODO: //TODO:
List<MethodConstraint> tempMC= new ArrayList<>(); HashMap<MethodConstraint, String> tempMC= new HashMap<>();
for(String tph: posOfTphs.keySet()) { for(String tph: posOfTphs.keySet()) {
for(TPHConstraint allCons: allConstraints) { for(TPHConstraint allCons: allConstraints) {
if((posOfTphs.get(tph).fst.equals(PositionFinder.Position.METHOD) || posOfTphs.get(tph).fst.equals(PositionFinder.Position.CONSTRUCTOR)) && checkUpperBound(allConstraints,tph)) { if((posOfTphs.get(tph).fst.equals(PositionFinder.Position.METHOD) || posOfTphs.get(tph).fst.equals(PositionFinder.Position.CONSTRUCTOR)) && checkUpperBound(allConstraints,tph)) {
MethodConstraint consToAdd = new MethodConstraint(tph, "Object", Relation.EXTENDS); MethodConstraint consToAdd = new MethodConstraint(tph, "Object", Relation.EXTENDS);
if (!checkForDuplicates(consToAdd, tempMC)) { if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
tempMC.add(consToAdd); tempMC.put(consToAdd, posOfTphs.get(tph).snd);
System.out.println(consToAdd); // System.out.println(consToAdd);
} }
} }
} }
@@ -251,7 +287,7 @@ public class FamilyOfGeneratedGenerics {
/** /**
* nimm die Menge cs_cl aus cs_m raus * nimm die Menge cs_cl aus cs_m raus
*/ */
public static List<MethodConstraint> methodTypesWithoutClassTypes(List<ClassConstraint> cs_cl, List<MethodConstraint> cs_m) { public static HashMap<MethodConstraint, String> methodTypesWithoutClassTypes(List<ClassConstraint> cs_cl, HashMap<MethodConstraint, String> cs_m) {
//TODO: //TODO:
List<TPHConstraint> tempCC = new ArrayList<>(); List<TPHConstraint> tempCC = new ArrayList<>();
for(ClassConstraint cc: cs_cl) { for(ClassConstraint cc: cs_cl) {
@@ -259,7 +295,7 @@ public class FamilyOfGeneratedGenerics {
tempCC.add(tphC); tempCC.add(tphC);
} }
List<TPHConstraint> tempMC = new ArrayList<>(); List<TPHConstraint> tempMC = new ArrayList<>();
for(MethodConstraint mc: cs_m) { for(MethodConstraint mc: cs_m.keySet()) {
TPHConstraint tphC = new TPHConstraint(mc.getLeft(), mc.getRight(), mc.getRel()); TPHConstraint tphC = new TPHConstraint(mc.getLeft(), mc.getRight(), mc.getRel());
tempMC.add(tphC); tempMC.add(tphC);
} }
@@ -294,6 +330,20 @@ public class FamilyOfGeneratedGenerics {
return false; return false;
} }
public static boolean checkForDuplicatesInMethods(TPHConstraint constraint, HashMap<MethodConstraint, String> hashMap, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
HashMap<MethodConstraint, String> tempMap = hashMap;
boolean hasSame = false;
for (TPHConstraint tphC: tempMap.keySet()) {
hasSame = constraint.getLeft() == tphC.getLeft() &&
constraint.getRight() == tphC.getRight() &&
constraint.getRel() == tphC.getRel() &&
posOfTphs.get(); //constraint already in ArrayList if true
if (hasSame)
return true;
}
return false;
}
public static List<TPHConstraint> buildTransitiveClosure(List list) { public static List<TPHConstraint> buildTransitiveClosure(List list) {
List<TPHConstraint> iterList = new ArrayList<>(list); List<TPHConstraint> iterList = new ArrayList<>(list);
List<TPHConstraint> runList = new ArrayList<>(list); List<TPHConstraint> runList = new ArrayList<>(list);

View File

@@ -154,7 +154,7 @@ public class UnifyTypeFactory {
public static Constraint<UnifyPair> convert(Constraint<Pair> constraint){ public static Constraint<UnifyPair> convert(Constraint<Pair> constraint){
Constraint<UnifyPair> unifyPairConstraint = constraint.stream() Constraint<UnifyPair> unifyPairConstraint = constraint.stream()
.map(UnifyTypeFactory::convert) .map(UnifyTypeFactory::convert)
.collect(Collectors.toCollection( () -> new Constraint<UnifyPair>(convert(constraint.getExtendConstraint())))); .collect(Collectors.toCollection( () -> new Constraint<UnifyPair> (constraint.isInherited(), convert(constraint.getExtendConstraint()))));
return unifyPairConstraint; return unifyPairConstraint;
} }

View File

@@ -17,13 +17,15 @@ public class MethodAssumption extends Assumption{
private ClassOrInterface receiver; private ClassOrInterface receiver;
private RefTypeOrTPHOrWildcardOrGeneric retType; private RefTypeOrTPHOrWildcardOrGeneric retType;
List<? extends RefTypeOrTPHOrWildcardOrGeneric> params; List<? extends RefTypeOrTPHOrWildcardOrGeneric> params;
private final Boolean isInherited;
public MethodAssumption(ClassOrInterface receiver, RefTypeOrTPHOrWildcardOrGeneric retType, public MethodAssumption(ClassOrInterface receiver, RefTypeOrTPHOrWildcardOrGeneric retType,
List<? extends RefTypeOrTPHOrWildcardOrGeneric> params, TypeScope scope){ List<? extends RefTypeOrTPHOrWildcardOrGeneric> params, TypeScope scope, Boolean isInherited){
super(scope); super(scope);
this.receiver = receiver; this.receiver = receiver;
this.retType = retType; this.retType = retType;
this.params = params; this.params = params;
this.isInherited = isInherited;
} }
/* /*
@@ -70,4 +72,8 @@ public class MethodAssumption extends Assumption{
return receiverType; return receiverType;
} }
public Boolean isInherited() {
return isInherited;
}
} }

View File

@@ -6,17 +6,32 @@ import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
//TODO: Remove this class
public class Constraint<A> extends HashSet<A> { public class Constraint<A> extends HashSet<A> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private Boolean isInherited = false;//wird nur für die Method-Constraints benoetigt
private Constraint<A> extendConstraint = null; private Constraint<A> extendConstraint = null;
public Constraint(){} public Constraint() {
super();
}
public Constraint(Constraint<A> extendConstraint) { public Constraint(Boolean isInherited) {
this.isInherited = isInherited;
}
public Constraint(Boolean isInherited, Constraint<A> extendConstraint) {
this.isInherited = isInherited;
this.extendConstraint = extendConstraint; this.extendConstraint = extendConstraint;
} }
public void setIsInherited(Boolean isInherited) {
this.isInherited = isInherited;
}
public Boolean isInherited() {
return isInherited;
}
public Constraint<A> getExtendConstraint() { public Constraint<A> getExtendConstraint() {
return extendConstraint; return extendConstraint;
} }
@@ -26,7 +41,7 @@ public class Constraint<A> extends HashSet<A> {
} }
public String toString() { public String toString() {
return super.toString() return super.toString() + " isInherited = " + isInherited
//" + extendsContraint: " + (extendConstraint != null ? extendConstraint.toStringBase() : "null" ) //" + extendsContraint: " + (extendConstraint != null ? extendConstraint.toStringBase() : "null" )
+ "\n" ; + "\n" ;
} }

View File

@@ -68,8 +68,9 @@ public class ConstraintSet<A> {
Constraint<B> newConst = as.stream() Constraint<B> newConst = as.stream()
.map(o) .map(o)
.collect(Collectors.toCollection((as.getExtendConstraint() != null) .collect(Collectors.toCollection((as.getExtendConstraint() != null)
? () -> new Constraint<B> (as.getExtendConstraint().stream().map(o).collect(Collectors.toCollection(Constraint::new))) ? () -> new Constraint<B> (as.isInherited(),
: () -> new Constraint<B> () as.getExtendConstraint().stream().map(o).collect(Collectors.toCollection(Constraint::new)))
: () -> new Constraint<B> (as.isInherited())
)); ));
//CSA2CSB.put(as, newConst); //CSA2CSB.put(as, newConst);

View File

@@ -1,107 +0,0 @@
package de.dhbwstuttgart.typeinference.constraints;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class ConstraintSet2 {
Set<OderConstraint> oderConstraints = new HashSet<>();
public ConstraintSet2(Set<OderConstraint> constraints){
if(constraints.isEmpty())throw new RuntimeException("Empty constraint set");
this.oderConstraints = constraints;
}
@Override
public String toString(){
BinaryOperator<String> b = (x,y) -> x+y;
return "ODER:" + this.oderConstraints.stream().reduce("", (x,y) -> x.toString()+ "\n" +y, b);
}
private class ConstraintSpliterator implements Spliterator<Set<Pair>> {
private List<OderConstraint> constraints;
private long i = 0;
private long max = 0;
private List<Integer> sizes;
private List<Long> bases = new ArrayList<>();
ConstraintSpliterator(List<OderConstraint> constraints){
this.constraints = constraints;
sizes = constraints.stream().map(OderConstraint::getSize).collect(Collectors.toList());
long base = 1;
for(int size : sizes){
bases.add(base);
base *= size;
}
i = 0;
max = estimateSize() - 1;
}
ConstraintSpliterator(List<OderConstraint> constraints, long start, long end){
this(constraints);
i = start;
max = end;
}
@Override
public boolean tryAdvance(Consumer<? super Set<Pair>> consumer) {
if(i > max) return false;
consumer.accept(get(i));
i++;
return true;
}
private Set<Pair> get(long num){
Set<Pair> ret = new HashSet<>();
Iterator<Long> baseIt = bases.iterator();
for(OderConstraint constraint : constraints){
ret.addAll(constraint.get((int) ((num/baseIt.next())%constraint.getSize())));
}
return ret;
}
@Override
public Spliterator<Set<Pair>> trySplit() {
if(max - i < 2) return null;
long middle = i + ((max- i) / 2);
long maxOld = max;
max = middle - 1;
return new ConstraintSpliterator(constraints, middle, maxOld);
}
@Override
public long estimateSize() {
long ret = 1;
for (int size : sizes)ret*=size;
return ret;
}
@Override
public int characteristics() {
return ORDERED | SIZED | IMMUTABLE | NONNULL;
}
}
public Stream<Set<Pair>> cartesianProductParallel(){
return StreamSupport.stream(new ConstraintSpliterator(oderConstraints.stream().collect(Collectors.toList())), true);
}
/*
public Map<String, TypePlaceholder> generateTPHMap() {
HashMap<String, TypePlaceholder> ret = new HashMap<>();
constraints.map((Pair p) -> {
if (p.TA1 instanceof TypePlaceholder) {
ret.put(((TypePlaceholder) p.TA1).getName(), (TypePlaceholder) p.TA1);
}
if (p.TA2 instanceof TypePlaceholder) {
ret.put(((TypePlaceholder) p.TA2).getName(), (TypePlaceholder) p.TA2);
}
return null;
});
return ret;
}
*/
}

View File

@@ -1,26 +0,0 @@
package de.dhbwstuttgart.typeinference.constraints;
import java.util.HashSet;
import java.util.Set;
public class ConstraintSetBuilder {
private Set<Pair> undConstraints = new HashSet<>();
private Set<OderConstraint> oderConstraints = new HashSet<>();
private boolean done = false;
public void addUndConstraint(Pair p){
undConstraints.add(p);
}
public void addOderConstraint(OderConstraint orConstraint) {
oderConstraints.add(orConstraint);
}
public ConstraintSet2 build(){
if(done)throw new RuntimeException("Trying to build cartesian product twice");
this.done = true;
if(!undConstraints.isEmpty())
oderConstraints.add(new OderConstraint(Set.of(undConstraints)));
return new ConstraintSet2(oderConstraints);
}
}

View File

@@ -1,24 +0,0 @@
package de.dhbwstuttgart.typeinference.constraints;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class OderConstraint {
private final List<Set<Pair>> cons;
public OderConstraint(Set<Set<Pair>> orCons){
if(orCons.isEmpty())throw new RuntimeException("Empty constraint set");
for(Set<Pair> c : orCons){
if(c.isEmpty())throw new RuntimeException("Empty constraint set");
}
this.cons = orCons.stream().collect(Collectors.toList());
}
public int getSize(){
return cons.size();
}
public Set<Pair> get(int l) {
return cons.get(l);
}
}

View File

@@ -179,7 +179,7 @@ public class TYPEStmt implements StatementVisitor{
!(x.TA2 instanceof TypePlaceholder)) !(x.TA2 instanceof TypePlaceholder))
? new Pair(x.TA1, new ExtendsWildcardType(x.TA2, x.TA2.getOffset()), PairOperator.EQUALSDOT) ? new Pair(x.TA1, new ExtendsWildcardType(x.TA2, x.TA2.getOffset()), PairOperator.EQUALSDOT)
: x) : x)
.collect(Collectors.toCollection(() -> new Constraint<Pair>())); .collect(Collectors.toCollection(() -> new Constraint<Pair>(oneMethodConstraint.isInherited())));
oneMethodConstraint.setExtendConstraint(extendsOneMethodConstraint); oneMethodConstraint.setExtendConstraint(extendsOneMethodConstraint);
extendsOneMethodConstraint.setExtendConstraint(oneMethodConstraint); extendsOneMethodConstraint.setExtendConstraint(oneMethodConstraint);
methodConstraints.add(extendsOneMethodConstraint); methodConstraints.add(extendsOneMethodConstraint);
@@ -574,7 +574,7 @@ public class TYPEStmt implements StatementVisitor{
protected Constraint<Pair> generateConstraint(MethodCall forMethod, MethodAssumption assumption, protected Constraint<Pair> generateConstraint(MethodCall forMethod, MethodAssumption assumption,
TypeInferenceBlockInformation info, GenericsResolver resolver){ TypeInferenceBlockInformation info, GenericsResolver resolver){
Constraint<Pair> methodConstraint = new Constraint<>(); Constraint<Pair> methodConstraint = new Constraint<>(assumption.isInherited());
ClassOrInterface receiverCl = assumption.getReceiver(); ClassOrInterface receiverCl = assumption.getReceiver();
/* /*
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>(); List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
@@ -636,7 +636,7 @@ public class TYPEStmt implements StatementVisitor{
public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { public RefTypeOrTPHOrWildcardOrGeneric getReturnType() {
throw new NotImplementedException(); throw new NotImplementedException();
} }
})); }, false));
} }
for(ClassOrInterface cl : info.getAvailableClasses()){ for(ClassOrInterface cl : info.getAvailableClasses()){
for(Method m : cl.getMethods()){ for(Method m : cl.getMethods()){
@@ -645,7 +645,7 @@ public class TYPEStmt implements StatementVisitor{
RefTypeOrTPHOrWildcardOrGeneric retType = m.getReturnType();//info.checkGTV(m.getReturnType()); RefTypeOrTPHOrWildcardOrGeneric retType = m.getReturnType();//info.checkGTV(m.getReturnType());
ret.add(new MethodAssumption(cl, retType, convertParams(m.getParameterList(),info), ret.add(new MethodAssumption(cl, retType, convertParams(m.getParameterList(),info),
createTypeScope(cl, m))); createTypeScope(cl, m), m.isInherited));
} }
} }
} }
@@ -680,7 +680,7 @@ public class TYPEStmt implements StatementVisitor{
for(Method m : cl.getConstructors()){ for(Method m : cl.getConstructors()){
if(m.getParameterList().getFormalparalist().size() == argList.getArguments().size()){ if(m.getParameterList().getFormalparalist().size() == argList.getArguments().size()){
ret.add(new MethodAssumption(cl, ofType, convertParams(m.getParameterList(), ret.add(new MethodAssumption(cl, ofType, convertParams(m.getParameterList(),
info), createTypeScope(cl, m))); info), createTypeScope(cl, m), m.isInherited));
} }
} }
} }

View File

@@ -682,8 +682,9 @@ public class RuleSet implements IRuleSet{
Function<? super Constraint<UnifyPair>,? extends Constraint<UnifyPair>> applyUni = b -> b.stream().map( Function<? super Constraint<UnifyPair>,? extends Constraint<UnifyPair>> applyUni = b -> b.stream().map(
x -> uni.apply(pair,x)).collect(Collectors.toCollection((b.getExtendConstraint() != null) x -> uni.apply(pair,x)).collect(Collectors.toCollection((b.getExtendConstraint() != null)
? () -> new Constraint<UnifyPair>( ? () -> new Constraint<UnifyPair>(
b.isInherited(),
b.getExtendConstraint().stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(Constraint::new))) b.getExtendConstraint().stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(Constraint::new)))
: () -> new Constraint<UnifyPair>() : () -> new Constraint<UnifyPair>(b.isInherited())
)); ));
oderConstraints.replaceAll(oc -> oc.stream().map(applyUni).collect(Collectors.toCollection(HashSet::new))); oderConstraints.replaceAll(oc -> oc.stream().map(applyUni).collect(Collectors.toCollection(HashSet::new)));
/* /*

View File

@@ -1304,6 +1304,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
Set<UnifyPair> a_new = aParDefIt.next(); Set<UnifyPair> a_new = aParDefIt.next();
List<Set<UnifyPair>> smallerSetasList = oup.smallerThan(a_new, nextSetasList); List<Set<UnifyPair>> smallerSetasList = oup.smallerThan(a_new, nextSetasList);
List<Set<UnifyPair>> notInherited = smallerSetasList.stream() List<Set<UnifyPair>> notInherited = smallerSetasList.stream()
.filter(x -> !((Constraint<UnifyPair>)x).isInherited())
.collect(Collectors.toCollection(ArrayList::new)); .collect(Collectors.toCollection(ArrayList::new));
List<Set<UnifyPair>> notErased = new ArrayList<>(); List<Set<UnifyPair>> notErased = new ArrayList<>();
notInherited.stream().forEach(x -> { notErased.addAll(oup.smallerEqThan(x, smallerSetasList)); }); notInherited.stream().forEach(x -> { notErased.addAll(oup.smallerEqThan(x, smallerSetasList)); });
@@ -1345,10 +1346,11 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
List<Set<UnifyPair>> greaterSetasList = oup.greaterThan(a_new, nextSetasList); List<Set<UnifyPair>> greaterSetasList = oup.greaterThan(a_new, nextSetasList);
//a_new muss hingefuegt werden, wenn es nicht vererbt ist, dann wird es spaeter wieder geloescht //a_new muss hingefuegt werden, wenn es nicht vererbt ist, dann wird es spaeter wieder geloescht
if (!((Constraint<UnifyPair>)a_new).isInherited()) {
greaterSetasList.add(a_new); greaterSetasList.add(a_new);
}
List<Set<UnifyPair>> notInherited = greaterSetasList.stream() List<Set<UnifyPair>> notInherited = greaterSetasList.stream()
.filter(x -> !((Constraint<UnifyPair>)x).isInherited())
.collect(Collectors.toCollection(ArrayList::new)); .collect(Collectors.toCollection(ArrayList::new));
List<Set<UnifyPair>> notErased = new ArrayList<>(); List<Set<UnifyPair>> notErased = new ArrayList<>();
@@ -1399,6 +1401,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
writeLog("Removed: " + nextSetasListOderConstraints); writeLog("Removed: " + nextSetasListOderConstraints);
List<Set<UnifyPair>> smallerSetasList = oup.smallerThan(a, nextSetasList); List<Set<UnifyPair>> smallerSetasList = oup.smallerThan(a, nextSetasList);
List<Set<UnifyPair>> notInherited = smallerSetasList.stream() List<Set<UnifyPair>> notInherited = smallerSetasList.stream()
.filter(x -> !((Constraint<UnifyPair>)x).isInherited())
.collect(Collectors.toCollection(ArrayList::new)); .collect(Collectors.toCollection(ArrayList::new));
List<Set<UnifyPair>> notErased = new ArrayList<>(); List<Set<UnifyPair>> notErased = new ArrayList<>();
notInherited.stream().forEach(x -> { notErased.addAll(oup.smallerEqThan(x, smallerSetasList)); }); notInherited.stream().forEach(x -> { notErased.addAll(oup.smallerEqThan(x, smallerSetasList)); });

View File

@@ -114,7 +114,7 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
assertFalse(classConstraints.isEmpty()); assertFalse(classConstraints.isEmpty());
assertTrue(classConstraints.size() == 6); assertTrue(classConstraints.size() == 6);
assertFalse(methodConstraints.isEmpty()); assertFalse(methodConstraints.isEmpty());
assertTrue(methodConstraints.size() == 5); assertTrue(methodConstraints.size() == 2);
} }

View File

@@ -56,6 +56,9 @@ public class TestExample42 {
System.out.println("ClassConstraints: " + classConstraints); System.out.println("ClassConstraints: " + classConstraints);
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),classConstraints,fillPosOfTphs()); List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),classConstraints,fillPosOfTphs());
System.out.println("MethodConstraints: " + methodConstraints); System.out.println("MethodConstraints: " + methodConstraints);
for(MethodConstraint mC: methodConstraints) {
System.out.println(mC);
}
List<TPHConstraint> testCons; List<TPHConstraint> testCons;
} }

View File

@@ -1,62 +0,0 @@
package typeinference;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSetBuilder;
import de.dhbwstuttgart.typeinference.constraints.OderConstraint;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import org.junit.Test;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class ConstraintSetTests {
@Test
public void cartesianProductTestSingleConstraint(){
ConstraintSetBuilder builder = new ConstraintSetBuilder();
builder.addOderConstraint(new OderConstraint(Set.of(Set.of(generatePair()))));
List<Set<Pair>> result = builder.build().cartesianProductParallel().collect(Collectors.toList());
assert result.size() == 1;
}
@Test
public void cartesianProductTestSingleOderConstraint(){
ConstraintSetBuilder builder = new ConstraintSetBuilder();
builder.addOderConstraint(new OderConstraint(Set.of(Set.of(generatePair()), Set.of(generatePair()))));
List<Set<Pair>> result = builder.build().cartesianProductParallel().collect(Collectors.toList());
assert result.size() == 2;
}
@Test
public void cartesianProductTestTwoOderConstraint(){
ConstraintSetBuilder builder = new ConstraintSetBuilder();
builder.addOderConstraint(new OderConstraint(Set.of(Set.of(generatePair()), Set.of(generatePair()))));
builder.addOderConstraint(new OderConstraint(Set.of(Set.of(generatePair()), Set.of(generatePair()))));
List<Set<Pair>> result = builder.build().cartesianProductParallel().collect(Collectors.toList());
assert result.size() == 4;
}
@Test
public void cartesianProductTestThreeOderConstraint(){
ConstraintSetBuilder builder = new ConstraintSetBuilder();
builder.addOderConstraint(new OderConstraint(Set.of(Set.of(generatePair()), Set.of(generatePair()))));
builder.addOderConstraint(new OderConstraint(Set.of(Set.of(generatePair()), Set.of(generatePair()))));
builder.addOderConstraint(new OderConstraint(Set.of(Set.of(generatePair()))));
List<Set<Pair>> result = builder.build().cartesianProductParallel().collect(Collectors.toList());
assert result.size() == 4;
ConstraintSetBuilder builder2 = new ConstraintSetBuilder();
builder2.addOderConstraint(new OderConstraint(Set.of(Set.of(generatePair()), Set.of(generatePair()))));
builder2.addOderConstraint(new OderConstraint(Set.of(Set.of(generatePair()), Set.of(generatePair()))));
builder2.addOderConstraint(new OderConstraint(Set.of(Set.of(generatePair()), Set.of(generatePair()))));
List<Set<Pair>> result2 = builder2.build().cartesianProductParallel().collect(Collectors.toList());
assert result2.stream().map( a -> a.stream().map(p -> p.TA1.toString()).reduce("", (x, y)-> x+" "+y)).collect(Collectors.toSet()).size() == 8;
assert result2.size() == 8;
}
public Pair generatePair(){
return new Pair(TypePlaceholder.fresh(new NullToken()), TypePlaceholder.fresh(new NullToken()));
}
}