a bug fix...
[IRC.git] / Robust / src / Util / Lattice.java
index 22ed8d64f99c711882b2c9728e1f6390c54bd00c..0418d237b3d7645a3fbf0582fe4d18350cb58098 100644 (file)
@@ -3,6 +3,7 @@ package Util;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.Map;
 import java.util.Set;
 
 public class Lattice<T> {
@@ -19,6 +20,7 @@ public class Lattice<T> {
     this.bottom = bottom;
 
     table.put(top, new HashSet<T>());
+    table.put(bottom, new HashSet<T>());
 
   }
 
@@ -34,6 +36,21 @@ public class Lattice<T> {
     return table.keySet();
   }
 
+  public Map<T, Set<T>> getTable() {
+    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;
@@ -49,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 {
@@ -66,17 +88,21 @@ 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
-      table.get(key).remove(getBottomItem());
+      if (!value.equals(getBottomItem())) {
+        table.get(key).remove(getBottomItem());
+      }
 
       return true;
     } else
@@ -129,7 +155,9 @@ public class Lattice<T> {
 
     Set<T> neighborSet = get(a);
 
-    if (neighborSet == null) {
+    if (a.equals(b)) {
+      return true;
+    } else if (neighborSet == null) {
       return false;
     } else if (neighborSet.contains(b)) {
       return true;
@@ -146,6 +174,17 @@ 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;
+    }
+
     if (a.equals(top)) {
       if (b.equals(top)) {
         return false;
@@ -172,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;
     }