From 931fb01d7413bb05c73a5d3db33cd465796b6d4b Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sun, 25 Oct 2015 11:12:36 +0100 Subject: [PATCH] finite closure --- src/de/dhbwstuttgart/typeinference/IPair.java | 4 +- .../unify/model/FiniteClosure.java | 62 ++++++++++++++++++- .../typinference/unify/model/Node.java | 22 +++++-- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/IPair.java b/src/de/dhbwstuttgart/typeinference/IPair.java index a98693cd..2a1c8056 100644 --- a/src/de/dhbwstuttgart/typeinference/IPair.java +++ b/src/de/dhbwstuttgart/typeinference/IPair.java @@ -1,5 +1,5 @@ package de.dhbwstuttgart.typeinference; -public interface IPair { - +public interface IPair{ + } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java index 7c10d8ff..c26166e6 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/FiniteClosure.java @@ -1,13 +1,71 @@ package de.dhbwstuttgart.typinference.unify.model; +import java.util.HashMap; +import java.util.HashSet; import java.util.Set; -import de.dhbwstuttgart.typeinference.IPair; +import de.dhbwstuttgart.typeinference.Pair.PairOperator; +import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -public class FiniteClosure implements IFiniteClosure { +public class FiniteClosure implements IFiniteClosure { + + private HashMap> inheritanceGraph; public FiniteClosure(Set pairs) { + inheritanceGraph = new HashMap>(); + + // Build the transitive closure of the inheritance tree + for(T pair : pairs) { + // TODO smaller oder smallerExtends? + if(pair.GetPairOp() != PairOperator.SmallerExtends) + continue; + + // Add nodes if not already in the graph + if(!inheritanceGraph.containsKey(pair.GetType1())) + inheritanceGraph.put(pair.GetType1(), new Node(pair.GetType1())); + if(!inheritanceGraph.containsKey(pair.GetType2())) + inheritanceGraph.put(pair.GetType2(), new Node(pair.GetType2())); + + Node childNode = inheritanceGraph.get(pair.GetType2()); + Node parentNode = inheritanceGraph.get(pair.GetType1()); + + // Add edge + parentNode.AddDescendant(childNode); + + // Add edges to build the transitive closure + parentNode.getPredecessors().stream().forEach(x -> x.AddDescendant(childNode)); + } + } + + /** + * Returns all types of the finite closure that are subtypes of the argument. + * @return The set of subtypes of the argument. + */ + public Set smaller(MType type) { + if(!inheritanceGraph.containsKey(type)) + return new HashSet<>(); + + return inheritanceGraph.get(type).getContentOfDescendants(); + } + + /** + * Returns all types of the finite closure that are supertypes of the argument. + * @return The set of supertypes of the argument. + */ + public Set greater(MType type) { + if(!inheritanceGraph.containsKey(type)) + return new HashSet<>(); + + return inheritanceGraph.get(type).getContentOfPredecessors(); + } + + public Set grArg(MType type) { + throw new NotImplementedException(); + } + + public Set smArg(MType type) { + throw new NotImplementedException(); } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/Node.java b/src/de/dhbwstuttgart/typinference/unify/model/Node.java index a7c6e534..f0eaa635 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/Node.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/Node.java @@ -1,9 +1,8 @@ package de.dhbwstuttgart.typinference.unify.model; import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; import java.util.Set; +import java.util.stream.Collectors; public class Node { private T content; @@ -16,10 +15,17 @@ public class Node { } public void AddDescendant(Node descendant) { + if(descendants.contains(descendant)) + return; + descendants.add(descendant); + descendant.AddPredecessor(this); } public void AddPredecessor(Node predecessor) { + if(predecessors.contains(predecessor)) + return; + predecessors.add(predecessor); predecessor.AddDescendant(this); } @@ -29,10 +35,18 @@ public class Node { } public Set> getPredecessors() { - return null; + return predecessors; } public Set> getDescendants() { - return null; + return descendants; + } + + public Set getContentOfDescendants() { + return descendants.stream().map(x -> x.getContent()).collect(Collectors.toSet()); + } + + public Set getContentOfPredecessors() { + return predecessors.stream().map(x -> x.getContent()).collect(Collectors.toSet()); } }