diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index 2ec26b2e..cea600d1 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -301,11 +301,7 @@ public class SourceFile * Erstellt die Finite Closure * @return FC_TTO-Object, welches die Finite Closure repräsentiert */ - // ino.end - // ino.method.makeFC.21403.definition public FC_TTO makeFC( TypeAssumptions ass ) - // ino.end - // ino.method.makeFC.21403.body { // Menge FC bilden @@ -850,7 +846,7 @@ public class SourceFile * @param withSuptypes - Gibt an, ob auch die subklassen der Packages den Assumptions angefügt werden sollen. * @return */ - private TypeAssumptions makeBasicAssumptionsFromJRE(Menge imports, boolean withSubtypes) + public TypeAssumptions makeBasicAssumptionsFromJRE(Menge imports, boolean withSubtypes) // ino.end // ino.method.makeBasicAssumptionsFromJRE.21409.body { diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 6bbd023f..c718a7bf 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -1,6 +1,10 @@ package de.dhbwstuttgart.syntaxtree.factory; +import java.util.HashSet; + +import de.dhbwstuttgart.myexception.NotImplementedException; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; +import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.ObjectType; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; @@ -8,34 +12,71 @@ import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.WildcardType; import de.dhbwstuttgart.typeinference.Menge; +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.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.SimpleType; +import de.dhbwstuttgart.typeinference.unify.model.SuperType; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; public class UnifyTypeFactory { - - public 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); + + public static FiniteClosure generateFC(TypeAssumptions fromAss){ + HashSet pairs = new HashSet<>(); + for(ClassAssumption cAss : fromAss.getClassAssumptions()){ + UnifyType tl = UnifyTypeFactory.convert(cAss.getAssumedClass().getType()); + Type superClass = cAss.getAssumedClass().getSuperClass(); + if(superClass != null){ + UnifyType tr = UnifyTypeFactory.convert(superClass); + pairs.add(smaller(tl, tr)); + } + } + return new FiniteClosure(pairs); } - public ExtendsWildcardType GetExtendsType(ObjectType extendedType) { - return new ExtendsWildcardType(extendedType); + public static MPair smaller(UnifyType tl, UnifyType tr){ + return new MPair(tl, tr,MPair.PairOperator.SMALLER); } - public SuperWildcardType GetSuperType(ObjectType superedType) { - return new SuperWildcardType(superedType); + public static UnifyType convert(Type t){ + //Es wurde versucht ein Typ umzuwandeln, welcher noch nicht von der Factory abgedeckt ist + if(t instanceof GenericTypeVar){ + return UnifyTypeFactory.convert((GenericTypeVar)t); + } + System.out.println("Der Typ "+t+" kann nicht umgewandelt werden"); + throw new NotImplementedException(); } - public WildcardType GetWildcardType() { - return new WildcardType(null, null, 0); + public static UnifyType convert(RefType t){ + UnifyType ret; + if(t.getParaList() != null && t.getParaList().size() > 0){ + Menge params = new Menge<>(); + for(Type pT : t.getParaList()){ + params.add(UnifyTypeFactory.convert(pT)); + } + ret = new SimpleType(t.get_Name(),params.toArray()); + }else{ + ret = new SimpleType(t.get_Name()); + } + return ret; } - public TypePlaceholder GetTypePlaceholder(String name) { - return TypePlaceholder.backdoorCreate(name); + public static UnifyType convert(TypePlaceholder tph){ + return new PlaceholderType(tph.get_Name()); + } + + public static UnifyType convert(ExtendsWildcardType t){ + return new ExtendsType(UnifyTypeFactory.convert(t.get_ExtendsType())); + } + + public static UnifyType convert(SuperWildcardType t){ + return new SuperType(UnifyTypeFactory.convert(t.get_SuperType())); + } + + public static UnifyType convert(GenericTypeVar t){ + return new SimpleType(t.get_Name()); } } diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/TypeAssumptions.java b/src/de/dhbwstuttgart/typeinference/assumptions/TypeAssumptions.java index d80ad80c..ca74c835 100755 --- a/src/de/dhbwstuttgart/typeinference/assumptions/TypeAssumptions.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/TypeAssumptions.java @@ -89,33 +89,6 @@ public class TypeAssumptions { return null; } - /** - * Ermittelt alle bekannten Methoden mit dem Namen withName - * @param withName - * @return - */ - @Deprecated - public Menge getMethods2(String withName){ - //TODO: Implementieren - return new Menge(); - } - - /** - * Liefert den Typ einer lokalen Variable. Zuerst werden die Parameter dieses AssumptionSets durchsucht, dann die lokalen Variablen. Anschließend die Felder der, "this" repräsentierenden Klasse - * @param withName - * @return - */ - @Deprecated - public Type getTypeOfLocalVar2(String withName){ - //TODO: Implementieren - return null; - } - - - - - - /** * Sucht nach Assumptions zu einer Methode mit dem Namen methodName und parameterCount Parametern. * @param methodName diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java index 62b65b27..fa640eb8 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java @@ -7,7 +7,7 @@ import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.SimpleType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; -import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; public interface IFiniteClosure { @@ -15,40 +15,40 @@ public interface IFiniteClosure { * Returns all types of the finite closure that are subtypes of the argument. * @return The set of subtypes of the argument. */ - public Set smaller(Type type); + public Set smaller(UnifyType type); /** * Returns all types of the finite closure that are supertypes of the argument. * @return The set of supertypes of the argument. */ - public Set greater(Type type); + public Set greater(UnifyType type); /** * Wo passt Type rein? * @param type * @return */ - public Set grArg(Type type); + public Set grArg(UnifyType type); /** * Was passt in Type rein? * @param type * @return */ - public Set smArg(Type type); + public Set smArg(UnifyType type); - public Set grArg(SimpleType type); - public Set smArg(SimpleType type); + public Set grArg(SimpleType type); + public Set smArg(SimpleType type); - public Set grArg(ExtendsType type); - public Set smArg(ExtendsType type); + public Set grArg(ExtendsType type); + public Set smArg(ExtendsType type); - public Set grArg(SuperType type); - public Set smArg(SuperType type); + public Set grArg(SuperType type); + public Set smArg(SuperType type); - public Set grArg(PlaceholderType type); - public Set smArg(PlaceholderType type); + public Set grArg(PlaceholderType type); + public Set smArg(PlaceholderType type); - public Optional getGenericType(String typeName); - public Set getAllTypes(String typeName); + public Optional getGenericType(String typeName); + public Set getAllTypes(String typeName); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java index f286e3b6..ef8775b1 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java @@ -5,7 +5,7 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.Unifier; /** @@ -14,9 +14,9 @@ import de.dhbwstuttgart.typeinference.unify.model.Unifier; */ public interface IUnify { - public Optional unify(Set terms); + public Optional unify(Set terms); - default public Optional unify(Type... terms) { + default public Optional unify(UnifyType... terms) { return unify(Arrays.stream(terms).collect(Collectors.toSet())); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java index 9fb3e454..4cf3ceef 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java @@ -7,18 +7,18 @@ import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; /** * An extends wildcard type "? extends T". */ -public final class ExtendsType extends Type { +public final class ExtendsType extends UnifyType { /** * The extended type */ - private Type extendedType; + private UnifyType extendedType; /** * Creates a new extends wildcard type. * @param extendedType The extended type e.g. Integer in "? extends Integer" */ - public ExtendsType(Type extendedType) { + public ExtendsType(UnifyType extendedType) { super("? extends " + extendedType.getName(), extendedType.getTypeParams()); this.extendedType = extendedType; } @@ -27,7 +27,7 @@ public final class ExtendsType extends Type { * Gets the type extended by this wildcard e.g. "Integer" for "? extends Integer" * @return The extended type. */ - public Type getExtendedType() { + public UnifyType getExtendedType() { return extendedType; } @@ -37,22 +37,22 @@ public final class ExtendsType extends Type { } @Override - public Type setTypeParams(TypeParams newTp) { + public UnifyType setTypeParams(TypeParams newTp) { return new ExtendsType(extendedType.setTypeParams(newTp)); } @Override - Set smArg(IFiniteClosure fc) { + Set smArg(IFiniteClosure fc) { return fc.smArg(this); } @Override - Set grArg(IFiniteClosure fc) { + Set grArg(IFiniteClosure fc) { return fc.grArg(this); } @Override - Type apply(Unifier unif) { + UnifyType apply(Unifier unif) { return new ExtendsType(extendedType.apply(unif)); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index e618482d..35fae6cb 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -11,12 +11,12 @@ import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; public class FiniteClosure implements IFiniteClosure { - private HashMap> inheritanceGraph; - private HashMap>> strInheritanceGraph; + private HashMap> inheritanceGraph; + private HashMap>> strInheritanceGraph; public FiniteClosure(Set pairs) { - inheritanceGraph = new HashMap>(); + inheritanceGraph = new HashMap>(); // Build the transitive closure of the inheritance tree for(MPair pair : pairs) { @@ -25,12 +25,12 @@ public class FiniteClosure implements IFiniteClosure { // Add nodes if not already in the graph if(!inheritanceGraph.containsKey(pair.getLhsType())) - inheritanceGraph.put(pair.getLhsType(), new Node(pair.getLhsType())); + inheritanceGraph.put(pair.getLhsType(), new Node(pair.getLhsType())); if(!inheritanceGraph.containsKey(pair.getRhsType())) - inheritanceGraph.put(pair.getRhsType(), new Node(pair.getRhsType())); + inheritanceGraph.put(pair.getRhsType(), new Node(pair.getRhsType())); - Node childNode = inheritanceGraph.get(pair.getLhsType()); - Node parentNode = inheritanceGraph.get(pair.getRhsType()); + Node childNode = inheritanceGraph.get(pair.getLhsType()); + Node parentNode = inheritanceGraph.get(pair.getRhsType()); // Add edge parentNode.AddDescendant(childNode); @@ -43,7 +43,7 @@ public class FiniteClosure implements IFiniteClosure { // Build the alternative representation with strings as keys strInheritanceGraph = new HashMap<>(); - for(Type key : inheritanceGraph.keySet()) { + for(UnifyType key : inheritanceGraph.keySet()) { if(!strInheritanceGraph.containsKey(key.getName())) strInheritanceGraph.put(key.getName(), new HashSet<>()); @@ -56,11 +56,11 @@ public class FiniteClosure implements IFiniteClosure { * @return The set of subtypes of the argument. */ @Override - public Set smaller(Type type) { + public Set smaller(UnifyType type) { if(!inheritanceGraph.containsKey(type)) return new HashSet<>(); - Set result = inheritanceGraph.get(type).getContentOfDescendants(); + Set result = inheritanceGraph.get(type).getContentOfDescendants(); result.add(type); return result; @@ -71,26 +71,26 @@ public class FiniteClosure implements IFiniteClosure { * @return The set of supertypes of the argument. */ @Override - public Set greater(Type type) { + public Set greater(UnifyType type) { if(!inheritanceGraph.containsKey(type)) return new HashSet<>(); - Set result = inheritanceGraph.get(type).getContentOfPredecessors(); + Set result = inheritanceGraph.get(type).getContentOfPredecessors(); result.add(type); return result; } @Override - public Set grArg(Type type) { + public Set grArg(UnifyType type) { return type.grArg(this); } @Override - public Set grArg(SimpleType type) { + public Set grArg(SimpleType type) { if(!inheritanceGraph.containsKey(type)) - return new HashSet(); + return new HashSet(); - Set result = new HashSet(); + Set result = new HashSet(); result.add(type); smaller(type).forEach(x -> result.add(new SuperType(x))); @@ -100,13 +100,13 @@ public class FiniteClosure implements IFiniteClosure { } @Override - public Set grArg(ExtendsType type) { + public Set grArg(ExtendsType type) { if(!inheritanceGraph.containsKey(type.getExtendedType())) - return new HashSet(); + return new HashSet(); - Set result = new HashSet(); + Set result = new HashSet(); - Type t = type.getExtendedType(); + UnifyType t = type.getExtendedType(); greater(t).forEach(x -> result.add(new ExtendsType(x))); @@ -114,13 +114,13 @@ public class FiniteClosure implements IFiniteClosure { } @Override - public Set grArg(SuperType type) { + public Set grArg(SuperType type) { if(!inheritanceGraph.containsKey(type.getSuperedType())) - return new HashSet(); + return new HashSet(); - Set result = new HashSet(); + Set result = new HashSet(); - Type t = type.getSuperedType(); + UnifyType t = type.getSuperedType(); smaller(t).forEach(x -> result.add(new SuperType(x))); @@ -128,21 +128,21 @@ public class FiniteClosure implements IFiniteClosure { } @Override - public Set grArg(PlaceholderType type) { + public Set grArg(PlaceholderType type) { return new HashSet<>(); } @Override - public Set smArg(Type type) { + public Set smArg(UnifyType type) { return type.smArg(this); } @Override - public Set smArg(SimpleType type) { + public Set smArg(SimpleType type) { if(!inheritanceGraph.containsKey(type)) - return new HashSet(); + return new HashSet(); - Set result = new HashSet(); + Set result = new HashSet(); result.add(type); smaller(type).forEach(x -> result.add(new ExtendsType(x))); @@ -150,13 +150,13 @@ public class FiniteClosure implements IFiniteClosure { return result; } - public Set smArg(ExtendsType type) { + public Set smArg(ExtendsType type) { if(!inheritanceGraph.containsKey(type.getExtendedType())) - return new HashSet(); + return new HashSet(); - Set result = new HashSet(); + Set result = new HashSet(); - Type t = type.getExtendedType(); + UnifyType t = type.getExtendedType(); result.add(t); smaller(t).forEach(x -> { @@ -169,13 +169,13 @@ public class FiniteClosure implements IFiniteClosure { @Override - public Set smArg(SuperType type) { + public Set smArg(SuperType type) { if(!inheritanceGraph.containsKey(type.getSuperedType())) - return new HashSet(); + return new HashSet(); - Set result = new HashSet(); + Set result = new HashSet(); - Type t = type.getSuperedType(); + UnifyType t = type.getSuperedType(); result.add(t); greater(t).forEach(x -> { @@ -187,19 +187,19 @@ public class FiniteClosure implements IFiniteClosure { } @Override - public Set smArg(PlaceholderType type) { + public Set smArg(PlaceholderType type) { return new HashSet<>(); } @Override - public Optional getGenericType(String typeName) { + public Optional getGenericType(String typeName) { if(!strInheritanceGraph.containsKey(typeName)) return Optional.empty(); - HashSet> candidates = strInheritanceGraph.get(typeName); + HashSet> candidates = strInheritanceGraph.get(typeName); - for(Node node : candidates) { - Type candidate = node.getContent(); + for(Node node : candidates) { + UnifyType candidate = node.getContent(); if(candidate.getTypeParams().arePlaceholders()) return Optional.of(candidate); } @@ -208,7 +208,7 @@ public class FiniteClosure implements IFiniteClosure { } @Override - public Set getAllTypes(String typeName) { + public Set getAllTypes(String typeName) { if(!strInheritanceGraph.containsKey(typeName)) return new HashSet<>(); return strInheritanceGraph.get(typeName).stream().map(x -> x.getContent()).collect(Collectors.toCollection(HashSet::new)); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java index 6b740c46..d7d17d52 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java @@ -26,8 +26,8 @@ public class MPair { }; } - private Type lhs; - private Type rhs; + private UnifyType lhs; + private UnifyType rhs; private PairOperator pairOp; /*public MPair(Type t1, Type t2) { @@ -36,17 +36,17 @@ public class MPair { pairOp = PairOperator.SMALLER; }*/ - public MPair(Type t1, Type t2, PairOperator op) { + public MPair(UnifyType t1, UnifyType t2, PairOperator op) { lhs = t1; rhs = t2; pairOp = op; } - public Type getLhsType() { + public UnifyType getLhsType() { return lhs; } - public Type getRhsType() { + public UnifyType getRhsType() { return rhs; } @@ -72,11 +72,11 @@ public class MPair { * @param subst The type replacing t. * @return A pair where occurrences of t are replaced by subst. */ - public MPair substitute(Type t, Type subst) { - Type newlhs = lhs; + public MPair substitute(UnifyType t, UnifyType subst) { + UnifyType newlhs = lhs; if(lhs.equals(t)) newlhs = subst; - Type newrhs = rhs; + UnifyType newrhs = rhs; if(rhs.equals(t)) newrhs = subst; if(newlhs == lhs && newrhs == rhs) return this; diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index 5a3d5341..f810e993 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -4,24 +4,24 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -public final class PlaceholderType extends Type{ +public final class PlaceholderType extends UnifyType{ public PlaceholderType(String name) { super(name); } @Override - Set smArg(IFiniteClosure fc) { + Set smArg(IFiniteClosure fc) { return fc.smArg(this); } @Override - Set grArg(IFiniteClosure fc) { + Set grArg(IFiniteClosure fc) { return fc.grArg(this); } @Override - public Type setTypeParams(TypeParams newTp) { + public UnifyType setTypeParams(TypeParams newTp) { return this; } @@ -31,7 +31,7 @@ public final class PlaceholderType extends Type{ } @Override - Type apply(Unifier unif) { + UnifyType apply(Unifier unif) { if(unif.hasSubstitute(this)) return unif.getSubstitute(this); return this; diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java index ef01652a..a8997439 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SimpleType.java @@ -4,8 +4,8 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -public final class SimpleType extends Type { - public SimpleType(String name, Type... typeParams) { +public final class SimpleType extends UnifyType { + public SimpleType(String name, UnifyType... typeParams) { super(name, new TypeParams(typeParams)); } @@ -14,22 +14,22 @@ public final class SimpleType extends Type { } @Override - Set smArg(IFiniteClosure fc) { + Set smArg(IFiniteClosure fc) { return fc.smArg(this); } @Override - Set grArg(IFiniteClosure fc) { + Set grArg(IFiniteClosure fc) { return fc.grArg(this); } @Override - Type apply(Unifier unif) { + UnifyType apply(Unifier unif) { return new SimpleType(typeName, typeParams.apply(unif)); } @Override - public Type setTypeParams(TypeParams newTp) { + public UnifyType setTypeParams(TypeParams newTp) { return new SimpleType(new String(typeName), newTp); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java index 2503e748..8283c685 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java @@ -4,16 +4,16 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -public final class SuperType extends Type { +public final class SuperType extends UnifyType { - private Type superedType; + private UnifyType superedType; - public SuperType(Type superedType) { + public SuperType(UnifyType superedType) { super("? super " + superedType.getName(), superedType.getTypeParams()); this.superedType = superedType; } - public Type getSuperedType() { + public UnifyType getSuperedType() { return superedType; } @@ -28,22 +28,22 @@ public final class SuperType extends Type { } @Override - public Type setTypeParams(TypeParams newTp) { + public UnifyType setTypeParams(TypeParams newTp) { return new SuperType(superedType.setTypeParams(newTp)); } @Override - Set smArg(IFiniteClosure fc) { + Set smArg(IFiniteClosure fc) { return fc.smArg(this); } @Override - Set grArg(IFiniteClosure fc) { + Set grArg(IFiniteClosure fc) { return fc.grArg(this); } @Override - Type apply(Unifier unif) { + UnifyType apply(Unifier unif) { return new SuperType(superedType.apply(unif)); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java index 966a561b..a95a1b43 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java @@ -3,15 +3,15 @@ package de.dhbwstuttgart.typeinference.unify.model; import java.util.Arrays; import java.util.Iterator; -public final class TypeParams implements Iterable{ - private final Type[] typeParams; +public final class TypeParams implements Iterable{ + private final UnifyType[] typeParams; - public TypeParams(Type... types) { + public TypeParams(UnifyType... types) { typeParams = types; } public boolean arePlaceholders() { - for(Type t : typeParams) + for(UnifyType t : typeParams) if(!(t instanceof PlaceholderType)) return false; return true; @@ -20,7 +20,7 @@ public final class TypeParams implements Iterable{ @Override public String toString() { String res = ""; - for(Type t : typeParams) + for(UnifyType t : typeParams) res += t + ","; return "<" + res.substring(0, res.length()-1) + ">"; } @@ -34,14 +34,14 @@ public final class TypeParams implements Iterable{ } public TypeParams apply(Unifier unif) { - Type[] newParams = new Type[typeParams.length]; + UnifyType[] newParams = new UnifyType[typeParams.length]; for(int i = 0; i < typeParams.length; i++) newParams[i] = typeParams[i].apply(unif); return new TypeParams(newParams); } public boolean occurs(PlaceholderType t) { - for(Type p : typeParams) + for(UnifyType p : typeParams) if(p instanceof PlaceholderType) if(p.equals(t)) return true; @@ -51,26 +51,26 @@ public final class TypeParams implements Iterable{ return false; } - public boolean contains(Type t) { - for(Type t1 : typeParams) + public boolean contains(UnifyType t) { + for(UnifyType t1 : typeParams) if(t1.equals(t1)) return true; return false; } - public Type get(int i) { + public UnifyType get(int i) { return typeParams[i]; } - public TypeParams set(Type t, int idx) { - Type[] newparams = Arrays.copyOf(typeParams, typeParams.length); + public TypeParams set(UnifyType t, int idx) { + UnifyType[] newparams = Arrays.copyOf(typeParams, typeParams.length); newparams[idx] = t; return new TypeParams(newparams); } @Override - public Iterator iterator() { + public Iterator iterator() { return Arrays.stream(typeParams).iterator(); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java index 0a1fde25..c277f8fc 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java @@ -5,12 +5,12 @@ import java.util.Map.Entry; import java.util.Set; import java.util.function.Function; -public class Unifier implements Function { - private HashMap substitutions = new HashMap<>(); +public class Unifier implements Function { + private HashMap substitutions = new HashMap<>(); public static Unifier IDENTITY = new Unifier(); - public Unifier(PlaceholderType source, Type target) { + public Unifier(PlaceholderType source, UnifyType target) { substitutions.put(source, target); } @@ -21,7 +21,7 @@ public class Unifier implements Function { } - public void Add(PlaceholderType source, Type target) { + public void Add(PlaceholderType source, UnifyType target) { Unifier tempU = new Unifier(source, target); for(PlaceholderType pt : substitutions.keySet()) substitutions.put(pt, substitutions.get(pt).apply(tempU)); @@ -29,7 +29,7 @@ public class Unifier implements Function { } @Override - public Type apply(Type t) { + public UnifyType apply(UnifyType t) { return t.apply(this); } @@ -41,18 +41,18 @@ public class Unifier implements Function { return substitutions.containsKey(t); } - public Type getSubstitute(Type t) { + public UnifyType getSubstitute(UnifyType t) { return substitutions.get(t); } - public Set> getSubstitutions() { + public Set> getSubstitutions() { return substitutions.entrySet(); } @Override public String toString() { String result = "{ "; - for(Entry entry : substitutions.entrySet()) + for(Entry entry : substitutions.entrySet()) result += "(" + entry.getKey() + " -> " + entry.getValue() + "), "; if(!substitutions.isEmpty()) result = result.substring(0, result.length()-2); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Type.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java similarity index 61% rename from src/de/dhbwstuttgart/typeinference/unify/model/Type.java rename to src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java index 3c179110..341eabd1 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Type.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java @@ -4,17 +4,17 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -public abstract class Type { +public abstract class UnifyType { protected final String typeName; protected final TypeParams typeParams; - protected Type(String name, Type... typeParams) { + protected UnifyType(String name, UnifyType... typeParams) { typeName = name; this.typeParams = new TypeParams(typeParams); } - protected Type(String name, TypeParams p) { + protected UnifyType(String name, TypeParams p) { typeName = name; typeParams = p; } @@ -27,19 +27,19 @@ public abstract class Type { return typeParams; } - public abstract Type setTypeParams(TypeParams newTp); + public abstract UnifyType setTypeParams(TypeParams newTp); - abstract Set smArg(IFiniteClosure fc); + abstract Set smArg(IFiniteClosure fc); - abstract Set grArg(IFiniteClosure fc); + abstract Set grArg(IFiniteClosure fc); - abstract Type apply(Unifier unif); + abstract UnifyType apply(Unifier unif); @Override public String toString() { String params = ""; if(typeParams.size() != 0) { - for(Type param : typeParams) + for(UnifyType param : typeParams) params += param.toString() + ","; params = "<" + params.substring(0, params.length()-1) + ">"; } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java b/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java index c8996dc8..e21bb728 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java @@ -8,16 +8,16 @@ import java.util.stream.Collectors; public class Mapping { - private HashMap backwardMap = new HashMap<>(); - private HashMap forwardMap = new HashMap<>(); - private Set irreversible = new HashSet<>(); + private HashMap backwardMap = new HashMap<>(); + private HashMap forwardMap = new HashMap<>(); + private Set irreversible = new HashSet<>(); public Mapping(Set types) { for(de.dhbwstuttgart.syntaxtree.type.Type t : types) { } } - public de.dhbwstuttgart.typeinference.unify.model.Type map(de.dhbwstuttgart.syntaxtree.type.Type type) { + public de.dhbwstuttgart.typeinference.unify.model.UnifyType map(de.dhbwstuttgart.syntaxtree.type.Type type) { return forwardMap.get(type); } @@ -25,7 +25,7 @@ public class Mapping { return new de.dhbwstuttgart.typeinference.unify.model.MPair(forwardMap.get(pair.TA1), forwardMap.get(pair.TA2), mapOp(pair.GetOperator())); } - public Set mapTypeSet(Set types) { + public Set mapTypeSet(Set types) { return types.stream().map(this::map).collect(Collectors.toCollection(HashSet::new)); } @@ -33,20 +33,20 @@ public class Mapping { return pairs.stream().map(this::map).collect(Collectors.toCollection(HashSet::new)); } - public Optional unmap(de.dhbwstuttgart.typeinference.unify.model.Type type) { + public Optional unmap(de.dhbwstuttgart.typeinference.unify.model.UnifyType type) { return irreversible.contains(type) ? Optional.of(backwardMap.get(type)) : Optional.empty(); } public Optional unmap(de.dhbwstuttgart.typeinference.unify.model.MPair mpair) { - de.dhbwstuttgart.typeinference.unify.model.Type lhs = mpair.getLhsType(); - de.dhbwstuttgart.typeinference.unify.model.Type rhs = mpair.getRhsType(); + de.dhbwstuttgart.typeinference.unify.model.UnifyType lhs = mpair.getLhsType(); + de.dhbwstuttgart.typeinference.unify.model.UnifyType rhs = mpair.getRhsType(); if(irreversible.contains(lhs) || irreversible.contains(rhs)) return Optional.empty(); return Optional.of(new de.dhbwstuttgart.typeinference.Pair(backwardMap.get(lhs), backwardMap.get(rhs), unmapOp(mpair.getPairOp()))); } - public Optional> unmapTypeSet(Set types) { + public Optional> unmapTypeSet(Set types) { Set result = types.stream().map(this::unmap).filter(x -> x.isPresent()).map(x -> x.get()).collect(Collectors.toCollection(HashSet::new)); return result.size() == types.size() ? Optional.of(result) : Optional.empty(); } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java index 6db1a512..4409ae7f 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java @@ -13,7 +13,7 @@ import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; -import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.TypeParams; import de.dhbwstuttgart.typeinference.unify.model.Unifier; @@ -24,15 +24,15 @@ import de.dhbwstuttgart.typeinference.unify.model.Unifier; public class MartelliMontanariUnify implements IUnify { @Override - public Optional unify(Set terms) { + public Optional unify(Set terms) { if(terms.size() < 2) return Optional.of(Unifier.IDENTITY); ArrayList termsQ = new ArrayList(); - Iterator iter = terms.iterator(); - Type prev = iter.next(); + Iterator iter = terms.iterator(); + UnifyType prev = iter.next(); while(iter.hasNext()) { - Type next = iter.next(); + UnifyType next = iter.next(); termsQ.add(new MPair(prev, next, PairOperator.EQUALS)); prev = next; } @@ -72,10 +72,10 @@ public class MartelliMontanariUnify implements IUnify { && pair.getRhsType().getTypeParams().occurs((PlaceholderType) pair.getLhsType())) return Optional.empty(); - Optional> optUni = eliminate(pair); + Optional> optUni = eliminate(pair); if(optUni.isPresent()) { - Entry substitution = optUni.get(); + Entry substitution = optUni.get(); mgu.Add(substitution.getKey(), substitution.getValue()); termsQ = termsQ.stream().map(mgu::apply).collect(Collectors.toCollection(ArrayList::new)); idx = idx+1 == termsQ.size() ? 0 : idx+1; @@ -95,8 +95,8 @@ public class MartelliMontanariUnify implements IUnify { private Optional> decompose(MPair pair) { Set result = new HashSet<>(); - Type rhs = pair.getRhsType(); - Type lhs = pair.getLhsType(); + UnifyType rhs = pair.getRhsType(); + UnifyType lhs = pair.getLhsType(); TypeParams rhsTypeParams = rhs.getTypeParams(); TypeParams lhsTypeParams = lhs.getTypeParams(); @@ -114,8 +114,8 @@ public class MartelliMontanariUnify implements IUnify { } private Optional swap(MPair pair) { - Type rhs = pair.getRhsType(); - Type lhs = pair.getLhsType(); + UnifyType rhs = pair.getRhsType(); + UnifyType lhs = pair.getLhsType(); if(!(lhs instanceof PlaceholderType) && (rhs instanceof PlaceholderType)) return Optional.of(new MPair(rhs, lhs, PairOperator.EQUALSDOT)); @@ -123,15 +123,15 @@ public class MartelliMontanariUnify implements IUnify { return Optional.empty(); } - private Optional> eliminate(MPair pair) { - Type rhs = pair.getRhsType(); - Type lhs = pair.getLhsType(); + private Optional> eliminate(MPair pair) { + UnifyType rhs = pair.getRhsType(); + UnifyType lhs = pair.getLhsType(); // TODO only apply when lhs is element of vars(termsQ)? if(!(lhs instanceof PlaceholderType)) return Optional.empty(); - return Optional.of(new AbstractMap.SimpleImmutableEntry((PlaceholderType) lhs, rhs)); + return Optional.of(new AbstractMap.SimpleImmutableEntry((PlaceholderType) lhs, rhs)); } } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index e41525f5..fedccd86 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -18,7 +18,7 @@ import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.SimpleType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; -import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.TypeParams; import de.dhbwstuttgart.typeinference.unify.model.Unifier; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; @@ -36,11 +36,11 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); - Type rhsType = pair.getRhsType(); + UnifyType rhsType = pair.getRhsType(); if(!(rhsType instanceof SuperType)) return Optional.empty(); - Type lhsType = pair.getLhsType(); + UnifyType lhsType = pair.getLhsType(); if(!(lhsType instanceof SimpleType) && !(lhsType instanceof PlaceholderType)) return Optional.empty(); @@ -52,11 +52,11 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); - Type lhsType = pair.getLhsType(); + UnifyType lhsType = pair.getLhsType(); if(!(lhsType instanceof ExtendsType)) return Optional.empty(); - Type rhsType = pair.getRhsType(); + UnifyType rhsType = pair.getRhsType(); if(!(rhsType instanceof SimpleType) && !(rhsType instanceof PlaceholderType)) return Optional.empty(); @@ -68,11 +68,11 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); - Type lhsType = pair.getLhsType(); + UnifyType lhsType = pair.getLhsType(); if(!(lhsType instanceof ExtendsType)) return Optional.empty(); - Type rhsType = pair.getRhsType(); + UnifyType rhsType = pair.getRhsType(); if(!(rhsType instanceof SuperType)) return Optional.empty(); @@ -84,12 +84,12 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); - Type lhsType = pair.getLhsType(); + UnifyType lhsType = pair.getLhsType(); if(!(lhsType instanceof SimpleType) && !(lhsType instanceof ExtendsType)) return Optional.empty(); - Type rhsType = pair.getRhsType(); + UnifyType rhsType = pair.getRhsType(); if(!(rhsType instanceof ExtendsType)) return Optional.empty(); @@ -117,12 +117,12 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); - Type lhsType = pair.getLhsType(); + UnifyType lhsType = pair.getLhsType(); if(!(lhsType instanceof SimpleType) && !(lhsType instanceof SuperType)) return Optional.empty(); - Type rhsType = pair.getRhsType(); + UnifyType rhsType = pair.getRhsType(); if(!(rhsType instanceof SuperType)) return Optional.empty(); @@ -150,11 +150,11 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); - Type lhsType = pair.getLhsType(); + UnifyType lhsType = pair.getLhsType(); if(lhsType instanceof PlaceholderType || lhsType.getTypeParams().empty()) return Optional.empty(); - Type rhsType = pair.getRhsType(); + UnifyType rhsType = pair.getRhsType(); if(!rhsType.getName().equals(lhsType.getName())) return Optional.empty(); @@ -181,11 +181,11 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); - Type lhsType = pair.getLhsType(); + UnifyType lhsType = pair.getLhsType(); if(!(lhsType instanceof SimpleType)) return Optional.empty(); - Type rhsType = pair.getRhsType(); + UnifyType rhsType = pair.getRhsType(); if(!(rhsType instanceof SimpleType)) return Optional.empty(); @@ -215,7 +215,7 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.EQUALSDOT) return Optional.empty(); - Type lhsType = pair.getLhsType(); + UnifyType lhsType = pair.getLhsType(); SimpleType lhsSType; if(lhsType instanceof SimpleType) @@ -230,7 +230,7 @@ public class RuleSet implements IRuleSet{ if(lhsSType.getTypeParams().empty()) return Optional.empty(); - Type rhsType = pair.getLhsType(); + UnifyType rhsType = pair.getLhsType(); SimpleType rhsSType; if(rhsType instanceof SimpleType) @@ -264,11 +264,11 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOT) return false; - Type lhsType = pair.getLhsType(); + UnifyType lhsType = pair.getLhsType(); if(!(lhsType instanceof SimpleType) && !(lhsType instanceof PlaceholderType)) return false; - Type rhsType = pair.getRhsType(); + UnifyType rhsType = pair.getRhsType(); if(!(rhsType instanceof SimpleType) && !(rhsType instanceof PlaceholderType)) return false; @@ -280,8 +280,8 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return false; - Type lhsType = pair.getLhsType(); - Type rhsType = pair.getRhsType(); + UnifyType lhsType = pair.getLhsType(); + UnifyType rhsType = pair.getRhsType(); return finiteClosure.grArg(lhsType).contains(rhsType); } @@ -313,11 +313,11 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOT) return Optional.empty(); - Type typeD = pair.getLhsType(); + UnifyType typeD = pair.getLhsType(); if(!(typeD instanceof SimpleType)) return Optional.empty(); - Type typeDs = pair.getRhsType(); + UnifyType typeDs = pair.getRhsType(); if(!(typeDs instanceof SimpleType)) return Optional.empty(); @@ -327,22 +327,22 @@ public class RuleSet implements IRuleSet{ if(typeD.getName().equals(typeDs.getName())) return Optional.empty(); - Optional opt = finiteClosure.getGenericType(typeD.getName()); + Optional opt = finiteClosure.getGenericType(typeD.getName()); if(!opt.isPresent()) return Optional.empty(); // The generic Version of Type D (D) - Type typeDgen = opt.get(); + UnifyType typeDgen = opt.get(); // Actually greater+ because the types are ensured to have different names - Set greater = finiteClosure.greater(typeDgen); + Set greater = finiteClosure.greater(typeDgen); opt = greater.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny(); if(!opt.isPresent()) return Optional.empty(); - Type newLhs = opt.get(); + UnifyType newLhs = opt.get(); TypeParams typeDParams = typeD.getTypeParams(); TypeParams typeDgenParams = typeDgen.getTypeParams(); @@ -359,36 +359,36 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); - Type typeD = pair.getLhsType(); + UnifyType typeD = pair.getLhsType(); if(!(typeD instanceof SimpleType) && !(typeD instanceof ExtendsType)) return Optional.empty(); - Type typeExtDs = pair.getRhsType(); + UnifyType typeExtDs = pair.getRhsType(); if(!(typeExtDs instanceof ExtendsType)) return Optional.empty(); if(typeD.getTypeParams().size() == 0 || typeExtDs.getTypeParams().size() == 0) return Optional.empty(); - Type typeDgen; + UnifyType typeDgen; if(typeD instanceof SimpleType) typeDgen = finiteClosure.getGenericType(typeD.getName()).orElse(null); else { - Optional opt = finiteClosure.getGenericType(((ExtendsType) typeD).getExtendedType().getName()); + Optional opt = finiteClosure.getGenericType(((ExtendsType) typeD).getExtendedType().getName()); typeDgen = opt.isPresent() ? new ExtendsType(opt.get()) : null; } if(typeDgen == null) return Optional.empty(); - Set grArg = finiteClosure.grArg(typeDgen); + Set grArg = finiteClosure.grArg(typeDgen); - Optional opt = grArg.stream().filter(x -> x.getName().equals(typeExtDs.getName())).findAny(); + Optional opt = grArg.stream().filter(x -> x.getName().equals(typeExtDs.getName())).findAny(); if(!opt.isPresent()) return Optional.empty(); - Type newLhs = ((ExtendsType) opt.get()).getExtendedType(); + UnifyType newLhs = ((ExtendsType) opt.get()).getExtendedType(); TypeParams typeDParams = typeD.getTypeParams(); TypeParams typeDgenParams = typeDgen.getTypeParams(); @@ -405,11 +405,11 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return Optional.empty(); - Type typeDs = pair.getLhsType(); + UnifyType typeDs = pair.getLhsType(); if(!(typeDs instanceof SimpleType) && !(typeDs instanceof SuperType)) return Optional.empty(); - Type typeSupD = pair.getRhsType(); + UnifyType typeSupD = pair.getRhsType(); if(!(typeSupD instanceof SuperType)) return Optional.empty(); @@ -417,31 +417,31 @@ public class RuleSet implements IRuleSet{ return Optional.empty(); - Optional opt = finiteClosure.getGenericType(((SuperType) typeSupD).getSuperedType().getName()); + Optional opt = finiteClosure.getGenericType(((SuperType) typeSupD).getSuperedType().getName()); if(!opt.isPresent()) return Optional.empty(); - Type typeDgen = opt.get(); - Type typeSupDgen = new SuperType(typeDgen); + UnifyType typeDgen = opt.get(); + UnifyType typeSupDgen = new SuperType(typeDgen); // Use of smArg instead of grArg because // a in grArg(b) => b in smArg(a) - Set smArg = finiteClosure.smArg(typeSupDgen); + Set smArg = finiteClosure.smArg(typeSupDgen); opt = smArg.stream().filter(x -> x.getName().equals(typeDs.getName())).findAny(); if(!opt.isPresent()) return Optional.empty(); // New RHS - Type newRhs = null; + UnifyType newRhs = null; if(typeDs instanceof SimpleType) newRhs = new ExtendsType(typeDs); else newRhs = new ExtendsType(((SuperType) typeDs).getSuperedType()); // New LHS - Type newLhs = opt.get(); + UnifyType newLhs = opt.get(); TypeParams typeDParams = typeSupD.getTypeParams(); TypeParams typeSupDsgenParams = typeSupDgen.getTypeParams(); @@ -458,23 +458,23 @@ public class RuleSet implements IRuleSet{ * @param D The other type * @return An array containing the values of pi for every type argument of C or an empty array if the search failed. */ - private int[] pi(Type C, Type D) { - Type cFromFc = null; + private int[] pi(UnifyType C, UnifyType D) { + UnifyType cFromFc = null; if(C instanceof SimpleType) cFromFc = finiteClosure.getGenericType(C.getName()).orElse(null); else if(C instanceof ExtendsType) { - Optional opt = finiteClosure.getGenericType(((ExtendsType) C).getExtendedType().getName()); + Optional opt = finiteClosure.getGenericType(((ExtendsType) C).getExtendedType().getName()); if(opt.isPresent()) cFromFc = new ExtendsType(opt.get()); } else if(C instanceof SuperType) { - Optional opt = finiteClosure.getGenericType(((SuperType) C).getSuperedType().getName()); + Optional opt = finiteClosure.getGenericType(((SuperType) C).getSuperedType().getName()); if(opt.isPresent()) cFromFc = new SuperType(opt.get()); } if(cFromFc == null) return new int[0]; - Optional opt = Optional.empty(); + Optional opt = Optional.empty(); if(D instanceof ExtendsType) { SimpleType dSType = (SimpleType) ((ExtendsType) D).getExtendedType(); opt = finiteClosure.grArg(cFromFc).stream() @@ -494,7 +494,7 @@ public class RuleSet implements IRuleSet{ if(!opt.isPresent()) return new int[0]; - Type dFromFc = opt.get(); + UnifyType dFromFc = opt.get(); Assert.assertEquals(cFromFc.getTypeParams().size(), dFromFc.getTypeParams().size()); Assert.assertTrue(dFromFc.getTypeParams().size() > 0); @@ -506,7 +506,7 @@ public class RuleSet implements IRuleSet{ boolean succ = true; for (int dArgIdx = 0; dArgIdx < dArgs.size() && succ; dArgIdx++) { - Type dArg = dArgs.get(dArgIdx); + UnifyType dArg = dArgs.get(dArgIdx); succ = false; for (int pi = 0; pi < cArgs.size(); pi++) if (cArgs.get(pi).getName().equals(dArg.getName())) { @@ -522,11 +522,11 @@ public class RuleSet implements IRuleSet{ @Override public Optional> subst(Set pairs) { - HashMap typeMap = new HashMap<>(); + HashMap typeMap = new HashMap<>(); for(MPair pair : pairs) { - Type t1 = pair.getLhsType(); - Type t2 = pair.getRhsType(); + UnifyType t1 = pair.getLhsType(); + UnifyType t2 = pair.getRhsType(); if(!typeMap.containsKey(t1)) typeMap.put(t1, 0); if(!typeMap.containsKey(t2)) @@ -542,7 +542,7 @@ public class RuleSet implements IRuleSet{ for(int i = 0; i < result.size(); i++) { MPair pair = result.get(i); PlaceholderType lhsType = null; - Type rhsType; + UnifyType rhsType; if(pair.getPairOp() == PairOperator.EQUALSDOT && pair.getLhsType() instanceof PlaceholderType) diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 548340b3..6fa2eda1 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -23,7 +23,7 @@ import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; -import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.TypeParams; import de.dhbwstuttgart.typeinference.unify.model.Unifier; @@ -223,46 +223,46 @@ public class Unify { for(MPair pair : eq2s) { PairOperator pairOp = pair.getPairOp(); - Type lhsType = pair.getLhsType(); - Type rhsType = pair.getRhsType(); + UnifyType lhsType = pair.getLhsType(); + UnifyType rhsType = pair.getRhsType(); // Case 1: (a <. Theta') if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) { - Type thetaPrime = pair.getRhsType(); + UnifyType thetaPrime = pair.getRhsType(); Set set = new HashSet<>(); IUnify unify = new MartelliMontanariUnify(); //Set cs = fc.getAllTypes(rhsType.getName()); - Type c = rhsType; + UnifyType c = rhsType; //Set thetaQs = cs.stream().flatMap(x -> fc.smaller(x).stream()).collect(Collectors.toCollection(HashSet::new)); - Set thetaQs = fc.smaller(c); + Set thetaQs = fc.smaller(c); - Set thetaQPrimes = new HashSet<>(); + Set thetaQPrimes = new HashSet<>(); TypeParams cParams = c.getTypeParams(); if(cParams.size() == 0) thetaQPrimes.add(c); else { - ArrayList> candidateParams = new ArrayList<>(); - for(Type param : cParams) + ArrayList> candidateParams = new ArrayList<>(); + for(UnifyType param : cParams) candidateParams.add(fc.grArg(param)); Set permutations = new HashSet(); - permuteParams(candidateParams, 0, permutations, new Type[candidateParams.size()]); + permuteParams(candidateParams, 0, permutations, new UnifyType[candidateParams.size()]); for(TypeParams tp : permutations) thetaQPrimes.add(c.setTypeParams(tp)); } - for(Type tqp : thetaQPrimes) { + for(UnifyType tqp : thetaQPrimes) { Optional opt = unify.unify(tqp, thetaPrime); if(opt.isPresent()) { Unifier unifier = opt.get(); - Set> substitutions = unifier.getSubstitutions(); - for(Entry sigma : substitutions) + Set> substitutions = unifier.getSubstitutions(); + for(Entry sigma : substitutions) set.add(new MPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); - for(Type tq : thetaQs) { - Set smaller = fc.smaller(unifier.apply(tq)); + for(UnifyType tq : thetaQs) { + Set smaller = fc.smaller(unifier.apply(tq)); smaller.stream().map(x -> new MPair(lhsType, x, PairOperator.EQUALSDOT)).forEach(x -> set.add(x)); } } @@ -279,7 +279,7 @@ public class Unify { // Case 3: (a <.? ? sup Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof SuperType) { Set set = new HashSet<>(); - for(Type theta : fc.smArg(rhsType)) + for(UnifyType theta : fc.smArg(rhsType)) set.add(new MPair(lhsType, theta, PairOperator.EQUALSDOT)); result.add(set); } @@ -294,7 +294,7 @@ public class Unify { // Case 5: (Theta <. a) else if(pairOp == PairOperator.SMALLERDOT && rhsType instanceof PlaceholderType) { Set set = new HashSet<>(); - for(Type thetaS : fc.greater(lhsType)) + for(UnifyType thetaS : fc.greater(lhsType)) set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT)); result.add(set); } @@ -302,7 +302,7 @@ public class Unify { // Case 6: (? ext Theta <.? a) else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof ExtendsType && rhsType instanceof PlaceholderType) { Set set = new HashSet<>(); - for(Type thetaS : fc.grArg(lhsType)) + for(UnifyType thetaS : fc.grArg(lhsType)) set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT)); result.add(set); } @@ -315,7 +315,7 @@ public class Unify { // Case 8: (Theta <.? a) else if(pairOp == PairOperator.SMALLERDOTWC && rhsType instanceof PlaceholderType) { Set set = new HashSet<>(); - for(Type thetaS : fc.grArg(lhsType)) + for(UnifyType thetaS : fc.grArg(lhsType)) set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT)); result.add(set); } @@ -324,22 +324,22 @@ public class Unify { return result; } - protected void permuteParams(ArrayList> candidates, int idx, Set result, Type[] current) { + protected void permuteParams(ArrayList> candidates, int idx, Set result, UnifyType[] current) { if(candidates.size() == idx) { result.add(new TypeParams(Arrays.copyOf(current, current.length))); return; } - Set localCandidates = candidates.get(idx); + Set localCandidates = candidates.get(idx); - for(Type t : localCandidates) { + for(UnifyType t : localCandidates) { current[idx] = t; permuteParams(candidates, idx+1, result, current); } } - private Set getAllInstantiations(Type t, IFiniteClosure fc) { - Set result = new HashSet<>(); + private Set getAllInstantiations(UnifyType t, IFiniteClosure fc) { + Set result = new HashSet<>(); result.add(t); return result; diff --git a/test/unify/FiniteClosureBuilder.java b/test/unify/FiniteClosureBuilder.java index d5b3b32b..30831a89 100644 --- a/test/unify/FiniteClosureBuilder.java +++ b/test/unify/FiniteClosureBuilder.java @@ -6,15 +6,14 @@ 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.Type; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; -import de.dhbwstuttgart.typeinference.unifynew.TypeFactory; public class FiniteClosureBuilder { private Set pairs = new HashSet<>(); - public void add(Type sub, Type sup) { + public void add(UnifyType sub, UnifyType sup) { pairs.add(new MPair(sub, sup, PairOperator.SMALLER)); } @@ -29,19 +28,19 @@ public class FiniteClosureBuilder { public IFiniteClosure getCollectionExample() { TypeFactory tf = new TypeFactory(); - Type collection = tf.getSimpleType("Collection"); - Type set = tf.getSimpleType("Set", "T"); - Type sortedSet = tf.getSimpleType("Set", "T"); - Type TreeSet = tf.getSimpleType("TreeSet", "T"); - Type hashSet = tf.getSimpleType("HashSet", "T"); - Type linkedHashSet = tf.getSimpleType("LinkedHashSet", "T"); - Type queue = tf.getSimpleType("Queue", "T"); - Type deque = tf.getSimpleType("Deque", "T"); - Type linkedList = tf.getSimpleType("LinkedList", "T"); - Type list = tf.getSimpleType("List", "T"); - Type vector = tf.getSimpleType("Vector", "T"); - Type stack = tf.getSimpleType("Stack", "T"); - Type arrayList = tf.getSimpleType("ArrayList", "T"); + UnifyType collection = tf.getSimpleType("Collection"); + UnifyType set = tf.getSimpleType("Set", "T"); + UnifyType sortedSet = tf.getSimpleType("Set", "T"); + UnifyType TreeSet = tf.getSimpleType("TreeSet", "T"); + UnifyType hashSet = tf.getSimpleType("HashSet", "T"); + UnifyType linkedHashSet = tf.getSimpleType("LinkedHashSet", "T"); + UnifyType queue = tf.getSimpleType("Queue", "T"); + UnifyType deque = tf.getSimpleType("Deque", "T"); + UnifyType linkedList = tf.getSimpleType("LinkedList", "T"); + UnifyType list = tf.getSimpleType("List", "T"); + UnifyType vector = tf.getSimpleType("Vector", "T"); + UnifyType stack = tf.getSimpleType("Stack", "T"); + UnifyType arrayList = tf.getSimpleType("ArrayList", "T"); add(set, collection); add(sortedSet, set); diff --git a/test/unify/FiniteClosureTest.java b/test/unify/FiniteClosureTest.java index 9b5c72e6..1b19969c 100644 --- a/test/unify/FiniteClosureTest.java +++ b/test/unify/FiniteClosureTest.java @@ -8,8 +8,7 @@ import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; -import de.dhbwstuttgart.typeinference.unifynew.TypeFactory; -import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; public class FiniteClosureTest { diff --git a/test/unify/GenerateFiniteClosure.java b/test/unify/GenerateFiniteClosure.java new file mode 100644 index 00000000..378fb019 --- /dev/null +++ b/test/unify/GenerateFiniteClosure.java @@ -0,0 +1,47 @@ +package unify; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import de.dhbwstuttgart.syntaxtree.ImportDeclarations; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +import de.dhbwstuttgart.syntaxtree.misc.UsedId; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; +import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; + +public class GenerateFiniteClosure { + + private TypeAssumptions generateAssumptionsFromImport(String importClass){ + SourceFile sf = new SourceFile(); + ImportDeclarations imports = new ImportDeclarations(); + imports.add(new UsedId(importClass,0)); + TypeAssumptions ass = sf.makeBasicAssumptionsFromJRE(imports, true); + return ass; + } + + @Test + public void generateTypeAssumptions(){ + Menge imports = new Menge<>(); + imports.add("java.util.Vector"); + imports.add("java.lang.Boolean"); + imports.add("java.util.ArrayList"); + + for(String importClass : imports){ + TypeAssumptions ass = generateAssumptionsFromImport(importClass); + assertTrue(ass.getClassAssumptionFor(new RefType(importClass,null,0))!=null); + } + } + + @Test + public void test() { + String importClass = "java.util.Vector"; + TypeAssumptions ass = generateAssumptionsFromImport(importClass); + FiniteClosure fc = UnifyTypeFactory.generateFC(ass); + System.out.println(fc); + } + +} diff --git a/test/unify/RuleSetTest.java b/test/unify/RuleSetTest.java index 1a1f55a5..1fe1359d 100644 --- a/test/unify/RuleSetTest.java +++ b/test/unify/RuleSetTest.java @@ -15,7 +15,6 @@ import de.dhbwstuttgart.typeinference.unify.model.SimpleType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unifynew.RuleSet; -import de.dhbwstuttgart.typeinference.unifynew.TypeFactory; public class RuleSetTest { diff --git a/test/unify/StandardUnifyTest.java b/test/unify/StandardUnifyTest.java index 8bfc0643..e75bf38c 100644 --- a/test/unify/StandardUnifyTest.java +++ b/test/unify/StandardUnifyTest.java @@ -9,10 +9,9 @@ import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; import de.dhbwstuttgart.typeinference.unify.model.MPair; -import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unifynew.MartelliMontanariUnify; -import de.dhbwstuttgart.typeinference.unifynew.TypeFactory; public class StandardUnifyTest { @@ -25,9 +24,9 @@ public class StandardUnifyTest { * Positive Tests */ - Type x = tf.getPlaceholderType("x"); - Type y = tf.getPlaceholderType("y"); - Type f = tf.getSimpleType("f", x); + UnifyType x = tf.getPlaceholderType("x"); + UnifyType y = tf.getPlaceholderType("y"); + UnifyType f = tf.getSimpleType("f", x); // {f = y} Set terms = new HashSet(); @@ -37,10 +36,10 @@ public class StandardUnifyTest { // TODO ist das ergebnis { (x -> ? extends a), (y -> g) } in der richtigen form oder // muss es { (x -> ? extends a), (y -> g) } sein? // {f,x> = f} - Type g = tf.getSimpleType("g", "x"); - Type f1 = tf.getSimpleType("f", g, x); - Type a = tf.getExtendsType(tf.getPlaceholderType("a")); - Type f2 = tf.getSimpleType("f", y, a); + UnifyType g = tf.getSimpleType("g", "x"); + UnifyType f1 = tf.getSimpleType("f", g, x); + UnifyType a = tf.getExtendsType(tf.getPlaceholderType("a")); + UnifyType f2 = tf.getSimpleType("f", y, a); terms = new HashSet<>(); diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/TypeFactory.java b/test/unify/TypeFactory.java similarity index 72% rename from src/de/dhbwstuttgart/typeinference/unifynew/TypeFactory.java rename to test/unify/TypeFactory.java index 9c18c4fc..965e324d 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/TypeFactory.java +++ b/test/unify/TypeFactory.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.typeinference.unifynew; +package unify; import java.util.Arrays; import java.util.stream.Collectors; @@ -7,15 +7,15 @@ import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.SimpleType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; -import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; public class TypeFactory { - public ExtendsType getExtendsType(Type extendedType) { + public ExtendsType getExtendsType(UnifyType extendedType) { return new ExtendsType(extendedType); } - public SuperType getSuperType(Type superedType) { + public SuperType getSuperType(UnifyType superedType) { return new SuperType(superedType); } @@ -23,12 +23,12 @@ public class TypeFactory { return new SimpleType(name); } - public SimpleType getSimpleType(String name, Type... typeParams) { + public SimpleType getSimpleType(String name, UnifyType... typeParams) { return new SimpleType(name, typeParams); } public SimpleType getSimpleType(String name, String... typeParams) { - return new SimpleType(name, Arrays.stream(typeParams).map(x -> getPlaceholderType(x)).collect(Collectors.toList()).toArray(new Type[0])); + return new SimpleType(name, Arrays.stream(typeParams).map(x -> getPlaceholderType(x)).collect(Collectors.toList()).toArray(new UnifyType[0])); } public PlaceholderType getPlaceholderType(String name) { diff --git a/test/unify/UnifyOldTest.java b/test/unify/UnifyOldTest.java index 85f5ffd5..9a28e30a 100644 --- a/test/unify/UnifyOldTest.java +++ b/test/unify/UnifyOldTest.java @@ -7,8 +7,13 @@ 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.RefType; +import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.Type; 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.Pair.PairOperator; @@ -19,7 +24,6 @@ public class UnifyOldTest { @Test public void unifyTestSimpleTypes() { // 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(); @@ -29,13 +33,13 @@ public class UnifyOldTest { */ // Init Types - RefType boolT = typeFactory.GetSimpleType("java.lang.Boolean"); - TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a"); + 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, typeFactory.GetExtendsType(boolT), + resultBuilder.addPair(aTph, this.GetExtendsType(boolT), PairOperator.Equal); Menge> expectedResult = resultBuilder.getNestedPairMenge(); @@ -55,17 +59,17 @@ public class UnifyOldTest { */ // Init Types - boolT = typeFactory.GetSimpleType("java.lang.Boolean"); - aTph = typeFactory.GetTypePlaceholder("a"); - TypePlaceholder bTph = typeFactory.GetTypePlaceholder("b"); + 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, typeFactory.GetExtendsType(boolT), + resultBuilder.addPair(aTph, this.GetExtendsType(boolT), PairOperator.Equal); resultBuilder.addPair(bTph, boolT, PairOperator.Equal); - resultBuilder.addPair(bTph, typeFactory.GetExtendsType(boolT), + resultBuilder.addPair(bTph, this.GetExtendsType(boolT), PairOperator.Equal); expectedResult = resultBuilder.getNestedPairMenge(); @@ -86,8 +90,8 @@ public class UnifyOldTest { * Test b <. a, a <. b */ - aTph = typeFactory.GetTypePlaceholder("a"); - bTph = typeFactory.GetTypePlaceholder("b"); + aTph = this.GetTypePlaceholder("a"); + bTph = this.GetTypePlaceholder("b"); // Expected Result resultBuilder.clear(); @@ -114,10 +118,10 @@ public class UnifyOldTest { * Test Integer <. a, a <. Boolean */ - RefType intT = typeFactory.GetSimpleType("java.lang.Integer"); - boolT = typeFactory.GetSimpleType("java.lang.Boolean"); - aTph = typeFactory.GetTypePlaceholder("a"); - bTph = typeFactory.GetTypePlaceholder("b"); + 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(); @@ -141,7 +145,6 @@ public class UnifyOldTest { public void unifyTestGenerics() { // 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(); @@ -150,15 +153,15 @@ public class UnifyOldTest { * Test a <. MyClass */ - TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a"); - RefType myType = typeFactory.GetSimpleType("MyClass", - typeFactory.GetTypePlaceholder("T"), - typeFactory.GetTypePlaceholder("F")); + 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, typeFactory.GetExtendsType(myType)); + resultBuilder.addPair(aTph, this.GetExtendsType(myType)); Menge> expectedResult = resultBuilder.getNestedPairMenge(); // Actual Result @@ -176,13 +179,13 @@ public class UnifyOldTest { * Test List> <. List */ - TypePlaceholder tTph = typeFactory.GetTypePlaceholder("T"); - RefType list = typeFactory.GetSimpleType("List", tTph); - RefType listlist = typeFactory.GetSimpleType("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(typeFactory.GetExtendsType(list), tTph, + resultBuilder.addPair(this.GetExtendsType(list), tTph, PairOperator.Equal); expectedResult = resultBuilder.getNestedPairMenge(); @@ -206,16 +209,15 @@ public class UnifyOldTest { public void unifyTestInheritance() { // 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(); // Init Types - RefType tBool = typeFactory.GetSimpleType("java.lang.Boolean"); - RefType tString = typeFactory.GetSimpleType("java.lang.String"); - RefType tInt = typeFactory.GetSimpleType("java.lang.Integer"); - TypePlaceholder tphA = typeFactory.GetTypePlaceholder("a"); + 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 @@ -227,10 +229,10 @@ public class UnifyOldTest { // Build expected result resultBuilder.addPair(tphA, tBool, PairOperator.Equal); - resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tBool), + resultBuilder.addPair(tphA, this.GetExtendsType(tBool), PairOperator.Equal); resultBuilder.addPair(tphA, tString, PairOperator.Equal); - resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tString), + resultBuilder.addPair(tphA, this.GetExtendsType(tString), PairOperator.Equal); // Assert @@ -299,5 +301,34 @@ public class UnifyOldTest { 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); + } } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index 8524c1dc..60486017 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -9,9 +9,8 @@ import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; -import de.dhbwstuttgart.typeinference.unify.model.Type; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; import de.dhbwstuttgart.typeinference.unify.model.TypeParams; -import de.dhbwstuttgart.typeinference.unifynew.TypeFactory; import de.dhbwstuttgart.typeinference.unifynew.Unify; public class UnifyTest extends Unify { @@ -61,18 +60,18 @@ public class UnifyTest extends Unify { @Test public void permuteParamsTest() { TypeFactory tf = new TypeFactory(); - ArrayList> candidates = new ArrayList<>(); + ArrayList> candidates = new ArrayList<>(); - Set p1 = new HashSet<>(); + Set p1 = new HashSet<>(); p1.add(tf.getPlaceholderType("p11")); p1.add(tf.getExtendsType(tf.getSimpleType("p12"))); p1.add(tf.getSimpleType("p13")); - Set p2 = new HashSet<>(); + Set p2 = new HashSet<>(); p2.add(tf.getPlaceholderType("p21")); p2.add(tf.getPlaceholderType("p22")); - Set p3 = new HashSet<>(); + Set p3 = new HashSet<>(); p3.add(tf.getSimpleType("p31", "T")); p3.add(tf.getSimpleType("p32")); @@ -81,7 +80,7 @@ public class UnifyTest extends Unify { candidates.add(p3); Set result = new HashSet<>(); - permuteParams(candidates, 0, result, new Type[candidates.size()]); + permuteParams(candidates, 0, result, new UnifyType[candidates.size()]); System.out.println(result); }