From 2f9d44d0b0a12865c11470cdbaa4f6e64acf0467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Wed, 12 Dec 2018 16:49:16 +0100 Subject: [PATCH 1/5] modified: ../../src/de/dhbwstuttgart/core/JavaTXCompiler.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java im Max den Fall a <.? ? extends a beruecksichtigt modified: ../../src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java ; in der Schleife generate fresh Type Var entfernt --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 ++ .../typeinference/unify/TypeUnifyTask.java | 2 +- .../unify/model/FiniteClosure.java | 20 ++++++++++++++++++- .../unify/model/OrderingUnifyPair.java | 2 +- .../unify/model/PlaceholderType.java | 9 ++++++--- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 08614f40..f7d5ad1f 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -225,6 +225,8 @@ public class JavaTXCompiler { 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) { } return results.stream().map((unifyPairs -> diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 7864ece1..7a61eab2 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -1144,7 +1144,7 @@ public class TypeUnifyTask extends RecursiveTask>> { Set resultPrime = new HashSet<>(); for(int i = 0; !allGen && i < theta.getTypeParams().size(); i++) { - if(freshTphs.size()-1 < i) + //if(freshTphs.size()-1 < i) freshTphs.add(PlaceholderType.freshPlaceholder()); resultPrime.add(new UnifyPair(freshTphs.get(i), theta.getTypeParams().get(i), PairOperator.SMALLERDOTWC, pair.getSubstitution(), pair)); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index 5eef0308..ee206fb7 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -26,7 +26,8 @@ import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; * The finite closure for the type unification * @author Florian Steurer */ -public class FiniteClosure extends Ordering implements IFiniteClosure { +public class FiniteClosure //extends Ordering //entfernt PL 2018-12-11 +implements IFiniteClosure { /** * A map that maps every type to the node in the inheritance graph that contains that type. @@ -561,14 +562,31 @@ public class FiniteClosure extends Ordering implements IFiniteClosure return this.inheritanceGraph.toString(); } + /* entfernt PL 2018-12-11 public int compare (UnifyType left, UnifyType right) { return compare(left, right, PairOperator.SMALLERDOT); } + */ public int compare (UnifyType left, UnifyType right, PairOperator pairop) { if ((left instanceof ExtendsType && right instanceof ReferenceType) || (right instanceof ExtendsType && left instanceof ReferenceType)) System.out.println(""); + //Die Faelle abfangen, bei den Variablen verglichen werden PL 2018-12-11 + UnifyType ex; + if (left instanceof PlaceholderType) { + if ((right instanceof WildcardType) + && ((ex = ((WildcardType)right).wildcardedType) instanceof PlaceholderType) + && ((PlaceholderType)left).getName().equals(((PlaceholderType)ex).getName())) {// a <.? ? extends a oder a <.? ? super a + return -1; + } + else {// a <. a + return 0; + } + } + if ((right instanceof PlaceholderType) && (left instanceof WildcardType)) { + return 0; + } UnifyPair up = new UnifyPair(left, right, pairop); TypeUnifyTask unifyTask = new TypeUnifyTask(); HashSet hs = new HashSet<>(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java index 8cebdd8a..c655e6fd 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -151,7 +151,7 @@ public class OrderingUnifyPair extends Ordering> { System.out.print(""); //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) + //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()) ||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()) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index ef41de65..01209998 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -1,5 +1,7 @@ package de.dhbwstuttgart.typeinference.unify.model; +import java.io.File; +import java.io.FileWriter; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -21,7 +23,7 @@ public final class PlaceholderType extends UnifyType{ * Static list containing the names of all existing placeholders. * Used for generating fresh placeholders. */ - protected static final HashSet EXISTING_PLACEHOLDERS = new HashSet(); + public static final ArrayList EXISTING_PLACEHOLDERS = new ArrayList(); /** * Prefix of auto-generated placeholder names. @@ -81,11 +83,12 @@ public final class PlaceholderType extends UnifyType{ * A user could later instantiate a type using the same name that is equivalent to this type. * @return A fresh placeholder type. */ - public static PlaceholderType freshPlaceholder() { + public synchronized static PlaceholderType freshPlaceholder() { String name = nextName + (char) (rnd.nextInt(22) + 97); // Returns random char between 'a' and 'z' // Add random chars while the name is in use. - while(EXISTING_PLACEHOLDERS.contains(name)); + while(EXISTING_PLACEHOLDERS.contains(name)) { name += (char) (rnd.nextInt(22) + 97); // Returns random char between 'a' and 'z' + } return new PlaceholderType(name, true); } From 9dbfbe9e6a10b862718640e23dcc9f8438f09273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Fri, 14 Dec 2018 09:31:58 +0100 Subject: [PATCH 2/5] modified: ../src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java modified: ../src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java --- .../typeinference/unify/TypeUnifyTask.java | 2 +- .../unify/model/FiniteClosure.java | 25 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 7a61eab2..fc8300c8 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -1144,7 +1144,7 @@ public class TypeUnifyTask extends RecursiveTask>> { Set resultPrime = new HashSet<>(); for(int i = 0; !allGen && i < theta.getTypeParams().size(); i++) { - //if(freshTphs.size()-1 < i) + if(freshTphs.size()-1 < i)//IST DAS RICHTIG??? PL 2018-12-12 freshTphs.add(PlaceholderType.freshPlaceholder()); resultPrime.add(new UnifyPair(freshTphs.get(i), theta.getTypeParams().get(i), PairOperator.SMALLERDOTWC, pair.getSubstitution(), pair)); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index ee206fb7..4aa769e5 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; +import java.util.List; import java.util.Optional; import java.util.Set; import java.util.function.BiFunction; @@ -572,6 +573,28 @@ implements IFiniteClosure { if ((left instanceof ExtendsType && right instanceof ReferenceType) || (right instanceof ExtendsType && left instanceof ReferenceType)) System.out.println(""); + /* + 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 al = new ArrayList<>(); + PlaceholderType xx =new PlaceholderType("xx"); + al.add(xx); + left = new ExtendsType(xx); + right = xx; + */ + /* + List al = new ArrayList<>(); + PlaceholderType xx =new PlaceholderType("xx"); + PlaceholderType yy =new PlaceholderType("yy"); + al.add(xx); + left = yy; + right = new ExtendsType(xx); + */ //Die Faelle abfangen, bei den Variablen verglichen werden PL 2018-12-11 UnifyType ex; if (left instanceof PlaceholderType) { @@ -580,7 +603,7 @@ implements IFiniteClosure { && ((PlaceholderType)left).getName().equals(((PlaceholderType)ex).getName())) {// a <.? ? extends a oder a <.? ? super a return -1; } - else {// a <. a + else { return 0; } } From a733f373a71abe91f451623d816863a8270211f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Fri, 14 Dec 2018 12:09:12 +0100 Subject: [PATCH 3/5] modified: ../../../../src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java --- src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index fc8300c8..50b268b6 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -491,6 +491,7 @@ public class TypeUnifyTask extends RecursiveTask>> { //for(Set a : newSet) { i++; Set> elems = new HashSet>(fstElems); + Set a_last = a; 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()); @@ -504,6 +505,8 @@ public class TypeUnifyTask extends RecursiveTask>> { || (!isUndefinedPairSetSet(res) && !isUndefinedPairSetSet(result)) || result.isEmpty()) { //alle Fehlerfaelle und alle korrekten Ergebnis jeweils adden + a_last.stream().filter(x -> !(x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName()) + ||x.getLhsType().getName().equals(x.getBasePair().getRhsType().getName()))); result.addAll(res); } //else { @@ -523,7 +526,7 @@ public class TypeUnifyTask extends RecursiveTask>> { if (!result.isEmpty() && !isUndefinedPairSetSet(res)) { 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(); + Iterator> nextSetasListIt = new ArrayList>(nextSetasList).iterator(); if (variance == 1) { System.out.println(""); while (nextSetasListIt.hasNext()) { From 1931c613925079e97cb8ef57ff20b668d77c543b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Fri, 14 Dec 2018 17:46:09 +0100 Subject: [PATCH 4/5] modified: src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java Loesung Fehler rausgemacht --- .../typeinference/unify/TypeUnifyTask.java | 58 +++++++++++++++++-- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 50b268b6..801cae50 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -452,8 +452,9 @@ public class TypeUnifyTask extends RecursiveTask>> { 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 a = null; while (nextSetasList.size() > 0) { //(nextSetasList.size() != 0) { - Set a = null; + Set a_last = a; if (variance == 1) { a = oup.max(nextSetasList.iterator()); nextSetasList.remove(a); @@ -491,7 +492,6 @@ public class TypeUnifyTask extends RecursiveTask>> { //for(Set a : newSet) { i++; Set> elems = new HashSet>(fstElems); - Set a_last = a; 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()); @@ -504,11 +504,57 @@ public class TypeUnifyTask extends RecursiveTask>> { if ((isUndefinedPairSetSet(res) && isUndefinedPairSetSet(result)) || (!isUndefinedPairSetSet(res) && !isUndefinedPairSetSet(result)) || result.isEmpty()) { + + if (!result.isEmpty() && !res.isEmpty() && !isUndefinedPairSetSet(res) && !isUndefinedPairSetSet(result)) { + + //Alle Variablen bestimmen die nicht hinzugefügt wurden in a + List vars_a = a.stream().filter(x -> (x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName()) + ||x.getLhsType().getName().equals(x.getBasePair().getRhsType().getName()))).map(y -> (PlaceholderType)y.getLhsType()).collect(Collectors.toCollection(ArrayList::new)); + Set fstElemRes = res.iterator().next(); + Set compRes = fstElemRes.stream().filter(x -> vars_a.contains(((PlaceholderType)x.getLhsType()))).collect(Collectors.toCollection(HashSet::new)); + + //Alle Variablen bestimmen die nicht hinzugefügt wurden in a_last + List varsLast_a = a_last.stream().filter(x -> (x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName()) + ||x.getLhsType().getName().equals(x.getBasePair().getRhsType().getName()))).map(y -> (PlaceholderType)y.getLhsType()).collect(Collectors.toCollection(ArrayList::new)); + //erstes Element genügt, da vars immer auf die gleichen Elemente zugeordnet werden muessen + Set fstElemResult = result.iterator().next(); + Set compResult = fstElemResult.stream().filter(x -> varsLast_a.contains(((PlaceholderType)x.getLhsType()))).collect(Collectors.toCollection(HashSet::new));; + + if (variance == 1) { + int resOfCompare = oup.compare(compResult, compRes); + if (resOfCompare == -1) { + writeLog("Geloescht result: " + result); + result = res; + } else { + if (resOfCompare == 0) { + result.addAll(res); + } //else { + if (resOfCompare == 1) { + writeLog("Geloescht res: " + res); + //result = result; + }}} + else { if (variance == -1) { + int resOfCompare = oup.compare(compResult, compRes); + if (resOfCompare == 1) { + writeLog("Geloescht result: " + result); + result = res; + } else { + if (resOfCompare == 0) { + result.addAll(res); + } else { + if (resOfCompare == -1) { + writeLog("Geloescht res: " + res); + //result = result; + }}}} + else { if (variance == 0) { + result.addAll(res); + }}} //alle Fehlerfaelle und alle korrekten Ergebnis jeweils adden - a_last.stream().filter(x -> !(x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName()) - ||x.getLhsType().getName().equals(x.getBasePair().getRhsType().getName()))); - result.addAll(res); - } + } + else { + result.addAll(res); + } + } //else { //wenn Korrekte Ergebnisse da und Feherfälle dazukommen Fehlerfälle ignorieren // if (isUndefinedPairSetSet(res) && !isUndefinedPairSetSet(result)) { From f2567142233378af8b60a723c632ff0dea34cfc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Tue, 18 Dec 2018 16:18:36 +0100 Subject: [PATCH 5/5] modified: ../../src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java Returntype der Binary korrigiert. --- .../dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 609600cd..2f705c67 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -254,7 +254,7 @@ public class TYPEStmt implements StatementVisitor{ numeric = new Constraint<>(); numeric.add(new Pair(binary.lexpr.getType(), integer, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.rexpr.getType(), integer, PairOperator.SMALLERDOT)); - numeric.add(new Pair(binary.getType(), integer, PairOperator.SMALLERDOT)); + numeric.add(new Pair(integer, binary.getType(), PairOperator.SMALLERDOT)); numericAdditionOrStringConcatenation.add(numeric); } //PL eingefuegt 2018-07-17 @@ -262,7 +262,7 @@ public class TYPEStmt implements StatementVisitor{ numeric = new Constraint<>(); numeric.add(new Pair(binary.lexpr.getType(), longg, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.rexpr.getType(), longg, PairOperator.SMALLERDOT)); - numeric.add(new Pair(binary.getType(), longg, PairOperator.SMALLERDOT)); + numeric.add(new Pair(longg, binary.getType(), PairOperator.SMALLERDOT)); numericAdditionOrStringConcatenation.add(numeric); } //PL eingefuegt 2018-07-17 @@ -270,7 +270,7 @@ public class TYPEStmt implements StatementVisitor{ numeric = new Constraint<>(); numeric.add(new Pair(binary.lexpr.getType(), floatt, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.rexpr.getType(), floatt, PairOperator.SMALLERDOT)); - numeric.add(new Pair(binary.getType(), floatt, PairOperator.SMALLERDOT)); + numeric.add(new Pair(floatt, binary.getType(), PairOperator.SMALLERDOT)); numericAdditionOrStringConcatenation.add(numeric); } //PL eingefuegt 2018-07-17 @@ -278,7 +278,7 @@ public class TYPEStmt implements StatementVisitor{ numeric = new Constraint<>(); numeric.add(new Pair(binary.lexpr.getType(), doublee, PairOperator.SMALLERDOT)); numeric.add(new Pair(binary.rexpr.getType(), doublee, PairOperator.SMALLERDOT)); - numeric.add(new Pair(binary.getType(), doublee, PairOperator.SMALLERDOT)); + numeric.add(new Pair(doublee, binary.getType(), PairOperator.SMALLERDOT)); numericAdditionOrStringConcatenation.add(numeric); } /* PL auskommentiert Anfang 2018-07-17 @@ -298,7 +298,7 @@ public class TYPEStmt implements StatementVisitor{ Constraint stringConcat = new Constraint<>(); stringConcat.add(new Pair(binary.lexpr.getType(), string, PairOperator.EQUALSDOT)); stringConcat.add(new Pair(binary.rexpr.getType(), string, PairOperator.EQUALSDOT)); - stringConcat.add(new Pair(binary.getType(), string, PairOperator.EQUALSDOT)); + stringConcat.add(new Pair(string, binary.getType(), PairOperator.EQUALSDOT)); numericAdditionOrStringConcatenation.add(stringConcat); } }