From eca187da7995b7abe3867cc11e7fa345b307d890 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Thu, 26 Mar 2020 12:44:56 +0100 Subject: [PATCH] modified: src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java modified: src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java modified: src/main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java modified: src/main/java/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java --- .../syntaxtree/visual/OutputGenerator.java | 6 ++ .../typeinference/typeAlgo/TYPEStmt.java | 7 ++ .../typeinference/unify/TypeUnifyTask.java | 24 +++++- .../unify/model/OrderingUnifyPair.java | 79 ++++++++++++++++--- .../typeinference/unify/model/UnifyPair.java | 10 ++- 5 files changed, 108 insertions(+), 18 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 3dfca38a..078a7407 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -7,6 +7,7 @@ import de.dhbwstuttgart.syntaxtree.type.*; import java.lang.reflect.Modifier; import java.util.Iterator; +import java.util.Optional; public class OutputGenerator implements ASTVisitor{ private static final String TAB = " "; @@ -123,6 +124,11 @@ public class OutputGenerator implements ASTVisitor{ f.accept(this); out.append("\n"); } + Optional fI; + if ((fI = classOrInterface.getfieldInitializations()).isPresent()) { + out.append("Initializations:"); + fI.get().accept(this); + } for(Method m : classOrInterface.getMethods()){ out.append(tabs); m.accept(this); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 684077e4..cf67b857 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -169,6 +169,13 @@ public class TYPEStmt implements StatementVisitor{ for(MethodAssumption m : this.getMethods(methodCall.name, methodCall.arglist, info)){ GenericsResolver resolver = getResolverInstance(); methodConstraints.add(generateConstraint(methodCall, m, info, resolver)); + resolver = getResolverInstance(); + Constraint oneMethodConstraint = generateConstraint(methodCall, m, info, resolver); + oneMethodConstraint = oneMethodConstraint.stream().map(x -> (x.TA1 instanceof TypePlaceholder && x.GetOperator() == PairOperator.EQUALSDOT && + !(x.TA2 instanceof TypePlaceholder)) ? + new Pair(x.TA1, new ExtendsWildcardType(x.TA2, x.TA2.getOffset()), PairOperator.EQUALSDOT) : + x).collect(Collectors.toCollection(Constraint::new)); + methodConstraints.add(oneMethodConstraint); } if(methodConstraints.size()<1){ throw new TypeinferenceException("Methode "+methodCall.name+" ist nicht vorhanden!",methodCall.getOffset()); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 8f13f01f..73fddb07 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -639,7 +639,8 @@ public class TypeUnifyTask extends RecursiveTask>> { .filter(x -> x.getGroundBasePair().getLhsType() instanceof PlaceholderType && x.getRhsType() instanceof ReferenceType && x.getPairOp() == PairOperator.EQUALSDOT) - .map(x -> ((PlaceholderType)x.getGroundBasePair().getLhsType()).getVariance()) + .map(x -> + ((PlaceholderType)x.getGroundBasePair().getLhsType()).getVariance()) .findAny(); //Fuer Operatorenaufrufe wird variance auf 2 gesetzt //Es werden alle Elemente des Kartesischen Produkts abgearbeitet @@ -671,6 +672,27 @@ public class TypeUnifyTask extends RecursiveTask>> { a_next = nextSetasList.iterator().next(); } */ + if (variance == 2) { + writeLog("VARIANCE2 " + nextSetasList.toString()); + Set XX = nextSetasList.iterator() + .next() + .stream() + .filter(x -> + x.getGroundBasePair().getLhsType() instanceof PlaceholderType && + ! (x.getRhsType() instanceof PlaceholderType) && + x.getPairOp() == PairOperator.EQUALSDOT) + .collect(Collectors.toCollection(HashSet::new)); + + Set sI = XX.stream() + .map(x -> + ((PlaceholderType)x.getGroundBasePair().getLhsType()).getVariance()) + .collect(Collectors.toCollection(HashSet::new));; + Optional oI = sI.stream() + .findAny(); + variance = oI.isPresent() ? oI.get() : 2; + System.out.println(""); + + } if (!nextSetasList.iterator().hasNext()) System.out.print(""); if (nextSetasList.iterator().next().stream().filter(x -> x.getLhsType().getName().equals("D")).findFirst().isPresent() && nextSetasList.size()>1) 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 8ce37eb6..690e4c43 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; import java.util.function.BinaryOperator; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -44,7 +45,7 @@ public class OrderingUnifyPair extends Ordering> { }} catch (ClassCastException e) { try { - ((FiniteClosure)fc).logFile.write("ClassCastException: " + left.toString() +"\n\n"); + ((FiniteClosure)fc).logFile.write("ClassCastException: " + left.toString() + " " + left.getGroundBasePair() + "\n\n"); ((FiniteClosure)fc).logFile.flush(); } catch (IOException ie) { @@ -197,17 +198,42 @@ public class OrderingUnifyPair extends Ordering> { //} //ODER-CONSTRAINT - Set lefteqOder = lefteq.stream() - .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getRhsType() instanceof ReferenceType)) + Set leftBase = left.stream().map(x -> x.getGroundBasePair()).collect(Collectors.toCollection(HashSet::new)); + Set rightBase = right.stream().map(x -> x.getGroundBasePair()).collect(Collectors.toCollection(HashSet::new)); + + Set lefteqOder = left.stream() + .filter(x -> { UnifyPair y = x.getGroundBasePair(); + try { + ((FiniteClosure)fc).logFile.write("leftBase: " + leftBase.toString() +"\n"); + ((FiniteClosure)fc).logFile.write("rightBase: " + rightBase.toString() +"\n\n"); + ((FiniteClosure)fc).logFile.write("left: " + left.toString() +"\n"); + ((FiniteClosure)fc).logFile.write("right: " + right.toString() +"\n\n"); + ((FiniteClosure)fc).logFile.write("y: " + y.toString() +"\n"); + ((FiniteClosure)fc).logFile.write("y.getLhsType() : " + y.getLhsType() .toString() +"\n\n"); + ((FiniteClosure)fc).logFile.write("y.getRhsType(): " + y.getRhsType().toString() +"\n"); + ((FiniteClosure)fc).logFile.write("x.getPairOp(): " + x.getPairOp().toString() +"\n\n"); + } + catch (IOException ie) { + } + return (y.getLhsType() instanceof PlaceholderType && + y.getRhsType() instanceof ReferenceType && + x.getPairOp() == PairOperator.EQUALSDOT);}) .collect(Collectors.toCollection(HashSet::new)); - Set righteqOder = righteq.stream() - .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getRhsType() instanceof ReferenceType)) + Set righteqOder = right.stream() + .filter(x -> { UnifyPair y = x.getGroundBasePair(); + return (y.getLhsType() instanceof PlaceholderType && + y.getRhsType() instanceof ReferenceType && + x.getPairOp() == PairOperator.EQUALSDOT);}) .collect(Collectors.toCollection(HashSet::new)); Set lefteqRet = left.stream() - .filter(x -> (x.getRhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)) + .filter(x -> { UnifyPair y = x.getGroundBasePair(); + return (y.getRhsType() instanceof PlaceholderType && + x.getPairOp() == PairOperator.EQUALSDOT);}) .collect(Collectors.toCollection(HashSet::new)); Set righteqRet = right.stream() - .filter(x -> (x.getRhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)) + .filter(x -> { UnifyPair y = x.getGroundBasePair(); + return (y.getRhsType() instanceof PlaceholderType && + x.getPairOp() == PairOperator.EQUALSDOT);}) .collect(Collectors.toCollection(HashSet::new)); Set leftleOder = left.stream() .filter(x -> (x.getPairOp() == PairOperator.SMALLERDOT)) @@ -215,12 +241,36 @@ public class OrderingUnifyPair extends Ordering> { Set rightleOder = right.stream() .filter(x -> (x.getPairOp() == PairOperator.SMALLERDOT)) .collect(Collectors.toCollection(HashSet::new)); + + + synchronized(this) { + try { + ((FiniteClosure)fc).logFile.write("leftBase: " + leftBase.toString() +"\n"); + ((FiniteClosure)fc).logFile.write("rightBase: " + rightBase.toString() +"\n\n"); + ((FiniteClosure)fc).logFile.write("left: " + left.toString() +"\n"); + ((FiniteClosure)fc).logFile.write("right: " + right.toString() +"\n\n"); + ((FiniteClosure)fc).logFile.write("lefteqOder: " + lefteqOder.toString() +"\n"); + ((FiniteClosure)fc).logFile.write("righteqOder: " + righteqOder.toString() +"\n\n"); + ((FiniteClosure)fc).logFile.write("lefteqRet: " + lefteqRet.toString() +"\n"); + ((FiniteClosure)fc).logFile.write("righteqRet: " + righteqRet.toString() +"\n\n"); + ((FiniteClosure)fc).logFile.write("leftleOder: " + leftleOder.toString() +"\n"); + ((FiniteClosure)fc).logFile.write("rightleOder: " + rightleOder.toString() +"\n\n"); + ((FiniteClosure)fc).logFile.flush(); + } + catch (IOException ie) { + } + } + + if (lefteqOder.size() == 1 && righteqOder.size() == 1 && lefteqRet.size() == 1 && righteqRet.size() == 1) { Match m = new Match(); - if (compareEq(lefteqOder.iterator().next(), righteqOder.iterator().next()) == -1) { + if (compareEq(lefteqOder.iterator().next().getGroundBasePair(), righteqOder.iterator().next().getGroundBasePair()) == -1) { ArrayList matchList = - leftleOder.stream().map(x -> { Iterator rightleOderIt = rightleOder.iterator(); - return new UnifyPair(x.getRhsType(), rightleOderIt.next().getRhsType(), PairOperator.EQUALSDOT);}) + leftleOder.stream().map(x -> { + UnifyPair rightElem = rightleOder.stream() + .filter(y -> y.getGroundBasePair().getLhsType().equals(x.getGroundBasePair().getLhsType())) + .findAny().get(); + return new UnifyPair(x.getRhsType(), rightElem.getRhsType(), PairOperator.EQUALSDOT);}) .collect(Collectors.toCollection(ArrayList::new)); if (m.match(matchList).isPresent()) { return -1; @@ -228,10 +278,13 @@ public class OrderingUnifyPair extends Ordering> { else { return 0; } - } else if (compareEq(lefteqOder.iterator().next(), righteqOder.iterator().next()) == 1) { + } else if (compareEq(lefteqOder.iterator().next().getGroundBasePair(), righteqOder.iterator().next().getGroundBasePair()) == 1) { ArrayList matchList = - rightleOder.stream().map(x -> { Iterator leftOderIt = leftleOder.iterator(); - return new UnifyPair(x.getRhsType(), leftOderIt.next().getRhsType(), PairOperator.EQUALSDOT);}) + rightleOder.stream().map(x -> { + UnifyPair leftElem = leftleOder.stream() + .filter(y -> y.getGroundBasePair().getLhsType().equals(x.getGroundBasePair().getLhsType())) + .findAny().get(); + return new UnifyPair(x.getRhsType(), leftElem.getRhsType(), PairOperator.EQUALSDOT);}) .collect(Collectors.toCollection(ArrayList::new)); if (m.match(matchList).isPresent()) { return 1; diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java index 06c464dc..bc6e3013 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java @@ -217,12 +217,14 @@ public class UnifyPair { public String toString() { String ret = ""; if (lhs instanceof PlaceholderType) { - ret = new Integer(((PlaceholderType)lhs).getVariance()).toString() + " " + ((PlaceholderType)lhs).isInnerType() - + " " + ((PlaceholderType)lhs).isWildcardable(); + ret = new Integer(((PlaceholderType)lhs).getVariance()).toString() + " " + + "WC: " + ((PlaceholderType)lhs).isWildcardable() + + ", IT: " + ((PlaceholderType)lhs).isInnerType(); } if (rhs instanceof PlaceholderType) { - ret = ret + ", " + new Integer(((PlaceholderType)rhs).getVariance()).toString() + " " + ((PlaceholderType)rhs).isInnerType() - + " " + ((PlaceholderType)rhs).isWildcardable(); + ret = ret + ", " + new Integer(((PlaceholderType)rhs).getVariance()).toString() + " " + + "WC: " + ((PlaceholderType)rhs).isWildcardable() + + ", IT: " + ((PlaceholderType)rhs).isInnerType(); } return "(" + lhs + " " + pairOp + " " + rhs + ", " + ret + ")"; //+ ", [" + getfBounded().toString()+ "])"; }