diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index addcc61e..22c3b9d9 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -153,11 +153,11 @@ public class UnifyTypeFactory { return constraints.map(UnifyTypeFactory::convert); } - //never used + //NEVER USED public static Constraint convert(Constraint constraint){ - Boolean isInherited = constraint.isInherited(); - Constraint unifyPairConstraint = constraint.stream().map(UnifyTypeFactory::convert).collect(Collectors.toCollection(Constraint::new)); - unifyPairConstraint.setIsInherited(isInherited); + Constraint unifyPairConstraint = constraint.stream() + .map(UnifyTypeFactory::convert) + .collect(Collectors.toCollection( () -> new Constraint (constraint.isInherited(), convert(constraint.getExtendConstraint())))); return unifyPairConstraint; } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java b/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java index 8159f181..4b8de165 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java @@ -7,7 +7,9 @@ import java.util.HashSet; import java.util.Set; public class Constraint extends HashSet { + private static final long serialVersionUID = 1L; private Boolean isInherited = false;//wird nur für die Method-Constraints benoetigt + private Constraint extendConstraint = null; public Constraint() { super(); @@ -17,6 +19,11 @@ public class Constraint extends HashSet { this.isInherited = isInherited; } + public Constraint(Boolean isInherited, Constraint extendConstraint) { + this.isInherited = isInherited; + this.extendConstraint = extendConstraint; + } + public void setIsInherited(Boolean isInherited) { this.isInherited = isInherited; } @@ -25,7 +32,21 @@ public class Constraint extends HashSet { return isInherited; } - public String toString() { - return super.toString() + " isInherited = " + isInherited + "\n"; + public Constraint getExtendConstraint() { + return extendConstraint; } + + public void setExtendConstraint(Constraint c) { + extendConstraint = c; + } + + public String toString() { + return super.toString() + " isInherited = " + isInherited + "\nextendsContraint: " + + (extendConstraint != null ? extendConstraint.toStringBase() : "null" ) + "\n" ; + } + + public String toStringBase() { + return super.toString(); + } + } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java b/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java index d8190e7d..31a466dd 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java @@ -45,20 +45,49 @@ public class ConstraintSet { return new GuavaSetOperations().cartesianProduct(allConstraints); } - public ConstraintSet map(Function o) { + public ConstraintSet map(Function o) { + Hashtable,Constraint> CSA2CSB = new Hashtable<>(); ConstraintSet ret = new ConstraintSet<>(); ret.undConstraints = undConstraints.stream().map(o).collect(Collectors.toCollection(Constraint::new)); List>> newOder = new ArrayList<>(); + /* + for(Set> oderConstraint : oderConstraints){ + oderConstraint.forEach(as -> { + Constraint newConst = as.stream() + .map(o) + .collect(Collectors.toCollection( + () -> new Constraint (as.isInherited()))); + CSA2CSB.put(as, newConst);} ); + } + */ + for(Set> oderConstraint : oderConstraints){ newOder.add( oderConstraint.parallelStream().map((Constraint as) -> { - Boolean isInherited = as.isInherited(); - Constraint newConst = as.stream().map(o).collect(Collectors.toCollection(Constraint::new)); - newConst.setIsInherited(isInherited); + + Constraint newConst = as.stream() + .map(o) + .collect(Collectors.toCollection((as.getExtendConstraint() != null) + ? () -> new Constraint (as.isInherited(), + as.getExtendConstraint().stream().map(o).collect(Collectors.toCollection(Constraint::new))) + : () -> new Constraint (as.isInherited()) + )); + + //CSA2CSB.put(as, newConst); + return newConst; + + /* + Constraint bs = CSA2CSB.get(as); + if (as.getExtendConstraint() != null) { + bs.setExtendConstraint(CSA2CSB.get(as.getExtendConstraint())); + } + return bs; + */ }).collect(Collectors.toSet()) ); } + ret.oderConstraints = newOder; return ret; } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 221d1016..870a5d3b 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -16,8 +16,10 @@ import de.dhbwstuttgart.typeinference.assumptions.FunNClass; import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.*; +import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import java.util.*; import java.util.stream.Collectors; @@ -168,16 +170,23 @@ public class TYPEStmt implements StatementVisitor{ Set> methodConstraints = new HashSet<>(); for(MethodAssumption m : this.getMethods(methodCall.name, methodCall.arglist, info)){ GenericsResolver resolver = getResolverInstance(); - methodConstraints.add(generateConstraint(methodCall, m, info, resolver)); - /* geloescht PL 2020-05-01 - 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(() -> new Constraint(true))); methodConstraints.add(oneMethodConstraint); - */ + + // geloescht PL 2020-05-01 + //resolver = getResolverInstance(); + //oneMethodConstraint = generateConstraint(methodCall, m, info, resolver); + //Boolean oneMethodConstraint_isInherited = oneMethodConstraint.isInherited(); + Constraint extendsOneMethodConstraint = 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(() -> new Constraint(true))); + oneMethodConstraint.setExtendConstraint(extendsOneMethodConstraint); + extendsOneMethodConstraint.setExtendConstraint(oneMethodConstraint); + methodConstraints.add(extendsOneMethodConstraint); } 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/RuleSet.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 98d59b54..93f33d08 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -680,7 +680,12 @@ public class RuleSet implements IRuleSet{ result1 = result1.stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(LinkedList::new)); Function,? extends Constraint> applyUni = b -> b.stream().map( - x -> uni.apply(pair,x)).collect(Collectors.toCollection(() -> new Constraint(b.isInherited()))); + x -> uni.apply(pair,x)).collect(Collectors.toCollection((b.getExtendConstraint() != null) + ? () -> new Constraint( + b.isInherited(), + b.getExtendConstraint().stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(Constraint::new))) + : () -> new Constraint(b.isInherited()) + )); oderConstraints.replaceAll(oc -> oc.stream().map(applyUni).collect(Collectors.toCollection(HashSet::new))); /* oderConstraints = oderConstraints.stream().map( diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 63a68773..02e2ed2a 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -588,8 +588,9 @@ public class TypeUnifyTask extends RecursiveTask>> { return result; } Set> nextSet = remainingSets.remove(0); - writeLog("nextSet: " + nextSet.toString()); + //writeLog("nextSet: " + nextSet.toString()); List> nextSetasList =new ArrayList<>(nextSet); + /* try { //List> //nextSetasList = oup.sortedCopy(nextSet);//new ArrayList<>(nextSet); @@ -597,6 +598,7 @@ public class TypeUnifyTask extends RecursiveTask>> { catch (java.lang.IllegalArgumentException e) { System.out.print(""); } + */ Set> result = new HashSet<>(); int variance = 0; @@ -654,9 +656,9 @@ public class TypeUnifyTask extends RecursiveTask>> { System.out.print(""); if (nextSetasList.iterator().next().stream().filter(x -> x.getLhsType().getName().equals("D")).findFirst().isPresent() && nextSetasList.size()>1) System.out.print(""); - writeLog("nextSetasList: " + nextSetasList.toString()); + //writeLog("nextSetasList: " + nextSetasList.toString()); Set nextSetElem = nextSetasList.get(0); - writeLog("BasePair1: " + nextSetElem + " " + nextSetElem.iterator().next().getBasePair()); + //writeLog("BasePair1: " + nextSetElem + " " + nextSetElem.iterator().next().getBasePair()); /* sameEqSet-Bestimmung: Wenn a = ty \in nextSet dann enthaelt sameEqSet alle Paare a < ty1 oder ty2 < a aus fstElems */ Set sameEqSet = new HashSet<>(); @@ -700,52 +702,15 @@ public class TypeUnifyTask extends RecursiveTask>> { List> nextSetasListOderConstraints = new ArrayList<>(); //List> nextSetasListRestMin = new ArrayList<>(); //List> nextSetasListRestOder = new ArrayList<>(); - writeLog("qextSet: " + nextSet.toString()); - writeLog("WhileAnfangNextSetasList: " + nextSetasList.toString()); + writeLog("nextSet: " + nextSet.toString()); + writeLog("nextSetasList: " + nextSetasList.toString()); if (variance == 1) { - a = oup.max(nextSetasList.iterator()); + Set a_final = a = oup.max(nextSetasList.iterator()); nextSetasList.remove(a); - if (oderConstraint && nextSetasListOderConstraints.isEmpty()) { - Set a_final = a; - Optional opt_a_rec = a - .stream() - .filter(x -> x.getGroundBasePair().getLhsType() instanceof PlaceholderType && - ! (x.getRhsType() instanceof PlaceholderType) && - x.getPairOp() == PairOperator.EQUALSDOT).findAny(); - if (opt_a_rec.isPresent()) { - a_rec = opt_a_rec.get(); - PlaceholderType phd; - if ((phd = ((PlaceholderType)a_rec.getGroundBasePair().getLhsType())).isWildcardable()) { - Set a_super = a.stream() - .map(x -> new UnifyPair(x.getLhsType(), x.getRhsType(), x.getPairOp())) - .collect(Collectors.toCollection(() -> new Constraint<>(((Constraint)a_final).isInherited()))); - a_super.remove(a_rec); - a_super.add(new UnifyPair(a_rec.getLhsType(), new SuperType(a_rec.getRhsType()), a_rec.getPairOp())); - Set a_extends = a.stream() - .map(x -> new UnifyPair(x.getLhsType(), x.getRhsType(), x.getPairOp())) - .collect(Collectors.toCollection(() -> new Constraint<>(((Constraint)a_final).isInherited()))); - a_extends.remove(a_rec); - a_extends.add(new UnifyPair(a_rec.getLhsType(), new ExtendsType(a_rec.getRhsType()), a_rec.getPairOp())); - if (phd.isInnerType()) { - nextSetasList.add(a_super); - nextSetasListOderConstraints.add(a_super); - nextSetasList.add(a); - nextSetasListOderConstraints.add(a); - a = a_extends; - } - else { - nextSetasList.add(a_extends); - nextSetasListOderConstraints.add(a_extends); - nextSetasList.add(a); - nextSetasListOderConstraints.add(a); - a = a_super; - } - } - } - } - else { - nextSetasListOderConstraints.remove(a); - } + if (oderConstraint) { + nextSetasListOderConstraints.add(((Constraint)a).getExtendConstraint()); + } + writeLog("nextSetasListOderConstraints: " + nextSetasListOderConstraints); nextSetasListRest = new ArrayList<>(nextSetasList); Iterator> nextSetasListItRest = new ArrayList>(nextSetasListRest).iterator(); while (nextSetasListItRest.hasNext()) { 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 8ee39d7f..3a86af14 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -655,6 +655,7 @@ implements IFiniteClosure { */ public int compare (UnifyType left, UnifyType right, PairOperator pairop) { + //try {logFile.write("left: "+ left + " right: " + right + " pairop: " + pairop);} catch (IOException ie) {} if (left.getName().equals("Matrix") || right.getName().equals("Matrix")) System.out.println(""); /* @@ -746,7 +747,10 @@ implements IFiniteClosure { //Gleichungen der Form a <./=. Theta oder Theta <./=. a oder a <./=. b sind ok. long greaterLen = greaterRes.stream().filter(delFun).count(); if (greaterLen == 0) return 1; - else return 0; + else { + //try {logFile.write("0 left: "+ left + " right: " + right + " pairop: " + pairop);} catch (IOException ie) {} + 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 d139cb88..ce2fbd21 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -242,8 +242,8 @@ public class OrderingUnifyPair extends OrderingExtend> { .filter(x -> (x.getPairOp() == PairOperator.SMALLERDOT)) .collect(Collectors.toCollection(HashSet::new)); - - /* synchronized(this) { + /* + synchronized(this) { try { ((FiniteClosure)fc).logFile.write("leftBase: " + leftBase.toString() +"\n"); ((FiniteClosure)fc).logFile.write("rightBase: " + rightBase.toString() +"\n\n"); @@ -262,6 +262,7 @@ public class OrderingUnifyPair extends OrderingExtend> { } */ + Integer compareEq; if (lefteqOder.size() == 1 && righteqOder.size() == 1 && lefteqRet.size() == 1 && righteqRet.size() == 1) { Match m = new Match(); @@ -274,26 +275,51 @@ public class OrderingUnifyPair extends OrderingExtend> { return new UnifyPair(x.getRhsType(), leftElem.getRhsType(), PairOperator.EQUALSDOT);}) .collect(Collectors.toCollection(ArrayList::new)); if (m.match(matchList).isPresent()) { + //try { ((FiniteClosure)fc).logFile.write("result1: -1 \n\n"); } catch (IOException ie) {} return -1; } else { + //try { ((FiniteClosure)fc).logFile.write("result1: 0 \n\n"); } catch (IOException ie) {} return 0; } } else if (compareEq == 1) { ArrayList matchList = leftleOder.stream().map(x -> { UnifyPair rightElem = rightleOder.stream() - .filter(y -> y.getGroundBasePair().getLhsType().equals(x.getGroundBasePair().getLhsType())) + .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()) { + //try { ((FiniteClosure)fc).logFile.write("result2: 1 \n\n"); } catch (IOException ie) {} return 1; } else { + //try { ((FiniteClosure)fc).logFile.write("result2: 0 \n\n"); } catch (IOException ie) {} return 0; } } else { + /* + 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.write("result3: 0 \n\n"); + ((FiniteClosure)fc).logFile.flush(); + } + catch (IOException ie) { + } + } + */ return 0; } }