Test GenerateFiniteClosure anfügen

This commit is contained in:
JanUlrich 2016-03-17 16:54:43 +01:00
parent 1762101330
commit 1f825360df
26 changed files with 433 additions and 350 deletions

View File

@ -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<UsedId> imports, boolean withSubtypes)
public TypeAssumptions makeBasicAssumptionsFromJRE(Menge<UsedId> imports, boolean withSubtypes)
// ino.end
// ino.method.makeBasicAssumptionsFromJRE.21409.body
{

View File

@ -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<Type> typeParams = new Menge<Type>();
for(Type t : parameters)
typeParams.add(t);
return new RefType(name, typeParams, null, 0);
public static FiniteClosure generateFC(TypeAssumptions fromAss){
HashSet<MPair> 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<UnifyType> 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());
}
}

View File

@ -89,33 +89,6 @@ public class TypeAssumptions {
return null;
}
/**
* Ermittelt alle bekannten Methoden mit dem Namen withName
* @param withName
* @return
*/
@Deprecated
public Menge<MethodAssumption> getMethods2(String withName){
//TODO: Implementieren
return new Menge<MethodAssumption>();
}
/**
* 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

View File

@ -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<Type> smaller(Type type);
public Set<UnifyType> 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<Type> greater(Type type);
public Set<UnifyType> greater(UnifyType type);
/**
* Wo passt Type rein?
* @param type
* @return
*/
public Set<Type> grArg(Type type);
public Set<UnifyType> grArg(UnifyType type);
/**
* Was passt in Type rein?
* @param type
* @return
*/
public Set<Type> smArg(Type type);
public Set<UnifyType> smArg(UnifyType type);
public Set<Type> grArg(SimpleType type);
public Set<Type> smArg(SimpleType type);
public Set<UnifyType> grArg(SimpleType type);
public Set<UnifyType> smArg(SimpleType type);
public Set<Type> grArg(ExtendsType type);
public Set<Type> smArg(ExtendsType type);
public Set<UnifyType> grArg(ExtendsType type);
public Set<UnifyType> smArg(ExtendsType type);
public Set<Type> grArg(SuperType type);
public Set<Type> smArg(SuperType type);
public Set<UnifyType> grArg(SuperType type);
public Set<UnifyType> smArg(SuperType type);
public Set<Type> grArg(PlaceholderType type);
public Set<Type> smArg(PlaceholderType type);
public Set<UnifyType> grArg(PlaceholderType type);
public Set<UnifyType> smArg(PlaceholderType type);
public Optional<Type> getGenericType(String typeName);
public Set<Type> getAllTypes(String typeName);
public Optional<UnifyType> getGenericType(String typeName);
public Set<UnifyType> getAllTypes(String typeName);
}

View File

