commenting / refactoring

This commit is contained in:
Florian Steurer 2016-04-12 13:39:08 +02:00
parent ff0d1e907f
commit 1b9a9c0841
3 changed files with 77 additions and 24 deletions

View File

@ -41,11 +41,11 @@ public class FiniteClosure implements IFiniteClosure {
Node<UnifyType> parentNode = inheritanceGraph.get(pair.getRhsType()); Node<UnifyType> parentNode = inheritanceGraph.get(pair.getRhsType());
// Add edge // Add edge
parentNode.AddDescendant(childNode); parentNode.addDescendant(childNode);
// Add edges to build the transitive closure // Add edges to build the transitive closure
parentNode.getPredecessors().stream().forEach(x -> x.AddDescendant(childNode)); parentNode.getPredecessors().stream().forEach(x -> x.addDescendant(childNode));
childNode.getDescendants().stream().forEach(x -> x.AddPredecessor(parentNode)); childNode.getDescendants().stream().forEach(x -> x.addPredecessor(parentNode));
} }
// Build the alternative representation with strings as keys // Build the alternative representation with strings as keys

View File

@ -4,35 +4,45 @@ import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
/**
* A real function type in java.
* @author Florian Steurer
*/
public class FunNType extends UnifyType { public class FunNType extends UnifyType {
/**
* Creates a FunN-Type with the specified TypeParameters.
*/
protected FunNType(TypeParams p) { protected FunNType(TypeParams p) {
super("FuN", p); super("FuN", p);
} }
public static FunNType getFunNType(TypeParams tp) { /**
if(!validateTypeParams(tp)) * Creates a new FunNType.
throw new IllegalArgumentException("Invalid TypeParams for a FunNType: " + tp); * @param tp The parameters of the type.
* @return A FunNType.
* @throws IllegalArgumentException is thrown when there are to few type parameters or there are wildcard-types.
*/
public static FunNType getFunNType(TypeParams tp) throws IllegalArgumentException {
if(tp.size() == 0)
throw new IllegalArgumentException("FunNTypes need at least one type parameter");
for(UnifyType t : tp)
if(t instanceof WildcardType)
throw new IllegalArgumentException("Invalid TypeParams for a FunNType: " + tp);
return new FunNType(tp); return new FunNType(tp);
} }
private static boolean validateTypeParams(TypeParams tp) { /**
if(tp.size() == 0) * Returns the degree of the function type, e.g. 2 for FunN<Integer, Integer, Integer>.
return false; */
for(UnifyType t : tp) public int getN() {
if(t instanceof WildcardType) return typeParams.size()-1;
return false;
return true;
} }
@Override @Override
public UnifyType setTypeParams(TypeParams newTp) { public UnifyType setTypeParams(TypeParams newTp) {
return getFunNType(newTp); return getFunNType(newTp);
} }
public int getN() {
return typeParams.size()-1;
}
@Override @Override
Set<UnifyType> smArg(IFiniteClosure fc) { Set<UnifyType> smArg(IFiniteClosure fc) {
@ -46,13 +56,14 @@ public class FunNType extends UnifyType {
@Override @Override
UnifyType apply(Unifier unif) { UnifyType apply(Unifier unif) {
// TODO Auto-generated method stub // TODO this bypasses the validation of the type parameters.
return null; // Wildcard types can be unified into FunNTypes.
return new FunNType(typeParams.apply(unif));
} }
@Override @Override
public int hashCode() { public int hashCode() {
return 31 + typeParams.hashCode(); return 181 + typeParams.hashCode();
} }
@Override @Override

View File

@ -4,48 +4,90 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/**
* A node of a directed graph.
* @author Florian Steurer
*
* @param <T> The type of the content of the node.
*/
class Node<T> { class Node<T> {
/**
* The content of the node.
*/
private T content; private T content;
/**
* The set of predecessors
*/
private HashSet<Node<T>> predecessors = new HashSet<>(); private HashSet<Node<T>> predecessors = new HashSet<>();
/**
* The set of descendants
*/
private HashSet<Node<T>> descendants = new HashSet<>(); private HashSet<Node<T>> descendants = new HashSet<>();
/**
* Creates a node containing the specified content.
*/
public Node(T content) { public Node(T content) {
this.content = content; this.content = content;
} }
public void AddDescendant(Node<T> descendant) { /**
* Adds a directed edge from this node to the descendant (this -> descendant)
*/
public void addDescendant(Node<T> descendant) {
if(descendants.contains(descendant)) if(descendants.contains(descendant))
return; return;
descendants.add(descendant); descendants.add(descendant);
descendant.AddPredecessor(this); descendant.addPredecessor(this);
} }
public void AddPredecessor(Node<T> predecessor) { /**
* Adds a directed edge from the predecessor to this node (predecessor -> this)
*/
public void addPredecessor(Node<T> predecessor) {
if(predecessors.contains(predecessor)) if(predecessors.contains(predecessor))
return; return;
predecessors.add(predecessor); predecessors.add(predecessor);
predecessor.AddDescendant(this); predecessor.addDescendant(this);
} }
/**
* The content of this node.
*/
public T getContent() { public T getContent() {
return content; return content;
} }
/**
* Returns all predecessors (nodes that have a directed edge to this node)
*/
public Set<Node<T>> getPredecessors() { public Set<Node<T>> getPredecessors() {
return predecessors; return predecessors;
} }
/**
* Returns all descendants. All nodes M, where there is a edge from this node to the node M.
* @return
*/
public Set<Node<T>> getDescendants() { public Set<Node<T>> getDescendants() {
return descendants; return descendants;
} }
/**
* Retrieves the content of all descendants.
*/
public Set<T> getContentOfDescendants() { public Set<T> getContentOfDescendants() {
return descendants.stream().map(x -> x.getContent()).collect(Collectors.toSet()); return descendants.stream().map(x -> x.getContent()).collect(Collectors.toSet());
} }
/**
* Retrieves the content of all predecessors.
*/
public Set<T> getContentOfPredecessors() { public Set<T> getContentOfPredecessors() {
return predecessors.stream().map(x -> x.getContent()).collect(Collectors.toSet()); return predecessors.stream().map(x -> x.getContent()).collect(Collectors.toSet());
} }