From e00d76ce3b1cdfdf12997e05b726a35c1fcd4400 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Wed, 13 Jan 2021 19:39:01 +0100 Subject: [PATCH] Occurs-Check mit Abbruch eingebaut finalResult zum Attribut der Klasse gemacht --- .../typeinference/unify/RuleSet.java | 2 +- .../typeinference/unify/TypeUnify2Task.java | 2 +- .../typeinference/unify/TypeUnifyTask.java | 59 ++++++++++++++----- src/test/java/AllgemeinTest.java | 3 +- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 93f33d08..50ea9a2e 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -655,7 +655,6 @@ public class RuleSet implements IRuleSet{ else t1.getTypeParams().forEach(x -> occuringTypes.push(x)); } - Queue result1 = new LinkedList(pairs); ArrayList result = new ArrayList(); boolean applied = false; @@ -669,6 +668,7 @@ public class RuleSet implements IRuleSet{ && pair.getLhsType() instanceof PlaceholderType) lhsType = (PlaceholderType) pair.getLhsType(); rhsType = pair.getRhsType(); //PL eingefuegt 2017-09-29 statt !((rhsType = pair.getRhsType()) instanceof PlaceholderType) + 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. diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java index 798c6f05..317f3660 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java @@ -34,7 +34,7 @@ public class TypeUnify2Task extends TypeUnifyTask { System.out.println("two"); } one = true; - Set> res = unify2(setToFlatten, eq, oderConstraintsField, fc, parallel, rekTiefeField, true); + Set> res = unify2(setToFlatten, eq, oderConstraintsField, fc, parallel, rekTiefeField); /*if (isUndefinedPairSetSet(res)) { return new HashSet<>(); } else diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 66ceb6d0..580fee5c 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -110,6 +110,9 @@ public class TypeUnifyTask extends RecursiveTask>> { protected boolean parallel; + //Gives if unify is not called from checkA + private boolean finalresult = true; + int rekTiefeField; Integer nOfUnify = 0; @@ -257,7 +260,7 @@ public class TypeUnifyTask extends RecursiveTask>> { ArrayList>> remainingOderconstraints = oderConstraintsField.stream() .filter(x -> x.size()>1) .collect(Collectors.toCollection(ArrayList::new)); - Set> res = unify(neweq, remainingOderconstraints, fc, parallel, rekTiefeField, true); + Set> res = unify(neweq, remainingOderconstraints, fc, parallel, rekTiefeField); noOfThread--; try { logFile.close(); @@ -299,7 +302,7 @@ public class TypeUnifyTask extends RecursiveTask>> { * @param fc The finite closure * @return The set of all principal type unifiers */ - protected Set> unify(final Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe, Boolean finalresult) { + protected Set> unify(final Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe) { //Set 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()); @@ -308,9 +311,7 @@ public class TypeUnifyTask extends RecursiveTask>> { //} //.collect(Collectors.toCollection(HashSet::new))); - /* - * Step 1: Repeated application of reduce, adapt, erase, swap - */ + synchronized (usedTasks) { if (this.myIsCancelled()) { return new HashSet<>(); @@ -339,6 +340,29 @@ public class TypeUnifyTask extends RecursiveTask>> { return ret; } + /* + * Occurs-Check durchfuehren + */ + + Set ocurrPairs = eq.stream().filter(x -> { + UnifyType lhs, rhs; + return (lhs = x.getLhsType()) instanceof PlaceholderType + && !((rhs = x.getRhsType()) instanceof PlaceholderType) + && rhs.getTypeParams().occurs((PlaceholderType)lhs);}) + .map(x -> { x.setUndefinedPair(); return x;}) + .collect(Collectors.toCollection(HashSet::new)); + writeLog("ocurrPairs: " + ocurrPairs); + if (ocurrPairs.size() > 0) { + Set> ret = new HashSet<>(); + ret.add(ocurrPairs); + return ret; + } + + + + /* + * Step 1: Repeated application of reduce, adapt, erase, swap + */ Set eq0; Set eq0Prime; Optional> eqSubst = Optional.of(eq); @@ -457,12 +481,12 @@ public class TypeUnifyTask extends RecursiveTask>> { //Aufruf von computeCartesianRecursive ANFANG //writeLog("topLevelSets: " + topLevelSets.toString()); - return computeCartesianRecursive(new ArrayList<>(topLevelSets), eq, oderConstraintsOutput, fc, parallel, rekTiefe, finalresult); + return computeCartesianRecursive(new ArrayList<>(topLevelSets), eq, oderConstraintsOutput, fc, parallel, rekTiefe); } - Set> unify2(Set> setToFlatten, Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe, Boolean finalresult) { + Set> unify2(Set> setToFlatten, Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe) { //Aufruf von computeCartesianRecursive ENDE //keine Ahnung woher das kommt @@ -551,12 +575,12 @@ public class TypeUnifyTask extends RecursiveTask>> { } } else if(eqPrimePrime.isPresent()) { - Set> unifyres = unifyres1 = unify(eqPrimePrime.get(), newOderConstraints, fc, parallel, rekTiefe, finalresult); + Set> unifyres = unifyres1 = unify(eqPrimePrime.get(), newOderConstraints, fc, parallel, rekTiefe); eqPrimePrimeSet.addAll(unifyres); } else { - Set> unifyres = unifyres2 = unify(eqPrime, newOderConstraints, fc, parallel, rekTiefe, finalresult); + Set> unifyres = unifyres2 = unify(eqPrime, newOderConstraints, fc, parallel, rekTiefe); eqPrimePrimeSet.addAll(unifyres); @@ -596,10 +620,9 @@ public class TypeUnifyTask extends RecursiveTask>> { * @param fc The finite closure * @param parallel If the algorithm should be parallelized run * @param rekTiefe Deep of recursive calls - * @param finalresult Gives if unify is not called from checkA * @return The set of all principal type unifiers */ - Set> computeCartesianRecursive(ArrayList>> topLevelSets, Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe, Boolean finalresult) { + Set> computeCartesianRecursive(ArrayList>> topLevelSets, Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe) { //ArrayList>> remainingSets = new ArrayList<>(topLevelSets); Set> fstElems = new HashSet<>(); fstElems.addAll(topLevelSets.stream() @@ -610,7 +633,7 @@ public class TypeUnifyTask extends RecursiveTask>> { .filter(x -> x.size()>1) .collect(Collectors.toCollection(ArrayList::new)); if (remainingSets.isEmpty()) {//Alle Elemente sind 1-elementig - Set> result = unify2(fstElems, eq, oderConstraints, fc, parallel, rekTiefe, finalresult); + Set> result = unify2(fstElems, eq, oderConstraints, fc, parallel, rekTiefe); return result; } Set> nextSet = remainingSets.remove(0); @@ -1173,7 +1196,7 @@ public class TypeUnifyTask extends RecursiveTask>> { } 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, finalresult); + res = unify2(elems, eq, oderConstraints, fc, parallel, rekTiefe); }}} if (!isUndefinedPairSetSet(res) && isUndefinedPairSetSet(result)) { //wenn korrektes Ergebnis gefunden alle Fehlerfaelle loeschen @@ -1523,7 +1546,9 @@ public class TypeUnifyTask extends RecursiveTask>> { unitedSubst.addAll(sameEq.getAllSubstitutions()); unitedSubst.addAll(sameEq.getAllBases()); localEq.add(new UnifyPair(aPair.getRhsType(), sameEq.getRhsType(), sameEq.getPairOp(), unitedSubst, null)); - Set> localRes = unify(localEq, new ArrayList<>(), fc, false, 0, false); + finalresult = false; + Set> localRes = unify(localEq, new ArrayList<>(), fc, false, 0); + finalresult = true; if (isUndefinedPairSetSet(localRes)) { if (result.isEmpty() || isUndefinedPairSetSet(result)) { result.addAll(localRes); @@ -1539,7 +1564,9 @@ public class TypeUnifyTask extends RecursiveTask>> { unitedSubst.addAll(sameEq.getAllSubstitutions()); unitedSubst.addAll(sameEq.getAllBases()); localEq.add(new UnifyPair(sameEq.getLhsType(), aPair.getRhsType(), sameEq.getPairOp(), unitedSubst, null)); - Set> localRes = unify(localEq, new ArrayList<>(), fc, false, 0, false); + finalresult = false; + Set> localRes = unify(localEq, new ArrayList<>(), fc, false, 0); + finalresult = true; if (isUndefinedPairSetSet(localRes)) { if (result.isEmpty() || isUndefinedPairSetSet(result)) { result.addAll(localRes); @@ -2519,7 +2546,7 @@ public class TypeUnifyTask extends RecursiveTask>> { void writeLog(String str) { synchronized ( this ) { - if (log) { + if (log && finalresult) { try { logFile.write("Thread no.:" + thNo + "\n"); logFile.write("noOfThread:" + noOfThread + "\n"); diff --git a/src/test/java/AllgemeinTest.java b/src/test/java/AllgemeinTest.java index c937035e..50a67b38 100644 --- a/src/test/java/AllgemeinTest.java +++ b/src/test/java/AllgemeinTest.java @@ -40,7 +40,8 @@ public class AllgemeinTest { //String className = "FCTest3"; //String className = "Var"; //String className = "Put"; - String className = "Twice"; + //String className = "Twice"; + String className = "TestSubTypless"; //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";