2
0

Compare commits

..

5 Commits

19 changed files with 2043 additions and 1073 deletions

@ -1,9 +1,9 @@
package de.dhbwstuttgart.bytecode.insertGenerics;
import com.ibm.icu.text.CurrencyMetaInfo;
import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import java.util.ArrayList;
@ -16,15 +16,12 @@ public class FamilyOfGeneratedGenerics {
public HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTPHs = new HashMap<>();
public List<ClassConstraint> classConstraints = new ArrayList<>();
public List<MethodConstraint> methodConstraints = new ArrayList<>();
public HashMap<MethodConstraint, String> methodsWithPosition = new HashMap<>();
public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) {
this.allConstraints = tphExtractor.allCons;
this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph);
this.classConstraints = getClassConstraints(allConstraints,posOfTPHs);
// this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs);
// this.methodConstraints =
}
public static List<ClassConstraint> getClassConstraints(List<TPHConstraint> cs, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { //Inputparameter List<TPHConstraint> constraintsSet weg
@ -50,72 +47,19 @@ public class FamilyOfGeneratedGenerics {
return cs_cl;
}
// 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) {
public static List<MethodConstraint> getMethodConstraints(List<TPHConstraint> cs, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
//TODO: Regeln
HashMap<MethodConstraint, String> cs_m = new HashMap<>();
HashMap<MethodConstraint, String> methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs);
for (MethodConstraint cons: methodConstraints1.keySet()) {
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)) {
List<MethodConstraint> cs_m = new ArrayList<>();
List<MethodConstraint> methodConstraints1 = typeOfTheMethodInClSigma(cs, cs_m, posOfTphs);
for (MethodConstraint cons: methodConstraints1) {
if (!checkForDuplicates(cons, cs_m)) {
cs_m.add(cons);
}
}
HashMap<MethodConstraint, String> methodConstraints3 = secondTransitiveSubtypeForMethodTypes(cs, cs_cl, cs_m);
for (MethodConstraint cons: methodConstraints3.keySet()) {
if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) {
cs_m.add(cons);
}
}
HashMap<MethodConstraint, String> methodConstraints4 = hasNoSupertypeForMethodTypes(cs, posOfTphs);
for (MethodConstraint cons: methodConstraints4.keySet()) {
if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) {
cs_m.add(cons);
}
}
HashMap<MethodConstraint, String> methodConstraints5 = methodTypesWithoutClassTypes(cs_cl, cs_m);
cs_m = methodConstraints5;
//List<MethodConstraint> methodConstraints2 = firstTransitiveSubtypeForMethodTypes();
return cs_m;
}
/**
* Def. FGG: erste Zeile von cs_cl
* {T < .T' | T is a type variable in a type of a node of a field}
@ -187,79 +131,83 @@ public class FamilyOfGeneratedGenerics {
return tempCC;
}
/**
public static boolean checkUpperBound(List<TPHConstraint> cs, String tph) {
for(int i=0; i<cs.size(); i++) {
if(cs.get(i).getLeft() == tph) {
return false;
}
}
return true;
}
/**
* 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 HashMap<MethodConstraint, String> typeOfTheMethodInClSigma(List<TPHConstraint> allConstraints, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { // cl_\sigma??
public static List<MethodConstraint> typeOfTheMethodInClSigma(List<TPHConstraint> allConstraints, List<MethodConstraint> cs_m, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { // cl_\sigma??
//TODO:
HashMap<MethodConstraint, String> tempMC= new HashMap<>();
List<MethodConstraint> 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).fst == PositionFinder.Position.METHOD || posOfTphs.get(tph).fst == PositionFinder.Position.CONSTRUCTOR)) {
MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel());
if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
tempMC.put(consToAdd, posOfTphs.get(tph).snd);
if (!checkForDuplicates(consToAdd, tempCC)) {
tempCC.add(consToAdd);
}
}
}
}
}
return tempMC;
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 HashMap<MethodConstraint, String> firstTransitiveSubtypeForMethodTypes(List<TPHConstraint> allConstraints, HashMap<MethodConstraint, String> cs_m, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { //transitive closure of cs
public static List<MethodConstraint> firstTransitiveSubtypeForMethodTypes(List<TPHConstraint> allConstraints, List<MethodConstraint> cs_m, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) { //transitive closure of cs
//TODO:
HashMap<MethodConstraint, String> tempMC= new HashMap<>();
List<MethodConstraint> tempCC= new ArrayList<>();
List<TPHConstraint> tcOfCs = buildTransitiveClosure(allConstraints);
for(MethodConstraint mC1 : cs_m.keySet()) { //(R <. R')
for(MethodConstraint mC2 : cs_m.keySet()) { //(S <. S')
String lSide = mC1.getRight(); //R'
String rSide = mC2.getLeft(); //S
for(MethodConstraint mC1 : cs_m) {
for(MethodConstraint mC2 : cs_m) {
String rightSide = mC1.getRight();
String leftSide = mC2.getLeft();
for(TPHConstraint tphC : tcOfCs) {
if(tphC.getLeft().equals(lSide)&&tphC.getRight().equals(rSide)) { //is it (R',S)
MethodConstraint consToAdd = new MethodConstraint(lSide, rSide, tphC.getRel()); //create (R'<.S)
if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
tempMC.put(consToAdd, posOfTphs.get());
}
if(tphC.getLeft().equals(leftSide)&&tphC.getRight().equals(rightSide)) {
tempCC.add((MethodConstraint) tphC);
}
}
}
}
return tempMC;
return tempCC;
}
/**
/**
* 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 HashMap<MethodConstraint, String> secondTransitiveSubtypeForMethodTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl, HashMap<MethodConstraint, String> cs_m, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
public static List<MethodConstraint> secondTransitiveSubtypeForMethodTypes(List<TPHConstraint> allConstraints, List<ClassConstraint> cs_cl, List<MethodConstraint> cs_m, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
//TODO:
HashMap<MethodConstraint, String> tempMC= new HashMap<>();
List<MethodConstraint> tempCC= new ArrayList<>();
List<TPHConstraint> tcOfCs = buildTransitiveClosure(allConstraints);
for(ClassConstraint cC : cs_cl) {
for(MethodConstraint mC : cs_m.keySet()) {
String leftSide = mC.getRight();
String rightSide = cC.getLeft();
for(MethodConstraint mC : cs_m) {
String rightSide = mC.getRight();
String leftSide = cC.getLeft();
for(TPHConstraint tphC : tcOfCs) {
if(tphC.getLeft().equals(leftSide)&&tphC.getRight().equals(rightSide)) {
MethodConstraint consToAdd = new MethodConstraint(tphC.getLeft(), tphC.getRight(), tphC.getRel());
if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
tempMC.put(consToAdd, posOfTphs.get());
// System.out.println(consToAdd);
}
tempCC.add((MethodConstraint) tphC);
}
}
}
}
return tempMC;
return tempCC;
}
/**
@ -267,54 +215,18 @@ public class FamilyOfGeneratedGenerics {
* {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 HashMap<MethodConstraint, String> hasNoSupertypeForMethodTypes(List<TPHConstraint> allConstraints, HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs) {
public static List<MethodConstraint> hasNoSupertypeForMethodTypes() {
//TODO:
HashMap<MethodConstraint, String> tempMC= new HashMap<>();
for(String tph: posOfTphs.keySet()) {
for(TPHConstraint allCons: allConstraints) {
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);
if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) {
tempMC.put(consToAdd, posOfTphs.get(tph).snd);
// System.out.println(consToAdd);
}
}
}
}
return tempMC;
return null;
}
/**
* nimm die Menge cs_cl aus cs_m raus
*/
public static HashMap<MethodConstraint, String> methodTypesWithoutClassTypes(List<ClassConstraint> cs_cl, HashMap<MethodConstraint, String> cs_m) {
public static List<MethodConstraint> methodTypesWithoutClassTypes() {
//TODO:
List<TPHConstraint> tempCC = new ArrayList<>();
for(ClassConstraint cc: cs_cl) {
TPHConstraint tphC = new TPHConstraint(cc.getLeft(), cc.getRight(), cc.getRel());
tempCC.add(tphC);
}
List<TPHConstraint> tempMC = new ArrayList<>();
for(MethodConstraint mc: cs_m.keySet()) {
TPHConstraint tphC = new TPHConstraint(mc.getLeft(), mc.getRight(), mc.getRel());
tempMC.add(tphC);
}
List<TPHConstraint> tempMC2 = new ArrayList<>();
tempMC2.addAll(tempMC);
List<MethodConstraint> tempMCToReturn = new ArrayList<>();
for(TPHConstraint cc: tempCC) {
for(TPHConstraint mc: tempMC) {
if(cc.getLeft().equals(mc.getLeft()) && cc.getRight().equals(mc.getRight())) {
tempMC2.remove(mc);
}
}
}
for(TPHConstraint tphC: tempMC2) {
MethodConstraint mCons = new MethodConstraint(tphC.getLeft(), tphC.getRight(), tphC.getRel());
tempMCToReturn.add(mCons);
}
return tempMCToReturn;
return null;
}
public static boolean checkForDuplicates(TPHConstraint constraint, List list) {
@ -330,20 +242,6 @@ public class FamilyOfGeneratedGenerics {
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) {
List<TPHConstraint> iterList = new ArrayList<>(list);
List<TPHConstraint> runList = new ArrayList<>(list);
@ -366,15 +264,6 @@ public class FamilyOfGeneratedGenerics {
return tcList;
}
public static boolean checkUpperBound(List<TPHConstraint> cs, String tph) {
for(int i=0; i<cs.size(); i++) {
if(cs.get(i).getLeft() == tph) {
return false;
}
}
return true;
}
public static HashMap<String, PairTphMethod<PositionFinder.Position, String>> positionConverter(HashMap<String, Boolean> allTphs, List<MethodAndTPH> listOfMethodsAndTphs) {
HashMap<String, PairTphMethod<PositionFinder.Position, String>> convertedPositions = new HashMap<>();

@ -20,12 +20,10 @@ import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.TypeScope;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
@ -48,26 +46,16 @@ import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask;
import de.dhbwstuttgart.typeinference.unify.UnifyResultListener;
import de.dhbwstuttgart.typeinference.unify.UnifyResultListenerImpl;
import de.dhbwstuttgart.typeinference.unify.UnifyResultModel;
import de.dhbwstuttgart.typeinference.unify.UnifyTaskModel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.antlr.v4.runtime.Token;
import org.apache.commons.io.output.NullOutputStream;
public class JavaTXCompiler {
@ -76,7 +64,6 @@ public class JavaTXCompiler {
Boolean resultmodel = true;
public final Map<File, SourceFile> sourceFiles = new HashMap<>();
Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"src/test/java/logFiles" geschrieben werden soll?
public volatile UnifyTaskModel usedTasks = new UnifyTaskModel();
private final ClassLoader classLoader;
public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException {
@ -194,7 +181,7 @@ public class JavaTXCompiler {
public List<ClassOrInterface> getAvailableClasses(SourceFile forSourceFile) throws ClassNotFoundException {
//PL 2018-09-18: List durch Set ersetzt, damit die Klassen nur einmal hinzugefuegt werden
//PL 2018-09-18: List durch Set ersetzt, damit die Klassen nur einmal hinzugefuegt werden
//List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
Set<ClassOrInterface> allClasses = new HashSet<>();
@ -229,321 +216,6 @@ public class JavaTXCompiler {
return new ArrayList<>(allClasses);
}
/*
* public List<ResultSet> typeInferenceOld() throws ClassNotFoundException {
* List<ClassOrInterface> allClasses = new
* ArrayList<>();//environment.getAllAvailableClasses(); //Alle Importierten
* Klassen in allen geparsten Sourcefiles kommen ins FC for(SourceFile sf :
* this.sourceFiles.values()) { allClasses.addAll(getAvailableClasses(sf));
* allClasses.addAll(sf.getClasses()); }
*
* final ConstraintSet<Pair> cons = getConstraints();
*
* FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses);
* System.out.println(finiteClosure); ConstraintSet<UnifyPair> unifyCons =
* UnifyTypeFactory.convert(cons);
*
* TypeUnify unify = new TypeUnify(); Set<Set<UnifyPair>> results = new
* HashSet<>(); try { File logPath = new
* File(System.getProperty("user.dir")+"/target/logFiles/"); logPath.mkdirs();
* FileWriter logFile = new FileWriter(new File(logPath, "log"));
* logFile.write("FC:\\" + finiteClosure.toString()+"\n"); for(SourceFile sf :
* this.sourceFiles.values()) { logFile.write(ASTTypePrinter.print(sf)); }
* logFile.flush(); Set<List<Constraint<UnifyPair>>> cardProd =
* unifyCons.cartesianProduct(); for (List<Constraint<UnifyPair>> xCons :
* cardProd ){ Set<UnifyPair> xConsSet = new HashSet<>(); for
* (Constraint<UnifyPair> constraint : xCons) { xConsSet.addAll(constraint); }
* //.collect(Collectors.toCollection(ArrayList::new))))
* System.out.println(xConsSet); Set<String> methodParaTypeVarNames =
* allClasses.stream().map(x -> x.getMethods().stream().map(y ->
* y.getParameterList().getFormalparalist() .stream().filter(z -> z.getType()
* instanceof TypePlaceholder) .map(z ->
* ((TypePlaceholder)z.getType()).getName()).collect(Collectors.toCollection(
* HashSet::new))) .reduce(new HashSet<String>(), (a,b) -> { a.addAll(b); return
* a;}, (a,b) -> { a.addAll(b); return a;} ) ) .reduce(new HashSet<String>(),
* (a,b) -> { a.addAll(b); return a;} );
*
* Set<String> constructorParaTypeVarNames = allClasses.stream().map(x ->
* x.getConstructors().stream().map(y ->
* y.getParameterList().getFormalparalist() .stream().filter(z -> z.getType()
* instanceof TypePlaceholder) .map(z ->
* ((TypePlaceholder)z.getType()).getName()).collect(Collectors.toCollection(
* HashSet::new))) .reduce(new HashSet<String>(), (a,b) -> { a.addAll(b); return
* a;}, (a,b) -> { a.addAll(b); return a;} ) ) .reduce(new HashSet<String>(),
* (a,b) -> { a.addAll(b); return a;} );
*
* Set<String> paraTypeVarNames = methodParaTypeVarNames;
* paraTypeVarNames.addAll(constructorParaTypeVarNames);
*
* Set<String> returnTypeVarNames = allClasses.stream().map(x ->
* x.getMethods().stream().filter(y -> y.getReturnType() instanceof
* TypePlaceholder) .map(z ->
* ((TypePlaceholder)z.getReturnType()).getName()).collect(Collectors.
* toCollection(HashSet::new))).reduce((a,b) -> { a.addAll(b); return a;}
* ).get();
*
* Set<String> fieldTypeVarNames = allClasses.stream().map(x ->
* x.getFieldDecl().stream().filter(y -> y.getReturnType() instanceof
* TypePlaceholder) .map(z ->
* ((TypePlaceholder)z.getReturnType()).getName()).collect(Collectors.
* toCollection(HashSet::new))).reduce((a,b) -> { a.addAll(b); return a;}
* ).get();
*
* returnTypeVarNames.addAll(fieldTypeVarNames);
*
* xConsSet = xConsSet.stream().map(x -> { //Hier muss ueberlegt werden, ob //1.
* alle Argument- und Retuntyp-Variablen in allen UnifyPairs // mit
* disableWildcardtable() werden. //2. alle Typvariablen mit Argument- oder
* Retuntyp-Variablen //in Beziehung auch auf disableWildcardtable() gesetzt
* werden muessen //PL 2018-04-23 if ((x.getLhsType() instanceof
* PlaceholderType)) { if (paraTypeVarNames.contains(x.getLhsType().getName()))
* { ((PlaceholderType)x.getLhsType()).setVariance((byte)1);
* ((PlaceholderType)x.getLhsType()).disableWildcardtable(); } if
* (returnTypeVarNames.contains(x.getLhsType().getName())) {
* ((PlaceholderType)x.getLhsType()).setVariance((byte)-1);
* ((PlaceholderType)x.getLhsType()).disableWildcardtable(); } } if
* ((x.getRhsType() instanceof PlaceholderType)) { if
* (paraTypeVarNames.contains(x.getRhsType().getName())) {
* ((PlaceholderType)x.getRhsType()).setVariance((byte)1);
* ((PlaceholderType)x.getRhsType()).disableWildcardtable(); } if
* (returnTypeVarNames.contains(x.getRhsType().getName())) {
* ((PlaceholderType)x.getRhsType()).setVariance((byte)-1);
* ((PlaceholderType)x.getRhsType()).disableWildcardtable(); } } return x;//HIER
* DIE JEWEILS RECHT BZW. LINKE SEITE AUF GLEICHE VARIANZ SETZEN WIE DIE JEWEILS
* ANDERE SEITE }).map( y -> { if ((y.getLhsType() instanceof PlaceholderType)
* && (y.getRhsType() instanceof PlaceholderType)) { if
* (((PlaceholderType)y.getLhsType()).getVariance() != 0 &&
* ((PlaceholderType)y.getRhsType()).getVariance() == 0) {
* ((PlaceholderType)y.getRhsType()).setVariance(((PlaceholderType)y.getLhsType(
* )).getVariance()); } if (((PlaceholderType)y.getLhsType()).getVariance() == 0
* && ((PlaceholderType)y.getRhsType()).getVariance() != 0) {
* ((PlaceholderType)y.getLhsType()).setVariance(((PlaceholderType)y.getRhsType(
* )).getVariance()); } } return y; } )
* .collect(Collectors.toCollection(HashSet::new));
* varianceInheritance(xConsSet); Set<Set<UnifyPair>> result =
* unify.unifySequential(xConsSet, finiteClosure, logFile, log);
* //Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
* System.out.println("RESULT: " + result); logFile.write("RES: " +
* result.toString()+"\n"); logFile.flush(); results.addAll(result); }
*
* results = results.stream().map(x -> { Optional<Set<UnifyPair>> res = new
* RuleSet().subst(x.stream().map(y -> { if (y.getPairOp() ==
* PairOperator.SMALLERDOTWC) y.setPairOp(PairOperator.EQUALSDOT); return y;
* //alle Paare a <.? b erden durch a =. b ersetzt
* }).collect(Collectors.toCollection(HashSet::new))); if (res.isPresent())
* {//wenn subst ein Erg liefert wurde was veraendert return new
* TypeUnifyTask().applyTypeUnificationRules(res.get(), finiteClosure); } else
* return x; //wenn nichts veraendert wurde wird x zurueckgegeben
* }).collect(Collectors.toCollection(HashSet::new));
* System.out.println("RESULT Final: " + results); logFile.write("RES_FINAL: " +
* results.toString()+"\n"); logFile.flush(); logFile.write("PLACEHOLDERS: " +
* PlaceholderType.EXISTING_PLACEHOLDERS); logFile.flush(); } catch (IOException
* e) { e.printStackTrace(); } return results.stream().map((unifyPairs -> new
* ResultSet(UnifyTypeFactory.convert(unifyPairs,
* generateTPHMap(cons))))).collect(Collectors.toList()); }
*/
/**
* Vererbt alle Variancen bei Paaren (a <. theta) oder (Theta <. a) wenn a eine
* Variance !=0 hat auf alle Typvariablen in Theta.
*
*
*/
/*
* private void varianceInheritance(Set<UnifyPair> eq) { Set<PlaceholderType>
* usedTPH = new HashSet<>(); Set<PlaceholderType> phSet = eq.stream().map(x ->
* { Set<PlaceholderType> pair = new HashSet<>(); if (x.getLhsType() instanceof
* PlaceholderType) pair.add((PlaceholderType)x.getLhsType()); if
* (x.getRhsType() instanceof PlaceholderType)
* pair.add((PlaceholderType)x.getRhsType()); return pair; }).reduce(new
* HashSet<>(), (a,b) -> { a.addAll(b); return a;} , (c,d) -> { c.addAll(d);
* return c;});
*
* ArrayList<PlaceholderType> phSetVariance = new ArrayList<>(phSet);
* phSetVariance.removeIf(x -> (x.getVariance() == 0));
* while(!phSetVariance.isEmpty()) { PlaceholderType a =
* phSetVariance.remove(0); usedTPH.add(a); //HashMap<PlaceholderType,Integer>
* ht = new HashMap<>(); //ht.put(a, a.getVariance()); Set<UnifyPair> eq1 = new
* HashSet<>(eq); eq1.removeIf(x -> !(x.getLhsType() instanceof PlaceholderType
* && ((PlaceholderType)x.getLhsType()).equals(a))); eq1.stream().forEach(x -> {
* x.getRhsType().accept(new distributeVariance(), a.getVariance());}); eq1 =
* new HashSet<>(eq); eq1.removeIf(x -> !(x.getRhsType() instanceof
* PlaceholderType && ((PlaceholderType)x.getRhsType()).equals(a)));
* eq1.stream().forEach(x -> { x.getLhsType().accept(new distributeVariance(),
* a.getVariance());}); phSetVariance = new ArrayList<>(phSet);
* phSetVariance.removeIf(x -> (x.getVariance() == 0 || usedTPH.contains(x))); }
* }
*/
public UnifyResultModel typeInferenceAsync(UnifyResultListener resultListener, Writer logFile)
throws ClassNotFoundException, IOException {
List<ClassOrInterface> allClasses = new ArrayList<>();// environment.getAllAvailableClasses();
// Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC
for (File f : this.sourceFiles.keySet()) {
SourceFile sf = sourceFiles.get(f);
allClasses.addAll(getAvailableClasses(sf));
allClasses.addAll(sf.getClasses());
allClasses.addAll(CompilationEnvironment.loadDefaultPackageClasses(f,classLoader).stream().map(ASTFactory::createClass).collect(Collectors.toList()));
}
final ConstraintSet<Pair> cons = getConstraints();
Set<Set<UnifyPair>> results = new HashSet<>();
UnifyResultModel urm = null;
// urm.addUnifyResultListener(resultListener);
try {
logFile = logFile == null
? new FileWriter(new File("log_" + sourceFiles.keySet().iterator().next().getName()))
: logFile;
IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile, classLoader);
System.out.println(finiteClosure);
urm = new UnifyResultModel(cons, finiteClosure);
urm.addUnifyResultListener(resultListener);
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
Function<UnifyPair, UnifyPair> distributeInnerVars = x -> {
UnifyType lhs, rhs;
if (((lhs = x.getLhsType()) instanceof PlaceholderType)
&& ((rhs = x.getRhsType()) instanceof PlaceholderType)
&& (((PlaceholderType) lhs).isInnerType() || ((PlaceholderType) rhs).isInnerType())) {
((PlaceholderType) lhs).setInnerType(true);
((PlaceholderType) rhs).setInnerType(true);
}
return x;
};
logFile.write(unifyCons.toString());
unifyCons = unifyCons.map(distributeInnerVars);
logFile.write(unifyCons.toString());
TypeUnify unify = new TypeUnify();
// Set<Set<UnifyPair>> results = new HashSet<>(); Nach vorne gezogen
logFile.write("FC:\\" + finiteClosure.toString() + "\n");
for (SourceFile sf : this.sourceFiles.values()) {
logFile.write(ASTTypePrinter.print(sf));
}
logFile.flush();
Set<String> methodParaTypeVarNames = allClasses.stream().map(x -> x.getMethods().stream()
.map(y -> y.getParameterList().getFormalparalist().stream()
.filter(z -> z.getType() instanceof TypePlaceholder)
.map(z -> ((TypePlaceholder) z.getType()).getName())
.collect(Collectors.toCollection(HashSet::new)))
.reduce(new HashSet<String>(), (a, b) -> {
a.addAll(b);
return a;
}, (a, b) -> {
a.addAll(b);
return a;
})).reduce(new HashSet<String>(), (a, b) -> {
a.addAll(b);
return a;
});
Set<String> constructorParaTypeVarNames = allClasses.stream().map(x -> x.getConstructors().stream()
.map(y -> y.getParameterList().getFormalparalist().stream()
.filter(z -> z.getType() instanceof TypePlaceholder)
.map(z -> ((TypePlaceholder) z.getType()).getName())
.collect(Collectors.toCollection(HashSet::new)))
.reduce(new HashSet<String>(), (a, b) -> {
a.addAll(b);
return a;
}, (a, b) -> {
a.addAll(b);
return a;
})).reduce(new HashSet<String>(), (a, b) -> {
a.addAll(b);
return a;
});
Set<String> paraTypeVarNames = methodParaTypeVarNames;
paraTypeVarNames.addAll(constructorParaTypeVarNames);
Set<String> returnTypeVarNames = allClasses.stream()
.map(x -> x.getMethods().stream().filter(y -> y.getReturnType() instanceof TypePlaceholder)
.map(z -> ((TypePlaceholder) z.getReturnType()).getName())
.collect(Collectors.toCollection(HashSet::new)))
.reduce((a, b) -> {
a.addAll(b);
return a;
}).get();
Set<String> fieldTypeVarNames = allClasses.stream()
.map(x -> x.getFieldDecl().stream().filter(y -> y.getReturnType() instanceof TypePlaceholder)
.map(z -> ((TypePlaceholder) z.getReturnType()).getName())
.collect(Collectors.toCollection(HashSet::new)))
.reduce((a, b) -> {
a.addAll(b);
return a;
}).get();
returnTypeVarNames.addAll(fieldTypeVarNames);
unifyCons = unifyCons.map(x -> {
// Hier muss ueberlegt werden, ob
// 1. alle Argument- und Retuntyp-Variablen in allen UnifyPairs
// mit disableWildcardtable() werden.
// 2. alle Typvariablen mit Argument- oder Retuntyp-Variablen
// in Beziehung auch auf disableWildcardtable() gesetzt werden muessen
// PL 2018-04-23
if ((x.getLhsType() instanceof PlaceholderType)) {
if (paraTypeVarNames.contains(x.getLhsType().getName())) {
((PlaceholderType) x.getLhsType()).setVariance((byte) 1);
((PlaceholderType) x.getLhsType()).disableWildcardtable();
}
if (returnTypeVarNames.contains(x.getLhsType().getName())) {
((PlaceholderType) x.getLhsType()).setVariance((byte) -1);
((PlaceholderType) x.getLhsType()).disableWildcardtable();
}
}
if ((x.getRhsType() instanceof PlaceholderType)) {
if (paraTypeVarNames.contains(x.getRhsType().getName())) {
((PlaceholderType) x.getRhsType()).setVariance((byte) 1);
((PlaceholderType) x.getRhsType()).disableWildcardtable();
}
if (returnTypeVarNames.contains(x.getRhsType().getName())) {
((PlaceholderType) x.getRhsType()).setVariance((byte) -1);
((PlaceholderType) x.getRhsType()).disableWildcardtable();
}
}
return x;// HIER DIE JEWEILS RECHT BZW. LINKE SEITE AUF GLEICHE VARIANZ SETZEN WIE DIE
// JEWEILS ANDERE SEITE
});
Set<PlaceholderType> varianceTPHold;
Set<PlaceholderType> varianceTPH = new HashSet<>();
varianceTPH = varianceInheritanceConstraintSet(unifyCons);
/*
* PL 2018-11-07 wird in varianceInheritanceConstraintSet erledigt do { //PL
* 2018-11-05 Huellenbildung Variance auf alle TPHs der Terme auf der jeweiligen
* //anderen Seite übertragen varianceTPHold = new HashSet<>(varianceTPH);
* varianceTPH = varianceInheritanceConstraintSet(unifyCons); unifyCons.map( y
* -> { if ((y.getLhsType() instanceof PlaceholderType) && (y.getRhsType()
* instanceof PlaceholderType)) { if
* (((PlaceholderType)y.getLhsType()).getVariance() != 0 &&
* ((PlaceholderType)y.getRhsType()).getVariance() == 0) {
* ((PlaceholderType)y.getRhsType()).setVariance(((PlaceholderType)y.getLhsType(
* )).getVariance()); } if (((PlaceholderType)y.getLhsType()).getVariance() == 0
* && ((PlaceholderType)y.getRhsType()).getVariance() != 0) {
* ((PlaceholderType)y.getLhsType()).setVariance(((PlaceholderType)y.getRhsType(
* )).getVariance()); } } return y; } ); } while
* (!varianceTPHold.equals(varianceTPH));
*/
// Set<Set<UnifyPair>> result = unify.unifySequential(xConsSet, finiteClosure,
// logFile, log);
// Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
List<Set<Constraint<UnifyPair>>> oderConstraints = unifyCons.getOderConstraints()/*.stream().map(x -> {
Set<Set<UnifyPair>> ret = new HashSet<>();
for (Constraint<UnifyPair> y : x) {
ret.add(new HashSet<>(y));
}
return ret;
}).collect(Collectors.toCollection(ArrayList::new))*/;
unify.unifyAsync(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, urm,
usedTasks);
} catch (IOException e) {
System.err.println("kein LogFile");
}
return urm;
}
public List<ResultSet> typeInference() throws ClassNotFoundException, IOException {
List<ClassOrInterface> allClasses = new ArrayList<>();// environment.getAllAvailableClasses();
@ -692,46 +364,12 @@ public class JavaTXCompiler {
}
})));
*/
System.out.println("Unify nach Oder-Constraints-Anpassung:" + unifyCons.toString());
Set<PlaceholderType> varianceTPHold;
Set<PlaceholderType> varianceTPH = new HashSet<>();
varianceTPH = varianceInheritanceConstraintSet(unifyCons);
/*
* PL 2018-11-07 wird in varianceInheritanceConstraintSet erledigt do { //PL
* 2018-11-05 Huellenbildung Variance auf alle TPHs der Terme auf der jeweiligen
* //anderen Seite übertragen varianceTPHold = new HashSet<>(varianceTPH);
* varianceTPH = varianceInheritanceConstraintSet(unifyCons); unifyCons.map( y
* -> { if ((y.getLhsType() instanceof PlaceholderType) && (y.getRhsType()
* instanceof PlaceholderType)) { if
* (((PlaceholderType)y.getLhsType()).getVariance() != 0 &&
* ((PlaceholderType)y.getRhsType()).getVariance() == 0) {
* ((PlaceholderType)y.getRhsType()).setVariance(((PlaceholderType)y.getLhsType(
* )).getVariance()); } if (((PlaceholderType)y.getLhsType()).getVariance() == 0
* && ((PlaceholderType)y.getRhsType()).getVariance() != 0) {
* ((PlaceholderType)y.getLhsType()).setVariance(((PlaceholderType)y.getRhsType(
* )).getVariance()); } } return y; } ); } while
* (!varianceTPHold.equals(varianceTPH));
*/
// Set<Set<UnifyPair>> result = unify.unifySequential(xConsSet, finiteClosure,
// logFile, log);
// Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
List<Set<Constraint<UnifyPair>>> oderConstraints = unifyCons.getOderConstraints()//.stream().map(x -> {
/*Set<Set<UnifyPair>> ret = new HashSet<>();
for (Constraint<UnifyPair> y : x) {
ret.add(new HashSet<>(y));
}
return ret;
}).collect(Collectors.toCollection(ArrayList::new))*/;
List<Set<Constraint<UnifyPair>>> oderConstraints = unifyCons.getOderConstraints();
if (resultmodel) {
/* UnifyResultModel Anfang */
UnifyResultModel urm = new UnifyResultModel(cons, finiteClosure);
UnifyResultListenerImpl li = new UnifyResultListenerImpl();
urm.addUnifyResultListener(li);
unify.unifyParallel(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, urm,
usedTasks);
unify.unifyParallel(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, urm);
System.out.println("RESULT Final: " + li.getResults());
logFile.write("RES_FINAL: " + li.getResults().toString() + "\n");
logFile.flush();
@ -743,7 +381,7 @@ public class JavaTXCompiler {
// oderConstraints, finiteClosure, logFile, log, new UnifyResultModel(cons,
// finiteClosure));
Set<Set<UnifyPair>> result = unify.unifyOderConstraints(unifyCons.getUndConstraints(), oderConstraints,
finiteClosure, logFile, log, new UnifyResultModel(cons, finiteClosure), usedTasks);
finiteClosure, logFile, log, new UnifyResultModel(cons, finiteClosure));
System.out.println("RESULT: " + result);
logFile.write("RES: " + result.toString() + "\n");
logFile.flush();
@ -894,6 +532,10 @@ public class JavaTXCompiler {
/**
* @param path - can be null, then class file output is in the same directory as the parsed source files
*/
List<ResultSet> typeinferenceResult = this.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = getGeneratedGenericResultsForAllSourceFiles(
typeinferenceResult);
generateBytecode(path, typeinferenceResult, simplifyResultsForAllSourceFiles);
public void generateBytecode(File path) throws ClassNotFoundException, IOException, BytecodeGeneratorError {
List<ResultSet> typeinferenceResult = this.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = getGeneratedGenericResultsForAllSourceFiles(

@ -28,8 +28,8 @@ public class TypeUnify {
* @param cons
* @return
*/
public Set<Set<UnifyPair>> unify(Set<UnifyPair> undConstrains, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) {
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks);
public Set<Set<UnifyPair>> unify(Set<UnifyPair> undConstrains, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret) {
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret);
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(unifyTask);
Set<Set<UnifyPair>> res = unifyTask.join();
@ -54,8 +54,8 @@ public class TypeUnify {
* @param ret
* @return
*/
public UnifyResultModel unifyAsync(Set<UnifyPair> undConstrains, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) {
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks);
public UnifyResultModel unifyAsync(Set<UnifyPair> undConstrains, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret) {
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret);
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(unifyTask);
return ret;
@ -68,12 +68,11 @@ public class TypeUnify {
* @param fc
* @param logFile
* @param log
* @param cons
* @param ret
* @return
*/
public UnifyResultModel unifyParallel(Set<UnifyPair> undConstrains, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) {
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks);
public UnifyResultModel unifyParallel(Set<UnifyPair> undConstrains, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret) {
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret);
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(unifyTask);
Set<Set<UnifyPair>> res = unifyTask.join();
@ -102,11 +101,10 @@ public class TypeUnify {
* @param fc
* @param logFile
* @param log
* @param cons
* @return
*/
public Set<Set<UnifyPair>> unifyOderConstraints(Set<UnifyPair> undConstrains, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) {
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, logFile, log, 0, ret, usedTasks);
public Set<Set<UnifyPair>> unifyOderConstraints(Set<UnifyPair> undConstrains, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret) {
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, logFile, log, 0, ret);
Set<Set<UnifyPair>> res = unifyTask.compute();
try {
logFile.write("\nnoShortendElements: " + unifyTask.noShortendElements +"\n");

@ -0,0 +1,30 @@
package de.dhbwstuttgart.typeinference.unify;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import java.io.Writer;
import java.util.List;
import java.util.Set;
public class TypeUnify2Future {
Set<Set<UnifyPair>> setToFlatten;
public TypeUnify2Future(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, Set<UnifyPair> nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm) {
super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm);
this.setToFlatten = setToFlatten;
this.nextSetElement = nextSetElement;
}
Set<UnifyPair> getNextSetElement() {
return nextSetElement;
}
@Override
protected Set<Set<UnifyPair>> compute() {
Set<Set<UnifyPair>> res = unify2(setToFlatten, eq, oderConstraintsField, fc, parallel, rekTiefeField, true);
return res;
}
}

@ -18,8 +18,8 @@ public class TypeUnify2Task extends TypeUnifyTask {
Set<Set<UnifyPair>> setToFlatten;
public TypeUnify2Task(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, Set<UnifyPair> nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks) {
super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm, usedTasks);
public TypeUnify2Task(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, Set<UnifyPair> nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm) {
super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm);
this.setToFlatten = setToFlatten;
this.nextSetElement = nextSetElement;
}
@ -30,35 +30,7 @@ public class TypeUnify2Task extends TypeUnifyTask {
@Override
protected Set<Set<UnifyPair>> compute() {
if (one) {
System.out.println("two");
}
one = true;
Set<Set<UnifyPair>> res = unify2(setToFlatten, eq, oderConstraintsField, fc, parallel, rekTiefeField, true);
/*if (isUndefinedPairSetSet(res)) {
return new HashSet<>(); }
else
*/
//writeLog("xxx");
//noOfThread--;
synchronized (usedTasks) {
if (this.myIsCancelled()) {
return new HashSet<>();
}
else {
return res;
}
}
return res;
}
public void closeLogFile() {
try {
logFile.close();
}
catch (IOException ioE) {
System.err.println("no log-File" + thNo);
}
}
}

File diff suppressed because it is too large Load Diff

@ -60,7 +60,33 @@ import com.google.common.collect.Ordering;
* Implementation of the type unification algorithm
* @author Florian Steurer
*/
public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
public class TypeUnifyTask {
public static Set<Set<UnifyPair>> typeUnifyTask(Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm){
Set<UnifyPair> neweq = new HashSet<>(eq);
/* 1-elementige Oder-Constraints werden in und-Constraints umgewandelt */
oderConstraints.stream()
.filter(x -> x.size()==1)
.map(y -> y.stream().findFirst().get()).forEach(x -> neweq.addAll(x));
ArrayList<Set<Constraint<UnifyPair>>> remainingOderconstraints = oderConstraints.stream()
.filter(x -> x.size()>1)
.collect(Collectors.toCollection(ArrayList::new));
Set<Set<UnifyPair>> res = unify(neweq, remainingOderconstraints, fc, parallel, rekTiefe, true);
try {
logFile.close();
}
catch (IOException ioE) {
System.err.println("no log-File");
}
if (isUndefinedPairSetSet(res)) {
throw new TypeinferenceException("Unresolved constraints: " + res.toString(), new NullToken()); //return new HashSet<>();
}
else {
return res;
}
}
private static final long serialVersionUID = 1L;
private static int i = 0;
@ -71,16 +97,11 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
* Element, das aus dem nextSet den Gleichunen dieses Threads hinzugefuegt wurde
*/
Set<UnifyPair> nextSetElement;
Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm
/**
* Fuer die Threads
*/
UnifyResultModel urm;
protected static int noOfThread = 0;
private static int totalnoOfThread = 0;
int thNo;
protected boolean one = false;
Integer MaxNoOfThreads = 8;
public static final String rootDirectory = System.getProperty("user.dir")+"/test/logFiles/";
Writer logFile;
@ -123,31 +144,13 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
static int noBacktracking;
static Integer noShortendElements = 0;
Boolean myIsCanceled = false;
volatile UnifyTaskModel usedTasks;
public TypeUnifyTask() {
rules = new RuleSet();
}
/*
public TypeUnifyTask(Set<UnifyPair> eq, IFiniteClosure fc, boolean parallel, FileWriter logFile, Boolean log) {
this.eq = eq;
this.fc = fc;
this.oup = new OrderingUnifyPair(fc);
this.parallel = parallel;
this.logFile = logFile;
this.log = log;
rules = new RuleSet(logFile);
noOfThread++;
thNo = noOfThread;
}
*/
public TypeUnifyTask(Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks) {
public TypeUnifyTask(Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm) {
synchronized (this) {
this.eq = eq;
//this.oderConstraints = oderConstraints.stream().map(x -> x.stream().map(y -> new HashSet<>(y)).collect(Collectors.toSet(HashSet::new))).collect(Collectors.toList(ArrayList::new));
@ -167,21 +170,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
this.parallel = parallel;
this.logFile = logFile;
this.log = log;
noOfThread++;
totalnoOfThread++;
//writeLog("thNo1 " + thNo);
thNo = totalnoOfThread;
writeLog("thNo2 " + thNo);
try {
this.logFile = //new OutputStreamWriter(new NullOutputStream());
//new FileWriter(new File(System.getProperty("user.dir")+"/src/test/resources/logFiles/"+"Thread_"+thNo));
new FileWriter(new File(System.getProperty("user.dir")+"/logFiles/"+"Thread_"+thNo));
logFile.write("");
}
catch (IOException e) {
System.err.println("log-File nicht vorhanden");
}
/*Abbruchtest
if (thNo > 10) {
System.out.println("cancel");
@ -199,56 +188,10 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
rules = new RuleSet(logFile);
this.rekTiefeField = rekTiefe;
this.urm = urm;
this.usedTasks = usedTasks;
this.usedTasks.add(this);
}
}
/**
* Vererbt alle Variancen
* @param eq The set of constraints
*/
/* PL 2018-05- 17 verschoben nach JavaTXCompiler
private void varianceInheritance(Set<UnifyPair> eq) {
Set<PlaceholderType> usedTPH = new HashSet<>();
Set<PlaceholderType> phSet = eq.stream().map(x -> {
Set<PlaceholderType> pair = new HashSet<>();
if (x.getLhsType() instanceof PlaceholderType) pair.add((PlaceholderType)x.getLhsType());
if (x.getRhsType() instanceof PlaceholderType) pair.add((PlaceholderType)x.getRhsType());
return pair;
}).reduce(new HashSet<>(), (a,b) -> { a.addAll(b); return a;} , (c,d) -> { c.addAll(d); return c;});
ArrayList<PlaceholderType> phSetVariance = new ArrayList<>(phSet);
phSetVariance.removeIf(x -> (x.getVariance() == 0));
while(!phSetVariance.isEmpty()) {
PlaceholderType a = phSetVariance.remove(0);
usedTPH.add(a);
//HashMap<PlaceholderType,Integer> ht = new HashMap<>();
//ht.put(a, a.getVariance());
Set<UnifyPair> eq1 = new HashSet<>(eq);
eq1.removeIf(x -> !(x.getLhsType() instanceof PlaceholderType && ((PlaceholderType)x.getLhsType()).equals(a)));
eq1.stream().forEach(x -> { x.getRhsType().accept(new distributeVariance(), a.getVariance());});
eq1 = new HashSet<>(eq);
eq1.removeIf(x -> !(x.getRhsType() instanceof PlaceholderType && ((PlaceholderType)x.getRhsType()).equals(a)));
eq1.stream().forEach(x -> { x.getLhsType().accept(new distributeVariance(), a.getVariance());});
phSetVariance = new ArrayList<>(phSet);
phSetVariance.removeIf(x -> (x.getVariance() == 0 || usedTPH.contains(x)));
}
}
*/
void myCancel(Boolean b) {
myIsCanceled = true;
}
public boolean myIsCancelled() {
return myIsCanceled;
}
protected Set<Set<UnifyPair>> compute() {
if (one) {
System.out.println("two");
}
one = true;
Set<UnifyPair> neweq = new HashSet<>(eq);
/* 1-elementige Oder-Constraints werden in und-Constraints umgewandelt */
oderConstraintsField.stream()
@ -258,7 +201,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
.filter(x -> x.size()>1)
.collect(Collectors.toCollection(ArrayList::new));
Set<Set<UnifyPair>> res = unify(neweq, remainingOderconstraints, fc, parallel, rekTiefeField, true);
noOfThread--;
try {
logFile.close();
}
@ -269,15 +211,10 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
throw new TypeinferenceException("Unresolved constraints: " + res.toString(), new NullToken()); //return new HashSet<>();
}
else {
synchronized (usedTasks) {
if (this.myIsCancelled()) {
return new HashSet<>();
}
else {
return res;
}
}
}
}
/*
@Override
@ -299,28 +236,9 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
* @param fc The finite closure
* @return The set of all principal type unifiers
*/
protected Set<Set<UnifyPair>> unify(final Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe, Boolean finalresult) {
//Set<UnifyPair> aas = eq.stream().filter(x -> x.getLhsType().getName().equals("AA") //&& x.getPairOp().equals(PairOperator.SMALLERDOT)
// ).collect(Collectors.toCollection(HashSet::new));
//writeLog(nOfUnify.toString() + " AA: " + aas.toString());
//if (aas.isEmpty()) {
// System.out.println("");
//}
protected static Set<Set<UnifyPair>> unify(final Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe, Boolean finalresult) {
//.collect(Collectors.toCollection(HashSet::new)));
/*
* Step 1: Repeated application of reduce, adapt, erase, swap
*/
synchronized (usedTasks) {
if (this.myIsCancelled()) {
return new HashSet<>();
}
}
rekTiefe++;
nOfUnify++;
writeLog(nOfUnify.toString() + " Unifikation: " + eq.toString());
writeLog(nOfUnify.toString() + " Oderconstraints: " + oderConstraints.toString());
/*
* Variancen auf alle Gleichungen vererben
@ -457,7 +375,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
//Aufruf von computeCartesianRecursive ANFANG
//writeLog("topLevelSets: " + topLevelSets.toString());
return computeCartesianRecursive(new HashSet<>(), new ArrayList<>(topLevelSets), eq, oderConstraintsOutput, fc, parallel, rekTiefe, finalresult);
return computeCartesianRecursive(new HashSet<>(), new ArrayList<>(topLevelSets), eq, oderConstraintsOutput, fc, rekTiefe, finalresult);
}
@ -465,22 +383,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
Set<Set<UnifyPair>> unify2(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe, Boolean finalresult) {
//Aufruf von computeCartesianRecursive ENDE
//keine Ahnung woher das kommt
//Set<Set<UnifyPair>> setToFlatten = topLevelSets.stream().map(x -> x.iterator().next()).collect(Collectors.toCollection(HashSet::new));
//Muss auskommentiert werden, wenn computeCartesianRecursive ANFANG
// Cartesian product over all (up to 10) top level sets
//Set<Set<Set<UnifyPair>>> eqPrimeSet = setOps.cartesianProduct(topLevelSets)
// .stream().map(x -> new HashSet<>(x))
// .collect(Collectors.toCollection(HashSet::new));
//Muss auskommentiert werden, wenn computeCartesianRecursive ENDE
synchronized (usedTasks) {
if (this.myIsCancelled()) {
return new HashSet<>();
}
}
Set<Set<UnifyPair>> eqPrimePrimeSet = new HashSet<>();
Set<TypeUnifyTask> forks = new HashSet<>();
@ -503,47 +406,9 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
Set<Set<UnifyPair>> unifyres1 = null;
Set<Set<UnifyPair>> unifyres2 = null;
if (!ocString.equals(newOderConstraints.toString())) writeLog("nach Subst: " + newOderConstraints);
//writeLog("nach Subst: " + eqPrimePrime);
/*
* Step 6 a) Restart (fork) for pairs where subst was applied
*/
/*
if(parallel) {
if (eqPrime.equals(eq) && !eqPrimePrime.isPresent()
&& oderConstraints.isEmpty()) //PL 2017-09-29 //(!eqPrimePrime.isPresent()) auskommentiert und durch
//PL 2017-09-29 dies ersetzt //(!eqPrimePrime.isPresent())
//PL 2018-05-18 beide Bedingungen muessen gelten, da eqPrime Veränderungen in allem ausser subst
//eqPrimePrime Veraenderungen in subst repraesentieren.
eqPrimePrimeSet.add(eqPrime);
else if(eqPrimePrime.isPresent()) {
//System.out.println("nextStep: " + eqPrimePrime.get());
TypeUnifyTask fork = new TypeUnifyTask(eqPrimePrime.get(), fc, true, logFile, log);
forks.add(fork);
fork.fork();
}
else {
//System.out.println("nextStep: " + eqPrime);
TypeUnifyTask fork = new TypeUnifyTask(eqPrime, fc, true, logFile, log);
forks.add(fork);
fork.fork();
}
}
else */
{// sequentiell (Step 6b is included)
if (printtag) System.out.println("nextStep: " + eqPrimePrime);
if (eqPrime.equals(eq) && !eqPrimePrime.isPresent()
&& oderConstraints.isEmpty()) { //PL 2017-09-29 //(!eqPrimePrime.isPresent()) auskommentiert und durch
//PL 2017-09-29 dies ersetzt //(!eqPrimePrime.isPresent())
//PL 2018-05-18 beide Bedingungen muessen gelten, da eqPrime Veränderungen in allem ausser subst
//eqPrimePrime Veraenderungen in subst repraesentieren.
//try {
//if (isSolvedForm(eqPrime)) {
// writeLog("eqPrime:" + eqPrime.toString()+"\n");
//}
//}
//catch (IOException e) {
// System.err.println("log-File nicht vorhanden");
//}
&& oderConstraints.isEmpty()) {
eqPrimePrimeSet.add(eqPrime);
if (finalresult && isSolvedForm(eqPrime)) {
writeLog("eqPrime:" + eqPrime.toString()+"\n");
@ -562,23 +427,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
eqPrimePrimeSet.addAll(unifyres);
}
}
//Muss auskommentiert werden, wenn computeCartesianRecursive ANFANG
//}
//Muss auskommentiert werden, wenn computeCartesianRecursive ENDE
/*
* Step 6 b) Build the union over everything.
*/
/*
* PL 2019-01-22: geloescht
if(parallel)
for(TypeUnifyTask fork : forks)
eqPrimePrimeSet.addAll(fork.join());
*/
/*
* Step 7: Filter empty sets;
*/
eqPrimePrimeSet = eqPrimePrimeSet.stream().filter(x -> isSolvedForm(x) || this.isUndefinedPairSet(x)).collect(Collectors.toCollection(HashSet::new));
if (!eqPrimePrimeSet.isEmpty() && !isUndefinedPairSetSet(eqPrimePrimeSet)) {
writeLog("Result1 " + eqPrimePrimeSet.toString());
@ -588,9 +436,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
Set<Set<UnifyPair>> computeCartesianRecursive(Set<Set<UnifyPair>> fstElems, ArrayList<Set<? extends Set<UnifyPair>>> topLevelSets, Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe, Boolean finalresult) {
//ArrayList<Set<Set<UnifyPair>>> remainingSets = new ArrayList<>(topLevelSets);
Set<Set<UnifyPair>> computeCartesianRecursive(Set<Set<UnifyPair>> fstElems, ArrayList<Set<? extends Set<UnifyPair>>> topLevelSets, Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, int rekTiefe, Boolean finalresult) {
fstElems.addAll(topLevelSets.stream()
.filter(x -> x.size()==1)
.map(y -> y.stream().findFirst().get())
@ -603,17 +449,8 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
return result;
}
Set<? extends Set<UnifyPair>> nextSet = remainingSets.remove(0);
//writeLog("nextSet: " + nextSet.toString());
List<Set<UnifyPair>> nextSetasList =new ArrayList<>(nextSet);
/*
try {
//List<Set<UnifyPair>>
//nextSetasList = oup.sortedCopy(nextSet);//new ArrayList<>(nextSet);
}
catch (java.lang.IllegalArgumentException e) {
System.out.print("");
}
*/
Set<Set<UnifyPair>> result = new HashSet<>();
int variance = 0;
@ -670,25 +507,15 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
System.out.print("");
if (nextSetasList.iterator().next().stream().filter(x -> x.getLhsType().getName().equals("D")).findFirst().isPresent() && nextSetasList.size()>1)
System.out.print("");
//writeLog("nextSetasList: " + nextSetasList.toString());
Set<UnifyPair> nextSetElem = nextSetasList.get(0);
//writeLog("BasePair1: " + nextSetElem + " " + nextSetElem.iterator().next().getBasePair());
/* sameEqSet-Bestimmung: Wenn a = ty \in nextSet dann enthaelt sameEqSet alle Paare a < ty1 oder ty2 < a aus fstElems */
Set<UnifyPair> sameEqSet = new HashSet<>();
Optional<UnifyPair> optOrigPair = null;
//if (variance != 2) {
if (!oderConstraint) {
optOrigPair = nextSetElem.stream().filter(x -> (
//x.getBasePair() != null && ist gegeben wenn variance != 2
//x.getBasePair().getPairOp().equals(PairOperator.SMALLERDOT) &&
(x.getPairOp().equals(PairOperator.EQUALSDOT)
/*
(x.getBasePair().getLhsType() instanceof PlaceholderType
&& x.getLhsType().equals(x.getBasePair().getLhsType()))
|| (x.getBasePair().getRhsType() instanceof PlaceholderType
&& x.getLhsType().equals(x.getBasePair().getRhsType())
*/
(x.getPairOp().equals(PairOperator.EQUALSDOT)
))).filter(x -> //Sicherstellen, dass bei a = ty a auch wirklich die gesuchte Typvariable ist
x.getLhsType().equals(x.getBasePair().getLhsType()) ||
x.getLhsType().equals(x.getBasePair().getRhsType())
@ -837,7 +664,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
}
/* PL 2019-03-11 Ende eingefuegt Vergleich mit anderen Paaren ggf. loeschen */
if(parallel && (variance == 1) && noOfThread <= MaxNoOfThreads) {
if(parallel && (variance == 1)) {
Set<TypeUnify2Task> forks = new HashSet<>();
Set<UnifyPair> newEqOrig = new HashSet<>(eq);
Set<Set<UnifyPair>> newElemsOrig = new HashSet<>(elems);
@ -845,14 +672,8 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
newElemsOrig.add(a);
/* FORK ANFANG */
TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm, usedTasks);
TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm);
//forks.add(forkOrig);
synchronized(usedTasks) {
if (this.myIsCancelled()) {
return new HashSet<>();
}
forkOrig.fork();
}
/* FORK ENDE */
synchronized (this) {
@ -901,41 +722,14 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
Set<Set<UnifyPair>> newElems = new HashSet<>(elems);
List<Set<Constraint<UnifyPair>>> newOderConstraints = new ArrayList<>(oderConstraints);
newElems.add(nSaL);
TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm, usedTasks);
forks.add(fork);
synchronized(usedTasks) {
if (this.myIsCancelled()) {
return new HashSet<>();
}
fork.fork();
}
TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm);
}
//res = unify2(newElemsOrig, newEqOrig, newOderConstraintsOrig, fc, parallel, rekTiefe);
/* FORK ANFANG */
synchronized (this) {
writeLog("wait "+ forkOrig.thNo);
noOfThread--;
res = forkOrig.join();
synchronized (usedTasks) {
if (this.myIsCancelled()) {
return new HashSet<>();
}
}
//noOfThread++;
forkOrig.writeLog("final Orig 1");
forkOrig.closeLogFile();
//Set<Set<UnifyPair>> fork_res = forkOrig.join();
writeLog("JoinOrig " + new Integer(forkOrig.thNo).toString());
//noOfThread--; an das Ende von compute verschoben
//add_res.add(fork_res);
};
/* FORK ENDE */
forks.forEach(x -> writeLog("wait: " + x.thNo));
res = forkOrig.join();
for(TypeUnify2Task fork : forks) {
synchronized (this) {
noOfThread--;
Set<Set<UnifyPair>> fork_res = fork.join();
synchronized (usedTasks) {
if (this.myIsCancelled()) {
@ -952,28 +746,18 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
aParDef.add(fork.getNextSetElement());
}
fork.writeLog("final 1");
fork.closeLogFile();
};
}
//noOfThread++;
} else {
if(parallel && (variance == -1) && noOfThread <= MaxNoOfThreads) {
if((variance == -1)) {
Set<TypeUnify2Task> forks = new HashSet<>();
Set<UnifyPair> newEqOrig = new HashSet<>(eq);
Set<Set<UnifyPair>> newElemsOrig = new HashSet<>(elems);
List<Set<Constraint<UnifyPair>>> newOderConstraintsOrig = new ArrayList<>(oderConstraints);
newElemsOrig.add(a);
/* FORK ANFANG */
TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm, usedTasks);
//forks.add(forkOrig);
synchronized(usedTasks) {
if (this.myIsCancelled()) {
return new HashSet<>();
}
forkOrig.fork();
}
/* FORK ENDE */
TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm);
synchronized (this) {
writeLog("a in " + variance + " "+ a);
@ -1628,7 +1412,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
return true;}).reduce((xx, yy) -> xx || yy).get();
}
protected boolean isUndefinedPairSet(Set<UnifyPair> s) {
protected static boolean isUndefinedPairSet(Set<UnifyPair> s) {
if (s.size() >= 1 ) {
Boolean ret = s.stream().map(x -> x.isUndefinedPair()).reduce(true, (x,y)-> (x && y));
return ret;
@ -1638,7 +1422,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
}
}
protected boolean isUndefinedPairSetSet(Set<Set<UnifyPair>> s) {
protected static boolean isUndefinedPairSetSet(Set<Set<UnifyPair>> s) {
if (s.size() >= 1) {
Boolean ret = s.stream(). map(x -> isUndefinedPairSet(x)).reduce(true, (x,y)-> (x && y));
return ret;
@ -1697,7 +1481,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
/*
* Apply rules until the queue is empty
*/
while(!eqQueue.isEmpty()) {
while(!eqQueue.isEmpty()) {
UnifyPair pair = eqQueue.pollFirst();
// ReduceUp, ReduceLow, ReduceUpLow

@ -1,18 +0,0 @@
package de.dhbwstuttgart.typeinference.unify;
import java.util.List;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class UnifyResultEvent {
private List<ResultSet> newTypeResult;
public UnifyResultEvent(List<ResultSet> newTypeResult) {
this.newTypeResult = newTypeResult;
}
public List<ResultSet> getNewTypeResult() {
return newTypeResult;
}
}

@ -1,7 +0,0 @@
package de.dhbwstuttgart.typeinference.unify;
public interface UnifyResultListener {
void onNewTypeResultFound(UnifyResultEvent evt);
}

@ -1,21 +0,0 @@
package de.dhbwstuttgart.typeinference.unify;
import java.util.ArrayList;
import java.util.List;
import de.dhbwstuttgart.typeinference.result.ResultSet;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
public class UnifyResultListenerImpl implements UnifyResultListener {
List<ResultSet> results = new ArrayList<>();
public synchronized void onNewTypeResultFound(UnifyResultEvent evt) {
results.addAll(evt.getNewTypeResult());
}
public List<ResultSet> getResults() {
return results;
}
}

@ -1,59 +0,0 @@
package de.dhbwstuttgart.typeinference.unify;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.result.ResultSet;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
public class UnifyResultModel {
ConstraintSet<de.dhbwstuttgart.typeinference.constraints.Pair> cons;
IFiniteClosure fc;
public UnifyResultModel(ConstraintSet<de.dhbwstuttgart.typeinference.constraints.Pair> cons,
IFiniteClosure fc) {
this.cons = cons;
this.fc = fc;
}
private List<UnifyResultListener> listeners = new ArrayList<>();
public void addUnifyResultListener(UnifyResultListener listenerToAdd) {
listeners.add(listenerToAdd);
}
public void removeUnifyResultListener(UnifyResultListener listenerToRemove) {
listeners.remove(listenerToRemove);
}
public void notify(Set<Set<UnifyPair>> eqPrimePrimeSet) {
Set<Set<UnifyPair>> eqPrimePrimeSetRet = eqPrimePrimeSet.stream().map(x -> {
Optional<Set<UnifyPair>> res = new RuleSet().subst(x.stream().map(y -> {
if (y.getPairOp() == PairOperator.SMALLERDOTWC) y.setPairOp(PairOperator.EQUALSDOT);
return y; //alle Paare a <.? b erden durch a =. b ersetzt
}).collect(Collectors.toCollection(HashSet::new)));
if (res.isPresent()) {//wenn subst ein Erg liefert wurde was veraendert
return new TypeUnifyTask().applyTypeUnificationRules(res.get(), fc);
}
else return x; //wenn nichts veraendert wurde wird x zurueckgegeben
}).collect(Collectors.toCollection(HashSet::new));
List<ResultSet> newResult = eqPrimePrimeSetRet.stream().map(unifyPairs ->
new ResultSet(UnifyTypeFactory.convert(unifyPairs, de.dhbwstuttgart.typeinference.constraints.Pair.generateTPHMap(cons))))
.collect(Collectors.toList());
UnifyResultEvent evt = new UnifyResultEvent(newResult);
for (UnifyResultListener listener : listeners) {
listener.onNewTypeResultFound(evt);
}
}
}

@ -1,18 +0,0 @@
package de.dhbwstuttgart.typeinference.unify;
import java.util.ArrayList;
public class UnifyTaskModel {
ArrayList<TypeUnifyTask> usedTasks = new ArrayList<>();
public synchronized void add(TypeUnifyTask t) {
usedTasks.add(t);
}
public synchronized void cancel() {
for(TypeUnifyTask t : usedTasks) {
t.myCancel(true);
}
}
}

@ -29,8 +29,6 @@ public class UnifyPair {
*/
private PairOperator pairOp;
private boolean undefinedPair = false;
/**
* Unifier/substitute that generated this pair
* PL 2018-03-15
@ -114,10 +112,7 @@ public class UnifyPair {
public void addSubstitutions(Set<UnifyPair> sup) {
substitution.addAll(sup);
}
public void setUndefinedPair() {
undefinedPair = true;
}
public Set<UnifyPair> getSubstitution() {
return new HashSet<>(substitution);
}
@ -125,10 +120,6 @@ public class UnifyPair {
public UnifyPair getBasePair() {
return basePair;
}
public boolean isUndefinedPair() {
return undefinedPair;
}
public Set<UnifyPair> getAllSubstitutions () {
Set<UnifyPair> ret = new HashSet<>();
ret.addAll(new ArrayList<>(getSubstitution()));

@ -40,7 +40,7 @@ public class AllgemeinTest {
//String className = "FCTest3";
//String className = "Var";
//String className = "Put";
String className = "Twice";
String className = "Cycle";
//PL 2019-10-24: genutzt fuer unterschiedliche Tests
path = System.getProperty("user.dir")+"/src/test/resources/AllgemeinTest/" + className + ".jav";
//path = System.getProperty("user.dir")+"/src/test/resources/AllgemeinTest/Overloading_Generics.jav";

@ -15,7 +15,7 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
/*
Example method:
A id(B i) return i;
gives constraint: B <. A and A <. Object, which are method constraints
gives constraint: B <. A, which is a method constraint
*/
List<TPHConstraint> inputConstraints = new ArrayList<>();
@ -32,8 +32,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
/*
MethodConstraints should be the same as the input constraint
*/
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, new ArrayList<ClassConstraint>(), tphPositions);
assertTrue(methodConstraints.size() == 2);
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, tphPositions);
assertTrue(methodConstraints.size() == 1);
assertTrue(methodConstraints.get(0).getLeft().equals("B"));
assertTrue(methodConstraints.get(0).getRight().equals("A"));
}
@ -46,7 +46,7 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
return f;
}
}
gives constraint: A <. B and B <. Object which are class constraints
gives constraint: A <. B, which is a class constraint
*/
List<TPHConstraint> inputConstraints = new ArrayList<>();
@ -109,12 +109,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions);
System.out.println(classConstraints);
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, classConstraints, tphPositions);
System.out.println(methodConstraints);
assertFalse(classConstraints.isEmpty());
assertTrue(classConstraints.size() == 6);
assertFalse(methodConstraints.isEmpty());
assertTrue(methodConstraints.size() == 2);
}
@ -169,13 +165,9 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions);
System.out.println(classConstraints);
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, classConstraints, tphPositions);
System.out.println(methodConstraints);
assertFalse(classConstraints.isEmpty());
assertTrue(classConstraints.size() == 3);
assertFalse(methodConstraints.isEmpty());
assertTrue(methodConstraints.size()==9);
}

@ -1,11 +1,16 @@
/*
package insertGenerics;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.insertGenerics.*;
import de.dhbwstuttgart.bytecode.insertGenerics.ClassConstraint;
import de.dhbwstuttgart.bytecode.insertGenerics.FamilyOfGeneratedGenerics;
import de.dhbwstuttgart.bytecode.insertGenerics.MethodConstraint;
import de.dhbwstuttgart.bytecode.insertGenerics.PositionFinder;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -22,31 +27,19 @@ public class TestExample42 {
return cs;
}
public HashMap<String, PairTphMethod<PositionFinder.Position, String>> fillPosOfTphs() {
HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs = new HashMap<>();
// TPHs "U" und "L" auskommentiert, da nach Vorgaben L zu Z umbenannt und U als void interpretiert wird
PairTphMethod<PositionFinder.Position, String> posOfK = new PairTphMethod<>(PositionFinder.Position.FIELD, null);
// PairTphMethod<PositionFinder.Position, String> posOfL = new PairTphMethod<>(PositionFinder.Position.METHOD, "id");
PairTphMethod<PositionFinder.Position, String> posOfM = new PairTphMethod<>(PositionFinder.Position.METHOD, "id");
PairTphMethod<PositionFinder.Position, String> posOfN = new PairTphMethod<>(PositionFinder.Position.METHOD, "id");
PairTphMethod<PositionFinder.Position, String> posOfP = new PairTphMethod<>(PositionFinder.Position.METHOD, "setA");
PairTphMethod<PositionFinder.Position, String> posOfQ = new PairTphMethod<>(PositionFinder.Position.METHOD, "setA");
// PairTphMethod<PositionFinder.Position, String> posOfU = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
PairTphMethod<PositionFinder.Position, String> posOfV = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
PairTphMethod<PositionFinder.Position, String> posOfW = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
PairTphMethod<PositionFinder.Position, String> posOfZ = new PairTphMethod<>(PositionFinder.Position.METHOD, "m");
posOfTphs.put("K", posOfK);
// posOfTphs.put("L", posOfL);
posOfTphs.put("M", posOfM);
posOfTphs.put("N", posOfN);
posOfTphs.put("P", posOfP);
posOfTphs.put("Q", posOfQ);
// posOfTphs.put("U", posOfU);
posOfTphs.put("V", posOfV);
posOfTphs.put("W", posOfW);
posOfTphs.put("Z", posOfZ);
public HashMap<String, PositionFinder.Position> fillPosOfTphs() {
BufferedReader l;
HashMap<String, PositionFinder.Position> posOfTphs = new HashMap<>();
posOfTphs.put("K", PositionFinder.Position.FIELD);
posOfTphs.put("L", PositionFinder.Position.METHOD);
posOfTphs.put("M", PositionFinder.Position.METHOD);
posOfTphs.put("N", PositionFinder.Position.METHOD);
posOfTphs.put("P", PositionFinder.Position.METHOD);
posOfTphs.put("Q", PositionFinder.Position.METHOD);
posOfTphs.put("U", PositionFinder.Position.METHOD);
posOfTphs.put("V", PositionFinder.Position.METHOD);
posOfTphs.put("W", PositionFinder.Position.METHOD);
posOfTphs.put("Z", PositionFinder.Position.METHOD);
return posOfTphs;
}
@ -54,12 +47,10 @@ public class TestExample42 {
public void genericTest() {
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs());
System.out.println("ClassConstraints: " + classConstraints);
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),classConstraints,fillPosOfTphs());
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),fillPosOfTphs());
System.out.println("MethodConstraints: " + methodConstraints);
for(MethodConstraint mC: methodConstraints) {
System.out.println(mC);
}
List<TPHConstraint> testCons;
}
}
*/

@ -1,61 +0,0 @@
package insertGenerics;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.insertGenerics.*;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class TestExample42_allInOneMethod {
public List<TPHConstraint> fillConstraintsList() {
List<TPHConstraint> cs = new ArrayList<>();
cs.add(new TPHConstraint("M", "N", Relation.EXTENDS));
cs.add(new TPHConstraint("N", "Z", Relation.EXTENDS));
cs.add(new TPHConstraint("Q", "K", Relation.EXTENDS));
cs.add(new TPHConstraint("K", "P", Relation.EXTENDS));
cs.add(new TPHConstraint("W", "M", Relation.EXTENDS));
cs.add(new TPHConstraint("Z", "V", Relation.EXTENDS));
return cs;
}
public HashMap<String, PairTphMethod<PositionFinder.Position, String>> fillPosOfTphs() {
HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTphs = new HashMap<>();
// TPHs "U" und "L" auskommentiert, da nach Vorgaben L zu Z umbenannt und U als void interpretiert wird
PairTphMethod<PositionFinder.Position, String> posOfK = new PairTphMethod<>(PositionFinder.Position.FIELD, null);
// PairTphMethod<PositionFinder.Position, String> posOfL = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
PairTphMethod<PositionFinder.Position, String> posOfM = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
PairTphMethod<PositionFinder.Position, String> posOfN = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
PairTphMethod<PositionFinder.Position, String> posOfP = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
PairTphMethod<PositionFinder.Position, String> posOfQ = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
// PairTphMethod<PositionFinder.Position, String> posOfU = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
PairTphMethod<PositionFinder.Position, String> posOfV = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
PairTphMethod<PositionFinder.Position, String> posOfW = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
PairTphMethod<PositionFinder.Position, String> posOfZ = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod");
posOfTphs.put("K", posOfK);
// posOfTphs.put("L", posOfL);
posOfTphs.put("M", posOfM);
posOfTphs.put("N", posOfN);
posOfTphs.put("P", posOfP);
posOfTphs.put("Q", posOfQ);
// posOfTphs.put("U", posOfU);
posOfTphs.put("V", posOfV);
posOfTphs.put("W", posOfW);
posOfTphs.put("Z", posOfZ);
return posOfTphs;
}
@Test
public void genericTest() {
List<ClassConstraint> classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs());
System.out.println("ClassConstraints: " + classConstraints);
List<MethodConstraint> methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),classConstraints,fillPosOfTphs());
System.out.println("MethodConstraints: " + methodConstraints);
List<TPHConstraint> testCons;
}
}

@ -0,0 +1,11 @@
class Cycle{
<A> A und(A a, A b){
return a;
}
m(a){
return und(m(m(a)), a);
}
}

@ -0,0 +1,5 @@
class Twice{
m(x, f){
return f.apply(f.apply(x));
}
}