From 28e0e8e94d38966ff773083bceba96b7b5f35238 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sat, 7 Nov 2015 14:35:54 +0100 Subject: [PATCH] visitor pattern for grarg smarg --- .../unify/interfaces/IFiniteClosure.java | 18 +++- .../typinference/unify/model/ExtendsType.java | 19 ++++ .../unify/model/FiniteClosure.java | 95 +++++++++++++------ .../unify/model/PlaceholderType.java | 14 +++ .../typinference/unify/model/SimpleType.java | 14 +++ .../typinference/unify/model/SuperType.java | 14 +++ .../typinference/unify/model/Type.java | 7 ++ 7 files changed, 145 insertions(+), 36 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java index 83f17ca9..ae3384ed 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java @@ -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 greater(Type type); public Set grArg(Type type); - public Set smArg(Type type); - public boolean isSubtype(Type subT, Type superT); + public Set grArg(SimpleType type); + public Set smArg(SimpleType type); - public boolean isSupertype(Type superT, Type subT); + public Set grArg(ExtendsType type); + public Set smArg(ExtendsType type); - public boolean isInGrArg(Type grArgT, Type t); + public Set grArg(SuperType type); + public Set smArg(SuperType type); - public boolean isInSmArg(Type smArgT, Type t); + public Set grArg(PlaceholderType type); + public Set smArg(PlaceholderType type); public Type getType(String typeName); } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java index 7b26ca17..ff5b5b79 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java @@ -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 smArg(IFiniteClosure fc) { + return fc.smArg(this); + } + + @Override + public Set grArg(IFiniteClosure fc) { + return fc.grArg(this); + } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java index 1d969043..ff1c5106 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java @@ -46,14 +46,12 @@ public class FiniteClosure implements IFiniteClosure { */ @Override public Set smaller(Type type) { - return smaller(type.getName()); - } - - private Set smaller(String typeName) { - if(!inheritanceGraph.containsKey(typeName)) + if(!inheritanceGraph.containsKey(type.getName())) return new HashSet<>(); - return inheritanceGraph.get(typeName).getContentOfDescendants(); + Set result = inheritanceGraph.get(type.getName()).getContentOfDescendants(); + result.add(type); + return result; } @Override @@ -70,53 +68,88 @@ public class FiniteClosure implements IFiniteClosure { */ @Override public Set greater(Type type) { - return greater(type.typeName); - } - - private Set greater(String typeName) { - if(!inheritanceGraph.containsKey(typeName)) + if(!inheritanceGraph.containsKey(type.getName())) return new HashSet<>(); - return inheritanceGraph.get(typeName).getContentOfPredecessors(); + Set result = inheritanceGraph.get(type.getName()).getContentOfPredecessors(); + result.add(type); + return result; } + @Override public Set grArg(Type type) { - return grArg(type.getName()); + return type.grArg(this); + } + + @Override + public Set grArg(SimpleType type) { + if(!inheritanceGraph.containsKey(type.getName())) + return new HashSet(); + + Set result = new HashSet(); + + 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 grArg(String typeName) { + @Override + public Set grArg(ExtendsType type) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set grArg(SuperType type) { // TODO Auto-generated method stub return null; } + @Override + public Set grArg(PlaceholderType type) { + // TODO Auto-generated method stub + return null; + } + @Override public Set smArg(Type type) { - return smArg(type.getName()); + return type.smArg(this); } - private Set smArg(String typeName) { - // TODO Auto-generated method stub + @Override + public Set smArg(SimpleType type) { + if(!inheritanceGraph.containsKey(type.getName())) + return new HashSet(); + + Set result = new HashSet(); + + 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 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 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 smArg(PlaceholderType type) { + // TODO Auto-generated method stub + return null; } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java index 03e66c80..0c9aacd1 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java @@ -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 smArg(IFiniteClosure fc) { + return fc.smArg(this); + } + + @Override + public Set grArg(IFiniteClosure fc) { + return fc.grArg(this); + } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java b/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java index 8104202d..8f1ceaae 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java @@ -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 smArg(IFiniteClosure fc) { + return fc.smArg(this); + } + + @Override + public Set grArg(IFiniteClosure fc) { + return fc.grArg(this); + } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java index a62fd9f3..d9116f08 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java @@ -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 smArg(IFiniteClosure fc) { + return fc.smArg(this); + } + + @Override + public Set grArg(IFiniteClosure fc) { + return fc.grArg(this); + } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/Type.java b/src/de/dhbwstuttgart/typinference/unify/model/Type.java index 52c58868..676bb574 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/Type.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/Type.java @@ -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 smArg(IFiniteClosure fc); + + public abstract Set grArg(IFiniteClosure fc); + @Override public int hashCode() {