visitor pattern for grarg smarg

This commit is contained in:
Florian Steurer 2015-11-07 14:35:54 +01:00
parent 6b709f0198
commit 28e0e8e94d
7 changed files with 145 additions and 36 deletions

View File

@ -1,6 +1,11 @@
package de.dhbwstuttgart.typeinference.unify.interfaces;
import java.util.Set;
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typinference.unify.model.SimpleType;
import de.dhbwstuttgart.typinference.unify.model.SuperType;
import de.dhbwstuttgart.typinference.unify.model.Type;
public interface IFiniteClosure {
@ -18,16 +23,19 @@ public interface IFiniteClosure {
public Set<Type> greater(Type type);
public Set<Type> grArg(Type type);
public Set<Type> smArg(Type type);
public boolean isSubtype(Type subT, Type superT);
public Set<Type> grArg(SimpleType type);
public Set<Type> smArg(SimpleType type);
public boolean isSupertype(Type superT, Type subT);
public Set<Type> grArg(ExtendsType type);
public Set<Type> smArg(ExtendsType type);
public boolean isInGrArg(Type grArgT, Type t);
public Set<Type> grArg(SuperType type);
public Set<Type> smArg(SuperType type);
public boolean isInSmArg(Type smArgT, Type t);
public Set<Type> grArg(PlaceholderType type);
public Set<Type> smArg(PlaceholderType type);
public Type getType(String typeName);
}

View File

@ -1,5 +1,9 @@
package de.dhbwstuttgart.typinference.unify.model;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
public class ExtendsType extends Type {
private Type extendedType;
@ -15,4 +19,19 @@ public class ExtendsType extends Type {
public TypeParams getTypeParams() {
return extendedType.getTypeParams();
}
@Override
public String toString() {
return "? extends " + extendedType;
}
@Override
public Set<Type> smArg(IFiniteClosure fc) {
return fc.smArg(this);
}
@Override
public Set<Type> grArg(IFiniteClosure fc) {
return fc.grArg(this);
}
}

View File

@ -46,14 +46,12 @@ public class FiniteClosure implements IFiniteClosure {
*/
@Override
public Set<Type> smaller(Type type) {
return smaller(type.getName());
}
private Set<Type> smaller(String typeName) {
if(!inheritanceGraph.containsKey(typeName))
if(!inheritanceGraph.containsKey(type.getName()))
return new HashSet<>();
return inheritanceGraph.get(typeName).getContentOfDescendants();
Set<Type> result = inheritanceGraph.get(type.getName()).getContentOfDescendants();
result.add(type);
return result;
}
@Override
@ -70,53 +68,88 @@ public class FiniteClosure implements IFiniteClosure {
*/
@Override
public Set<Type> greater(Type type) {
return greater(type.typeName);
}
private Set<Type> greater(String typeName) {
if(!inheritanceGraph.containsKey(typeName))
if(!inheritanceGraph.containsKey(type.getName()))
return new HashSet<>();
return inheritanceGraph.get(typeName).getContentOfPredecessors();
Set<Type> result = inheritanceGraph.get(type.getName()).getContentOfPredecessors();
result.add(type);
return result;
}
@Override
public Set<Type> grArg(Type type) {
return grArg(type.getName());
return type.grArg(this);
}
@Override
public Set<Type> grArg(SimpleType type) {
if(!inheritanceGraph.containsKey(type.getName()))
return new HashSet<Type>();
Set<Type> result = new HashSet<Type>();
Type t = inheritanceGraph.get(type.getName()).getContent();
result.add(t);
smaller(type).forEach(x -> result.add(new SuperType(x)));
greater(type).forEach(x -> result.add(new ExtendsType(x)));
return result;
}
private Set<Type> grArg(String typeName) {
@Override
public Set<Type> grArg(ExtendsType type) {
// TODO Auto-generated method stub
return null;
}
@Override
public Set<Type> grArg(SuperType type) {
// TODO Auto-generated method stub
return null;
}
@Override
public Set<Type> grArg(PlaceholderType type) {
// TODO Auto-generated method stub
return null;
}
@Override
public Set<Type> smArg(Type type) {
return smArg(type.getName());
return type.smArg(this);
}
private Set<Type> smArg(String typeName) {
// TODO Auto-generated method stub
@Override
public Set<Type> smArg(SimpleType type) {
if(!inheritanceGraph.containsKey(type.getName()))
return new HashSet<Type>();
Set<Type> result = new HashSet<Type>();
Type t = inheritanceGraph.get(type.getName()).getContent();
result.add(t);
smaller(type).forEach(x -> result.add(new ExtendsType(x)));
greater(type).forEach(x -> result.add(new ExtendsType(x)));
return result;
}
public Set<Type> smArg(ExtendsType type) {
return null;
}
@Override
public boolean isSubtype(Type subT, Type superT) {
return smaller(superT).contains(subT);
}
@Override
public boolean isSupertype(Type superT, Type subT) {
return smaller(superT).contains(subT);
}
@Override
public boolean isInGrArg(Type grArgT, Type t) {
return grArg(grArgT).contains(t);
public Set<Type> smArg(SuperType type) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isInSmArg(Type smArgT, Type t) {
return smArg(smArgT).contains(t);
public Set<Type> smArg(PlaceholderType type) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -1,8 +1,22 @@
package de.dhbwstuttgart.typinference.unify.model;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
public class PlaceholderType extends Type{
public PlaceholderType(String name) {
typeName = name;
}
@Override
public Set<Type> smArg(IFiniteClosure fc) {
return fc.smArg(this);
}
@Override
public Set<Type> grArg(IFiniteClosure fc) {
return fc.grArg(this);
}
}

View File

@ -1,8 +1,22 @@
package de.dhbwstuttgart.typinference.unify.model;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
public class SimpleType extends Type {
public SimpleType(String name, Type... typeParams) {
this.typeName = name;
this.typeParams = new TypeParams(typeParams);
}
@Override
public Set<Type> smArg(IFiniteClosure fc) {
return fc.smArg(this);
}
@Override
public Set<Type> grArg(IFiniteClosure fc) {
return fc.grArg(this);
}
}

View File

@ -1,5 +1,9 @@
package de.dhbwstuttgart.typinference.unify.model;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
public class SuperType extends Type {
private Type superedType;
@ -21,4 +25,14 @@ public class SuperType extends Type {
public TypeParams getTypeParams() {
return superedType.getTypeParams();
}
@Override
public Set<Type> smArg(IFiniteClosure fc) {
return fc.smArg(this);
}
@Override
public Set<Type> grArg(IFiniteClosure fc) {
return fc.grArg(this);
}
}

View File

@ -1,6 +1,9 @@
package de.dhbwstuttgart.typinference.unify.model;
import java.util.Arrays;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
public abstract class Type {
@ -34,6 +37,10 @@ public abstract class Type {
return other.getTypeParams().equals(typeParams);
}
public abstract Set<Type> smArg(IFiniteClosure fc);
public abstract Set<Type> grArg(IFiniteClosure fc);
@Override
public int hashCode() {