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());
// 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

View File

@ -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<Integer, Integer, Integer>.
*/
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<UnifyType> 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

View File

@ -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 <T> The type of the content of the node.
*/
class Node<T> {
/**
* The content of the node.
*/
private T content;
/**
* The set of predecessors
*/
private HashSet<Node<T>> predecessors = new HashSet<>();
/**
* The set of descendants
*/
private HashSet<Node<T>> descendants = new HashSet<>();
/**
* Creates a node containing the specified content.
*/
public Node(T 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))
return;
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))
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<Node<T>> getPredecessors() {
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() {
return descendants;
}
/**
* Retrieves the content of all descendants.
*/
public Set<T> getContentOfDescendants() {
return descendants.stream().map(x -> x.getContent()).collect(Collectors.toSet());
}
/**
* Retrieves the content of all predecessors.
*/
public Set<T> getContentOfPredecessors() {
return predecessors.stream().map(x -> x.getContent()).collect(Collectors.toSet());
}