From 1b9a9c0841f1d7492b41689728b7cc49121a9b27 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Tue, 12 Apr 2016 13:39:08 +0200 Subject: [PATCH] commenting / refactoring --- .../unify/model/FiniteClosure.java | 6 +-- .../typeinference/unify/model/FunNType.java | 45 ++++++++++------- .../typeinference/unify/model/Node.java | 50 +++++++++++++++++-- 3 files changed, 77 insertions(+), 24 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index ddac6b91..1ad6f28b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -41,11 +41,11 @@ public class FiniteClosure implements IFiniteClosure { Node parentNode = inheritanceGraph.get(pair.getRhsType()); // Add edge - parentNode.AddDescendant(childNode); + parentNode.addDescendant(childNode); // Add edges to build the transitive closure - parentNode.getPredecessors().stream().forEach(x -> x.AddDescendant(childNode)); - childNode.getDescendants().stream().forEach(x -> x.AddPredecessor(parentNode)); + parentNode.getPredecessors().stream().forEach(x -> x.addDescendant(childNode)); + childNode.getDescendants().stream().forEach(x -> x.addPredecessor(parentNode)); } // Build the alternative representation with strings as keys diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java b/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java index 3363be75..94ac1cf8 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java @@ -4,35 +4,45 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; +/** + * A real function type in java. + * @author Florian Steurer + */ public class FunNType extends UnifyType { + /** + * Creates a FunN-Type with the specified TypeParameters. + */ protected FunNType(TypeParams p) { super("FuN", p); } - public static FunNType getFunNType(TypeParams tp) { - if(!validateTypeParams(tp)) - throw new IllegalArgumentException("Invalid TypeParams for a FunNType: " + tp); + /** + * Creates a new FunNType. + * @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); } - private static boolean validateTypeParams(TypeParams tp) { - if(tp.size() == 0) - return false; - for(UnifyType t : tp) - if(t instanceof WildcardType) - return false; - return true; + /** + * Returns the degree of the function type, e.g. 2 for FunN. + */ + public int getN() { + return typeParams.size()-1; } @Override public UnifyType setTypeParams(TypeParams newTp) { return getFunNType(newTp); } - - public int getN() { - return typeParams.size()-1; - } @Override Set smArg(IFiniteClosure fc) { @@ -46,13 +56,14 @@ public class FunNType extends UnifyType { @Override UnifyType apply(Unifier unif) { - // TODO Auto-generated method stub - return null; + // TODO this bypasses the validation of the type parameters. + // Wildcard types can be unified into FunNTypes. + return new FunNType(typeParams.apply(unif)); } @Override public int hashCode() { - return 31 + typeParams.hashCode(); + return 181 + typeParams.hashCode(); } @Override diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java index d338d16b..d24b6825 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java @@ -4,48 +4,90 @@ import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; +/** + * A node of a directed graph. + * @author Florian Steurer + * + * @param The type of the content of the node. + */ class Node { + + /** + * The content of the node. + */ private T content; + /** + * The set of predecessors + */ private HashSet> predecessors = new HashSet<>(); + + /** + * The set of descendants + */ private HashSet> descendants = new HashSet<>(); + /** + * Creates a node containing the specified content. + */ public Node(T content) { this.content = content; } - public void AddDescendant(Node descendant) { + /** + * Adds a directed edge from this node to the descendant (this -> descendant) + */ + public void addDescendant(Node descendant) { if(descendants.contains(descendant)) return; descendants.add(descendant); - descendant.AddPredecessor(this); + descendant.addPredecessor(this); } - public void AddPredecessor(Node predecessor) { + /** + * Adds a directed edge from the predecessor to this node (predecessor -> this) + */ + public void addPredecessor(Node predecessor) { if(predecessors.contains(predecessor)) return; predecessors.add(predecessor); - predecessor.AddDescendant(this); + predecessor.addDescendant(this); } + /** + * The content of this node. + */ public T getContent() { return content; } + /** + * Returns all predecessors (nodes that have a directed edge to this node) + */ public Set> getPredecessors() { return predecessors; } + /** + * Returns all descendants. All nodes M, where there is a edge from this node to the node M. + * @return + */ public Set> getDescendants() { return descendants; } + /** + * Retrieves the content of all descendants. + */ public Set getContentOfDescendants() { return descendants.stream().map(x -> x.getContent()).collect(Collectors.toSet()); } + /** + * Retrieves the content of all predecessors. + */ public Set getContentOfPredecessors() { return predecessors.stream().map(x -> x.getContent()).collect(Collectors.toSet()); }