@ -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<Unifier> unify(Set<Type> terms);
public Optional<Unifier> unify(Set<UnifyType> terms);
default public Optional<Unifier> unify(Type... terms) {
default public Optional<Unifier> unify(UnifyType... terms) {
return unify(Arrays.stream(terms).collect(Collectors.toSet()));
}

View File

@ -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<Type> smArg(IFiniteClosure fc) {
Set<UnifyType> smArg(IFiniteClosure fc) {
return fc.smArg(this);
}
@Override
Set<Type> grArg(IFiniteClosure fc) {
Set<UnifyType> grArg(IFiniteClosure fc) {
return fc.grArg(this);
}
@Override
Type apply(Unifier unif) {
UnifyType apply(Unifier unif) {
return new ExtendsType(extendedType.apply(unif));
}

View File

@ -11,12 +11,12 @@ import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
public class FiniteClosure implements IFiniteClosure {
private HashMap<Type, Node<Type>> inheritanceGraph;
private HashMap<String, HashSet<Node<Type>>> strInheritanceGraph;
private HashMap<UnifyType, Node<UnifyType>> inheritanceGraph;
private HashMap<String, HashSet<Node<UnifyType>>> strInheritanceGraph;
public FiniteClosure(Set<MPair> pairs) {
inheritanceGraph = new HashMap<Type, Node<Type>>();
inheritanceGraph = new HashMap<UnifyType, Node<UnifyType>>();
// 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<Type>(pair.getLhsType()));
inheritanceGraph.put(pair.getLhsType(), new Node<UnifyType>(pair.getLhsType()));
if(!inheritanceGraph.containsKey(pair.getRhsType()))
inheritanceGraph.put(pair.getRhsType(), new Node<Type>(pair.getRhsType()));
inheritanceGraph.put(pair.getRhsType(), new Node<UnifyType>(pair.getRhsType()));
Node<Type> childNode = inheritanceGraph.get(pair.getLhsType());
Node<Type> parentNode = inheritanceGraph.get(pair.getRhsType());
Node<UnifyType> childNode = inheritanceGraph.get(pair.getLhsType());
Node<UnifyType> 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<Type> smaller(Type type) {
public Set<UnifyType> smaller(UnifyType type) {
if(!inheritanceGraph.containsKey(type))
return new HashSet<>();
Set<Type> result = inheritanceGraph.get(type).getContentOfDescendants();
Set<UnifyType> 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<Type> greater(Type type) {
public Set<UnifyType> greater(UnifyType type) {
if(!inheritanceGraph.containsKey(type))
return new HashSet<>();
Set<Type> result = inheritanceGraph.get(type).getContentOfPredecessors();
Set<UnifyType> result = inheritanceGraph.get(type).getContentOfPredecessors();
result.add(type);
return result;
}
@Override
public Set<Type> grArg(Type type) {
public Set<UnifyType> grArg(UnifyType type) {
return type.grArg(this);
}
@Override
public Set<Type> grArg(SimpleType type) {
public Set<UnifyType> grArg(SimpleType type) {
if(!inheritanceGraph.containsKey(type))
return new HashSet<Type>();
return new HashSet<UnifyType>();
Set<Type> result = new HashSet<Type>();
Set<UnifyType> result = new HashSet<UnifyType>();
result.add(type);
smaller(type).forEach(x -> result.add(new SuperType(x)));
@ -100,13 +100,13 @@ public class FiniteClosure implements IFiniteClosure {
}
@Override
public Set<Type> grArg(ExtendsType type) {
public Set<UnifyType> grArg(ExtendsType type) {
if(!inheritanceGraph.containsKey(type.getExtendedType()))
return new HashSet<Type>();
return new HashSet<UnifyType>();
Set<Type> result = new HashSet<Type>();
Set<UnifyType> result = new HashSet<UnifyType>();
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<Type> grArg(SuperType type) {
public Set<UnifyType> grArg(SuperType type) {
if(!inheritanceGraph.containsKey(type.getSuperedType()))
return new HashSet<Type>();
return new HashSet<UnifyType>();
Set<Type> result = new HashSet<Type>();
Set<UnifyType> result = new HashSet<UnifyType>();
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<Type> grArg(PlaceholderType type) {
public Set<UnifyType> grArg(PlaceholderType type) {
return new HashSet<>();
}
@Override
public Set<Type> smArg(Type type) {
public Set<UnifyType> smArg(UnifyType type) {
return type.smArg(this);
}
@Override
public Set<Type> smArg(SimpleType type) {
public Set<UnifyType> smArg(SimpleType type) {
if(!inheritanceGraph.containsKey(type))
return new HashSet<Type>();
return new HashSet<UnifyType>();
Set<Type> result = new HashSet<Type>();
Set<UnifyType> result = new HashSet<UnifyType>();
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<Type> smArg(ExtendsType type) {
public Set<UnifyType> smArg(ExtendsType type) {
if(!inheritanceGraph.containsKey(type.getExtendedType()))
return new HashSet<Type>();
return new HashSet<UnifyType>();
Set<Type> result = new HashSet<Type>();
Set<UnifyType> result = new HashSet<UnifyType>();
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<Type> smArg(SuperType type) {
public Set<UnifyType> smArg(SuperType type) {
if(!inheritanceGraph.containsKey(type.getSuperedType()))
return new HashSet<Type>();
return new HashSet<UnifyType>();
Set<Type> result = new HashSet<Type>();
Set<UnifyType> result = new HashSet<UnifyType>();
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<Type> smArg(PlaceholderType type) {
public Set<UnifyType> smArg(PlaceholderType type) {
return new HashSet<>();
}
@Override
public Optional<Type> getGenericType(String typeName) {
public Optional<UnifyType> getGenericType(String typeName) {
if(!strInheritanceGraph.containsKey(typeName))
return Optional.empty();
HashSet<Node<Type>> candidates = strInheritanceGraph.get(typeName);
HashSet<Node<UnifyType>> candidates = strInheritanceGraph.get(typeName);
for(Node<Type> node : candidates) {
Type candidate = node.getContent();
for(Node<UnifyType> 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<Type> getAllTypes(String typeName) {
public Set<UnifyType> getAllTypes(String typeName) {
if(!strInheritanceGraph.containsKey(typeName))
return new HashSet<>();
return strInheritanceGraph.get(typeName).stream().map(x -> x.getContent()).collect(Collectors.toCollection(HashSet::new));

View File

@ -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;

View File

@ -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<Type> smArg(IFiniteClosure fc) {
Set<UnifyType> smArg(IFiniteClosure fc) {
return fc.smArg(this);
}
@Override
Set<Type> grArg(IFiniteClosure fc) {
Set<UnifyType> 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;

View File

@ -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<Type> smArg(IFiniteClosure fc) {
Set<UnifyType> smArg(IFiniteClosure fc) {
return fc.smArg(this);
}
@Override
Set<Type> grArg(IFiniteClosure fc) {
Set<UnifyType> 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);
}

View File

@ -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<Type> smArg(IFiniteClosure fc) {
Set<UnifyType> smArg(IFiniteClosure fc) {
return fc.smArg(this);
}
@Override
Set<Type> grArg(IFiniteClosure fc) {
Set<UnifyType> grArg(IFiniteClosure fc) {
return fc.grArg(this);
}
@Override
Type apply(Unifier unif) {
UnifyType apply(Unifier unif) {
return new SuperType(superedType.apply(unif));
}

View File

@ -3,15 +3,15 @@ package de.dhbwstuttgart.typeinference.unify.model;
import java.util.Arrays;
import java.util.Iterator;
public final class TypeParams implements Iterable<Type>{
private final Type[] typeParams;
public final class TypeParams implements Iterable<UnifyType>{
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<Type>{
@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<Type>{
}
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<Type>{
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<Type> iterator() {
public Iterator<UnifyType> iterator() {
return Arrays.stream(typeParams).iterator();
}

View File

@ -5,12 +5,12 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;
public class Unifier implements Function<Type, Type> {
private HashMap<PlaceholderType, Type> substitutions = new HashMap<>();
public class Unifier implements Function<UnifyType, UnifyType> {
private HashMap<PlaceholderType, UnifyType> 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<Type, Type> {
}
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<Type, Type> {
}
@Override
public Type apply(Type t) {
public UnifyType apply(UnifyType t) {
return t.apply(this);
}
@ -41,18 +41,18 @@ public class Unifier implements Function<Type, Type> {
return substitutions.containsKey(t);
}
public Type getSubstitute(Type t) {
public UnifyType getSubstitute(UnifyType t) {
return substitutions.get(t);
}
public Set<Entry<PlaceholderType, Type>> getSubstitutions() {
public Set<Entry<PlaceholderType, UnifyType>> getSubstitutions() {
return substitutions.entrySet();
}
@Override
public String toString() {
String result = "{ ";
for(Entry<PlaceholderType, Type> entry : substitutions.entrySet())
for(Entry<PlaceholderType, UnifyType> entry : substitutions.entrySet())
result += "(" + entry.getKey() + " -> " + entry.getValue() + "), ";
if(!substitutions.isEmpty())
result = result.substring(0, result.length()-2);

View File

@ -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<Type> smArg(IFiniteClosure fc);
abstract Set<UnifyType> smArg(IFiniteClosure fc);
abstract Set<Type> grArg(IFiniteClosure fc);
abstract Set<UnifyType> 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) + ">";
}

View File

@ -8,16 +8,16 @@ import java.util.stream.Collectors;
public class Mapping {
private HashMap<de.dhbwstuttgart.typeinference.unify.model.Type, de.dhbwstuttgart.syntaxtree.type.Type> backwardMap = new HashMap<>();
private HashMap<de.dhbwstuttgart.syntaxtree.type.Type, de.dhbwstuttgart.typeinference.unify.model.Type> forwardMap = new HashMap<>();
private Set<de.dhbwstuttgart.typeinference.unify.model.Type> irreversible = new HashSet<>();
private HashMap<de.dhbwstuttgart.typeinference.unify.model.UnifyType, de.dhbwstuttgart.syntaxtree.type.Type> backwardMap = new HashMap<>();
private HashMap<de.dhbwstuttgart.syntaxtree.type.Type, de.dhbwstuttgart.typeinference.unify.model.UnifyType> forwardMap = new HashMap<>();
private Set<de.dhbwstuttgart.typeinference.unify.model.UnifyType> irreversible = new HashSet<>();
public Mapping(Set<de.dhbwstuttgart.syntaxtree.type.Type> 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<de.dhbwstuttgart.typeinference.unify.model.Type> mapTypeSet(Set<de.dhbwstuttgart.syntaxtree.type.Type> types) {
public Set<de.dhbwstuttgart.typeinference.unify.model.UnifyType> mapTypeSet(Set<de.dhbwstuttgart.syntaxtree.type.Type> 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<de.dhbwstuttgart.syntaxtree.type.Type> unmap(de.dhbwstuttgart.typeinference.unify.model.Type type) {
public Optional<de.dhbwstuttgart.syntaxtree.type.Type> unmap(de.dhbwstuttgart.typeinference.unify.model.UnifyType type) {
return irreversible.contains(type) ? Optional.of(backwardMap.get(type)) : Optional.empty();
}
public Optional<de.dhbwstuttgart.typeinference.Pair> 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<Set<de.dhbwstuttgart.syntaxtree.type.Type>> unmapTypeSet(Set<de.dhbwstuttgart.typeinference.unify.model.Type> types) {
public Optional<Set<de.dhbwstuttgart.syntaxtree.type.Type>> unmapTypeSet(Set<de.dhbwstuttgart.typeinference.unify.model.UnifyType> types) {
Set<de.dhbwstuttgart.syntaxtree.type.Type> 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();
}

View File

@ -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<Unifier> unify(Set<Type> terms) {
public Optional<Unifier> unify(Set<UnifyType> terms) {
if(terms.size() < 2)
return Optional.of(Unifier.IDENTITY);
ArrayList<MPair> termsQ = new ArrayList<MPair>();
Iterator<Type> iter = terms.iterator();
Type prev = iter.next();
Iterator<UnifyType> 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<Entry<PlaceholderType, Type>> optUni = eliminate(pair);
Optional<Entry<PlaceholderType, UnifyType>> optUni = eliminate(pair);
if(optUni.isPresent()) {
Entry<PlaceholderType, Type> substitution = optUni.get();
Entry<PlaceholderType, UnifyType> 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<Set<MPair>> decompose(MPair pair) {
Set<MPair> 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<MPair> 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<Entry<PlaceholderType, Type>> eliminate(MPair pair) {
Type rhs = pair.getRhsType();
Type lhs = pair.getLhsType();
private Optional<Entry<PlaceholderType, UnifyType>> 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, Type>((PlaceholderType) lhs, rhs));
return Optional.of(new AbstractMap.SimpleImmutableEntry<PlaceholderType, UnifyType>((PlaceholderType) lhs, rhs));
}
}

View File

@ -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<Type> opt = finiteClosure.getGenericType(typeD.getName());
Optional<UnifyType> opt = finiteClosure.getGenericType(typeD.getName());
if(!opt.isPresent())
return Optional.empty();
// The generic Version of Type D (D<a1, a2, a3, ... >)
Type typeDgen = opt.get();
UnifyType typeDgen = opt.get();
// Actually greater+ because the types are ensured to have different names
Set<Type> greater = finiteClosure.greater(typeDgen);
Set<UnifyType> 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<Type> opt = finiteClosure.getGenericType(((ExtendsType) typeD).getExtendedType().getName());
Optional<UnifyType> opt = finiteClosure.getGenericType(((ExtendsType) typeD).getExtendedType().getName());
typeDgen = opt.isPresent() ? new ExtendsType(opt.get()) : null;
}
if(typeDgen == null)
return Optional.empty();
Set<Type> grArg = finiteClosure.grArg(typeDgen);
Set<UnifyType> grArg = finiteClosure.grArg(typeDgen);
Optional<Type> opt = grArg.stream().filter(x -> x.getName().equals(typeExtDs.getName())).findAny();
Optional<UnifyType> 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<Type> opt = finiteClosure.getGenericType(((SuperType) typeSupD).getSuperedType().getName());
Optional<UnifyType> 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<Type> smArg = finiteClosure.smArg(typeSupDgen);
Set<UnifyType> 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<Type> opt = finiteClosure.getGenericType(((ExtendsType) C).getExtendedType().getName());
Optional<UnifyType> opt = finiteClosure.getGenericType(((ExtendsType) C).getExtendedType().getName());
if(opt.isPresent()) cFromFc = new ExtendsType(opt.get());
}
else if(C instanceof SuperType) {
Optional<Type> opt = finiteClosure.getGenericType(((SuperType) C).getSuperedType().getName());
Optional<UnifyType> opt = finiteClosure.getGenericType(((SuperType) C).getSuperedType().getName());
if(opt.isPresent()) cFromFc = new SuperType(opt.get());
}
if(cFromFc == null)
return new int[0];
Optional<Type> opt = Optional.empty();
Optional<UnifyType> 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<Set<MPair>> subst(Set<MPair> pairs) {
HashMap<Type, Integer> typeMap = new HashMap<>();
HashMap<UnifyType, Integer> 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)

View File

@ -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<MPair> set = new HashSet<>();
IUnify unify = new MartelliMontanariUnify();
//Set<Type> cs = fc.getAllTypes(rhsType.getName());
Type c = rhsType;
UnifyType c = rhsType;
//Set<Type> thetaQs = cs.stream().flatMap(x -> fc.smaller(x).stream()).collect(Collectors.toCollection(HashSet::new));
Set<Type> thetaQs = fc.smaller(c);
Set<UnifyType> thetaQs = fc.smaller(c);
Set<Type> thetaQPrimes = new HashSet<>();
Set<UnifyType> thetaQPrimes = new HashSet<>();
TypeParams cParams = c.getTypeParams();
if(cParams.size() == 0)
thetaQPrimes.add(c);
else {
ArrayList<Set<Type>> candidateParams = new ArrayList<>();
for(Type param : cParams)
ArrayList<Set<UnifyType>> candidateParams = new ArrayList<>();
for(UnifyType param : cParams)
candidateParams.add(fc.grArg(param));
Set<TypeParams> permutations = new HashSet<TypeParams>();
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<Unifier> opt = unify.unify(tqp, thetaPrime);
if(opt.isPresent()) {
Unifier unifier = opt.get();
Set<Entry<PlaceholderType, Type>> substitutions = unifier.getSubstitutions();
for(Entry<PlaceholderType, Type> sigma : substitutions)
Set<Entry<PlaceholderType, UnifyType>> substitutions = unifier.getSubstitutions();
for(Entry<PlaceholderType, UnifyType> sigma : substitutions)
set.add(new MPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT));
for(Type tq : thetaQs) {
Set<Type> smaller = fc.smaller(unifier.apply(tq));
for(UnifyType tq : thetaQs) {
Set<UnifyType> 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<MPair> 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<MPair> 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<MPair> 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<MPair> 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<Set<Type>> candidates, int idx, Set<TypeParams> result, Type[] current) {
protected void permuteParams(ArrayList<Set<UnifyType>> candidates, int idx, Set<TypeParams> result, UnifyType[] current) {
if(candidates.size() == idx) {
result.add(new TypeParams(Arrays.copyOf(current, current.length)));
return;
}
Set<Type> localCandidates = candidates.get(idx);
Set<UnifyType> localCandidates = candidates.get(idx);
for(Type t : localCandidates) {
for(UnifyType t : localCandidates) {
current[idx] = t;
permuteParams(candidates, idx+1, result, current);
}
}
private Set<Type> getAllInstantiations(Type t, IFiniteClosure fc) {
Set<Type> result = new HashSet<>();
private Set<UnifyType> getAllInstantiations(UnifyType t, IFiniteClosure fc) {
Set<UnifyType> result = new HashSet<>();
result.add(t);
return result;

View File

@ -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<MPair> 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);

View File

@ -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 {

View File

@ -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<String> 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);
}
}

View File

@ -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 {

View File

@ -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<x> = y}
Set<MPair> terms = new HashSet<MPair>();
@ -37,10 +36,10 @@ public class StandardUnifyTest {
// TODO ist das ergebnis { (x -> ? extends a), (y -> g<x>) } in der richtigen form oder
// muss es { (x -> ? extends a), (y -> g<? extends a>) } sein?
// {f<g<x>,x> = f<y, ? extends a>}
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<>();

View File

@ -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) {

View File

@ -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<Menge<Pair>> 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<T, F>
*/
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<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
// Actual Result
@ -176,13 +179,13 @@ public class UnifyOldTest {
* Test List<List<T>> <. List<T>
*/
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<Type> typeParams = new Menge<Type>();
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);
}
}

View File

@ -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<Set<Type>> candidates = new ArrayList<>();
ArrayList<Set<UnifyType>> candidates = new ArrayList<>();
Set<Type> p1 = new HashSet<>();
Set<UnifyType> p1 = new HashSet<>();
p1.add(tf.getPlaceholderType("p11"));
p1.add(tf.getExtendsType(tf.getSimpleType("p12")));
p1.add(tf.getSimpleType("p13"));
Set<Type> p2 = new HashSet<>();
Set<UnifyType> p2 = new HashSet<>();
p2.add(tf.getPlaceholderType("p21"));
p2.add(tf.getPlaceholderType("p22"));
Set<Type> p3 = new HashSet<>();
Set<UnifyType> 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<TypeParams> result = new HashSet<>();
permuteParams(candidates, 0, result, new Type[candidates.size()]);
permuteParams(candidates, 0, result, new UnifyType[candidates.size()]);
System.out.println(result);
}