diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index 11d5f8c56..d271fd84b 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -55,7 +55,7 @@ import de.dhbwstuttgart.typeinference.exceptions.DebugException; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; import de.dhbwstuttgart.typeinference.unify.Unify; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; @@ -261,9 +261,9 @@ public class SourceFile UnifyConstraintsSet unifyConstraints = UnifyTypeFactory.convert(oderConstraints); //Unmögliche ConstraintsSets aussortieren durch Unifizierung - Function,Menge>> unifier = (pairs)->{ - Menge> retValue = new Menge<>(); - Set> unifiedPairs = new Unify().unify(pairs, finiteClosure); + Function,Menge>> unifier = (pairs)->{ + Menge> retValue = new Menge<>(); + Set> unifiedPairs = new Unify().unify(pairs, finiteClosure); return retValue;}; //oderConstraints.filterWrongConstraints(unifier); @@ -276,7 +276,7 @@ public class SourceFile //////////////// //Karthesisches Produkt bilden: //////////////// - Set> xConstraints = unifyConstraints.cartesianProduct(); + Set> xConstraints = unifyConstraints.cartesianProduct(); @@ -288,7 +288,7 @@ public class SourceFile // Unifizierung der Constraints: ////////////////////////////// boolean unifyFail = true; - for(Set constraints : xConstraints){ + for(Set constraints : xConstraints){ //Alle durch das Karthesische Produkt entstandenen Möglichkeiten durchgehen: //Menge> result = new Menge>(); @@ -358,16 +358,16 @@ public class SourceFile return cardprodret; }); */ - Set> unifyResult = new Unify().unify(constraints, finiteClosure); + Set> unifyResult = new Unify().unify(constraints, finiteClosure); - Menge> convertedResult = unifyResult.parallelStream().>map((Set resultSet)->{ - Menge innerConvert = resultSet.stream().map((MPair mp)->UnifyTypeFactory.convert(mp)) + Menge> convertedResult = unifyResult.parallelStream().>map((Set resultSet)->{ + Menge innerConvert = resultSet.stream().map((UnifyPair mp)->UnifyTypeFactory.convert(mp)) .collect(Menge::new, Menge::add, Menge::addAll); return innerConvert; }).collect(Menge::new, Menge::add, Menge::addAll); Menge convertedConstraints = constraints.stream().map( - (MPair mp)->{return UnifyTypeFactory.convert(mp);} + (UnifyPair mp)->{return UnifyTypeFactory.convert(mp);} ).collect(Menge::new, Menge::add, Menge::addAll); //Dann den Ergebnissen anfügen diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index df9032091..0e0da3fc9 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -29,7 +29,7 @@ import de.dhbwstuttgart.typeinference.assumptions.ClassAssumption; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.ReferenceType; @@ -43,7 +43,7 @@ public class UnifyTypeFactory { private final static NullSyntaxTreeNode NULL_NODE = new NullSyntaxTreeNode(); public static FiniteClosure generateFC(TypeAssumptions fromAss){ - HashSet pairs = new HashSet<>(); + HashSet pairs = new HashSet<>(); for(ClassAssumption cAss : fromAss.getClassAssumptions()){ UnifyType tl = UnifyTypeFactory.convert(cAss.getAssumedClass().getType()); RefType superClass = cAss.getAssumedClass().getSuperClass(); @@ -55,8 +55,8 @@ public class UnifyTypeFactory { return new FiniteClosure(pairs); } - public static MPair smaller(UnifyType tl, UnifyType tr){ - return new MPair(tl, tr, PairOperator.SMALLER); + public static UnifyPair smaller(UnifyType tl, UnifyType tr){ + return new UnifyPair(tl, tr, PairOperator.SMALLER); } public static UnifyType convert(Type t){ @@ -129,18 +129,18 @@ public class UnifyTypeFactory { return ret; } - public static MPair convert(EinzelElement p) { + public static UnifyPair convert(EinzelElement p) { return convert(p.getItem()); } - public static MPair convert(Pair p) { + public static UnifyPair convert(Pair p) { if(!p.OperatorSmaller())throw new NotImplementedException(); - MPair ret = smaller(UnifyTypeFactory.convert(p.TA1) + UnifyPair ret = smaller(UnifyTypeFactory.convert(p.TA1) , UnifyTypeFactory.convert(p.TA2)); return ret; } - public static Pair convert(MPair mp) { + public static Pair convert(UnifyPair mp) { Type tl = UnifyTypeFactory.convert(mp.getLhsType()); Type tr = UnifyTypeFactory.convert(mp.getRhsType()); return new Pair(tl, tr, mp.getPairOp()); diff --git a/src/de/dhbwstuttgart/typeinference/UnifyConstraintsSet.java b/src/de/dhbwstuttgart/typeinference/UnifyConstraintsSet.java index 16aa7b8d4..5c7ed8b77 100644 --- a/src/de/dhbwstuttgart/typeinference/UnifyConstraintsSet.java +++ b/src/de/dhbwstuttgart/typeinference/UnifyConstraintsSet.java @@ -6,9 +6,9 @@ import java.util.Vector; import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.*; import de.dhbwstuttgart.typeinference.unify.Unifier; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; -public class UnifyConstraintsSet extends UndMenge implements Iterable{ +public class UnifyConstraintsSet extends UndMenge implements Iterable{ private static final Logger log = Logger.getLogger(UnifyConstraintsSet.class.getName()); private Menge constraintsSet; @@ -53,18 +53,18 @@ public class UnifyConstraintsSet extends UndMenge implements Iterable uCons = this.filterUndConstraints(); - Vector alleUndConstraints = new Vector<>(); + Vector alleUndConstraints = new Vector<>(); for(UnifyUndConstraint undConstraint : uCons){ alleUndConstraints.addAll(undConstraint.getConstraintPairs()); } this.filterWrongConstraints( (pairs)->{ - Set undConstraintsUndPairs = new Menge<>(); + Set undConstraintsUndPairs = new Menge<>(); undConstraintsUndPairs.addAll(pairs); undConstraintsUndPairs.addAll(alleUndConstraints); log.debug("Versuche Pairs auszusondern:\n"+pairs, Section.TYPEINFERENCE); log.debug("Unifiziere:\n"+undConstraintsUndPairs, Section.TYPEINFERENCE); - Set> unifyResult = unifier.apply(undConstraintsUndPairs); + Set> unifyResult = unifier.apply(undConstraintsUndPairs); return unifyResult; }); } @@ -90,7 +90,7 @@ public class UnifyConstraintsSet extends UndMenge implements Iterable> getSet() { + public Menge> getSet() { return this.constraintsSet; } } diff --git a/src/de/dhbwstuttgart/typeinference/UnifyOderConstraint.java b/src/de/dhbwstuttgart/typeinference/UnifyOderConstraint.java index 2d3c9375c..49dbf6de8 100644 --- a/src/de/dhbwstuttgart/typeinference/UnifyOderConstraint.java +++ b/src/de/dhbwstuttgart/typeinference/UnifyOderConstraint.java @@ -9,9 +9,9 @@ import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.unify.Unifier; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; -public class UnifyOderConstraint extends OderMenge{ +public class UnifyOderConstraint extends OderMenge{ private Set oderConstraintPairs; private final static Logger logger = Logger.getLogger(UnifyOderConstraint.class.getName()); @@ -38,8 +38,8 @@ public class UnifyOderConstraint extends OderMenge{ * Liefert alle in diesem OderConstraint enthaltene Constraints. Dabei gehen die Verkn�pfungen (Oder/Und) verloren. * @return */ - public Menge getConstraintPairs(){ - Menge ret = new Menge(); + public Menge getConstraintPairs(){ + Menge ret = new Menge(); for(UnifyUndConstraint oC : this.oderConstraintPairs){ ret.addAll(oC.getConstraintPairs()); } @@ -50,7 +50,7 @@ public class UnifyOderConstraint extends OderMenge{ * Falls die Type des toAdd-Pairs nicht vom Typ RefType bzw. TypePlaceholder sind, so werden sie in einen RefType umgewandelt. * @param toAdd */ - public void addConstraint(MPair toAdd){ + public void addConstraint(UnifyPair toAdd){ oderConstraintPairs.add(new UnifySingleConstraint(toAdd)); } @@ -86,7 +86,7 @@ public class UnifyOderConstraint extends OderMenge{ void filterWrongConstraints(Unifier unifier) { Set filteredConstraints = new Menge<>(); for(UnifyUndConstraint cons : this.getUndConstraints()){ - Set> unifierResult = unifier.apply(cons.getConstraintPairs()); + Set> unifierResult = unifier.apply(cons.getConstraintPairs()); if(!unifierResult.isEmpty()){ filteredConstraints.add(cons); }else{ @@ -104,7 +104,7 @@ public class UnifyOderConstraint extends OderMenge{ } @Override - public Set> getSet() { + public Set> getSet() { return this.oderConstraintPairs; } diff --git a/src/de/dhbwstuttgart/typeinference/UnifySingleConstraint.java b/src/de/dhbwstuttgart/typeinference/UnifySingleConstraint.java index 66cc2bbc3..2860d32fa 100644 --- a/src/de/dhbwstuttgart/typeinference/UnifySingleConstraint.java +++ b/src/de/dhbwstuttgart/typeinference/UnifySingleConstraint.java @@ -8,36 +8,36 @@ import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.exceptions.DebugException; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; public class UnifySingleConstraint extends UnifyUndConstraint{ - private MPair constraintPair; //entspricht θ condition θ' + private UnifyPair constraintPair; //entspricht θ condition θ' @Override - public Menge> getSet() { - Menge> ret = new Menge<>(); + public Menge> getSet() { + Menge> ret = new Menge<>(); ret.add(new EinzelElement<>(constraintPair)); return ret; } - public UnifySingleConstraint(MPair toAdd) { + public UnifySingleConstraint(UnifyPair toAdd) { this.addConstraint(toAdd); } - public MPair getPair(){ + public UnifyPair getPair(){ return constraintPair; } @Override //Methode überschreiben, damit immer nur ein Vector mit nur einem Element zurückgeliefert wird. - public Menge getConstraintPairs(){ - Menge ret = new Menge<>(); + public Menge getConstraintPairs(){ + Menge ret = new Menge<>(); ret.add(constraintPair); return ret; } - public void addConstraint(MPair toAdd){ + public void addConstraint(UnifyPair toAdd){ if(constraintPair != null)throw new DebugException("Ein Constraint darf nur aus einem ConstraintPair bestehen. Das hinzufügen von "+ toAdd + " ist nicht möglich."); constraintPair = toAdd; diff --git a/src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java b/src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java index f963e12f2..d13ead18b 100644 --- a/src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java +++ b/src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java @@ -7,24 +7,24 @@ import java.util.Vector; import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.typeinference.exceptions.DebugException; import de.dhbwstuttgart.typeinference.unify.Unifier; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; /** * Stellt ein Constraint dar, welches aus mehreren Constraint-Paaren besteht. Diese gelten alle stets gleichzeitig / sind per "Und" miteinander verknüpft. * @author janulrich * */ -public class UnifyUndConstraint extends UndMenge { +public class UnifyUndConstraint extends UndMenge { - Menge> set = new Menge<>(); + Menge> set = new Menge<>(); @Override - public Menge> getSet() { + public Menge> getSet() { return set; } - public Set getConstraintPairs() { - Set> ret = this.cartesianProduct(); + public Set getConstraintPairs() { + Set> ret = this.cartesianProduct(); if(ret.size() != 1){ //UndConstraints enthalten nur SingleConstraints, wodurch das Karthesische Produkt nur aus einem Element bestehen kann. throw new DebugException("Fehler in ConstraintPairs-Bildung"); @@ -38,7 +38,7 @@ public class UnifyUndConstraint extends UndMenge { return ret; } - public void add(MPair pair){ + public void add(UnifyPair pair){ set.add(new EinzelElement<>(pair)); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/Mapping.java b/src/de/dhbwstuttgart/typeinference/unify/Mapping.java index 5c9be5014..b6ec1e34f 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/Mapping.java +++ b/src/de/dhbwstuttgart/typeinference/unify/Mapping.java @@ -24,15 +24,15 @@ public class Mapping { return forwardMap.get(type); } - public de.dhbwstuttgart.typeinference.unify.model.MPair map(de.dhbwstuttgart.typeinference.Pair pair) { - return new de.dhbwstuttgart.typeinference.unify.model.MPair(forwardMap.get(pair.TA1), forwardMap.get(pair.TA2), mapOp(pair.GetOperator())); + public de.dhbwstuttgart.typeinference.unify.model.UnifyPair map(de.dhbwstuttgart.typeinference.Pair pair) { + return new de.dhbwstuttgart.typeinference.unify.model.UnifyPair(forwardMap.get(pair.TA1), forwardMap.get(pair.TA2), mapOp(pair.GetOperator())); } public Set mapTypeSet(Set types) { return types.stream().map(this::map).collect(Collectors.toCollection(HashSet::new)); } - public Set mapPairSet(Set pairs) { + public Set mapPairSet(Set pairs) { return pairs.stream().map(this::map).collect(Collectors.toCollection(HashSet::new)); } @@ -40,7 +40,7 @@ public class Mapping { return irreversible.contains(type) ? Optional.of(backwardMap.get(type)) : Optional.empty(); } - public Optional unmap(de.dhbwstuttgart.typeinference.unify.model.MPair mpair) { + public Optional unmap(de.dhbwstuttgart.typeinference.unify.model.UnifyPair mpair) { de.dhbwstuttgart.typeinference.unify.model.UnifyType lhs = mpair.getLhsType(); de.dhbwstuttgart.typeinference.unify.model.UnifyType rhs = mpair.getRhsType(); @@ -54,7 +54,7 @@ public class Mapping { return result.size() == types.size() ? Optional.of(result) : Optional.empty(); } - public Optional> unmapPairSet(Set pairs) { + public Optional> unmapPairSet(Set pairs) { Set result = pairs.stream().map(this::unmap).filter(x -> x.isPresent()).map(x -> x.get()).collect(Collectors.toCollection(HashSet::new)); return result.size() == pairs.size() ? Optional.of(result) : Optional.empty(); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unify/MartelliMontanariUnify.java index 22759fbca..3531b7161 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/MartelliMontanariUnify.java @@ -10,7 +10,7 @@ import java.util.Set; import java.util.stream.Collectors; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; @@ -28,12 +28,12 @@ public class MartelliMontanariUnify implements IUnify { if(terms.size() < 2) return Optional.of(Unifier.Identity()); - ArrayList termsQ = new ArrayList(); + ArrayList termsQ = new ArrayList(); Iterator iter = terms.iterator(); UnifyType prev = iter.next(); while(iter.hasNext()) { UnifyType next = iter.next(); - termsQ.add(new MPair(prev, next, PairOperator.EQUALSDOT)); + termsQ.add(new UnifyPair(prev, next, PairOperator.EQUALSDOT)); prev = next; } @@ -41,14 +41,14 @@ public class MartelliMontanariUnify implements IUnify { int idx = 0; while(idx < termsQ.size()) { - MPair pair = termsQ.get(idx); + UnifyPair pair = termsQ.get(idx); if(delete(pair)) { termsQ.remove(idx); continue; } - Optional> optSet = decompose(pair); + Optional> optSet = decompose(pair); if(optSet == null) return Optional.empty(); // Unification failed @@ -59,7 +59,7 @@ public class MartelliMontanariUnify implements IUnify { continue; } - Optional optPair = swap(pair); + Optional optPair = swap(pair); if(optPair.isPresent()) { termsQ.add(optPair.get()); @@ -88,12 +88,12 @@ public class MartelliMontanariUnify implements IUnify { return Optional.of(mgu); } - private boolean delete(MPair pair) { + private boolean delete(UnifyPair pair) { return pair.getRhsType().equals(pair.getLhsType()); } - private Optional> decompose(MPair pair) { - Set result = new HashSet<>(); + private Optional> decompose(UnifyPair pair) { + Set result = new HashSet<>(); UnifyType rhs = pair.getRhsType(); UnifyType lhs = pair.getLhsType(); @@ -112,22 +112,22 @@ public class MartelliMontanariUnify implements IUnify { return Optional.empty(); for(int i = 0; i < rhsTypeParams.size(); i++) - result.add(new MPair(rhsTypeParams.get(i), lhsTypeParams.get(i), PairOperator.EQUALSDOT)); + result.add(new UnifyPair(rhsTypeParams.get(i), lhsTypeParams.get(i), PairOperator.EQUALSDOT)); return Optional.of(result); } - private Optional swap(MPair pair) { + private Optional swap(UnifyPair pair) { UnifyType rhs = pair.getRhsType(); UnifyType lhs = pair.getLhsType(); if(!(lhs instanceof PlaceholderType) && (rhs instanceof PlaceholderType)) - return Optional.of(new MPair(rhs, lhs, PairOperator.EQUALSDOT)); + return Optional.of(new UnifyPair(rhs, lhs, PairOperator.EQUALSDOT)); return Optional.empty(); } - private Optional> eliminate(MPair pair) { + private Optional> eliminate(UnifyPair pair) { UnifyType rhs = pair.getRhsType(); UnifyType lhs = pair.getLhsType(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index d07106755..9ce84a430 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -15,7 +15,7 @@ import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.FunNType; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.ReferenceType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; @@ -34,7 +34,7 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional reduceUp(MPair pair) { + public Optional reduceUp(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); @@ -46,11 +46,11 @@ public class RuleSet implements IRuleSet{ if(!(lhsType instanceof ReferenceType) && !(lhsType instanceof PlaceholderType)) return Optional.empty(); - return Optional.of(new MPair(lhsType, ((SuperType) rhsType).getSuperedType(), PairOperator.SMALLERDOT)); + return Optional.of(new UnifyPair(lhsType, ((SuperType) rhsType).getSuperedType(), PairOperator.SMALLERDOT)); } @Override - public Optional reduceLow(MPair pair) { + public Optional reduceLow(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); @@ -62,11 +62,11 @@ public class RuleSet implements IRuleSet{ if(!(rhsType instanceof ReferenceType) && !(rhsType instanceof PlaceholderType)) return Optional.empty(); - return Optional.of(new MPair(((ExtendsType) lhsType).getExtendedType(), rhsType, PairOperator.SMALLERDOT)); + return Optional.of(new UnifyPair(((ExtendsType) lhsType).getExtendedType(), rhsType, PairOperator.SMALLERDOT)); } @Override - public Optional reduceUpLow(MPair pair) { + public Optional reduceUpLow(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); @@ -78,11 +78,11 @@ public class RuleSet implements IRuleSet{ if(!(rhsType instanceof SuperType)) return Optional.empty(); - return Optional.of(new MPair(((ExtendsType) lhsType).getExtendedType(),((SuperType) rhsType).getSuperedType(), PairOperator.SMALLERDOT)); + return Optional.of(new UnifyPair(((ExtendsType) lhsType).getExtendedType(),((SuperType) rhsType).getSuperedType(), PairOperator.SMALLERDOT)); } @Override - public Optional> reduceExt(MPair pair) { + public Optional> reduceExt(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -125,16 +125,16 @@ public class RuleSet implements IRuleSet{ if(pi.length == 0) return Optional.empty(); - Set result = new HashSet<>(); + Set result = new HashSet<>(); for(int rhsIdx = 0; rhsIdx < extYParams.size(); rhsIdx++) - result.add(new MPair(xParams.get(pi[rhsIdx]), extYParams.get(rhsIdx), PairOperator.SMALLERDOTWC)); + result.add(new UnifyPair(xParams.get(pi[rhsIdx]), extYParams.get(rhsIdx), PairOperator.SMALLERDOTWC)); return Optional.of(result); } @Override - public Optional> reduceSup(MPair pair) { + public Optional> reduceSup(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -171,7 +171,7 @@ public class RuleSet implements IRuleSet{ TypeParams supYParams = supY.getTypeParams(); TypeParams xParams = x.getTypeParams(); - Set result = new HashSet<>(); + Set result = new HashSet<>(); int[] pi = pi(xParams, supYParams); @@ -179,13 +179,13 @@ public class RuleSet implements IRuleSet{ return Optional.empty(); for(int rhsIdx = 0; rhsIdx < supYParams.size(); rhsIdx++) - result.add(new MPair(supYParams.get(rhsIdx), xParams.get(pi[rhsIdx]), PairOperator.SMALLERDOTWC)); + result.add(new UnifyPair(supYParams.get(rhsIdx), xParams.get(pi[rhsIdx]), PairOperator.SMALLERDOTWC)); return Optional.of(result); } @Override - public Optional> reduceEq(MPair pair) { + public Optional> reduceEq(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -205,18 +205,18 @@ public class RuleSet implements IRuleSet{ return Optional.empty(); // Keine Permutation wie im Paper nötig - Set result = new HashSet<>(); + Set result = new HashSet<>(); TypeParams lhsTypeParams = lhsType.getTypeParams(); TypeParams rhsTypeParams = rhsType.getTypeParams(); for(int i = 0; i < lhsTypeParams.size(); i++) - result.add(new MPair(lhsTypeParams.get(i), rhsTypeParams.get(i), PairOperator.EQUALSDOT)); + result.add(new UnifyPair(lhsTypeParams.get(i), rhsTypeParams.get(i), PairOperator.EQUALSDOT)); return Optional.of(result); } @Override - public Optional> reduce1(MPair pair) { + public Optional> reduce1(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); @@ -251,16 +251,16 @@ public class RuleSet implements IRuleSet{ TypeParams rhsTypeParams = d.getTypeParams(); TypeParams lhsTypeParams = c.getTypeParams(); - Set result = new HashSet<>(); + Set result = new HashSet<>(); for(int rhsIdx = 0; rhsIdx < rhsTypeParams.size(); rhsIdx++) - result.add(new MPair(lhsTypeParams.get(pi[rhsIdx]), rhsTypeParams.get(rhsIdx), PairOperator.SMALLERDOTWC)); + result.add(new UnifyPair(lhsTypeParams.get(pi[rhsIdx]), rhsTypeParams.get(rhsIdx), PairOperator.SMALLERDOTWC)); return Optional.of(result); } @Override - public Optional> reduce2(MPair pair) { + public Optional> reduce2(UnifyPair pair) { if(pair.getPairOp() != PairOperator.EQUALSDOT) return Optional.empty(); @@ -304,18 +304,18 @@ public class RuleSet implements IRuleSet{ //if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size()) // return Optional.empty(); - Set result = new HashSet<>(); + Set result = new HashSet<>(); TypeParams rhsTypeParams = rhsSType.getTypeParams(); TypeParams lhsTypeParams = lhsSType.getTypeParams(); for(int i = 0; i < rhsTypeParams.size(); i++) - result.add(new MPair(lhsTypeParams.get(i), rhsTypeParams.get(i), PairOperator.EQUALSDOT)); + result.add(new UnifyPair(lhsTypeParams.get(i), rhsTypeParams.get(i), PairOperator.EQUALSDOT)); return Optional.of(result); } @Override - public boolean erase1(MPair pair) { + public boolean erase1(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return false; @@ -331,7 +331,7 @@ public class RuleSet implements IRuleSet{ } @Override - public boolean erase2(MPair pair) { + public boolean erase2(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return false; @@ -342,7 +342,7 @@ public class RuleSet implements IRuleSet{ } @Override - public boolean erase3(MPair pair) { + public boolean erase3(UnifyPair pair) { if(pair.getPairOp() != PairOperator.EQUALSDOT) return false; @@ -350,7 +350,7 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional swap(MPair pair) { + public Optional swap(UnifyPair pair) { if(pair.getPairOp() != PairOperator.EQUALSDOT) return Optional.empty(); @@ -360,11 +360,11 @@ public class RuleSet implements IRuleSet{ if(!(pair.getRhsType() instanceof PlaceholderType)) return Optional.empty(); - return Optional.of(new MPair(pair.getRhsType(), pair.getLhsType(), PairOperator.EQUALSDOT)); + return Optional.of(new UnifyPair(pair.getRhsType(), pair.getLhsType(), PairOperator.EQUALSDOT)); } @Override - public Optional adapt(MPair pair) { + public Optional adapt(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); @@ -406,11 +406,11 @@ public class RuleSet implements IRuleSet{ for(int i = 1; i < typeDParams.size(); i++) unif.Add((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i)); - return Optional.of(new MPair(unif.apply(newLhs), typeDs, PairOperator.SMALLERDOT)); + return Optional.of(new UnifyPair(unif.apply(newLhs), typeDs, PairOperator.SMALLERDOT)); } @Override - public Optional adaptExt(MPair pair) { + public Optional adaptExt(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -452,11 +452,11 @@ public class RuleSet implements IRuleSet{ for(int i = 1; i < typeDParams.size(); i++) unif.Add((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i)); - return Optional.of(new MPair(unif.apply(newLhs), typeExtDs, PairOperator.SMALLERDOTWC)); + return Optional.of(new UnifyPair(unif.apply(newLhs), typeExtDs, PairOperator.SMALLERDOTWC)); } @Override - public Optional adaptSup(MPair pair) { + public Optional adaptSup(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -504,7 +504,7 @@ public class RuleSet implements IRuleSet{ for(int i = 1; i < typeDParams.size(); i++) unif.Add((PlaceholderType) typeSupDsgenParams.get(i), typeDParams.get(i)); - return Optional.of(new MPair(unif.apply(newLhs), newRhs, PairOperator.SMALLERDOTWC)); + return Optional.of(new UnifyPair(unif.apply(newLhs), newRhs, PairOperator.SMALLERDOTWC)); } /** @@ -534,12 +534,12 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional> subst(Set pairs) { + public Optional> subst(Set pairs) { HashMap typeMap = new HashMap<>(); Stack occuringTypes = new Stack<>(); - for(MPair pair : pairs) { + for(UnifyPair pair : pairs) { occuringTypes.push(pair.getLhsType()); occuringTypes.push(pair.getRhsType()); } @@ -558,12 +558,12 @@ public class RuleSet implements IRuleSet{ t1.getTypeParams().forEach(x -> occuringTypes.push(x)); } - Queue result1 = new LinkedList(pairs); - ArrayList result = new ArrayList(); + Queue result1 = new LinkedList(pairs); + ArrayList result = new ArrayList(); boolean applied = false; while(!result1.isEmpty()) { - MPair pair = result1.poll(); + UnifyPair pair = result1.poll(); PlaceholderType lhsType = null; UnifyType rhsType; @@ -587,7 +587,7 @@ public class RuleSet implements IRuleSet{ } @Override - public Optional reduceWildcardLow(MPair pair) { + public Optional reduceWildcardLow(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -596,11 +596,11 @@ public class RuleSet implements IRuleSet{ if(!(lhsType instanceof ExtendsType) || !(rhsType instanceof ExtendsType)) return Optional.empty(); - return Optional.of(new MPair(((ExtendsType) lhsType).getExtendedType(), ((ExtendsType) rhsType).getExtendedType(), PairOperator.SMALLERDOT)); + return Optional.of(new UnifyPair(((ExtendsType) lhsType).getExtendedType(), ((ExtendsType) rhsType).getExtendedType(), PairOperator.SMALLERDOT)); } @Override - public Optional reduceWildcardLowRight(MPair pair) { + public Optional reduceWildcardLowRight(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -609,11 +609,11 @@ public class RuleSet implements IRuleSet{ if(!(lhsType instanceof ReferenceType) || !(rhsType instanceof ExtendsType)) return Optional.empty(); - return Optional.of(new MPair(lhsType, ((ExtendsType) rhsType).getExtendedType(), PairOperator.SMALLERDOT)); + return Optional.of(new UnifyPair(lhsType, ((ExtendsType) rhsType).getExtendedType(), PairOperator.SMALLERDOT)); } @Override - public Optional reduceWildcardUp(MPair pair) { + public Optional reduceWildcardUp(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -622,11 +622,11 @@ public class RuleSet implements IRuleSet{ if(!(lhsType instanceof SuperType) || !(rhsType instanceof SuperType)) return Optional.empty(); - return Optional.of(new MPair(((SuperType) rhsType).getSuperedType(), ((SuperType) lhsType).getSuperedType(), PairOperator.SMALLERDOT)); + return Optional.of(new UnifyPair(((SuperType) rhsType).getSuperedType(), ((SuperType) lhsType).getSuperedType(), PairOperator.SMALLERDOT)); } @Override - public Optional reduceWildcardUpRight(MPair pair) { + public Optional reduceWildcardUpRight(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -635,11 +635,11 @@ public class RuleSet implements IRuleSet{ if(!(lhsType instanceof ReferenceType) || !(rhsType instanceof SuperType)) return Optional.empty(); - return Optional.of(new MPair(((SuperType) rhsType).getSuperedType(), lhsType, PairOperator.SMALLERDOTWC)); + return Optional.of(new UnifyPair(((SuperType) rhsType).getSuperedType(), lhsType, PairOperator.SMALLERDOTWC)); } @Override - public Optional reduceWildcardLowUp(MPair pair) { + public Optional reduceWildcardLowUp(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -648,11 +648,11 @@ public class RuleSet implements IRuleSet{ if(!(lhsType instanceof ExtendsType) || !(rhsType instanceof SuperType)) return Optional.empty(); - return Optional.of(new MPair(((ExtendsType) lhsType).getExtendedType(), ((SuperType) rhsType).getSuperedType(), PairOperator.EQUALSDOT)); + return Optional.of(new UnifyPair(((ExtendsType) lhsType).getExtendedType(), ((SuperType) rhsType).getSuperedType(), PairOperator.EQUALSDOT)); } @Override - public Optional reduceWildcardUpLow(MPair pair) { + public Optional reduceWildcardUpLow(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -661,11 +661,11 @@ public class RuleSet implements IRuleSet{ if(!(lhsType instanceof SuperType) || !(rhsType instanceof ExtendsType)) return Optional.empty(); - return Optional.of(new MPair(((SuperType) lhsType).getSuperedType(), ((ExtendsType) rhsType).getExtendedType(), PairOperator.EQUALSDOT)); + return Optional.of(new UnifyPair(((SuperType) lhsType).getSuperedType(), ((ExtendsType) rhsType).getExtendedType(), PairOperator.EQUALSDOT)); } @Override - public Optional reduceWildcardLeft(MPair pair) { + public Optional reduceWildcardLeft(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); @@ -676,13 +676,13 @@ public class RuleSet implements IRuleSet{ UnifyType lhsType = pair.getLhsType(); if(lhsType instanceof WildcardType) - return Optional.of(new MPair(((WildcardType) lhsType).getWildcardedType(), rhsType, PairOperator.EQUALSDOT)); + return Optional.of(new UnifyPair(((WildcardType) lhsType).getWildcardedType(), rhsType, PairOperator.EQUALSDOT)); return Optional.empty(); } @Override - public Optional> reduceFunN(MPair pair) { + public Optional> reduceFunN(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); @@ -698,17 +698,17 @@ public class RuleSet implements IRuleSet{ if(funNLhsType.getN() != funNRhsType.getN()) return Optional.empty(); - Set result = new HashSet(); + Set result = new HashSet(); - result.add(new MPair(funNLhsType.getTypeParams().get(0), funNRhsType.getTypeParams().get(0), PairOperator.SMALLERDOT)); + result.add(new UnifyPair(funNLhsType.getTypeParams().get(0), funNRhsType.getTypeParams().get(0), PairOperator.SMALLERDOT)); for(int i = 1; i < funNLhsType.getTypeParams().size(); i++) - result.add(new MPair(funNRhsType.getTypeParams().get(i), funNLhsType.getTypeParams().get(i), PairOperator.SMALLERDOT)); + result.add(new UnifyPair(funNRhsType.getTypeParams().get(i), funNLhsType.getTypeParams().get(i), PairOperator.SMALLERDOT)); return Optional.of(result); } @Override - public Optional> greaterFunN(MPair pair) { + public Optional> greaterFunN(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); @@ -720,22 +720,22 @@ public class RuleSet implements IRuleSet{ FunNType funNLhsType = (FunNType) lhsType; - Set result = new HashSet(); + Set result = new HashSet(); UnifyType[] freshPlaceholders = new UnifyType[funNLhsType.getTypeParams().size()]; for(int i = 0; i < freshPlaceholders.length; i++) freshPlaceholders[i] = PlaceholderType.freshPlaceholder(); - result.add(new MPair(funNLhsType.getTypeParams().get(0), freshPlaceholders[0], PairOperator.SMALLERDOT)); + result.add(new UnifyPair(funNLhsType.getTypeParams().get(0), freshPlaceholders[0], PairOperator.SMALLERDOT)); for(int i = 1; i < funNLhsType.getTypeParams().size(); i++) - result.add(new MPair(freshPlaceholders[i], funNLhsType.getTypeParams().get(i), PairOperator.SMALLERDOT)); - result.add(new MPair(rhsType, funNLhsType.setTypeParams(new TypeParams(freshPlaceholders)), PairOperator.EQUALSDOT)); + result.add(new UnifyPair(freshPlaceholders[i], funNLhsType.getTypeParams().get(i), PairOperator.SMALLERDOT)); + result.add(new UnifyPair(rhsType, funNLhsType.setTypeParams(new TypeParams(freshPlaceholders)), PairOperator.EQUALSDOT)); return Optional.of(result); } @Override - public Optional> smallerFunN(MPair pair) { + public Optional> smallerFunN(UnifyPair pair) { if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); @@ -747,16 +747,16 @@ public class RuleSet implements IRuleSet{ FunNType funNRhsType = (FunNType) rhsType; - Set result = new HashSet(); + Set result = new HashSet(); UnifyType[] freshPlaceholders = new UnifyType[funNRhsType.getTypeParams().size()]; for(int i = 0; i < freshPlaceholders.length; i++) freshPlaceholders[i] = PlaceholderType.freshPlaceholder(); - result.add(new MPair(freshPlaceholders[0], funNRhsType.getTypeParams().get(0), PairOperator.SMALLERDOT)); + result.add(new UnifyPair(freshPlaceholders[0], funNRhsType.getTypeParams().get(0), PairOperator.SMALLERDOT)); for(int i = 1; i < funNRhsType.getTypeParams().size(); i++) - result.add(new MPair(funNRhsType.getTypeParams().get(i), freshPlaceholders[i], PairOperator.SMALLERDOT)); - result.add(new MPair(lhsType, funNRhsType.setTypeParams(new TypeParams(freshPlaceholders)), PairOperator.EQUALSDOT)); + result.add(new UnifyPair(funNRhsType.getTypeParams().get(i), freshPlaceholders[i], PairOperator.SMALLERDOT)); + result.add(new UnifyPair(lhsType, funNRhsType.setTypeParams(new TypeParams(freshPlaceholders)), PairOperator.EQUALSDOT)); return Optional.of(result); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/Unifier.java index b348b28c6..0a08a210f 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/Unifier.java @@ -2,10 +2,10 @@ package de.dhbwstuttgart.typeinference.unify; import java.util.Set; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; public interface Unifier { - public Set> apply (Set E); + public Set> apply (Set E); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/Unify.java b/src/de/dhbwstuttgart/typeinference/unify/Unify.java index d781a2a40..6f4fa5142 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/Unify.java @@ -17,7 +17,7 @@ import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; @@ -32,19 +32,19 @@ import de.dhbwstuttgart.typeinference.unify.model.Unifier; */ public class Unify { - public Set> unify(Set eq, IFiniteClosure fc) { + public Set> unify(Set eq, IFiniteClosure fc) { /* * Step 1: Repeated application of reduce, adapt, erase, swap */ - Set eq0 = applyTypeUnificationRules(eq, fc); + Set eq0 = applyTypeUnificationRules(eq, fc); /* * Step 2 and 3: Create a subset eq1s of pairs where both sides are TPH and eq2s of the other pairs */ - Set eq1s = new HashSet<>(); - Set eq2s = new HashSet<>(); + Set eq1s = new HashSet<>(); + Set eq2s = new HashSet<>(); splitEq(eq0, eq1s, eq2s); /* @@ -56,21 +56,21 @@ public class Unify { // There are up to 10 toplevel set. 8 of 10 are the result of the // cartesian product of the sets created by pattern matching. - List>> topLevelSets = new ArrayList<>(); + List>> topLevelSets = new ArrayList<>(); if(eq1s.size() != 0) { - Set> wrap = new HashSet<>(); + Set> wrap = new HashSet<>(); wrap.add(eq1s); topLevelSets.add(wrap); // Add Eq1' } // Add the set of [a =. Theta | (a=. Theta) in Eq2'] - Set bufferSet = eq2s.stream() + Set bufferSet = eq2s.stream() .filter(x -> x.getPairOp() == PairOperator.EQUALSDOT && x.getLhsType() instanceof PlaceholderType) .collect(Collectors.toSet()); if(bufferSet.size() != 0) { - Set> wrap = new HashSet<>(); + Set> wrap = new HashSet<>(); wrap.add(bufferSet); topLevelSets.add(wrap); eq2s.removeAll(bufferSet); @@ -78,8 +78,8 @@ public class Unify { // Sets that originate from pair pattern matching // Sets of the "second level" - Set undefinedPairs = new HashSet<>(); - Set>>> secondLevelSets = calculatePairSets(eq2s, fc, undefinedPairs); + Set undefinedPairs = new HashSet<>(); + Set>>> secondLevelSets = calculatePairSets(eq2s, fc, undefinedPairs); // If pairs occured that did not match one of the cartesian product cases, // those pairs are contradictory and the unification is impossible. @@ -92,17 +92,17 @@ public class Unify { ISetOperations setOps = new GuavaSetOperations(); // Sub cartesian products of the second level (pattern matched) sets - for(Set>> secondLevelSet : secondLevelSets) { - List>> secondLevelSetList = new ArrayList<>(secondLevelSet); - Set>> cartResult = setOps.cartesianProduct(secondLevelSetList); + for(Set>> secondLevelSet : secondLevelSets) { + List>> secondLevelSetList = new ArrayList<>(secondLevelSet); + Set>> cartResult = setOps.cartesianProduct(secondLevelSetList); - Set> flat = new HashSet<>(); + Set> flat = new HashSet<>(); cartResult.stream().forEach(x -> flat.addAll(x)); topLevelSets.add(flat); } // Cartesian product over all (up to 10) top level sets - Set>> eqPrimeSet = setOps.cartesianProduct(topLevelSets) + Set>> eqPrimeSet = setOps.cartesianProduct(topLevelSets) .stream().map(x -> new HashSet<>(x)) .collect(Collectors.toCollection(HashSet::new)); //System.out.println(result); @@ -111,19 +111,19 @@ public class Unify { * Step 5: Substitution */ - Set> eqPrimeSetFlat = new HashSet<>(); - for(Set> setToFlatten : eqPrimeSet) { - Set buffer = new HashSet<>(); + Set> eqPrimeSetFlat = new HashSet<>(); + for(Set> setToFlatten : eqPrimeSet) { + Set buffer = new HashSet<>(); setToFlatten.stream().forEach(x -> buffer.addAll(x)); eqPrimeSetFlat.add(buffer); } IRuleSet rules = new RuleSet(fc); - Set> changed = new HashSet<>(); - Set> eqPrimePrimeSet = new HashSet<>(); + Set> changed = new HashSet<>(); + Set> eqPrimePrimeSet = new HashSet<>(); - for(Set eqPrime : eqPrimeSetFlat) { - Optional> eqPrimePrime = rules.subst(eqPrime); + for(Set eqPrime : eqPrimeSetFlat) { + Optional> eqPrimePrime = rules.subst(eqPrime); if(eqPrime.equals(eq)) eqPrimePrimeSet.add(eqPrime); @@ -142,7 +142,7 @@ public class Unify { * b) Build the union over everything */ - for(Set eqss : changed) + for(Set eqss : changed) eqPrimePrimeSet.addAll(this.unify(eqss, fc)); /* @@ -152,8 +152,8 @@ public class Unify { } - protected boolean isSolvedForm(Set eqPrimePrime) { - for(MPair pair : eqPrimePrime) { + protected boolean isSolvedForm(Set eqPrimePrime) { + for(UnifyPair pair : eqPrimePrime) { UnifyType lhsType = pair.getLhsType(); UnifyType rhsType = pair.getRhsType(); @@ -167,7 +167,7 @@ public class Unify { return true; } - protected Set applyTypeUnificationRules(Set eq, IFiniteClosure fc) { + protected Set applyTypeUnificationRules(Set eq, IFiniteClosure fc) { /* * Rule Application Strategy: @@ -182,8 +182,8 @@ public class Unify { */ - LinkedHashSet targetSet = new LinkedHashSet(); - LinkedList eqQueue = new LinkedList<>(); + LinkedHashSet targetSet = new LinkedHashSet(); + LinkedList eqQueue = new LinkedList<>(); IRuleSet rules = new RuleSet(fc); /* @@ -195,10 +195,10 @@ public class Unify { * Apply rules until the queue is empty */ while(!eqQueue.isEmpty()) { - MPair pair = eqQueue.pollFirst(); + UnifyPair pair = eqQueue.pollFirst(); // ReduceUp, ReduceLow, ReduceUpLow - Optional opt = rules.reduceUpLow(pair); + Optional opt = rules.reduceUpLow(pair); opt = opt.isPresent() ? opt : rules.reduceLow(pair); opt = opt.isPresent() ? opt : rules.reduceUp(pair); opt = opt.isPresent() ? opt : rules.reduceWildcardLow(pair); @@ -216,7 +216,7 @@ public class Unify { } // Reduce1, Reduce2, ReduceExt, ReduceSup, ReduceEq - Optional> optSet = rules.reduce1(pair); + Optional> optSet = rules.reduce1(pair); optSet = optSet.isPresent() ? optSet : rules.reduce2(pair); optSet = optSet.isPresent() ? optSet : rules.reduceExt(pair); optSet = optSet.isPresent() ? optSet : rules.reduceSup(pair); @@ -246,9 +246,9 @@ public class Unify { return targetSet; } - protected void swapAddOrErase(MPair pair, IRuleSet rules, Collection collection) { - Optional opt = rules.swap(pair); - MPair pair2 = opt.isPresent() ? opt.get() : pair; + protected void swapAddOrErase(UnifyPair pair, IRuleSet rules, Collection collection) { + Optional opt = rules.swap(pair); + UnifyPair pair2 = opt.isPresent() ? opt.get() : pair; if(rules.erase1(pair2) || rules.erase3(pair2) || rules.erase2(pair2)) return; @@ -256,8 +256,8 @@ public class Unify { collection.add(pair2); } - protected void splitEq(Set eq, Set eq1s, Set eq2s) { - for(MPair pair : eq) + protected void splitEq(Set eq, Set eq1s, Set eq2s) { + for(UnifyPair pair : eq) if(pair.getLhsType() instanceof PlaceholderType && pair.getRhsType() instanceof PlaceholderType) eq1s.add(pair); else @@ -265,14 +265,14 @@ public class Unify { } - protected Set>>> calculatePairSets(Set eq2s, IFiniteClosure fc, Set undefined) { - List>>> result = new ArrayList<>(); + protected Set>>> calculatePairSets(Set eq2s, IFiniteClosure fc, Set undefined) { + List>>> result = new ArrayList<>(); // Init all 8 cases for(int i = 0; i < 8; i++) result.add(new HashSet<>()); - for(MPair pair : eq2s) { + for(UnifyPair pair : eq2s) { PairOperator pairOp = pair.getPairOp(); UnifyType lhsType = pair.getLhsType(); @@ -321,8 +321,8 @@ public class Unify { .filter(x -> x.size() > 0).collect(Collectors.toCollection(HashSet::new)); } - protected Set> unifyCase1(PlaceholderType a, UnifyType thetaPrime, IFiniteClosure fc) { - Set> result = new HashSet<>(); + protected Set> unifyCase1(PlaceholderType a, UnifyType thetaPrime, IFiniteClosure fc) { + Set> result = new HashSet<>(); IUnify unify = new MartelliMontanariUnify(); Set cs = fc.getAllTypesByName(thetaPrime.getName()); @@ -352,15 +352,15 @@ public class Unify { continue; Unifier unifier = opt.get(); - Set substitutionSet = new HashSet<>(); + Set substitutionSet = new HashSet<>(); for (Entry sigma : unifier.getSubstitutions()) - substitutionSet.add(new MPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); + substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); for (UnifyType tq : thetaQs) { Set smaller = fc.smaller(unifier.apply(tq)); for(UnifyType theta : smaller) { - Set resultPrime = new HashSet<>(); - resultPrime.add(new MPair(a, theta, PairOperator.EQUALSDOT)); + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, theta, PairOperator.EQUALSDOT)); resultPrime.addAll(substitutionSet); result.add(resultPrime); } @@ -372,8 +372,8 @@ public class Unify { return result; } - protected Set> unifyCase2(PlaceholderType a, ExtendsType extThetaPrime, IFiniteClosure fc) { - Set> result = new HashSet<>(); + protected Set> unifyCase2(PlaceholderType a, ExtendsType extThetaPrime, IFiniteClosure fc) { + Set> result = new HashSet<>(); IUnify unify = new MartelliMontanariUnify(); UnifyType thetaPrime = extThetaPrime.getExtendedType(); @@ -405,16 +405,16 @@ public class Unify { Unifier unifier = opt.get(); - Set substitutionSet = new HashSet<>(); + Set substitutionSet = new HashSet<>(); for (Entry sigma : unifier.getSubstitutions()) - substitutionSet.add(new MPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); + substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); for (UnifyType tq : thetaQs) { ExtendsType extTq = new ExtendsType(tq); Set smArg = fc.smArg(unifier.apply(extTq)); for(UnifyType theta : smArg) { - Set resultPrime = new HashSet<>(); - resultPrime.add(new MPair(a, theta, PairOperator.EQUALSDOT)); + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, theta, PairOperator.EQUALSDOT)); resultPrime.addAll(substitutionSet); result.add(resultPrime); } @@ -426,60 +426,60 @@ public class Unify { return result; } - protected Set> unifyCase3(PlaceholderType a, SuperType subThetaPrime, IFiniteClosure fc) { - Set> result = new HashSet<>(); + protected Set> unifyCase3(PlaceholderType a, SuperType subThetaPrime, IFiniteClosure fc) { + Set> result = new HashSet<>(); UnifyType aPrime = PlaceholderType.freshPlaceholder(); UnifyType supAPrime = new SuperType(aPrime); UnifyType thetaPrime = subThetaPrime.getSuperedType(); //for(UnifyType theta : fc.smArg(subThetaPrime)) { - Set resultPrime = new HashSet<>(); - resultPrime.add(new MPair(a, aPrime, PairOperator.EQUALSDOT)); - resultPrime.add(new MPair(thetaPrime, aPrime, PairOperator.SMALLERDOT)); + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, aPrime, PairOperator.EQUALSDOT)); + resultPrime.add(new UnifyPair(thetaPrime, aPrime, PairOperator.SMALLERDOT)); result.add(resultPrime); resultPrime = new HashSet<>(); - resultPrime.add(new MPair(a, supAPrime, PairOperator.EQUALSDOT)); - resultPrime.add(new MPair(thetaPrime, aPrime, PairOperator.SMALLERDOT)); + resultPrime.add(new UnifyPair(a, supAPrime, PairOperator.EQUALSDOT)); + resultPrime.add(new UnifyPair(thetaPrime, aPrime, PairOperator.SMALLERDOT)); result.add(resultPrime); //} return result; } - protected Set> unifyCase4(PlaceholderType a, UnifyType thetaPrime, IFiniteClosure fc) { - Set> result = new HashSet<>(); - Set resultPrime = new HashSet<>(); - resultPrime.add(new MPair(a, thetaPrime, PairOperator.EQUALSDOT)); + protected Set> unifyCase4(PlaceholderType a, UnifyType thetaPrime, IFiniteClosure fc) { + Set> result = new HashSet<>(); + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, thetaPrime, PairOperator.EQUALSDOT)); result.add(resultPrime); return result; } - protected Set> unifyCase5(UnifyType theta, PlaceholderType a, IFiniteClosure fc) { - Set> result = new HashSet<>(); + protected Set> unifyCase5(UnifyType theta, PlaceholderType a, IFiniteClosure fc) { + Set> result = new HashSet<>(); for(UnifyType thetaS : fc.greater(theta)) { - Set resultPrime = new HashSet<>(); - resultPrime.add(new MPair(a, thetaS, PairOperator.EQUALSDOT)); + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, thetaS, PairOperator.EQUALSDOT)); result.add(resultPrime); } return result; } - protected Set> unifyCase6(ExtendsType extTheta, PlaceholderType a, IFiniteClosure fc) { - Set> result = new HashSet<>(); + protected Set> unifyCase6(ExtendsType extTheta, PlaceholderType a, IFiniteClosure fc) { + Set> result = new HashSet<>(); for(UnifyType thetaS : fc.grArg(extTheta)) { - Set resultPrime = new HashSet<>(); - resultPrime.add(new MPair(a, thetaS, PairOperator.EQUALSDOT)); + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, thetaS, PairOperator.EQUALSDOT)); result.add(resultPrime); } return result; } - protected Set> unifyCase7(SuperType supTheta, PlaceholderType a, IFiniteClosure fc) { - Set> result = new HashSet<>(); + protected Set> unifyCase7(SuperType supTheta, PlaceholderType a, IFiniteClosure fc) { + Set> result = new HashSet<>(); IUnify unify = new MartelliMontanariUnify(); UnifyType theta = supTheta.getSuperedType(); @@ -511,16 +511,16 @@ public class Unify { Unifier unifier = opt.get(); - Set substitutionSet = new HashSet<>(); + Set substitutionSet = new HashSet<>(); for (Entry sigma : unifier.getSubstitutions()) - substitutionSet.add(new MPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); + substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); for (UnifyType tq : thetaQs) { Set smaller = fc.smaller(unifier.apply(tq)); for(UnifyType thetaPrime : smaller) { - Set resultPrime = new HashSet<>(); - resultPrime.add(new MPair(a, new SuperType(thetaPrime), PairOperator.EQUALSDOT)); + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, new SuperType(thetaPrime), PairOperator.EQUALSDOT)); resultPrime.addAll(substitutionSet); result.add(resultPrime); } @@ -532,11 +532,11 @@ public class Unify { return result; } - protected Set> unifyCase8(UnifyType theta, PlaceholderType a, IFiniteClosure fc) { - Set> result = new HashSet<>(); + protected Set> unifyCase8(UnifyType theta, PlaceholderType a, IFiniteClosure fc) { + Set> result = new HashSet<>(); for(UnifyType thetaS : fc.grArg(theta)) { - Set resultPrime = new HashSet<>(); - resultPrime.add(new MPair(a, thetaS, PairOperator.EQUALSDOT)); + Set resultPrime = new HashSet<>(); + resultPrime.add(new UnifyPair(a, thetaS, PairOperator.EQUALSDOT)); result.add(resultPrime); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java index 23c444e08..11c98481f 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java @@ -3,46 +3,46 @@ package de.dhbwstuttgart.typeinference.unify.interfaces; import java.util.Optional; import java.util.Set; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; public interface IRuleSet { - public Optional reduceUp(MPair pair); - public Optional reduceLow(MPair pair); - public Optional reduceUpLow(MPair pair); - public Optional> reduceExt(MPair pair); - public Optional> reduceSup(MPair pair); - public Optional> reduceEq(MPair pair); - public Optional> reduce1(MPair pair); - public Optional> reduce2(MPair pair); + public Optional reduceUp(UnifyPair pair); + public Optional reduceLow(UnifyPair pair); + public Optional reduceUpLow(UnifyPair pair); + public Optional> reduceExt(UnifyPair pair); + public Optional> reduceSup(UnifyPair pair); + public Optional> reduceEq(UnifyPair pair); + public Optional> reduce1(UnifyPair pair); + public Optional> reduce2(UnifyPair pair); /* * Missing Rules */ - public Optional reduceWildcardLow(MPair pair); - public Optional reduceWildcardLowRight(MPair pair); - public Optional reduceWildcardUp(MPair pair); - public Optional reduceWildcardUpRight(MPair pair); - public Optional reduceWildcardLowUp(MPair pair); - public Optional reduceWildcardUpLow(MPair pair); - public Optional reduceWildcardLeft(MPair pair); + public Optional reduceWildcardLow(UnifyPair pair); + public Optional reduceWildcardLowRight(UnifyPair pair); + public Optional reduceWildcardUp(UnifyPair pair); + public Optional reduceWildcardUpRight(UnifyPair pair); + public Optional reduceWildcardLowUp(UnifyPair pair); + public Optional reduceWildcardUpLow(UnifyPair pair); + public Optional reduceWildcardLeft(UnifyPair pair); /* * FunN Rules */ - public Optional> reduceFunN(MPair pair); - public Optional> greaterFunN(MPair pair); - public Optional> smallerFunN(MPair pair); + public Optional> reduceFunN(UnifyPair pair); + public Optional> greaterFunN(UnifyPair pair); + public Optional> smallerFunN(UnifyPair pair); - public boolean erase1(MPair pair); - public boolean erase2(MPair pair); - public boolean erase3(MPair pair); + public boolean erase1(UnifyPair pair); + public boolean erase2(UnifyPair pair); + public boolean erase3(UnifyPair pair); - public Optional swap(MPair pair); + public Optional swap(UnifyPair pair); - public Optional adapt(MPair pair); - public Optional adaptExt(MPair pair); - public Optional adaptSup(MPair pair); + public Optional adapt(UnifyPair pair); + public Optional adaptExt(UnifyPair pair); + public Optional adaptSup(UnifyPair pair); - public Optional> subst(Set pair); + public Optional> subst(Set pair); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index 5ef8c93a6..6fff42cce 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -4,32 +4,30 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; - + import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.unify.MartelliMontanariUnify; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; -import de.dhbwstuttgart.typeinference.unify.model.PairOperator; public class FiniteClosure implements IFiniteClosure { private HashMap> inheritanceGraph; private HashMap>> strInheritanceGraph; - private Set pairs; + private Set pairs; private Set basicTypes; //TODO im konstruktor mitgeben um typenabzuhandeln die keine extends beziehung haben. (Damit die FC diese Typen auch kennt) //(ALternative: immer die extends zu object beziehung einfügen) - public FiniteClosure(Set pairs) { + public FiniteClosure(Set pairs) { this.pairs = new HashSet<>(pairs); inheritanceGraph = new HashMap>(); // Build the transitive closure of the inheritance tree - for(MPair pair : pairs) { + for(UnifyPair pair : pairs) { if(pair.getPairOp() != PairOperator.SMALLER) continue; @@ -328,7 +326,7 @@ public class FiniteClosure implements IFiniteClosure { if(!strInheritanceGraph.containsKey(t.getName())) return false; - for(MPair pair : pairs) + for(UnifyPair pair : pairs) if(pair.getLhsType().equals(t)) return true; @@ -347,7 +345,7 @@ public class FiniteClosure implements IFiniteClosure { if(!strInheritanceGraph.containsKey(t.getName())) return Optional.empty(); - for(MPair pair : pairs) + for(UnifyPair pair : pairs) if(pair.getLhsType().getName().equals(t.getName())) return Optional.of(pair.getLhsType()); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java index 971566e69..a46ee6d3d 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java @@ -37,8 +37,8 @@ public class Unifier implements Function /*, Set*/ return t.apply(this); } - public MPair apply(MPair p) { - return new MPair(this.apply(p.getLhsType()), this.apply(p.getRhsType()), p.getPairOp()); + public UnifyPair apply(UnifyPair p) { + return new UnifyPair(this.apply(p.getLhsType()), this.apply(p.getRhsType()), p.getPairOp()); } public boolean hasSubstitute(PlaceholderType t) { diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java similarity index 53% rename from src/de/dhbwstuttgart/typeinference/unify/model/MPair.java rename to src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java index 254ba142a..cdc7b8186 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java @@ -1,6 +1,6 @@ package de.dhbwstuttgart.typeinference.unify.model; -public class MPair { +public class UnifyPair { private UnifyType lhs; private UnifyType rhs; @@ -12,7 +12,7 @@ public class MPair { pairOp = PairOperator.SMALLER; }*/ - public MPair(UnifyType t1, UnifyType t2, PairOperator op) { + public UnifyPair(UnifyType t1, UnifyType t2, PairOperator op) { lhs = t1; rhs = t2; pairOp = op; @@ -32,33 +32,16 @@ public class MPair { @Override public boolean equals(Object obj) { - if(!(obj instanceof MPair)) + if(!(obj instanceof UnifyPair)) return false; - MPair other = (MPair) obj; + UnifyPair other = (UnifyPair) obj; return other.getPairOp() == pairOp && other.getLhsType().equals(lhs) && other.getRhsType().equals(rhs); } - - /** - * Substitutes the occurrences of Type t on the left or right side of the pair with the Type subst. - * @param t Type to be replaced. - * @param subst The type replacing t. - * @return A pair where occurrences of t are replaced by subst. - */ - public MPair substitute(UnifyType t, UnifyType subst) { - UnifyType newlhs = lhs; - if(lhs.equals(t)) newlhs = subst; - - UnifyType newrhs = rhs; - if(rhs.equals(t)) newrhs = subst; - - if(newlhs == lhs && newrhs == rhs) return this; - return new MPair(newlhs, newrhs, pairOp); - } - + @Override public int hashCode() { return 17 + 31 * lhs.hashCode() + 31 * rhs.hashCode() + 31 * pairOp.hashCode(); diff --git a/test/unify/FiniteClosureBuilder.java b/test/unify/FiniteClosureBuilder.java index dcda97fd0..8b8b5f733 100644 --- a/test/unify/FiniteClosureBuilder.java +++ b/test/unify/FiniteClosureBuilder.java @@ -5,16 +5,16 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; public class FiniteClosureBuilder { - private Set pairs = new HashSet<>(); + private Set pairs = new HashSet<>(); public void add(UnifyType sub, UnifyType sup) { - pairs.add(new MPair(sub, sup, PairOperator.SMALLER)); + pairs.add(new UnifyPair(sub, sup, PairOperator.SMALLER)); } public IFiniteClosure getFiniteClosure() { diff --git a/test/unify/FiniteClosureTest.java b/test/unify/FiniteClosureTest.java index 87e648a5a..519cf5096 100644 --- a/test/unify/FiniteClosureTest.java +++ b/test/unify/FiniteClosureTest.java @@ -10,7 +10,7 @@ import org.junit.Assert; import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; diff --git a/test/unify/RuleSetTest.java b/test/unify/RuleSetTest.java index dad10d5d2..0dc9f319a 100644 --- a/test/unify/RuleSetTest.java +++ b/test/unify/RuleSetTest.java @@ -11,7 +11,7 @@ import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.RuleSet; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.ReferenceType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; @@ -28,14 +28,14 @@ public class RuleSetTest { * Positive Tests */ - MPair reduce1 = new MPair(tf.getSimpleType("type"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); - MPair reduce2 = new MPair(tf.getPlaceholderType("T"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); + UnifyPair reduce1 = new UnifyPair(tf.getSimpleType("type"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); + UnifyPair reduce2 = new UnifyPair(tf.getPlaceholderType("T"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); - MPair expected1 = new MPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); - MPair expected2 = new MPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); + UnifyPair expected1 = new UnifyPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); + UnifyPair expected2 = new UnifyPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); - Optional opt1 = rules.reduceUp(reduce1); - Optional opt2 = rules.reduceUp(reduce2); + Optional opt1 = rules.reduceUp(reduce1); + Optional opt2 = rules.reduceUp(reduce2); Assert.assertTrue(opt1.isPresent()); Assert.assertTrue(opt2.isPresent()); @@ -46,10 +46,10 @@ public class RuleSetTest { * Negative Tests */ - MPair noreduce1 = new MPair(tf.getSuperType(tf.getSimpleType("type")), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); - MPair noreduce2 = new MPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); - MPair noreduce3 = new MPair(tf.getPlaceholderType("T"), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); - MPair noreduce4 = new MPair(tf.getPlaceholderType("T"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.EQUALSDOT); + UnifyPair noreduce1 = new UnifyPair(tf.getSuperType(tf.getSimpleType("type")), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); + UnifyPair noreduce2 = new UnifyPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); + UnifyPair noreduce3 = new UnifyPair(tf.getPlaceholderType("T"), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); + UnifyPair noreduce4 = new UnifyPair(tf.getPlaceholderType("T"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.EQUALSDOT); Assert.assertFalse(rules.reduceUp(noreduce1).isPresent()); Assert.assertFalse(rules.reduceUp(noreduce2).isPresent()); @@ -65,14 +65,14 @@ public class RuleSetTest { /* * Positive Tests */ - MPair reduce1 = new MPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getSimpleType("type"), PairOperator.SMALLERDOT); - MPair reduce2 = new MPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT); + UnifyPair reduce1 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getSimpleType("type"), PairOperator.SMALLERDOT); + UnifyPair reduce2 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT); - MPair expected1 = new MPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); - MPair expected2 = new MPair(tf.getSimpleType("type"), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT); + UnifyPair expected1 = new UnifyPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); + UnifyPair expected2 = new UnifyPair(tf.getSimpleType("type"), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT); - Optional opt1 = rules.reduceLow(reduce1); - Optional opt2 = rules.reduceLow(reduce2); + Optional opt1 = rules.reduceLow(reduce1); + Optional opt2 = rules.reduceLow(reduce2); Assert.assertTrue(opt1.isPresent()); Assert.assertTrue(opt2.isPresent()); @@ -82,10 +82,10 @@ public class RuleSetTest { /* * Negative Tests */ - MPair noreduce1 = new MPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); - MPair noreduce2 = new MPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); - MPair noreduce3 = new MPair(tf.getSuperType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.SMALLERDOT); - MPair noreduce4 = new MPair(tf.getExtendsType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.EQUALSDOT); + UnifyPair noreduce1 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); + UnifyPair noreduce2 = new UnifyPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); + UnifyPair noreduce3 = new UnifyPair(tf.getSuperType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.SMALLERDOT); + UnifyPair noreduce4 = new UnifyPair(tf.getExtendsType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.EQUALSDOT); Assert.assertFalse(rules.reduceLow(noreduce1).isPresent()); Assert.assertFalse(rules.reduceLow(noreduce2).isPresent()); @@ -101,14 +101,14 @@ public class RuleSetTest { /* * Positive Tests */ - MPair reduce1 = new MPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); - MPair reduce2 = new MPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getSuperType(tf.getPlaceholderType("T")), PairOperator.SMALLERDOT); + UnifyPair reduce1 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); + UnifyPair reduce2 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getSuperType(tf.getPlaceholderType("T")), PairOperator.SMALLERDOT); - MPair expected1 = new MPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); - MPair expected2 = new MPair(tf.getSimpleType("type"), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT); + UnifyPair expected1 = new UnifyPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT); + UnifyPair expected2 = new UnifyPair(tf.getSimpleType("type"), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT); - Optional opt1 = rules.reduceUpLow(reduce1); - Optional opt2 = rules.reduceUpLow(reduce2); + Optional opt1 = rules.reduceUpLow(reduce1); + Optional opt2 = rules.reduceUpLow(reduce2); Assert.assertTrue(opt1.isPresent()); Assert.assertTrue(opt2.isPresent()); @@ -118,10 +118,10 @@ public class RuleSetTest { /* * Negative Tests */ - MPair noreduce1 = new MPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); - MPair noreduce2 = new MPair(tf.getSuperType(tf.getPlaceholderType("T")), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); - MPair noreduce3 = new MPair(tf.getSuperType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.SMALLERDOT); - MPair noreduce4 = new MPair(tf.getExtendsType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.EQUALSDOT); + UnifyPair noreduce1 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); + UnifyPair noreduce2 = new UnifyPair(tf.getSuperType(tf.getPlaceholderType("T")), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT); + UnifyPair noreduce3 = new UnifyPair(tf.getSuperType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.SMALLERDOT); + UnifyPair noreduce4 = new UnifyPair(tf.getExtendsType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.EQUALSDOT); Assert.assertFalse(rules.reduceUpLow(noreduce1).isPresent()); Assert.assertFalse(rules.reduceUpLow(noreduce2).isPresent()); @@ -154,13 +154,13 @@ public class RuleSetTest { fcb.add(buffer, d1); IRuleSet rules = new RuleSet(fcb.getFiniteClosure()); - MPair pair = new MPair(c2, d2, PairOperator.SMALLERDOT); + UnifyPair pair = new UnifyPair(c2, d2, PairOperator.SMALLERDOT); System.out.println("------ Reduce1 ------"); - Optional> res = rules.reduce1(pair); + Optional> res = rules.reduce1(pair); System.out.println(res); - pair = new MPair(c2, c2, PairOperator.SMALLERDOT); + pair = new UnifyPair(c2, c2, PairOperator.SMALLERDOT); res = rules.reduce1(pair); System.out.println(res); @@ -169,17 +169,17 @@ public class RuleSetTest { */ // Case 1: D <. C and C <* D - pair = new MPair(d2, c2, PairOperator.SMALLERDOT); + pair = new UnifyPair(d2, c2, PairOperator.SMALLERDOT); Assert.assertFalse(rules.reduce1(pair).isPresent()); // Case 2: D =. C - pair = new MPair(c2, d2, PairOperator.EQUALSDOT); + pair = new UnifyPair(c2, d2, PairOperator.EQUALSDOT); Assert.assertFalse(rules.reduce1(pair).isPresent()); // Case 3: C <. D and !(C <* D) fcb.clear(); rules = new RuleSet(fcb.getFiniteClosure()); - pair = new MPair(c1, d1, PairOperator.SMALLERDOT); + pair = new UnifyPair(c1, d1, PairOperator.SMALLERDOT); Assert.assertFalse(rules.reduce1(pair).isPresent()); } @@ -199,17 +199,17 @@ public class RuleSetTest { // C ReferenceType c2 = tf.getSimpleType("C", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2"))); - MPair pair1 = new MPair(c1, c2, PairOperator.EQUALSDOT); - MPair pair2 = new MPair(tf.getExtendsType(c1), tf.getExtendsType(c2), PairOperator.EQUALSDOT); - MPair pair3 = new MPair(tf.getSuperType(c1), tf.getSuperType(c2), PairOperator.EQUALSDOT); + UnifyPair pair1 = new UnifyPair(c1, c2, PairOperator.EQUALSDOT); + UnifyPair pair2 = new UnifyPair(tf.getExtendsType(c1), tf.getExtendsType(c2), PairOperator.EQUALSDOT); + UnifyPair pair3 = new UnifyPair(tf.getSuperType(c1), tf.getSuperType(c2), PairOperator.EQUALSDOT); - Optional> opt1 = rules.reduce2(pair1); + Optional> opt1 = rules.reduce2(pair1); System.out.println(opt1); - Optional> opt2 = rules.reduce2(pair2); + Optional> opt2 = rules.reduce2(pair2); System.out.println(opt2); - Optional> opt3 = rules.reduce2(pair3); + Optional> opt3 = rules.reduce2(pair3); System.out.println(opt3); @@ -218,10 +218,10 @@ public class RuleSetTest { */ ReferenceType d1 = tf.getSimpleType("D", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2"))); - pair1 = new MPair(d1, c1, PairOperator.EQUALSDOT); // Case 1: D =. C - pair2 = new MPair(tf.getExtendsType(c1), c2, PairOperator.EQUALSDOT); // Case 2: ? extends C =. C - pair3 = new MPair(tf.getExtendsType(c1), tf.getSuperType(c2), PairOperator.EQUALSDOT); // Case 3: ? extends C =. ? super C - MPair pair4 = new MPair(c1, c2, PairOperator.SMALLERDOT); // Case 4: C <. C + pair1 = new UnifyPair(d1, c1, PairOperator.EQUALSDOT); // Case 1: D =. C + pair2 = new UnifyPair(tf.getExtendsType(c1), c2, PairOperator.EQUALSDOT); // Case 2: ? extends C =. C + pair3 = new UnifyPair(tf.getExtendsType(c1), tf.getSuperType(c2), PairOperator.EQUALSDOT); // Case 3: ? extends C =. ? super C + UnifyPair pair4 = new UnifyPair(c1, c2, PairOperator.SMALLERDOT); // Case 4: C <. C Assert.assertFalse(rules.reduceEq(pair1).isPresent()); Assert.assertFalse(rules.reduceEq(pair2).isPresent()); @@ -254,18 +254,18 @@ public class RuleSetTest { fcb.add(buffer, y1); IRuleSet rules = new RuleSet(fcb.getFiniteClosure()); - MPair pair1 = new MPair(x2, extY1, PairOperator.SMALLERDOTWC); - MPair pair2 = new MPair(tf.getExtendsType(x2), extY1, PairOperator.SMALLERDOTWC); - MPair pair3 = new MPair(extY1, extY1, PairOperator.SMALLERDOTWC); + UnifyPair pair1 = new UnifyPair(x2, extY1, PairOperator.SMALLERDOTWC); + UnifyPair pair2 = new UnifyPair(tf.getExtendsType(x2), extY1, PairOperator.SMALLERDOTWC); + UnifyPair pair3 = new UnifyPair(extY1, extY1, PairOperator.SMALLERDOTWC); System.out.println("------ ReduceExt ------"); - Optional> opt1 = rules.reduceExt(pair1); + Optional> opt1 = rules.reduceExt(pair1); System.out.println(opt1); - Optional> opt2 = rules.reduceExt(pair2); + Optional> opt2 = rules.reduceExt(pair2); System.out.println(opt2); - Optional> opt3 = rules.reduceExt(pair3); + Optional> opt3 = rules.reduceExt(pair3); System.out.println(opt3); /* @@ -273,21 +273,21 @@ public class RuleSetTest { */ // Case 1: X <.? Y - pair1 = new MPair(x2, extY1.getExtendedType(), PairOperator.SMALLERDOTWC); + pair1 = new UnifyPair(x2, extY1.getExtendedType(), PairOperator.SMALLERDOTWC); Assert.assertFalse(rules.reduceExt(pair1).isPresent()); // Case 2: X =. ? super Y - pair2 = new MPair(x2, extY1, PairOperator.EQUALSDOT); + pair2 = new UnifyPair(x2, extY1, PairOperator.EQUALSDOT); Assert.assertFalse(rules.reduceExt(pair2).isPresent()); // Case 3: ? extends Y <.? ? extends X - pair3 = new MPair(extY1, tf.getExtendsType(x2), PairOperator.SMALLERDOTWC); + pair3 = new UnifyPair(extY1, tf.getExtendsType(x2), PairOperator.SMALLERDOTWC); Assert.assertFalse(rules.reduceExt(pair3).isPresent()); // Case 4: X <. ? extends Y and ? extends Y not in grArg(X) fcb.clear(); rules = new RuleSet(fcb.getFiniteClosure()); - pair1 = new MPair(x2, extY1, PairOperator.SMALLERDOTWC); + pair1 = new UnifyPair(x2, extY1, PairOperator.SMALLERDOTWC); Assert.assertFalse(rules.reduceExt(pair1).isPresent()); } @@ -316,18 +316,18 @@ public class RuleSetTest { fcb.add(buffer, x1); IRuleSet rules = new RuleSet(fcb.getFiniteClosure()); - MPair pair1 = new MPair(x2, supY1, PairOperator.SMALLERDOTWC); - MPair pair2 = new MPair(tf.getSuperType(x2), supY1, PairOperator.SMALLERDOTWC); - MPair pair3 = new MPair(supY1, supY1, PairOperator.SMALLERDOTWC); + UnifyPair pair1 = new UnifyPair(x2, supY1, PairOperator.SMALLERDOTWC); + UnifyPair pair2 = new UnifyPair(tf.getSuperType(x2), supY1, PairOperator.SMALLERDOTWC); + UnifyPair pair3 = new UnifyPair(supY1, supY1, PairOperator.SMALLERDOTWC); System.out.println("------ ReduceSup ------"); - Optional> opt1 = rules.reduceSup(pair1); + Optional> opt1 = rules.reduceSup(pair1); System.out.println(opt1); - Optional> opt2 = rules.reduceSup(pair2); + Optional> opt2 = rules.reduceSup(pair2); System.out.println(opt2); - Optional> opt3 = rules.reduceSup(pair3); + Optional> opt3 = rules.reduceSup(pair3); System.out.println(opt3); /* @@ -335,21 +335,21 @@ public class RuleSetTest { */ // Case 1: X <.? Y - pair1 = new MPair(x2, supY1.getSuperedType(), PairOperator.SMALLERDOTWC); + pair1 = new UnifyPair(x2, supY1.getSuperedType(), PairOperator.SMALLERDOTWC); Assert.assertFalse(rules.reduceSup(pair1).isPresent()); // Case 2: X =. ? super Y - pair2 = new MPair(x2, supY1, PairOperator.EQUALSDOT); + pair2 = new UnifyPair(x2, supY1, PairOperator.EQUALSDOT); Assert.assertFalse(rules.reduceSup(pair2).isPresent()); // Case 3: ? super Y <.? ? super X - pair3 = new MPair(supY1, tf.getSuperType(x2), PairOperator.SMALLERDOTWC); + pair3 = new UnifyPair(supY1, tf.getSuperType(x2), PairOperator.SMALLERDOTWC); Assert.assertFalse(rules.reduceSup(pair3).isPresent()); // Case 4: X <. ? super Y and ? super Y not in grArg(X) fcb.clear(); rules = new RuleSet(fcb.getFiniteClosure()); - pair1 = new MPair(x2, supY1, PairOperator.SMALLERDOTWC); + pair1 = new UnifyPair(x2, supY1, PairOperator.SMALLERDOTWC); Assert.assertFalse(rules.reduceSup(pair1).isPresent()); } @@ -369,8 +369,8 @@ public class RuleSetTest { // C ReferenceType c2 = tf.getSimpleType("C", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2"))); - MPair pair = new MPair(c1, c2, PairOperator.SMALLERDOTWC); - Optional> res = rules.reduceEq(pair); + UnifyPair pair = new UnifyPair(c1, c2, PairOperator.SMALLERDOTWC); + Optional> res = rules.reduceEq(pair); System.out.println(res); /* @@ -379,11 +379,11 @@ public class RuleSetTest { // Case 1: D <.? C ReferenceType d1 = tf.getSimpleType("D", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2"))); - pair = new MPair(d1, c1, PairOperator.SMALLERDOTWC); + pair = new UnifyPair(d1, c1, PairOperator.SMALLERDOTWC); Assert.assertFalse(rules.reduceEq(pair).isPresent()); // Case 2: C <. C - pair = new MPair(c1, c2, PairOperator.SMALLERDOT); + pair = new UnifyPair(c1, c2, PairOperator.SMALLERDOT); Assert.assertFalse(rules.reduceEq(pair).isPresent()); } @@ -395,9 +395,9 @@ public class RuleSetTest { /* * Positive Tests */ - MPair erase1 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOT); - MPair erase2 = new MPair(tf.getSimpleType("HashSet", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOT); - MPair erase3 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); + UnifyPair erase1 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOT); + UnifyPair erase2 = new UnifyPair(tf.getSimpleType("HashSet", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOT); + UnifyPair erase3 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); Assert.assertTrue(rules.erase1(erase1)); Assert.assertTrue(rules.erase1(erase2)); @@ -406,9 +406,9 @@ public class RuleSetTest { /* * Negative Tests */ - MPair noerase1 = new MPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); - MPair noerase2 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.EQUALSDOT); - MPair noerase3 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOTWC); + UnifyPair noerase1 = new UnifyPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); + UnifyPair noerase2 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.EQUALSDOT); + UnifyPair noerase3 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOTWC); Assert.assertFalse(rules.erase1(noerase1)); Assert.assertFalse(rules.erase1(noerase2)); @@ -423,11 +423,11 @@ public class RuleSetTest { /* * Positive Tests */ - MPair erase1 = new MPair(tf.getSimpleType("List", "T"), tf.getExtendsType(tf.getSimpleType("Collection")), PairOperator.SMALLERDOTWC); - MPair erase2 = new MPair(tf.getSimpleType("Collection"), tf.getSuperType(tf.getSimpleType("HashSet", "T")), PairOperator.SMALLERDOTWC); - MPair erase3 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOTWC); - MPair erase4 = new MPair(tf.getExtendsType(tf.getSimpleType("LinkedList", "T")), tf.getExtendsType(tf.getSimpleType("List", "T")), PairOperator.SMALLERDOTWC); - MPair erase5 = new MPair(tf.getSuperType(tf.getSimpleType("List", "T")), tf.getSuperType(tf.getSimpleType("Stack", "T")), PairOperator.SMALLERDOTWC); + UnifyPair erase1 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getExtendsType(tf.getSimpleType("Collection")), PairOperator.SMALLERDOTWC); + UnifyPair erase2 = new UnifyPair(tf.getSimpleType("Collection"), tf.getSuperType(tf.getSimpleType("HashSet", "T")), PairOperator.SMALLERDOTWC); + UnifyPair erase3 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOTWC); + UnifyPair erase4 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("LinkedList", "T")), tf.getExtendsType(tf.getSimpleType("List", "T")), PairOperator.SMALLERDOTWC); + UnifyPair erase5 = new UnifyPair(tf.getSuperType(tf.getSimpleType("List", "T")), tf.getSuperType(tf.getSimpleType("Stack", "T")), PairOperator.SMALLERDOTWC); Assert.assertTrue(rules.erase2(erase1)); @@ -439,10 +439,10 @@ public class RuleSetTest { /* * Negative Tests */ - MPair noerase1 = new MPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOTWC); - MPair noerase2 = new MPair(tf.getSuperType(tf.getSimpleType("List", "T")), tf.getSimpleType("ArrayList", "T"), PairOperator.SMALLERDOTWC); - MPair noerase3 = new MPair(tf.getExtendsType(tf.getSimpleType("List", "T")), tf.getSuperType(tf.getSimpleType("List", "T")), PairOperator.SMALLERDOTWC); - MPair noerase4 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); + UnifyPair noerase1 = new UnifyPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOTWC); + UnifyPair noerase2 = new UnifyPair(tf.getSuperType(tf.getSimpleType("List", "T")), tf.getSimpleType("ArrayList", "T"), PairOperator.SMALLERDOTWC); + UnifyPair noerase3 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("List", "T")), tf.getSuperType(tf.getSimpleType("List", "T")), PairOperator.SMALLERDOTWC); + UnifyPair noerase4 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); Assert.assertFalse(rules.erase2(noerase1)); Assert.assertFalse(rules.erase2(noerase2)); @@ -458,8 +458,8 @@ public class RuleSetTest { /* * Positive Tests */ - MPair erase1 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.EQUALSDOT); - MPair erase2 = new MPair(tf.getPlaceholderType("W"), tf.getPlaceholderType("W"), PairOperator.EQUALSDOT); + UnifyPair erase1 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.EQUALSDOT); + UnifyPair erase2 = new UnifyPair(tf.getPlaceholderType("W"), tf.getPlaceholderType("W"), PairOperator.EQUALSDOT); Assert.assertTrue(rules.erase3(erase1)); Assert.assertTrue(rules.erase3(erase2)); @@ -467,8 +467,8 @@ public class RuleSetTest { /* * Negative Tests */ - MPair noerase1 = new MPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.EQUALSDOT); - MPair noerase2 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); + UnifyPair noerase1 = new UnifyPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.EQUALSDOT); + UnifyPair noerase2 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); Assert.assertFalse(rules.erase3(noerase1)); Assert.assertFalse(rules.erase3(noerase2)); @@ -482,14 +482,14 @@ public class RuleSetTest { /* * Positive Tests */ - MPair swap1 = new MPair(tf.getExtendsType(tf.getSimpleType("MyClass")), tf.getPlaceholderType("P"), PairOperator.EQUALSDOT); - MPair swap2 = new MPair(tf.getSimpleType("MyClass"), tf.getPlaceholderType("X"), PairOperator.EQUALSDOT); + UnifyPair swap1 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("MyClass")), tf.getPlaceholderType("P"), PairOperator.EQUALSDOT); + UnifyPair swap2 = new UnifyPair(tf.getSimpleType("MyClass"), tf.getPlaceholderType("X"), PairOperator.EQUALSDOT); - MPair expected1 = new MPair(tf.getPlaceholderType("P"), tf.getExtendsType(tf.getSimpleType("MyClass")), PairOperator.EQUALSDOT); - MPair expected2 = new MPair(tf.getPlaceholderType("X"), tf.getSimpleType("MyClass"), PairOperator.EQUALSDOT); + UnifyPair expected1 = new UnifyPair(tf.getPlaceholderType("P"), tf.getExtendsType(tf.getSimpleType("MyClass")), PairOperator.EQUALSDOT); + UnifyPair expected2 = new UnifyPair(tf.getPlaceholderType("X"), tf.getSimpleType("MyClass"), PairOperator.EQUALSDOT); - Optional opt1 = rules.swap(swap1); - Optional opt2 = rules.swap(swap2); + Optional opt1 = rules.swap(swap1); + Optional opt2 = rules.swap(swap2); // swap((? extends MyClass =. P)) = (P =. MyClass) Assert.assertEquals(opt1.get(), expected1); @@ -500,15 +500,15 @@ public class RuleSetTest { /* * Negative Tests */ - MPair noswap1 = new MPair(tf.getPlaceholderType("Z"), tf.getPlaceholderType("X"), PairOperator.EQUALSDOT); - MPair noswap2 = new MPair(tf.getSimpleType("MyClass"), tf.getExtendsType(tf.getPlaceholderType("X")), PairOperator.EQUALSDOT); - MPair noswap3 = new MPair( tf.getPlaceholderType("X"), tf.getSimpleType("MyClass"), PairOperator.EQUALSDOT); - MPair noswap4 = new MPair(tf.getSimpleType("MyClass"), tf.getPlaceholderType("X"), PairOperator.SMALLERDOT); + UnifyPair noswap1 = new UnifyPair(tf.getPlaceholderType("Z"), tf.getPlaceholderType("X"), PairOperator.EQUALSDOT); + UnifyPair noswap2 = new UnifyPair(tf.getSimpleType("MyClass"), tf.getExtendsType(tf.getPlaceholderType("X")), PairOperator.EQUALSDOT); + UnifyPair noswap3 = new UnifyPair( tf.getPlaceholderType("X"), tf.getSimpleType("MyClass"), PairOperator.EQUALSDOT); + UnifyPair noswap4 = new UnifyPair(tf.getSimpleType("MyClass"), tf.getPlaceholderType("X"), PairOperator.SMALLERDOT); opt1 = rules.swap(noswap1); opt2 = rules.swap(noswap2); - Optional opt3 = rules.swap(noswap3); - Optional opt4 = rules.swap(noswap4); + Optional opt3 = rules.swap(noswap3); + Optional opt4 = rules.swap(noswap4); Assert.assertFalse(opt1.isPresent()); Assert.assertFalse(opt2.isPresent()); @@ -538,9 +538,9 @@ public class RuleSetTest { ReferenceType c2 = tf.getSimpleType("Type2", "Object"); ReferenceType c3 = tf.getSimpleType("Type3", "Object", "Number"); - MPair pair1 = new MPair(c1, c2, PairOperator.SMALLERDOT); - MPair pair2 = new MPair(c2, c3, PairOperator.SMALLERDOT); - MPair pair3 = new MPair(c1, c3, PairOperator.SMALLERDOT); + UnifyPair pair1 = new UnifyPair(c1, c2, PairOperator.SMALLERDOT); + UnifyPair pair2 = new UnifyPair(c2, c3, PairOperator.SMALLERDOT); + UnifyPair pair3 = new UnifyPair(c1, c3, PairOperator.SMALLERDOT); System.out.println("------ Adapt ------"); System.out.println(rules.adapt(pair1)); @@ -551,9 +551,9 @@ public class RuleSetTest { * Negative Tests */ - MPair noAdapt1 = new MPair(c2, c1, PairOperator.SMALLERDOT); - MPair noAdapt2 = new MPair(c1, c1, PairOperator.SMALLERDOT); - MPair noAdapt3 = new MPair(c1, c2, PairOperator.SMALLERDOTWC); + UnifyPair noAdapt1 = new UnifyPair(c2, c1, PairOperator.SMALLERDOT); + UnifyPair noAdapt2 = new UnifyPair(c1, c1, PairOperator.SMALLERDOT); + UnifyPair noAdapt3 = new UnifyPair(c1, c2, PairOperator.SMALLERDOTWC); Assert.assertFalse(rules.adapt(noAdapt1).isPresent()); Assert.assertFalse(rules.adapt(noAdapt2).isPresent()); @@ -588,13 +588,13 @@ public class RuleSetTest { ExtendsType extc2 = new ExtendsType(c2); ExtendsType extc3 = new ExtendsType(c3); - MPair pair1 = new MPair(c1, extc2, PairOperator.SMALLERDOTWC); - MPair pair2 = new MPair(c2, extc3, PairOperator.SMALLERDOTWC); - MPair pair3 = new MPair(c1, extc3, PairOperator.SMALLERDOTWC); - MPair pair4 = new MPair(extc1, extc2, PairOperator.SMALLERDOTWC); - MPair pair5 = new MPair(extc2, extc3, PairOperator.SMALLERDOTWC); - MPair pair6 = new MPair(extc1, extc3, PairOperator.SMALLERDOTWC); - MPair pair7 = new MPair(extc1, extc1, PairOperator.SMALLERDOTWC); + UnifyPair pair1 = new UnifyPair(c1, extc2, PairOperator.SMALLERDOTWC); + UnifyPair pair2 = new UnifyPair(c2, extc3, PairOperator.SMALLERDOTWC); + UnifyPair pair3 = new UnifyPair(c1, extc3, PairOperator.SMALLERDOTWC); + UnifyPair pair4 = new UnifyPair(extc1, extc2, PairOperator.SMALLERDOTWC); + UnifyPair pair5 = new UnifyPair(extc2, extc3, PairOperator.SMALLERDOTWC); + UnifyPair pair6 = new UnifyPair(extc1, extc3, PairOperator.SMALLERDOTWC); + UnifyPair pair7 = new UnifyPair(extc1, extc1, PairOperator.SMALLERDOTWC); System.out.println("------ AdaptExt ------"); System.out.println(rules.adaptExt(pair1)); @@ -609,11 +609,11 @@ public class RuleSetTest { * Negative Tests */ - MPair noAdapt1 = new MPair(extc2, extc1, PairOperator.SMALLERDOTWC); - MPair noAdapt2 = new MPair(extc1, c2, PairOperator.SMALLERDOTWC); - MPair noAdapt3 = new MPair(tf.getSuperType(c1), extc2, PairOperator.SMALLERDOTWC); - MPair noAdapt4 = new MPair(extc3, extc1, PairOperator.SMALLERDOTWC); - MPair noAdapt5 = new MPair(c1, extc2, PairOperator.SMALLERDOT); + UnifyPair noAdapt1 = new UnifyPair(extc2, extc1, PairOperator.SMALLERDOTWC); + UnifyPair noAdapt2 = new UnifyPair(extc1, c2, PairOperator.SMALLERDOTWC); + UnifyPair noAdapt3 = new UnifyPair(tf.getSuperType(c1), extc2, PairOperator.SMALLERDOTWC); + UnifyPair noAdapt4 = new UnifyPair(extc3, extc1, PairOperator.SMALLERDOTWC); + UnifyPair noAdapt5 = new UnifyPair(c1, extc2, PairOperator.SMALLERDOT); Assert.assertFalse(rules.adaptExt(noAdapt1).isPresent()); Assert.assertFalse(rules.adaptExt(noAdapt2).isPresent()); @@ -650,13 +650,13 @@ public class RuleSetTest { SuperType supc2 = new SuperType(c2); SuperType supc3 = new SuperType(c3); - MPair pair1 = new MPair(c2, supc1, PairOperator.SMALLERDOTWC); - MPair pair2 = new MPair(c3, supc2, PairOperator.SMALLERDOTWC); - MPair pair3 = new MPair(c3, supc1, PairOperator.SMALLERDOTWC); - MPair pair4 = new MPair(supc2, supc1, PairOperator.SMALLERDOTWC); - MPair pair5 = new MPair(supc3, supc2, PairOperator.SMALLERDOTWC); - MPair pair6 = new MPair(supc3, supc1, PairOperator.SMALLERDOTWC); - MPair pair7 = new MPair(supc1, supc1, PairOperator.SMALLERDOTWC); + UnifyPair pair1 = new UnifyPair(c2, supc1, PairOperator.SMALLERDOTWC); + UnifyPair pair2 = new UnifyPair(c3, supc2, PairOperator.SMALLERDOTWC); + UnifyPair pair3 = new UnifyPair(c3, supc1, PairOperator.SMALLERDOTWC); + UnifyPair pair4 = new UnifyPair(supc2, supc1, PairOperator.SMALLERDOTWC); + UnifyPair pair5 = new UnifyPair(supc3, supc2, PairOperator.SMALLERDOTWC); + UnifyPair pair6 = new UnifyPair(supc3, supc1, PairOperator.SMALLERDOTWC); + UnifyPair pair7 = new UnifyPair(supc1, supc1, PairOperator.SMALLERDOTWC); System.out.println("------ AdaptSup ------"); System.out.println(rules.adaptSup(pair1)); @@ -671,11 +671,11 @@ public class RuleSetTest { * Negative Tests */ - MPair noAdapt1 = new MPair(supc2, supc1, PairOperator.SMALLERDOTWC); - MPair noAdapt2 = new MPair(supc1, c2, PairOperator.SMALLERDOTWC); - MPair noAdapt3 = new MPair(tf.getExtendsType(c1), supc2, PairOperator.SMALLERDOTWC); - MPair noAdapt4 = new MPair(supc3, supc1, PairOperator.SMALLERDOTWC); - MPair noAdapt5 = new MPair(c1, supc2, PairOperator.SMALLERDOT); + UnifyPair noAdapt1 = new UnifyPair(supc2, supc1, PairOperator.SMALLERDOTWC); + UnifyPair noAdapt2 = new UnifyPair(supc1, c2, PairOperator.SMALLERDOTWC); + UnifyPair noAdapt3 = new UnifyPair(tf.getExtendsType(c1), supc2, PairOperator.SMALLERDOTWC); + UnifyPair noAdapt4 = new UnifyPair(supc3, supc1, PairOperator.SMALLERDOTWC); + UnifyPair noAdapt5 = new UnifyPair(c1, supc2, PairOperator.SMALLERDOT); Assert.assertFalse(rules.adaptExt(noAdapt1).isPresent()); Assert.assertFalse(rules.adaptExt(noAdapt2).isPresent()); diff --git a/test/unify/StandardUnifyTest.java b/test/unify/StandardUnifyTest.java index b8e8a4795..2664324cc 100644 --- a/test/unify/StandardUnifyTest.java +++ b/test/unify/StandardUnifyTest.java @@ -9,7 +9,7 @@ import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.MartelliMontanariUnify; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; public class StandardUnifyTest { @@ -28,7 +28,7 @@ public class StandardUnifyTest { UnifyType f = tf.getSimpleType("f", x); // {f = y} - Set terms = new HashSet(); + Set terms = new HashSet(); System.out.println(unify.unify(f, y).get()); diff --git a/test/unify/UnifyOldTest.java b/test/unify/UnifyOldTest.java index a2de8ce0d..e07f303c8 100644 --- a/test/unify/UnifyOldTest.java +++ b/test/unify/UnifyOldTest.java @@ -7,15 +7,10 @@ import org.junit.Test; import de.dhbwstuttgart.syntaxtree.factory.UnifyPairMengenBuilder; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.syntaxtree.factory.Unify_FC_TTO_Builder; -import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; -import de.dhbwstuttgart.syntaxtree.type.ObjectType; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; -import de.dhbwstuttgart.syntaxtree.type.Type; -import de.dhbwstuttgart.syntaxtree.type.RefType; -import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.syntaxtree.type.WildcardType; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.unify.Unify; @@ -25,318 +20,15 @@ public class UnifyOldTest { @Test public void unifyTest1() { // Init Factories and Builders + UnifyTypeFactory typeFactory = new UnifyTypeFactory(); Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder(); UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder(); UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder(); -<<<<<<< HEAD - /* - * Test a <. Boolean - */ - - // Init Types - RefType boolT = this.GetSimpleType("java.lang.Boolean"); - TypePlaceholder aTph = this.GetTypePlaceholder("a"); - - // Expected Result - resultBuilder.clear(); - resultBuilder.addPair(aTph, boolT, PairOperator.Equal); - resultBuilder.addPair(aTph, this.GetExtendsType(boolT), - PairOperator.Equal); - Menge> expectedResult = resultBuilder.getNestedPairMenge(); - - // Actual Result - assumptionBuilder.clear(); - assumptionBuilder.addPair(aTph, boolT); - Menge> actualResult = Unify.unify( - assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO()); - - // System.out.println(expectedResult); - // System.out.println(actualResult); - - Assert.assertTrue(mengeEquals(expectedResult, actualResult)); - - /* - * Test b <. a, a <. Boolean - */ - - // Init Types - boolT = this.GetSimpleType("java.lang.Boolean"); - aTph = this.GetTypePlaceholder("a"); - TypePlaceholder bTph = this.GetTypePlaceholder("b"); - - // Expected Result - resultBuilder.clear(); - resultBuilder.addPair(aTph, boolT, PairOperator.Equal); - resultBuilder.addPair(aTph, this.GetExtendsType(boolT), - PairOperator.Equal); - resultBuilder.addPair(bTph, boolT, PairOperator.Equal); - resultBuilder.addPair(bTph, this.GetExtendsType(boolT), - PairOperator.Equal); - expectedResult = resultBuilder.getNestedPairMenge(); - - // Actual Result - assumptionBuilder.clear(); - assumptionBuilder.addPair(bTph, aTph); - assumptionBuilder.addPair(aTph, boolT); - actualResult = Unify.unify(assumptionBuilder.getPairMenge(), - fcBuilder.Get_FC_TTO()); - - // System.out.println(expectedResult); - // System.out.println(actualResult); - - // NOTE: Elemente im actualResult sind nicht unique - // Assert.assertTrue(mengeEquals(expectedResult, actualResult)); - - /* - * Test b <. a, a <. b - */ - - aTph = this.GetTypePlaceholder("a"); - bTph = this.GetTypePlaceholder("b"); - - // Expected Result - resultBuilder.clear(); - resultBuilder.addPair(bTph, aTph); - resultBuilder.addPair(aTph, bTph); - - Menge buffer = resultBuilder.getPairMenge(); - expectedResult = new Menge>(); - expectedResult.add(buffer); - - // Actual Result - assumptionBuilder.clear(); - assumptionBuilder.addPair(bTph, aTph); - assumptionBuilder.addPair(aTph, bTph); - actualResult = Unify.unify(assumptionBuilder.getPairMenge(), - fcBuilder.Get_FC_TTO()); - - // System.out.println(expectedResult); - // System.out.println(actualResult); - - Assert.assertTrue(mengeEquals(expectedResult, actualResult)); - - /* - * Test Integer <. a, a <. Boolean - */ - - RefType intT = this.GetSimpleType("java.lang.Integer"); - boolT = this.GetSimpleType("java.lang.Boolean"); - aTph = this.GetTypePlaceholder("a"); - bTph = this.GetTypePlaceholder("b"); - - // Expected Result - resultBuilder.clear(); - expectedResult = resultBuilder.getNestedPairMenge(); - - // Actual Result - assumptionBuilder.clear(); - assumptionBuilder.addPair(intT, aTph); - assumptionBuilder.addPair(aTph, boolT); - actualResult = Unify.unify(assumptionBuilder.getPairMenge(), - fcBuilder.Get_FC_TTO()); - - // System.out.println(expectedResult); - // System.out.println(actualResult); - - Assert.assertTrue(mengeEquals(expectedResult, actualResult)); - - } - - @Test - public void unifyTestGenerics() { - - // Init Factories and Builders - Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder(); - UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder(); - UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder(); - - /* - * Test a <. MyClass - */ - - TypePlaceholder aTph = this.GetTypePlaceholder("a"); - RefType myType = this.GetSimpleType("MyClass", - this.GetTypePlaceholder("T"), - this.GetTypePlaceholder("F")); - - // Expected Result - resultBuilder.clear(); - resultBuilder.addPair(aTph, myType, PairOperator.Equal); - resultBuilder.addPair(aTph, this.GetExtendsType(myType)); - Menge> expectedResult = resultBuilder.getNestedPairMenge(); - - // Actual Result - assumptionBuilder.clear(); - assumptionBuilder.addPair(aTph, myType); - Menge> actualResult = Unify.unify( - assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO()); - - // System.out.println(expectedResult); - // System.out.println(actualResult); - - Assert.assertTrue(mengeEquals(expectedResult, actualResult)); - - /* - * Test List> <. List - */ - - TypePlaceholder tTph = this.GetTypePlaceholder("T"); - RefType list = this.GetSimpleType("List", tTph); - RefType listlist = this.GetSimpleType("List", list); - - // Expected Result - resultBuilder.clear(); - resultBuilder.addPair(this.GetExtendsType(list), tTph, - PairOperator.Equal); - expectedResult = resultBuilder.getNestedPairMenge(); - - // Actual Result - assumptionBuilder.clear(); - assumptionBuilder.addPair(listlist, list); - actualResult = Unify.unify(assumptionBuilder.getPairMenge(), - fcBuilder.Get_FC_TTO()); - - System.out.println(expectedResult); - System.out.println(actualResult); - - Assert.assertTrue(mengeEquals(expectedResult, actualResult)); - - /* - * Test List <. List> - */ - } - - @Test - public void unifyTestInheritance() { - - // Init Factories and Builders - Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder(); - UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder(); - UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder(); - - // Init Types - RefType tBool = this.GetSimpleType("java.lang.Boolean"); - RefType tString = this.GetSimpleType("java.lang.String"); - RefType tInt = this.GetSimpleType("java.lang.Integer"); - TypePlaceholder tphA = this.GetTypePlaceholder("a"); - - // Build inheritance hierachy - // Bool <. String <. Int - fcBuilder.AddInheritance(tBool, tString); - fcBuilder.AddInheritance(tString, tInt); - - // Build Assumptions - assumptionBuilder.addPair(tphA, tString); - - // Build expected result - resultBuilder.addPair(tphA, tBool, PairOperator.Equal); - resultBuilder.addPair(tphA, this.GetExtendsType(tBool), - PairOperator.Equal); - resultBuilder.addPair(tphA, tString, PairOperator.Equal); - resultBuilder.addPair(tphA, this.GetExtendsType(tString), - PairOperator.Equal); - - // Assert - Menge> actualResult = Unify.unify( - assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO()); - - // System.out.println(actualResult); - // System.out.println("-------------------"); - // System.out.println(resultBuilder.getNestedPairMenge()); - - Assert.assertTrue(mengeEquals(resultBuilder.getNestedPairMenge(), - actualResult)); - } - - @Test - public void unifyTestWildcards() { - - } - - private static boolean mengeEquals(Menge> m1, - Menge> m2) { - if (m1.size() != m2.size()) - return false; - - return containsAll(m1, m2) && containsAll(m2, m1); - } - - private static boolean containsAll(Menge> m1, - Menge> m2) { - for (Menge elem : m2) - if (!contains(m1, elem)) - return false; - return true; - } - - private static boolean contains(Menge> m1, Menge m2) { - for (Menge elem : m1) - if (mengePairEquals(elem, m2)) - return true; - return false; - } - - private static boolean mengePairEquals(Menge m1, Menge m2) { - if (m1.size() != m2.size()) - return false; - - return containsAllPair(m1, m2) && containsAllPair(m2, m1); - } - - private static boolean containsAllPair(Menge m1, Menge m2) { - for (Pair elem : m1) - if (contains(m2, elem)) - return true; - return false; - } - - private static boolean contains(Menge m, Pair p) { - for (Pair elem : m) - if (pairEquals(elem, p)) - return true; - return false; - - } - - private static boolean pairEquals(Pair p1, Pair p2) { - return (p1.TA1.equals(p2.TA1) && p1.TA2.equals(p2.TA2)) - || (p1.TA1.equals(p2.TA2) && p1.TA2.equals(p2.TA1)); - } - - - private RefType GetSimpleType(String name, Type... parameters) { - if(parameters.length == 0) - return new RefType(name, null, 0); - - Menge typeParams = new Menge(); - - for(Type t : parameters) - typeParams.add(t); - - return new RefType(name, typeParams, null, 0); - } - - private ExtendsWildcardType GetExtendsType(ObjectType extendedType) { - return new ExtendsWildcardType(extendedType); - } - - private SuperWildcardType GetSuperType(ObjectType superedType) { - return new SuperWildcardType(superedType); - } - - private WildcardType GetWildcardType() { - return new WildcardType(null, null, 0); - } - - private TypePlaceholder GetTypePlaceholder(String name) { - return TypePlaceholder.backdoorCreate(name); - } -======= TypePlaceholder a = typeFactory.GetTypePlaceholder("a"); ExtendsWildcardType extA = typeFactory.GetExtendsType(a); - TypePlaceholder b = typeFactory.GetTypePlaceholder("b"); - ExtendsWildcardType extB = typeFactory.GetExtendsType(b); + TypePlaceholder b = typeFactory.GetTypePlaceholder("a"); + ExtendsWildcardType extB = typeFactory.GetExtendsType(a); RefType integer = typeFactory.GetSimpleType("Integer"); SuperWildcardType supInt = typeFactory.GetSuperType(integer); RefType listsupint = typeFactory.GetSimpleType("List", supInt); @@ -643,6 +335,5 @@ public class UnifyOldTest { // return (p1.TA1.equals(p2.TA1) && p1.TA2.equals(p2.TA2)) // || (p1.TA1.equals(p2.TA2) && p1.TA2.equals(p2.TA1)); // } ->>>>>>> d89d06797e17a7a95f007fe24e9b6133b3b13179 } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index b4aaa86ef..0ee466e41 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -10,7 +10,7 @@ import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.Unify; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; @@ -48,10 +48,10 @@ public class UnifyTest extends Unify { * unify({ }) = { } */ - Set eq = new HashSet(); - Set> expected = new HashSet<>(); + Set eq = new HashSet(); + Set> expected = new HashSet<>(); expected.add(new HashSet<>()); - Set> actual = unify(eq, fc); + Set> actual = unify(eq, fc); Assert.assertEquals(expected, actual); /* @@ -62,12 +62,12 @@ public class UnifyTest extends Unify { UnifyType tphA = tf.getPlaceholderType("a"); eq = new HashSet<>(); - eq.add(new MPair(tphA, number, PairOperator.SMALLERDOT)); + eq.add(new UnifyPair(tphA, number, PairOperator.SMALLERDOT)); expected = new HashSet<>(); - addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, integer, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, doubl, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, number, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, integer, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, doubl, PairOperator.EQUALSDOT)); actual = unify(eq, fc); @@ -80,12 +80,12 @@ public class UnifyTest extends Unify { */ eq = new HashSet<>(); - eq.add(new MPair(integer, tphA, PairOperator.SMALLERDOT)); + eq.add(new UnifyPair(integer, tphA, PairOperator.SMALLERDOT)); expected = new HashSet<>(); - addAsSet(expected, new MPair(tphA, integer, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, object, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, integer, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, number, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, object, PairOperator.EQUALSDOT)); actual = unify(eq, fc); @@ -99,10 +99,10 @@ public class UnifyTest extends Unify { */ eq = new HashSet<>(); - eq.add(new MPair(tphA, number, PairOperator.SMALLERDOTWC)); + eq.add(new UnifyPair(tphA, number, PairOperator.SMALLERDOTWC)); expected = new HashSet<>(); - addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, number, PairOperator.EQUALSDOT)); actual = unify(eq, fc); @@ -118,15 +118,15 @@ public class UnifyTest extends Unify { UnifyType supNumber = tf.getSuperType(number); UnifyType supObject = tf.getSuperType(object); eq = new HashSet<>(); - eq.add(new MPair(tphA, supInteger, PairOperator.SMALLERDOTWC)); + eq.add(new UnifyPair(tphA, supInteger, PairOperator.SMALLERDOTWC)); expected = new HashSet<>(); - addAsSet(expected, new MPair(tphA, integer, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, object, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, supInteger, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, supNumber, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, supObject, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, integer, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, number, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, object, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, supInteger, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, supNumber, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, supObject, PairOperator.EQUALSDOT)); actual = unify(eq, fc); actual = filterGeneratedTPHsMultiple(actual); @@ -144,15 +144,15 @@ public class UnifyTest extends Unify { UnifyType extNumber = tf.getExtendsType(number); UnifyType extObject = tf.getExtendsType(object); UnifyType supDouble = tf.getSuperType(doubl); - eq.add(new MPair(number, tphA, PairOperator.SMALLERDOTWC)); + eq.add(new UnifyPair(number, tphA, PairOperator.SMALLERDOTWC)); expected = new HashSet<>(); - addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, extNumber, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, extObject, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, supInteger, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, supDouble, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, supNumber, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, number, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, extNumber, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, extObject, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, supInteger, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, supDouble, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, supNumber, PairOperator.EQUALSDOT)); actual = unify(eq, fc); @@ -165,11 +165,11 @@ public class UnifyTest extends Unify { */ eq = new HashSet<>(); - eq.add(new MPair(extNumber, tphA, PairOperator.SMALLERDOTWC)); + eq.add(new UnifyPair(extNumber, tphA, PairOperator.SMALLERDOTWC)); expected = new HashSet<>(); - addAsSet(expected, new MPair(tphA, extNumber, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, extObject, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, extNumber, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, extObject, PairOperator.EQUALSDOT)); actual = unify(eq, fc); @@ -186,15 +186,15 @@ public class UnifyTest extends Unify { UnifyType extDouble = tf.getExtendsType(doubl); eq = new HashSet<>(); - eq.add(new MPair(tphA, extNumber, PairOperator.SMALLERDOTWC)); + eq.add(new UnifyPair(tphA, extNumber, PairOperator.SMALLERDOTWC)); expected = new HashSet<>(); - addAsSet(expected, new MPair(tphA, extNumber, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, extInteger, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, extDouble, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, doubl, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, integer, PairOperator.EQUALSDOT)); - addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, extNumber, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, extInteger, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, extDouble, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, doubl, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, integer, PairOperator.EQUALSDOT)); + addAsSet(expected, new UnifyPair(tphA, number, PairOperator.EQUALSDOT)); actual = unify(eq, fc); @@ -207,7 +207,7 @@ public class UnifyTest extends Unify { */ eq = new HashSet<>(); - eq.add(new MPair(integer, number, PairOperator.SMALLERDOT)); + eq.add(new UnifyPair(integer, number, PairOperator.SMALLERDOT)); expected = new HashSet<>(); expected.add(new HashSet<>()); @@ -223,7 +223,7 @@ public class UnifyTest extends Unify { */ eq = new HashSet<>(); - eq.add(new MPair(integer, number, PairOperator.SMALLERDOTWC)); + eq.add(new UnifyPair(integer, number, PairOperator.SMALLERDOTWC)); expected = new HashSet<>(); @@ -239,10 +239,10 @@ public class UnifyTest extends Unify { UnifyType tphB = tf.getPlaceholderType("b"); eq = new HashSet<>(); - eq.add(new MPair(tphA, tphB, PairOperator.SMALLERDOT)); + eq.add(new UnifyPair(tphA, tphB, PairOperator.SMALLERDOT)); expected = new HashSet<>(); - addAsSet(expected, new MPair(tphA, tphB, PairOperator.SMALLERDOT)); + addAsSet(expected, new UnifyPair(tphA, tphB, PairOperator.SMALLERDOT)); actual = unify(eq, fc); @@ -255,10 +255,10 @@ public class UnifyTest extends Unify { */ eq = new HashSet<>(); - eq.add(new MPair(tphA, tphB, PairOperator.SMALLERDOTWC)); + eq.add(new UnifyPair(tphA, tphB, PairOperator.SMALLERDOTWC)); expected = new HashSet<>(); - addAsSet(expected, new MPair(tphA, tphB, PairOperator.SMALLERDOTWC)); + addAsSet(expected, new UnifyPair(tphA, tphB, PairOperator.SMALLERDOTWC)); actual = unify(eq, fc); @@ -296,12 +296,12 @@ public class UnifyTest extends Unify { UnifyType extB = tf.getExtendsType(tphB); UnifyType extNum = tf.getExtendsType(number); - Set eq = new HashSet(); - eq.add(new MPair(tf.getSimpleType("Vector", tphA), tf.getSimpleType("Vector", extB), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getSimpleType("List", tphB), tf.getSimpleType("List", extNum), PairOperator.SMALLERDOT)); + Set eq = new HashSet(); + eq.add(new UnifyPair(tf.getSimpleType("Vector", tphA), tf.getSimpleType("Vector", extB), PairOperator.SMALLERDOT)); + eq.add(new UnifyPair(tf.getSimpleType("List", tphB), tf.getSimpleType("List", extNum), PairOperator.SMALLERDOT)); - Set> expected = new HashSet<>(); - Set> actual = unify(eq, fc); + Set> expected = new HashSet<>(); + Set> actual = unify(eq, fc); System.out.println(actual); //Assert.assertEquals(actual, expected); @@ -315,8 +315,8 @@ public class UnifyTest extends Unify { UnifyType supB = tf.getSuperType(tphB); eq = new HashSet<>(); - eq.add(new MPair(tphA, supB, PairOperator.SMALLERDOTWC)); - eq.add(new MPair(tphB, number, PairOperator.EQUALSDOT)); + eq.add(new UnifyPair(tphA, supB, PairOperator.SMALLERDOTWC)); + eq.add(new UnifyPair(tphB, number, PairOperator.EQUALSDOT)); expected = new HashSet<>(); @@ -335,8 +335,8 @@ public class UnifyTest extends Unify { UnifyType extA = tf.getExtendsType(tphA); - eq = new HashSet(); - eq.add(new MPair(tf.getSimpleType("Vector", extA), tf.getSimpleType("Vector", extNum), PairOperator.SMALLERDOT)); + eq = new HashSet(); + eq.add(new UnifyPair(tf.getSimpleType("Vector", extA), tf.getSimpleType("Vector", extNum), PairOperator.SMALLERDOT)); expected = new HashSet<>(); actual = unify(eq, fc); @@ -351,8 +351,8 @@ public class UnifyTest extends Unify { * */ - eq = new HashSet(); - eq.add(new MPair(tf.getSimpleType("Vector", extNum), tf.getSimpleType("Vector", extA), PairOperator.SMALLERDOT)); + eq = new HashSet(); + eq.add(new UnifyPair(tf.getSimpleType("Vector", extNum), tf.getSimpleType("Vector", extA), PairOperator.SMALLERDOT)); expected = new HashSet<>(); actual = unify(eq, fc); @@ -370,10 +370,10 @@ public class UnifyTest extends Unify { * ? extends Number <.? b */ - eq = new HashSet(); - eq.add(new MPair(tf.getSimpleType("LinkedList", number), tf.getSimpleType("Deque", tphA), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getSimpleType("Deque", tphA), tf.getSimpleType("Queue", tphB), PairOperator.SMALLERDOT)); - eq.add(new MPair(extNum, tphB, PairOperator.SMALLERDOTWC)); + eq = new HashSet(); + eq.add(new UnifyPair(tf.getSimpleType("LinkedList", number), tf.getSimpleType("Deque", tphA), PairOperator.SMALLERDOT)); + eq.add(new UnifyPair(tf.getSimpleType("Deque", tphA), tf.getSimpleType("Queue", tphB), PairOperator.SMALLERDOT)); + eq.add(new UnifyPair(extNum, tphB, PairOperator.SMALLERDOTWC)); expected = new HashSet<>(); actual = unify(eq, fc); @@ -446,16 +446,16 @@ public class UnifyTest extends Unify { Assert.assertEquals(expected, actual); } - private Set> filterGeneratedTPHsMultiple(Set> set) { + private Set> filterGeneratedTPHsMultiple(Set> set) { return set.stream().map(x -> filterGeneratedTPHs(x)).collect(Collectors.toSet()); } - private Set filterGeneratedTPHs(Set set) { + private Set filterGeneratedTPHs(Set set) { return set.stream().filter(x -> !((x.getRhsType() instanceof PlaceholderType) && ((PlaceholderType) x.getRhsType()).isGenerated())). filter(x -> !((x.getLhsType() instanceof PlaceholderType) && ((PlaceholderType) x.getLhsType()).isGenerated())).collect(Collectors.toSet()); } - private void addAsSet(Set> addTo, MPair... mPairs) { + private void addAsSet(Set> addTo, UnifyPair... mPairs) { addTo.add(new HashSet<>(Arrays.stream(mPairs).collect(Collectors.toSet()))); } } diff --git a/test/unify/UnifyTypeFactoryTest.java b/test/unify/UnifyTypeFactoryTest.java index 8bfac1a92..10e5582cb 100644 --- a/test/unify/UnifyTypeFactoryTest.java +++ b/test/unify/UnifyTypeFactoryTest.java @@ -21,7 +21,7 @@ import de.dhbwstuttgart.typeinference.OderConstraint; import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.UndConstraint; import de.dhbwstuttgart.typeinference.UnifyConstraintsSet; -import de.dhbwstuttgart.typeinference.unify.model.MPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.ReferenceType; import de.dhbwstuttgart.typeinference.unify.model.UnifyType; @@ -45,24 +45,24 @@ public class UnifyTypeFactoryTest { @Test public void convertMPair() { - MPair mp1 = getSimpleMPair(); + UnifyPair mp1 = getSimpleMPair(); checkConvertMPair(mp1); TypePlaceholder tph1 = TypePlaceholder.fresh(new NullSyntaxTreeNode()); TypePlaceholder tph2 = TypePlaceholder.fresh(new NullSyntaxTreeNode()); - MPair mp2 = new MPair(getSimpleType(), tf.getPlaceholderType(tph1.getName().toString()), PairOperator.SMALLERDOT); + UnifyPair mp2 = new UnifyPair(getSimpleType(), tf.getPlaceholderType(tph1.getName().toString()), PairOperator.SMALLERDOT); checkConvertMPair(mp2); - MPair mp3 = new MPair(tf.getSuperType(getSimpleType()), getSimpleType(), PairOperator.SMALLERDOT); + UnifyPair mp3 = new UnifyPair(tf.getSuperType(getSimpleType()), getSimpleType(), PairOperator.SMALLERDOT); checkConvertMPair(mp3); - MPair mp4 = new MPair(tf.getPlaceholderType(tph2.getName().toString()), tf.getPlaceholderType(tph1.getName().toString()), PairOperator.SMALLERDOT); + UnifyPair mp4 = new UnifyPair(tf.getPlaceholderType(tph2.getName().toString()), tf.getPlaceholderType(tph1.getName().toString()), PairOperator.SMALLERDOT); checkConvertMPair(mp4); ReferenceType typeWithParams = tf.getSimpleType("Test", getSimpleType()); ReferenceType typeWithTPHParams = tf.getSimpleType("Test", tf.getPlaceholderType("Test")); - MPair mp5 = new MPair(typeWithTPHParams, typeWithParams, PairOperator.SMALLERDOT); + UnifyPair mp5 = new UnifyPair(typeWithTPHParams, typeWithParams, PairOperator.SMALLERDOT); checkConvertMPair(mp5); } @@ -97,15 +97,15 @@ public class UnifyTypeFactoryTest { Set> check = cons.cartesianProduct(); UnifyConstraintsSet converted = UnifyTypeFactory.convert(cons); - Set> cartesianProductOfConverted = converted.cartesianProduct(); + Set> cartesianProductOfConverted = converted.cartesianProduct(); assertTrue(cartesianProductOfConverted.size()==check.size()); assertTrue(cartesianProductOfConverted.iterator().next().size()==check.iterator().next().size()); Set> controlSet = new Menge<>(); - for(Set pairs : cartesianProductOfConverted){ + for(Set pairs : cartesianProductOfConverted){ Set tmpSet = new Menge<>(); - for(MPair mp : pairs){ + for(UnifyPair mp : pairs){ Pair p = checkConvertMPair(mp); tmpSet.add(p); } @@ -114,15 +114,15 @@ public class UnifyTypeFactoryTest { assertTrue(controlSet.equals(check)); } - private static MPair checkConvertPair(Pair p){ + private static UnifyPair checkConvertPair(Pair p){ System.out.println("Checking Pair: "+p); - MPair mp = UnifyTypeFactory.convert(p); + UnifyPair mp = UnifyTypeFactory.convert(p); assertTrue(p.TA1.get_Name().equals(mp.getLhsType().getName())); assertTrue(p.TA2.get_Name().equals(mp.getRhsType().getName())); return mp; } - private static Pair checkConvertMPair(MPair mp){ + private static Pair checkConvertMPair(UnifyPair mp){ System.out.println("Checking Pair: "+mp); Pair p = UnifyTypeFactory.convert(mp); assertTrue(p.TA1.get_Name().equals(mp.getLhsType().getName())); @@ -134,10 +134,10 @@ public class UnifyTypeFactoryTest { return tf.getSimpleType("String"); } - private static MPair getSimpleMPair(){ + private static UnifyPair getSimpleMPair(){ UnifyType lt = tf.getSimpleType("String"); UnifyType rt = tf.getSimpleType("String"); - MPair ret = new MPair(lt, rt, PairOperator.SMALLERDOT); + UnifyPair ret = new UnifyPair(lt, rt, PairOperator.SMALLERDOT); return ret; }