a bug fix...
[IRC.git] / Robust / src / Util / Lattice.java
index 76aa706c5131d755b210e200b7bd358987e7bb0e..0418d237b3d7645a3fbf0582fe4d18350cb58098 100644 (file)
@@ -20,6 +20,7 @@ public class Lattice<T> {
     this.bottom = bottom;
 
     table.put(top, new HashSet<T>());
+    table.put(bottom, new HashSet<T>());
 
   }
 
@@ -39,6 +40,17 @@ public class Lattice<T> {
     return table;
   }
 
+  public void setTable(Map<T, Set<T>> in) {
+    Set<T> keySet = in.keySet();
+    for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+      T key = (T) iterator.next();
+      Set<T> setIn = in.get(key);
+      Set<T> newSet = new HashSet<T>();
+      newSet.addAll(setIn);
+      table.put(key, newSet);
+    }
+  }
+
   public boolean put(T key) {
     if (table.containsKey(key)) {
       return false;
@@ -54,10 +66,15 @@ public class Lattice<T> {
   }
 
   public boolean put(T key, T value) {
+
+    if (isComparable(key, value) && isGreaterThan(key, value)) {
+      // this relation already exists
+      return false;
+    }
+
     Set<T> s;
 
     Set<T> topNeighbor = table.get(top);
-
     if (table.containsKey(key)) {
       s = table.get(key);
     } else {
@@ -71,14 +88,16 @@ public class Lattice<T> {
       size++;
       s.add(value);
 
-      if (!table.containsKey(value)) {
+      if ((!table.containsKey(value)) && (!value.equals(bottom))) {
         Set<T> lowerNeighbor = new HashSet<T>();
         lowerNeighbor.add(bottom);
         table.put(value, lowerNeighbor);
       }
 
       // if value is already connected with top, it is no longer to be
-      topNeighbor.remove(value);
+      if (!key.equals(top)) {
+        topNeighbor.remove(value);
+      }
 
       // if key is already connected with bottom,, it is no longer to be
       if (!value.equals(getBottomItem())) {
@@ -155,6 +174,13 @@ public class Lattice<T> {
 
   public boolean isGreaterThan(T a, T b) {
 
+    Set<T> visited = new HashSet<T>();
+    return isGreaterThan(a, b, visited);
+
+  }
+
+  public boolean isGreaterThan(T a, T b, Set<T> visited) {
+
     if (a.equals(b)) {
       return false;
     }
@@ -185,7 +211,10 @@ public class Lattice<T> {
       boolean reachable = false;
       for (Iterator<T> iterator = neighborSet.iterator(); iterator.hasNext();) {
         T neighbor = iterator.next();
-        reachable = reachable || isGreaterThan(neighbor, b);
+        if (!visited.contains(neighbor)) {
+          visited.add(neighbor);
+          reachable = reachable || isGreaterThan(neighbor, b, visited);
+        }
       }
       return reachable;
     }