diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index a860adf9..a9c2fa9e 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -25,11 +25,14 @@ import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; import de.dhbwstuttgart.typeinference.unify.RuleSet; import de.dhbwstuttgart.typeinference.unify.TypeUnify; import de.dhbwstuttgart.typeinference.unify.distributeVariance; +import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask; +import de.dhbwstuttgart.typeinference.unify.UnifyResultListenerImpl; +import de.dhbwstuttgart.typeinference.unify.UnifyResultModel; import java.io.File; import java.io.FileOutputStream; @@ -40,9 +43,12 @@ import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.antlr.v4.parse.ANTLRParser.throwsSpec_return; + public class JavaTXCompiler { final CompilationEnvironment environment; + Boolean resultmodel = true; public final Map sourceFiles = new HashMap<>(); Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"/test/logFiles/log" geschrieben werden soll? @@ -267,20 +273,366 @@ public class JavaTXCompiler { phSetVariance.removeIf(x -> (x.getVariance() == 0 || usedTPH.contains(x))); } } +<<<<<<< HEAD +======= + */ + + public UnifyResultModel typeInferenceAsync() throws ClassNotFoundException { + List 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()); + } - private Map generateTPHMap(ConstraintSet constraints) { - HashMap 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; + final ConstraintSet cons = getConstraints(); + Set> results = new HashSet<>(); + UnifyResultModel urm = null; + try { + FileWriter logFile = new FileWriter(new File(System.getProperty("user.dir")+"/src/test/java/logFiles/"+"log_"+sourceFiles.keySet().iterator().next().getName())); + + IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses,logFile); + System.out.println(finiteClosure); + urm = new UnifyResultModel(cons, finiteClosure); + ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); + + Function 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> 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 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(), (a,b) -> { a.addAll(b); return a;}, (a,b) -> { a.addAll(b); return a;} ) ) + .reduce(new HashSet(), (a,b) -> { a.addAll(b); return a;} ); + + Set 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(), (a,b) -> { a.addAll(b); return a;}, (a,b) -> { a.addAll(b); return a;} ) ) + .reduce(new HashSet(), (a,b) -> { a.addAll(b); return a;} ); + + Set paraTypeVarNames = methodParaTypeVarNames; + paraTypeVarNames.addAll(constructorParaTypeVarNames); + + + Set 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 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 varianceTPHold; + Set 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> result = unify.unifySequential(xConsSet, finiteClosure, logFile, log); + //Set> result = unify.unify(xConsSet, finiteClosure); + List>> oderConstraints = unifyCons.getOderConstraints().stream().map(x -> { + Set> ret = new HashSet<>(); + for (Constraint y : x) { + ret.add(new HashSet<>(y)); + } + return ret; + }).collect(Collectors.toCollection(ArrayList::new)); + unify.unifyAsync(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, urm); + } + catch (IOException e) { + System.err.println("kein LogFile"); + } + return urm; } + + + public List typeInference() throws ClassNotFoundException { + List 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 cons = getConstraints(); + Set> results = new HashSet<>(); + try { + FileWriter logFile = new FileWriter(new File(System.getProperty("user.dir")+"/src/test/java/logFiles/"+"log_"+sourceFiles.keySet().iterator().next().getName())); + + IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses,logFile); + System.out.println(finiteClosure); + ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); + + Function 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> 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 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(), (a,b) -> { a.addAll(b); return a;}, (a,b) -> { a.addAll(b); return a;} ) ) + .reduce(new HashSet(), (a,b) -> { a.addAll(b); return a;} ); + + Set 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(), (a,b) -> { a.addAll(b); return a;}, (a,b) -> { a.addAll(b); return a;} ) ) + .reduce(new HashSet(), (a,b) -> { a.addAll(b); return a;} ); + + Set paraTypeVarNames = methodParaTypeVarNames; + paraTypeVarNames.addAll(constructorParaTypeVarNames); + + + Set 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 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 varianceTPHold; + Set 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> result = unify.unifySequential(xConsSet, finiteClosure, logFile, log); + //Set> result = unify.unify(xConsSet, finiteClosure); + List>> oderConstraints = unifyCons.getOderConstraints().stream().map(x -> { + Set> ret = new HashSet<>(); + for (Constraint y : x) { + ret.add(new HashSet<>(y)); + } + return ret; + }).collect(Collectors.toCollection(ArrayList::new)); + 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); + System.out.println("RESULT Final: " + li.getResults()); + logFile.write("RES_FINAL: " + li.getResults().toString()+"\n"); + logFile.flush(); + return li.getResults(); + } + /* UnifyResultModel End */ + else { + Set> result = unify.unify(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, new UnifyResultModel(cons, finiteClosure)); + //Set> result = unify.unifyOderConstraints(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, new UnifyResultModel(cons, finiteClosure)); + System.out.println("RESULT: " + result); + logFile.write("RES: " + result.toString()+"\n"); + logFile.flush(); + results.addAll(result); + + + results = results.stream().map(x -> { + Optional> 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) { + System.err.println("kein LogFile"); + } + return results.stream().map((unifyPairs -> + new ResultSet(UnifyTypeFactory.convert(unifyPairs, Pair.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. + * @param eq The set of constraints + */ + private Set varianceInheritanceConstraintSet(ConstraintSet cons) { + Set eq = cons.getAll(); + Set usedTPH = new HashSet<>(); + Set phSet = eq.stream().map(x -> { + Set 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 phSetVariance = new ArrayList<>(phSet); + phSetVariance.removeIf(x -> (x.getVariance() == 0)); + while(!phSetVariance.isEmpty()) { + PlaceholderType a = phSetVariance.remove(0); + usedTPH.add(a); + //HashMap ht = new HashMap<>(); + //ht.put(a, a.getVariance()); + //ConstraintSet eq1 = cons; + //eq1.removeIf(x -> !(x.getLhsType() instanceof PlaceholderType && ((PlaceholderType)x.getLhsType()).equals(a))); + //durch if-Abfrage im foreach geloest + cons.forEach(x -> { + if (x.getLhsType() instanceof PlaceholderType && ((PlaceholderType)x.getLhsType()).equals(a)) { + x.getRhsType().accept(new distributeVariance(), a.getVariance()); + } + }); + //` eq1 = new HashSet<>(eq); + //eq1.removeIf(x -> !(x.getRhsType() instanceof PlaceholderType && ((PlaceholderType)x.getRhsType()).equals(a))); + //durch if-Abfrage im foreach geloest + cons.forEach(x -> { + if (x.getRhsType() instanceof PlaceholderType && ((PlaceholderType)x.getRhsType()).equals(a)) { + x.getLhsType().accept(new distributeVariance(), a.getVariance()); + } + }); + phSetVariance = new ArrayList<>(phSet); //macht vermutlich keinen Sinn PL 2018-10-18, doch, es koennen neue TPHs mit Variancen dazugekommen sein PL 2018-11-07 + phSetVariance.removeIf(x -> (x.getVariance() == 0 || usedTPH.contains(x))); + } + return usedTPH; + } + +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 + + private SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException { CompilationUnitContext tree = JavaTXParser.parse(sourceFile); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java b/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java index 01356fe9..581f9fbd 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.typeinference.constraints; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.unify.GuavaSetOperations; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; diff --git a/src/main/java/de/dhbwstuttgart/typeinference/constraints/Pair.java b/src/main/java/de/dhbwstuttgart/typeinference/constraints/Pair.java index 98679f7d..0c8ad7a5 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/constraints/Pair.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/constraints/Pair.java @@ -1,7 +1,10 @@ package de.dhbwstuttgart.typeinference.constraints; import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; @@ -110,5 +113,20 @@ public class Pair implements Serializable public boolean OperatorSmallerDot() { return eOperator == PairOperator.SMALLERDOT; } + + + static public Map generateTPHMap(ConstraintSet constraints) { + HashMap 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; + } } // ino.end diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java index d4e6b551..acb6afbb 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -4,10 +4,12 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; +import java.util.List; import java.util.Optional; import java.util.Queue; import java.util.Set; import java.util.Stack; +import java.util.function.Function; import java.util.stream.Collectors; import de.dhbwstuttgart.exceptions.DebugException; @@ -322,7 +324,14 @@ public class RuleSet implements IRuleSet{ if(lhsType instanceof ReferenceType) lhsSType = (ReferenceType) lhsType; +<<<<<<< HEAD else if(lhsType instanceof WildcardType) { +======= + rhsSType = (ReferenceType) rhsType; + } + else if (((lhsType instanceof ExtendsType) && (rhsType instanceof ExtendsType)) + || ((lhsType instanceof SuperType) && (rhsType instanceof SuperType))) { +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 UnifyType lhsSTypeRaw = ((WildcardType) lhsType).getWildcardedType(); if(lhsSTypeRaw instanceof ReferenceType) lhsSType = (ReferenceType) lhsSTypeRaw; @@ -609,8 +618,12 @@ public class RuleSet implements IRuleSet{ return succ ? permutation : new int[0]; } - @Override public Optional> subst(Set pairs) { + return subst(pairs, new ArrayList<>()); + } + + @Override + public Optional> subst(Set pairs, List>> oderConstraints) { HashMap typeMap = new HashMap<>(); Stack occuringTypes = new Stack<>(); @@ -650,13 +663,35 @@ public class RuleSet implements IRuleSet{ if(lhsType != null //&& !((rhsType = pair.getRhsType()) instanceof PlaceholderType) //PL geloescht am 2017-09-29 Begründung: auch Typvariablen muessen ersetzt werden. && typeMap.get(lhsType) > 1 // The type occurs in more pairs in the set than just the recent pair. - && !rhsType.getTypeParams().occurs(lhsType)) { + && !rhsType.getTypeParams().occurs(lhsType) + && !((rhsType instanceof WildcardType) && ((WildcardType)rhsType).getWildcardedType().equals(lhsType))) //PL eigefuegt 2018-02-18 + { Unifier uni = new Unifier(lhsType, rhsType); result = result.stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(ArrayList::new)); result1 = result1.stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(LinkedList::new)); + + Function,? extends HashSet> applyUni = b -> b.stream().map( + x -> uni.apply(pair,x)).collect(Collectors.toCollection(HashSet::new)); + List>> oderConstraintsRet = new ArrayList<>(); + for(Set> oc : oderConstraints) { + //Set> ocRet = new HashSet<>(); + //for(Set cs : oc) { + Set> csRet = oc.stream().map(applyUni).collect(Collectors.toCollection(HashSet::new)); + oderConstraintsRet.add(csRet); + //} + } + oderConstraints.replaceAll(oc -> oc.stream().map(applyUni).collect(Collectors.toCollection(HashSet::new))); + /* + oderConstraints = oderConstraints.stream().map( + a -> a.stream().map(applyUni + //b -> b.stream().map( + // x -> uni.apply(pair,x)).collect(Collectors.toCollection(HashSet::new) ) + ).collect(Collectors.toCollection(HashSet::new)) + ).collect(Collectors.toList(ArrayList::new)); + } + */ applied = true; } - result.add(pair); } @@ -935,6 +970,8 @@ public class RuleSet implements IRuleSet{ UnifyType extendedType = ((ExtendsType)lhsType).getExtendedType(); + if (extendedType.equals(rhsType)) return Optional.empty(); //PL 2019-02-18 eingefügt ? extends a <.? a + boolean isGen = extendedType instanceof PlaceholderType && !((PlaceholderType) extendedType).isGenerated(); Set result = new HashSet<>(); @@ -961,6 +998,8 @@ public class RuleSet implements IRuleSet{ UnifyType superedType = ((SuperType)lhsType).getSuperedType(); + if (superedType.equals(rhsType)) return Optional.empty(); //PL 2019-02-18 eingefügt ? super a <.? a + boolean isGen = superedType instanceof PlaceholderType && !((PlaceholderType) superedType).isGenerated(); Set result = new HashSet<>(); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java index fa190cb7..c89803fd 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java @@ -4,22 +4,106 @@ import java.io.FileWriter; import java.util.Set; import java.util.concurrent.ForkJoinPool; +<<<<<<< HEAD +======= +import de.dhbwstuttgart.typeinference.constraints.Constraint; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; +import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; public class TypeUnify { +<<<<<<< HEAD public Set> unify(Set eq, IFiniteClosure fc, FileWriter logFile, Boolean log) { TypeUnifyTask unifyTask = new TypeUnifyTask(eq, fc, true, logFile, log); +======= + + /** + * unify parallel ohne result modell + * @param undConstrains + * @param oderConstraints + * @param fc + * @param logFile + * @param log + * @param cons + * @return + */ + public Set> unify(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, FileWriter logFile, Boolean log, UnifyResultModel ret) { + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret); +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 ForkJoinPool pool = new ForkJoinPool(); pool.invoke(unifyTask); Set> res = unifyTask.join(); return res; } +<<<<<<< HEAD +======= + /** + * unify asynchron mit Rückgabe UnifyResultModel ohne dass alle results gesammelt sind + * @param undConstrains + * @param oderConstraints + * @param fc + * @param logFile + * @param log + * @param cons + * @param ret + * @return + */ + public UnifyResultModel unifyAsync(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, FileWriter 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; + } + + /** + * unify parallel mit Rückgabe UnifyResultModel nachdem alle results gesammelt sind + * @param undConstrains + * @param oderConstraints + * @param fc + * @param logFile + * @param log + * @param cons + * @param ret + * @return + */ + public UnifyResultModel unifyParallel(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, FileWriter 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> res = unifyTask.join(); + return ret; + } + + /* +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 public Set> unifySequential(Set eq, IFiniteClosure fc, FileWriter logFile, Boolean log) { TypeUnifyTask unifyTask = new TypeUnifyTask(eq, fc, false, logFile, log); Set> res = unifyTask.compute(); return res; } +<<<<<<< HEAD +======= + */ + + /** + * unify sequentiell mit oderconstraints + * @param undConstrains + * @param oderConstraints + * @param fc + * @param logFile + * @param log + * @param cons + * @return + */ + public Set> unifyOderConstraints(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, FileWriter logFile, Boolean log, UnifyResultModel ret) { + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, logFile, log, 0, ret); + Set> res = unifyTask.compute(); + return res; + } +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java new file mode 100644 index 00000000..43b294ad --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java @@ -0,0 +1,54 @@ +package de.dhbwstuttgart.typeinference.unify; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import de.dhbwstuttgart.typeinference.constraints.Constraint; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; + +public class TypeUnify2Task extends TypeUnifyTask { + + Set> setToFlatten; + + public TypeUnify2Task(Set> setToFlatten, Set eq, List>> oderConstraints, Set nextSetElement, IFiniteClosure fc, boolean parallel, FileWriter logFile, Boolean log, int rekTiefe, UnifyResultModel urm) { + super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm); + this.setToFlatten = setToFlatten; + this.nextSetElement = nextSetElement; + } + + Set getNextSetElement() { + return nextSetElement; + } + + @Override + protected Set> compute() { + if (one) { + System.out.println("two"); + } + one = true; + Set> res = unify2(setToFlatten, eq, oderConstraintsField, fc, parallel, rekTiefeField); + /*if (isUndefinedPairSetSet(res)) { + return new HashSet<>(); } + else + */ + noOfThread--; + return res; + } + + public void closeLogFile() { + + try { + logFile.close(); + } + catch (IOException ioE) { + System.err.println("no log-File" + thNo); + } + + } +} diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 7910b079..b4b671fa 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -18,6 +18,13 @@ import java.util.function.BinaryOperator; import java.util.stream.Collectors; import java.util.stream.Stream; +<<<<<<< HEAD +======= +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +import de.dhbwstuttgart.typeinference.constraints.Constraint; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.result.ResultSet; +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations; @@ -53,6 +60,24 @@ public class TypeUnifyTask extends RecursiveTask>> { private boolean printtag = false; Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"/test/logFiles/log" geschrieben werden soll? +<<<<<<< HEAD +======= + /** + * Element, das aus dem nextSet den Gleichunen dieses Threads hinzugefuegt wurde + */ + Set nextSetElement; + + /** + * Fuer die Threads + */ + UnifyResultModel urm; + protected static int noOfThread = 0; + private static int totalnoOfThread = 0; + int thNo; + protected boolean one = false; + Integer MaxNoOfThreads = 8; + +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 public static final String rootDirectory = System.getProperty("user.dir")+"/test/logFiles/"; FileWriter logFile; @@ -101,6 +126,46 @@ public class TypeUnifyTask extends RecursiveTask>> { rules = new RuleSet(logFile); } +<<<<<<< HEAD +======= + public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, FileWriter 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)); + this.oderConstraintsField = oderConstraints; /*.stream().map(x -> { + Set> ret = new HashSet<>(); + for (Constraint y : x) { + ret.add(new HashSet<>(y)); + } + return ret; + }).collect(Collectors.toCollection(ArrayList::new)); + */ + + //x.stream().map(y -> new HashSet<>(y)).collect(Collectors.toSet(HashSet::new))).collect(Collectors.toList(ArrayList::new)); + this.nextSetElement = nextSetElement; + this.fc = fc; + this.oup = new OrderingUnifyPair(fc); + this.parallel = parallel; + this.logFile = logFile; + this.log = log; + + noOfThread++; + totalnoOfThread++; + //writeLog("thNo1 " + thNo); + thNo = totalnoOfThread; + writeLog("thNo2 " + thNo); + try { + this.logFile = new FileWriter(new File(System.getProperty("user.dir")+"/src/test/java/logFiles/"+"Thread_"+thNo)); + } + catch (IOException e) { + System.err.println("log-File nicht vorhanden"); + } + rules = new RuleSet(logFile); + this.rekTiefeField = rekTiefe; + this.urm = urm; + } + } +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 /** * Vererbt alle Variancen @@ -134,6 +199,35 @@ public class TypeUnifyTask extends RecursiveTask>> { } } */ +<<<<<<< HEAD +======= + + protected Set> compute() { + if (one) { + System.out.println("two"); + } + one = true; + Set neweq = new HashSet<>(eq); + /* 1-elementige Oder-Constraints werden in und-Constraints umgewandelt */ + oderConstraintsField.stream() + .filter(x -> x.size()==1) + .map(y -> y.stream().findFirst().get()).forEach(x -> neweq.addAll(x)); + ArrayList>> remainingOderconstraints = oderConstraintsField.stream() + .filter(x -> x.size()>1) + .collect(Collectors.toCollection(ArrayList::new)); + Set> res = unify(neweq, remainingOderconstraints, fc, parallel, rekTiefeField); + noOfThread--; + try { + logFile.close(); + } + catch (IOException ioE) { + System.err.println("no log-File"); + } + if (isUndefinedPairSetSet(res)) { return new HashSet<>(); } + else return res; + } +/* +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 @Override protected Set> compute() { Set> res = unify(eq, fc, parallel); @@ -208,6 +302,7 @@ public class TypeUnifyTask extends RecursiveTask>> { } // Add the set of [a =. Theta | (a=. Theta) in Eq2'] + //TODO: Occurscheck anwenden als Fehler identifizieren Set bufferSet = eq2s.stream() .filter(x -> x.getPairOp() == PairOperator.EQUALSDOT && x.getLhsType() instanceof PlaceholderType) .collect(Collectors.toSet()); @@ -312,9 +407,21 @@ public class TypeUnifyTask extends RecursiveTask>> { /* * Step 5: Substitution */ +<<<<<<< HEAD //System.out.println("vor Subst: " + eqPrime); Optional> eqPrimePrime = rules.subst(eqPrime); +======= + //writeLog("vor Subst: " + eqPrime); + writeLog("vor Subst: " + oderConstraints); + String ocString = oderConstraints.toString(); + List>> newOderConstraints = new ArrayList<>(oderConstraints); + Optional> eqPrimePrime = rules.subst(eqPrime, newOderConstraints); + Set> unifyres1 = null; + Set> unifyres2 = null; + if (!ocString.equals(newOderConstraints.toString())) writeLog("nach Subst: " + newOderConstraints); + //writeLog("nach Subst: " + eqPrimePrime); +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 /* * Step 6 a) Restart (fork) for pairs where subst was applied */ @@ -343,22 +450,40 @@ public class TypeUnifyTask extends RecursiveTask>> { //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 { + //try { if (isSolvedForm(eqPrime)) { +<<<<<<< HEAD logFile.write(eqPrime.toString()+"\n"); logFile.flush(); } } catch (IOException e) { } +======= + writeLog("eqPrime:" + eqPrime.toString()+"\n"); + } + //} + //catch (IOException e) { + // System.err.println("log-File nicht vorhanden"); + //} +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 eqPrimePrimeSet.add(eqPrime); + urm.notify(eqPrimePrimeSet); } else if(eqPrimePrime.isPresent()) { +<<<<<<< HEAD Set> unifyres = unify(eqPrimePrime.get(), fc, false); +======= + Set> unifyres = unifyres1 = unify(eqPrimePrime.get(), newOderConstraints, fc, parallel, rekTiefe); +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 eqPrimePrimeSet.addAll(unifyres); } else { +<<<<<<< HEAD Set> unifyres = unify(eqPrime, fc, false); +======= + Set> unifyres = unifyres2 = unify(eqPrime, newOderConstraints, fc, parallel, rekTiefe); +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 eqPrimePrimeSet.addAll(unifyres); @@ -458,10 +583,68 @@ public class TypeUnifyTask extends RecursiveTask>> { if (variance == 1) { a = oup.max(nextSetasList.iterator()); nextSetasList.remove(a); +<<<<<<< HEAD +======= + nextSetasListRest = new ArrayList<>(nextSetasList); + Iterator> nextSetasListItRest = new ArrayList>(nextSetasListRest).iterator(); + while (nextSetasListItRest.hasNext()) { + Set a_next = nextSetasListItRest.next(); + if (//a.equals(a_next) || + (oup.compare(a, a_next) == 1)) { + nextSetasListRest.remove(a_next); + } + } + //Alle maximale Elemente in nextSetasListRest bestimmen + List> nextSetasListRestTest; + do { + nextSetasListRestTest = new ArrayList>(nextSetasListRest); + if (!nextSetasListRest.isEmpty()) { + Set max = oup.max(nextSetasListRest.iterator()); + Iterator> nextSetasListItRest2 = new ArrayList>(nextSetasListRest).iterator(); + while (nextSetasListItRest2.hasNext()) { + Set a_nextRest = nextSetasListItRest2.next(); + if (//a.equals(a_next) || + (oup.compare(max, a_nextRest) == 1)) { + nextSetasListRest.remove(a_nextRest); + } + + }} + } while(!nextSetasListRestTest.equals(nextSetasListRest)); + +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 } else if (variance == -1) { a = oup.min(nextSetasList.iterator()); nextSetasList.remove(a); +<<<<<<< HEAD +======= + nextSetasListRest = new ArrayList<>(nextSetasList); + Iterator> nextSetasListItRest = new ArrayList>(nextSetasListRest).iterator(); + while (nextSetasListItRest.hasNext()) { + Set a_next = nextSetasListItRest.next(); + if (//a.equals(a_next) || + (oup.compare(a, a_next) == -1)) { + nextSetasListRest.remove(a_next); + } + } + //Alle minimalen Elemente in nextSetasListRest bestimmen + + List> nextSetasListRestTest; + do { + nextSetasListRestTest = new ArrayList>(nextSetasListRest); + if (!nextSetasListRest.isEmpty()) { + Set min = oup.min(nextSetasListRest.iterator()); + Iterator> nextSetasListItRest2 = new ArrayList>(nextSetasListRest).iterator(); + while (nextSetasListItRest2.hasNext()) { + Set a_nextRest = nextSetasListItRest2.next(); + if (//a.equals(a_next) || + (oup.compare(min, a_nextRest) == -1)) { + nextSetasListRest.remove(a_nextRest); + } + + }} + } while(!nextSetasListRestTest.equals(nextSetasListRest)); +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 } else if (variance == 0) { a = nextSetasList.remove(0); @@ -492,10 +675,210 @@ public class TypeUnifyTask extends RecursiveTask>> { //for(Set a : newSet) { i++; Set> elems = new HashSet>(fstElems); +<<<<<<< HEAD elems.add(a); //if (remainingSets.isEmpty()) {//muss immer gegeben sein, weil nur 1 Element der topLevelSets mehr als ein Elemet enthaelt //writeLog("Vor unify2 Aufruf: " + eq.toString()); Set> res = unify2(elems, eq, fc, parallel); +======= + writeLog("a1: " + rekTiefe + " "+ "variance: "+ variance + " " + a.toString()+ "\n"); + //elems.add(a); PL 2019-01-20 Muss weg, weil das in jeweiligen Thread erfolgen muss. Fuer den sequentiellen Fall + //im else-Zweig + //if (remainingSets.isEmpty()) {//muss immer gegeben sein, weil nur 1 Element der topLevelSets mehr als ein Elemet enthaelt + //writeLog("Vor unify2 Aufruf: " + elems.toString()); + Set> res = new HashSet<>(); + Set>> add_res = new HashSet<>(); + Set> aParDef = new HashSet<>(); + if(parallel && (variance == 1) && noOfThread <= MaxNoOfThreads) { + Set forks = new HashSet<>(); + Set newEqOrig = new HashSet<>(eq); + Set> newElemsOrig = new HashSet<>(elems); + List>> newOderConstraintsOrig = new ArrayList<>(oderConstraints); + newElemsOrig.add(a); + + /* FORK ANFANG */ + TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm); + //forks.add(forkOrig); + forkOrig.fork(); + /* FORK ENDE */ + + synchronized (this) { + writeLog("a in " + variance + " "+ a); + writeLog("nextSetasListRest: " + nextSetasListRest.toString()); + } + while (!nextSetasListRest.isEmpty()) { + Set nSaL = nextSetasListRest.remove(0); + synchronized (this) { nextSetasList.remove(nSaL); + writeLog("1 RM" + nSaL.toString()); + } + Set newEq = new HashSet<>(eq); + Set> newElems = new HashSet<>(elems); + List>> newOderConstraints = new ArrayList<>(oderConstraints); + newElems.add(nSaL); + TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm); + forks.add(fork); + fork.fork(); + } + //res = unify2(newElemsOrig, newEqOrig, newOderConstraintsOrig, fc, parallel, rekTiefe); + + /* FORK ANFANG */ + synchronized (this) { + noOfThread--; + writeLog("wait "+ forkOrig.thNo); + res = forkOrig.join(); + forkOrig.writeLog("final Orig 1"); + forkOrig.closeLogFile(); + //Set> 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)); + for(TypeUnify2Task fork : forks) { + synchronized (this) { + Set> fork_res = fork.join(); + writeLog("Join " + new Integer(fork.thNo).toString()); + //noOfThread--; an das Ende von compute verschoben + writeLog("fork_res: " + fork_res.toString()); + writeLog(new Boolean((isUndefinedPairSetSet(fork_res))).toString()); + add_res.add(fork_res); + if (!isUndefinedPairSetSet(fork_res)) { + aParDef.add(fork.getNextSetElement()); + } + fork.writeLog("final 1"); + fork.closeLogFile(); + }; + } + noOfThread++; + } else { + if(parallel && (variance == -1) && noOfThread <= MaxNoOfThreads) { + Set forks = new HashSet<>(); + Set newEqOrig = new HashSet<>(eq); + Set> newElemsOrig = new HashSet<>(elems); + List>> newOderConstraintsOrig = new ArrayList<>(oderConstraints); + newElemsOrig.add(a); + + /* FORK ANFANG */ + TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm); + //forks.add(forkOrig); + forkOrig.fork(); + /* FORK ENDE */ + + synchronized (this) { + writeLog("a in " + variance + " "+ a); + writeLog("nextSetasListRest: " + nextSetasListRest.toString()); + } + while (!nextSetasListRest.isEmpty()) { + Set nSaL = nextSetasListRest.remove(0); + synchronized (this) { nextSetasList.remove(nSaL); + writeLog("-1 RM" + nSaL.toString()); + } + Set newEq = new HashSet<>(eq); + Set> newElems = new HashSet<>(elems); + List>> newOderConstraints = new ArrayList<>(oderConstraints); + newElems.add(nSaL); + TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm); + forks.add(fork); + fork.fork(); + } + //res = unify2(newElemsOrig, newEqOrig, newOderConstraintsOrig, fc, parallel, rekTiefe); + + /* FORK ANFANG */ + synchronized (this) { + noOfThread--; + writeLog("wait "+ forkOrig.thNo); + res = forkOrig.join(); + forkOrig.writeLog("final Orig -1"); + forkOrig.closeLogFile(); + //Set> 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)); + for(TypeUnify2Task fork : forks) { + synchronized (this) { + Set> fork_res = fork.join(); + writeLog("Join " + new Integer(fork.thNo).toString()); + //noOfThread--; an das Ende von compute verschoben + writeLog("fork_res: " + fork_res.toString()); + writeLog(new Boolean((isUndefinedPairSetSet(fork_res))).toString()); + add_res.add(fork_res); + if (!isUndefinedPairSetSet(fork_res)) { + aParDef.add(fork.getNextSetElement()); + } + fork.writeLog("final -1"); + fork.closeLogFile(); + }; + } + noOfThread++; + } else { + if(parallel && (variance == 2) && noOfThread <= MaxNoOfThreads) { + writeLog("var2einstieg"); + Set forks = new HashSet<>(); + Set newEqOrig = new HashSet<>(eq); + Set> newElemsOrig = new HashSet<>(elems); + List>> newOderConstraintsOrig = new ArrayList<>(oderConstraints); + newElemsOrig.add(a); + + /* FORK ANFANG */ + TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm); + //forks.add(forkOrig); + forkOrig.fork(); + /* FORK ENDE */ + + synchronized (this) { + writeLog("a in " + variance + " "+ a); + writeLog("nextSetasListRest: " + nextSetasListRest.toString()); + } + while (!nextSetasListRest.isEmpty()) { + Set nSaL = nextSetasListRest.remove(0); + //nextSetasList.remove(nSaL); + Set newEq = new HashSet<>(eq); + Set> newElems = new HashSet<>(elems); + List>> newOderConstraints = new ArrayList<>(oderConstraints); + newElems.add(nSaL); + TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm); + forks.add(fork); + fork.fork(); + } + //res = unify2(newElemsOrig, newEqOrig, newOderConstraintsOrig, fc, parallel, rekTiefe); + + /* FORK ANFANG */ + synchronized (this) { + noOfThread--; + writeLog("wait "+ forkOrig.thNo); + res = forkOrig.join(); + forkOrig.writeLog("final Orig 2"); + forkOrig.closeLogFile(); + //Set> fork_res = forkOrig.join(); + writeLog("JoinOrig " + new Integer(forkOrig.thNo).toString()); + //noOfThread--; an das Ende von compute verschoben + //add_res.add(fork_res); //vermutlich falsch + }; + /* FORK ENDE */ + forks.forEach(x -> writeLog("wait: " + x.thNo)); + for(TypeUnify2Task fork : forks) { + synchronized (this) { + Set> fork_res = fork.join(); + writeLog("Join " + new Integer(fork.thNo).toString()); + //noOfThread--; an das Ende von compute verschoben + add_res.add(fork_res); + fork.writeLog("final 2"); + fork.closeLogFile(); + }; + } + noOfThread++; + } else { + //parallel = false; //Wenn MaxNoOfThreads erreicht ist, sequentiell weiterarbeiten + elems.add(a); //PL 2019-01-16 muss das wirklich hin steht schon in Zeile 859 ja braucht man siehe Zeile 859 + res = unify2(elems, eq, oderConstraints, fc, parallel, rekTiefe); + }}} +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 if (!isUndefinedPairSetSet(res) && isUndefinedPairSetSet(result)) { //wenn korrektes Ergebnis gefunden alle Fehlerfaelle loeschen result = res; @@ -569,12 +952,13 @@ public class TypeUnifyTask extends RecursiveTask>> { //} /* auskommentiert um alle Max und min Betrachtung auszuschalten ANFANG */ - if (!result.isEmpty() && !isUndefinedPairSetSet(res)) { + if (!result.isEmpty() && (!isUndefinedPairSetSet(res) || !aParDef.isEmpty())) { if (nextSetasList.iterator().hasNext() && nextSetasList.iterator().next().stream().filter(x -> x.getLhsType().getName().equals("B")).findFirst().isPresent() && nextSetasList.size()>1) System.out.print(""); Iterator> nextSetasListIt = new ArrayList>(nextSetasList).iterator(); if (variance == 1) { System.out.println(""); +<<<<<<< HEAD while (nextSetasListIt.hasNext()) { Set a_next = nextSetasListIt.next(); if (a.equals(a_next) || @@ -583,11 +967,31 @@ public class TypeUnifyTask extends RecursiveTask>> { } else { System.out.println(""); +======= + writeLog("a: " + rekTiefe + " variance: " + variance + a.toString()); + writeLog("aParDef: " + aParDef.toString()); + aParDef.add(a); + Iterator> aParDefIt = aParDef.iterator(); + while(aParDefIt.hasNext()) { + Set a_new = aParDefIt.next(); + while (nextSetasListIt.hasNext()) { + Set a_next = nextSetasListIt.next(); + if (a_new.equals(a_next) || + (oup.compare(a_new, a_next) == 1)) { + writeLog("Removed: " + a_next.toString()); + nextSetasList.remove(a_next); + } + else { + writeLog("Not Removed: " + a_next.toString()); + System.out.println(""); + } +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 } - } + } } else { if (variance == -1) { System.out.println(""); +<<<<<<< HEAD while (nextSetasListIt.hasNext()) { Set a_next = nextSetasListIt.next(); if (a.equals(a_next) || @@ -602,11 +1006,62 @@ public class TypeUnifyTask extends RecursiveTask>> { else if (variance == 0) { break; } +======= + writeLog("a: " + rekTiefe + " variance: " + variance + a.toString()); + writeLog("aParDef: " + aParDef.toString()); + aParDef.add(a); + Iterator> aParDefIt = aParDef.iterator(); + while(aParDefIt.hasNext()) { + Set a_new = aParDefIt.next(); + while (nextSetasListIt.hasNext()) { + Set a_next = nextSetasListIt.next(); + if (a_new.equals(a_next) || + (oup.compare(a_new, a_next) == -1)) { + writeLog("Removed: " + a_next.toString()); + nextSetasList.remove(a_next); //PL geaendert 2019-01-09 + } + else { + System.out.println(""); + writeLog("Not Removed: " + a_next.toString()); + } + } + } + } + else { if (variance == 0) { + writeLog("a: " + rekTiefe + " variance: " + variance + a.toString()); + break; } + else { if (variance == 2) { + /* vorgezogen vor das if + if (parallel) { + for (Set> par_res : add_res) { + if (!isUndefinedPairSetSet(par_res) && isUndefinedPairSetSet(result)) { + //wenn korrektes Ergebnis gefunden alle Fehlerfaelle loeschen + result = par_res; + if (par_res.iterator().next() instanceof WildcardType) { + System.out.println(""); + } + } + else { + if ((isUndefinedPairSetSet(par_res) && isUndefinedPairSetSet(result)) + || (!isUndefinedPairSetSet(par_res) && !isUndefinedPairSetSet(result)) + || result.isEmpty()) { + //alle Fehlerfaelle und alle korrekten Ergebnis jeweils adden + writeLog("RES var2 ADD:" + result.toString() + " " + par_res.toString()); + result.addAll(par_res); + } + } + } + //break; + } + */ + }}} + writeLog("a: " + rekTiefe + " variance: " + variance + a.toString()); +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 } } /* auskommentiert um alle Max und min Betrachtung auszuschalten ENDE */ - if (isUndefinedPairSetSet(res)) { + if (isUndefinedPairSetSet(res) && aParDef.isEmpty()) { int nofstred= 0; Set abhSubst = res.stream() .map(b -> @@ -1112,7 +1567,17 @@ public class TypeUnifyTask extends RecursiveTask>> { allGen = false; break; } +<<<<<<< HEAD +======= + //if (thetaPrime.getName().equals("java.util.Vector") //Fuer Bug 127 + // && thetaPrime instanceof ReferenceType + // && ((ReferenceType)thetaPrime).getTypeParams().iterator().next() instanceof PlaceholderType) //.getName().equals("java.util.Vector")) + // && ((ReferenceType)((ReferenceType)thetaPrime).getTypeParams().iterator().next()).getTypeParams().iterator().next().getName().equals("java.lang.Integer")) { + // { + // System.out.println(""); + //} +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 Set cs = fc.getAllTypesByName(thetaPrime.getName());//cs= [java.util.Vector, java.util.Vector>, ????java.util.Vector???] @@ -1133,13 +1598,17 @@ public class TypeUnifyTask extends RecursiveTask>> { for(UnifyType c : csPHRenamed) { //PL 18-02-05 getChildren durch smaller ersetzt in getChildren werden die Varianlen nicht ersetzt. - Set thetaQs = fc.smaller(c, new HashSet<>()).stream().collect(Collectors.toCollection(HashSet::new)); + Set thetaQs = new HashSet<>(); + //TODO smaller wieder reinnehmen? + //thetaQs.add(c);// + thetaQs = fc.smaller(c, new HashSet<>()).stream().collect(Collectors.toCollection(HashSet::new)); //Set thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new)); //thetaQs.add(thetaPrime); //PL 18-02-05 wieder geloescht //PL 2017-10-03: War auskommentiert habe ich wieder einkommentiert, //da children offensichtlich ein echtes kleiner und kein kleinergleich ist //PL 18-02-06: eingefuegt, thetaQs der Form V> <. V'> werden entfernt + //TODO PL 19-01-14 wieder reinnehmen kurzfristig auskommentiert thetaQs = thetaQs.stream().filter(ut -> ut.getTypeParams().arePlaceholders()).collect(Collectors.toCollection(HashSet::new)); //PL 18-02-06: eingefuegt @@ -1201,13 +1670,16 @@ public class TypeUnifyTask extends RecursiveTask>> { if(allGen) { UnifyPair up = new UnifyPair(a, theta, PairOperator.EQUALSDOT, pair.getSubstitution(), pair); Iterator upit = up.getRhsType().getTypeParams().iterator(); - while (upit.hasNext()) ((PlaceholderType)upit.next()).setVariance(a.getVariance()); + //TODO PL 2019-01-24: upit.next() ist nicht unbedingt ein PlaceholderType -> Visitor erledigt + while (upit.hasNext()) upit.next().accept(new distributeVariance(), a.getVariance());//((PlaceholderType)upit.next()).setVariance(a.getVariance()); resultPrime.add(up); } else { UnifyPair up = new UnifyPair(a, theta.setTypeParams(new TypeParams(freshTphs.toArray(new UnifyType[0]))), PairOperator.EQUALSDOT, pair.getSubstitution(), pair); Iterator upit = up.getRhsType().getTypeParams().iterator(); - while (upit.hasNext()) ((PlaceholderType)upit.next()).setVariance(a.getVariance()); + distributeVariance dv = new distributeVariance(); + //TODO PL 2019-01-24: upit.next() ist nicht unbedingt ein PlaceholderType -> Visitor erledigt + while (upit.hasNext()) upit.next().accept(new distributeVariance(), a.getVariance()); //((PlaceholderType)upit.next()).setVariance(a.getVariance()); resultPrime.add(up); } resultPrime.addAll(substitutionSet); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultEvent.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultEvent.java new file mode 100644 index 00000000..a79e34ec --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultEvent.java @@ -0,0 +1,18 @@ +package de.dhbwstuttgart.typeinference.unify; + +import java.util.List; + +import de.dhbwstuttgart.typeinference.result.ResultSet; + +public class UnifyResultEvent { + + private List newTypeResult; + + public UnifyResultEvent(List newTypeResult) { + this.newTypeResult = newTypeResult; + } + + public List getNewTypeResult() { + return newTypeResult; + } +} diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListenerImpl.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListenerImpl.java new file mode 100644 index 00000000..ea34728b --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListenerImpl.java @@ -0,0 +1,21 @@ +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 results = new ArrayList<>(); + + public synchronized void onNewTypeResultFound(UnifyResultEvent evt) { + results.addAll(evt.getNewTypeResult()); + } + + public List getResults() { + return results; + } + +} diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModel.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModel.java new file mode 100644 index 00000000..7a4f0ccf --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModel.java @@ -0,0 +1,59 @@ +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 cons; + + IFiniteClosure fc; + + public UnifyResultModel(ConstraintSet cons, + IFiniteClosure fc) { + this.cons = cons; + this.fc = fc; + } + + private List listeners = new ArrayList<>(); + + public void addUnifyResultListener(UnifyResultListener listenerToAdd) { + listeners.add(listenerToAdd); + } + + public void removeUnifyResultListener(UnifyResultListener listenerToRemove) { + listeners.remove(listenerToRemove); + } + + public void notify(Set> eqPrimePrimeSet) { + Set> eqPrimePrimeSetRet = eqPrimePrimeSet.stream().map(x -> { + Optional> 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 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); + } + } +} diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java index a2b647c6..1ed46cb0 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.typeinference.unify.interfaces; +import java.util.List; import java.util.Optional; import java.util.Set; @@ -85,6 +86,13 @@ public interface IRuleSet { public Optional adaptExt(UnifyPair pair, IFiniteClosure fc); public Optional adaptSup(UnifyPair pair, IFiniteClosure fc); + /** + * Applies the subst-Rule to a set of pairs (usually Eq'). + * @param pairs The set of pairs where the subst rule should apply. + * @return An optional of the modified set, if there were any substitutions. An empty optional if there were no substitutions. + */ + public Optional> subst(Set pairs, List>> oderConstraints); + /** * Applies the subst-Rule to a set of pairs (usually Eq'). * @param pairs The set of pairs where the subst rule should apply. diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index 4aa769e5..87c1e821 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -10,6 +10,7 @@ import java.util.Optional; import java.util.Set; import java.util.function.BiFunction; import java.util.function.BinaryOperator; +import java.util.function.Predicate; import java.util.stream.Collectors; import com.google.common.collect.Ordering; @@ -91,6 +92,18 @@ implements IFiniteClosure { strInheritanceGraph.get(key.getName()).add(inheritanceGraph.get(key)); } } + + void testSmaller() { + UnifyType tq1, tq2, tq3; + tq1 = new ExtendsType(PlaceholderType.freshPlaceholder()); + List l1 = new ArrayList<>(); + List l2 = new ArrayList<>(); + l1.add(tq1); + tq2 = new ReferenceType("java.util.Vector", new TypeParams(l1)); + l2.add(tq2); + tq3 = new ReferenceType("java.util.Vector", new TypeParams(l2)); + Set smaller = smaller(tq3, new HashSet<>()); + } /** * Returns all types of the finite closure that are subtypes of the argument. @@ -570,15 +583,19 @@ implements IFiniteClosure { */ public int compare (UnifyType left, UnifyType right, PairOperator pairop) { - if ((left instanceof ExtendsType && right instanceof ReferenceType) - || (right instanceof ExtendsType && left instanceof ReferenceType)) + if (left.getName().equals("Matrix") || right.getName().equals("Matrix")) System.out.println(""); /* + pairop = PairOperator.SMALLERDOTWC; List al = new ArrayList<>(); PlaceholderType xx =new PlaceholderType("xx"); al.add(xx); left = new ExtendsType(new ReferenceType("Vector", new TypeParams(al))); - right = new ReferenceType("Vector", new TypeParams(new ArrayList<>(al))); + + List alr = new ArrayList<>(); + UnifyType exx = new ExtendsType(xx); + alr.add(exx); + right = new ExtendsType(new ReferenceType("Vector", new TypeParams(alr))); */ /* List al = new ArrayList<>(); @@ -615,8 +632,20 @@ implements IFiniteClosure { HashSet hs = new HashSet<>(); hs.add(up); Set smallerRes = unifyTask.applyTypeUnificationRules(hs, this); - //Gleichungen der Form a <./=. Theta oder Theta <./=. a oder a <./=. b sind ok. - long smallerLen = smallerRes.stream().filter(x -> !(x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType)).count(); + if (left.getName().equals("Matrix") || right.getName().equals("Matrix")) + {try { + logFile.write("\nsmallerRes: " + smallerRes);//"smallerHash: " + greaterHash.toString()); + logFile.flush(); + } + catch (IOException e) { + System.err.println("no LogFile");}} + //Gleichungen der Form a <./=. Theta oder Theta <./=. a oder a <./=. b sind ok. + Predicate delFun = x -> !((x.getLhsType() instanceof PlaceholderType || + x.getRhsType() instanceof PlaceholderType) + && !((x.getLhsType() instanceof WildcardType) && //? extends/super a <.? a + ((WildcardType)x.getLhsType()).getWildcardedType().equals(x.getRhsType())) + ); + long smallerLen = smallerRes.stream().filter(delFun).count(); if (smallerLen == 0) return -1; else { up = new UnifyPair(right, left, pairop); @@ -624,8 +653,15 @@ implements IFiniteClosure { hs = new HashSet<>(); hs.add(up); Set greaterRes = unifyTask.applyTypeUnificationRules(hs, this); + if (left.getName().equals("Matrix") || right.getName().equals("Matrix")) + {try { + logFile.write("\ngreaterRes: " + greaterRes);//"smallerHash: " + greaterHash.toString()); + logFile.flush(); + } + catch (IOException e) { + System.err.println("no LogFile");}} //Gleichungen der Form a <./=. Theta oder Theta <./=. a oder a <./=. b sind ok. - long greaterLen = greaterRes.stream().filter(x -> !(x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType)).count(); + long greaterLen = greaterRes.stream().filter(delFun).count(); if (greaterLen == 0) return 1; else return 0; } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java index c655e6fd..29b1c59c 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; +import java.util.List; import java.util.Optional; import java.util.Set; import java.util.function.BinaryOperator; @@ -87,6 +88,56 @@ public class OrderingUnifyPair extends Ordering> { * @see com.google.common.collect.Ordering#compare(java.lang.Object, java.lang.Object) */ public int compare (Set left, Set right) { +<<<<<<< HEAD +======= + /* + //pairop = PairOperator.SMALLERDOTWC; + List al = new ArrayList<>(); + PlaceholderType xx = PlaceholderType.freshPlaceholder(); + al.add(xx); + UnifyType t1 = new ExtendsType(new ReferenceType("Vector", new TypeParams(al))); + + //PlaceholderType yy =new PlaceholderType("yy"); + List alr = new ArrayList<>(); + UnifyType exx = new ExtendsType(xx); + alr.add(exx); + UnifyType t2 = new ExtendsType(new ReferenceType("Vector", new TypeParams(alr))); + + PlaceholderType a = PlaceholderType.freshPlaceholder(); + a.setInnerType(true); + UnifyPair p1 = new UnifyPair(a, t1, PairOperator.SMALLERDOTWC); + PlaceholderType b = PlaceholderType.freshPlaceholder(); + b.setInnerType(true); + UnifyPair p2 = new UnifyPair(b, t2, PairOperator.SMALLERDOTWC); + + List al3 = new ArrayList<>(); + al3.add(a); + + List al4 = new ArrayList<>(); + al4.add(b); + + UnifyPair p3 = new UnifyPair(new PlaceholderType("c"), new ReferenceType("Vector", new TypeParams(al3)), PairOperator.EQUALSDOT); + UnifyPair p4 = new UnifyPair(new PlaceholderType("c"), new ReferenceType("Vector", new TypeParams(al4)), PairOperator.EQUALSDOT); + + right = new HashSet<>(); + right.add(p1); + right.add(p3); + left = new HashSet<>(); + left.add(p2); + left.add(p4); + */ + + if ((left.size() == 1) && right.size() == 1) { + if (left.iterator().next().getLhsType().getName().equals("AFS")) { + System.out.println(""); + } + if (((right.iterator().next().getRhsType() instanceof SuperType) && (((SuperType)right.iterator().next().getRhsType()).getSuperedType().getName().equals("java.lang.Object"))) + ||((left.iterator().next().getRhsType() instanceof SuperType) && (((SuperType)left.iterator().next().getRhsType()).getSuperedType().getName().equals("java.lang.Object")))) + { + System.out.println(""); + } + } +>>>>>>> 72705ac8689aa3d283d170e673ca6663b9676ce8 Set lefteq = left.stream() .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)) .collect(Collectors.toCollection(HashSet::new)); @@ -152,9 +203,9 @@ public class OrderingUnifyPair extends Ordering> { //Set varsleft = lefteq.stream().map(x -> (PlaceholderType)x.getLhsType()).collect(Collectors.toCollection(HashSet::new)); //Set varsright = righteq.stream().map(x -> (PlaceholderType)x.getLhsType()).collect(Collectors.toCollection(HashSet::new)); //filtern des Paares a = Theta, das durch a <. Thata' generiert wurde (nur im Fall 1 relevant) andere Substitutioen werden rausgefiltert - lefteq.removeIf(x -> !(x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName()) + lefteq.removeIf(x -> (x.getBasePair()!=null) && !(x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName()) ||x.getLhsType().getName().equals(x.getBasePair().getRhsType().getName())));//removeIf(x -> !varsright.contains(x.getLhsType())); - righteq.removeIf(x -> !(x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName()) + righteq.removeIf(x -> (x.getBasePair()!=null) && !(x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName()) ||x.getLhsType().getName().equals(x.getBasePair().getRhsType().getName())));//.removeIf(x -> !varsleft.contains(x.getLhsType())); UnifyPair lseq = lefteq.iterator().next(); UnifyPair rseq = righteq.iterator().next(); @@ -183,7 +234,7 @@ public class OrderingUnifyPair extends Ordering> { if (leftlewc.iterator().next().getLhsType() instanceof PlaceholderType) { hm = rsleuni.stream().reduce(new HashMap(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner); Stream lslewcstr = lsleuni.stream().filter(x -> !(hm.get(x.getLhsType()) == null)); - si = lslewcstr.map(x -> fc.compare(x.getRhsType(), hm.get(x.getLhsType()).getRhsType(), PairOperator.SMALLERDOTWC)).reduce((x,y)-> { if (x == y) return x; else return 0; } ); + si = lslewcstr.map(x -> fc.compare(x.getRhsType(), hm.get(x.getLhsType()).getRhsType(), PairOperator.SMALLERDOTWC)).reduce((x,y)-> { if (x == y) return x; else return 0; } ); } //4. Fall else {