Procházet zdrojové kódy

Initial import of PACT

Caleb Fangmeier před 6 roky
revize
b5159b8607
7 změnil soubory, kde provedl 1510 přidání a 0 odebrání
  1. 6 0
      .classpath
  2. 17 0
      .project
  3. 8 0
      BracketException.java
  4. 791 0
      Node.java
  5. 153 0
      Nodes.java
  6. 325 0
      SwingApplication.java
  7. 210 0
      TestNode.java

+ 6 - 0
.classpath

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.5.0_06"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>PACT</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

+ 8 - 0
BracketException.java

@@ -0,0 +1,8 @@
+public class BracketException extends RuntimeException
+{
+}
+
+/* Location:           C:\Users\Jake\Downloads\pact2.0\pact.jar
+ * Qualified Name:     BracketException
+ * JD-Core Version:    0.6.0
+ */

+ 791 - 0
Node.java

@@ -0,0 +1,791 @@
+import java.io.PrintStream;
+import java.util.HashSet;
+import java.util.Iterator;
+
+public class Node
+{
+  Node parent;
+  String venn;
+  HashSet<Node> children;
+  HashSet<Node> linkNodes;
+  Node linkNode;
+
+  public Node(Node p, String s)
+  {
+    this.linkNode = null;
+    this.linkNodes = new HashSet<Node>();
+    this.venn = s;
+    this.parent = p;
+
+    this.children = new HashSet<Node>();
+    if (s.length() > 1)
+      setChildren(s);
+  }
+
+  private void setChildren(String s)
+  {
+    try
+    {
+      String vennChildren = s.substring(1, s.length() - 1);
+      int count = 0;
+      while (count < vennChildren.length())
+        if (vennChildren.charAt(count) == '(') {
+          int start = count;
+          count++;
+          int end = 0;
+          int brackets = 1;
+          while (true)
+          {
+            if (vennChildren.charAt(count) == '(') {
+              brackets++;
+            }
+            if (vennChildren.charAt(count) == ')') {
+              brackets--;
+              if (brackets == 0) {
+                count++;
+                break;
+              }
+            }
+            count++;
+          }
+          end = count;
+          Node child = new Node(this, vennChildren.substring(start, end));
+
+          this.children.add(child);
+        } else {
+          Node child = new Node(this, vennChildren.charAt(count));
+
+          this.children.add(child);
+          count++;
+        }
+    }
+    catch (StringIndexOutOfBoundsException se) {
+      throw new BracketException();
+    }
+  }
+
+  public String getVenn()
+  {
+    return this.venn;
+  }
+
+  public String toString()
+  {
+    return this.venn;
+  }
+
+  public HashSet<Node> getSiblings() {
+    if (this.parent != null) {
+      HashSet sibs = (HashSet)this.parent.children.clone();
+      sibs.remove(this);
+      return sibs;
+    }
+    return new HashSet();
+  }
+
+  public boolean equals(Node node)
+  {
+    String nodeVenn = node.getVenn();
+    if ((nodeVenn.length() == 1) && 
+      (this.venn.length() == nodeVenn.length())) {
+      return nodeVenn.equals(this.venn);
+    }
+
+    Iterator nodeIt = node.children.iterator();
+    int itCount = 0;
+    int eqCount = 0;
+    while (nodeIt.hasNext()) {
+      Iterator it = this.children.iterator();
+      Node toCheck = (Node)nodeIt.next();
+      itCount++;
+      while (it.hasNext()) {
+        if (((Node)it.next()).equals(toCheck)) {
+          eqCount++;
+          break;
+        }
+      }
+
+    }
+
+    if (itCount != eqCount) {
+      return false;
+    }
+    itCount = 0;
+    eqCount = 0;
+    Iterator it = this.children.iterator();
+    while (it.hasNext()) {
+      Iterator nIt = node.children.iterator();
+      Node toCheck = (Node)it.next();
+      itCount++;
+      while (nIt.hasNext()) {
+        if (toCheck.equals((Node)nIt.next())) {
+          eqCount++;
+          break;
+        }
+
+      }
+
+    }
+
+    return itCount == eqCount;
+  }
+
+  public boolean equals(Object o)
+  {
+    Node n = (Node)o;
+    return equals(n);
+  }
+
+  private int getNumAreas() {
+    int count = 0;
+    for (int i = 0; i < this.venn.length(); i++) {
+      if ((this.venn.charAt(i) != ')') && (this.venn.charAt(i) != '(')) {
+        count++;
+      }
+    }
+    return count;
+  }
+
+  public HashSet<Node> reducedCombine(Node node) {
+    HashSet result = new HashSet();
+
+    if (oneAreaShared(this, node)) {
+      result.add(combineAtRoot(node));
+      return result;
+    }
+    HashSet compCombine = combine(node);
+    Iterator it = compCombine.iterator();
+
+    if (!it.hasNext()) {
+      return result;
+    }
+    Node curr = (Node)it.next();
+    int smallest = curr.getNumAreas();
+    result.add(curr);
+
+    while (it.hasNext()) {
+      Node curr = (Node)it.next();
+      int currAreas = curr.getNumAreas();
+      if (currAreas < smallest) {
+        smallest = currAreas;
+        result.clear();
+        result.add(curr);
+      } else if (currAreas == smallest) {
+        result.add(curr);
+      }
+    }
+    return removeDuplicates(result);
+  }
+
+  private HashSet<Node> removeDuplicates(HashSet<Node> h) {
+    Iterator it = h.iterator();
+    HashSet result = new HashSet();
+    while (it.hasNext()) {
+      Node curr = (Node)it.next();
+      if (!contains(curr, result)) {
+        result.add(curr);
+      }
+    }
+    return result;
+  }
+
+  private boolean oneAreaShared(Node node1, Node node2)
+  {
+    HashSet result = new HashSet();
+    node2.deepSearch(node1, result);
+    HashSet largest = getLNodes(result);
+    if (largest.isEmpty()) {
+      return false;
+    }
+    return (Nodes.setNodesSize(largest) == 1) && (largest.size() == 1);
+  }
+
+  public HashSet<Node> combine(Node node) {
+    Node node1 = node.reduce();
+    Node node2 = reduce();
+
+    node2.resetSearch();
+    node1.resetSearch();
+    HashSet result = new HashSet();
+
+    if (node1.equals(node2)) {
+      result.add(node2);
+      return result;
+    }
+
+    HashSet sResult = new HashSet();
+    node2.deepSearch(node1, sResult);
+
+    if (sResult.isEmpty()) {
+      result.add(node2.combineAtRoot(node1));
+      return result;
+    }
+
+    HashSet largest = getLNodes(sResult);
+
+    HashSet unsharedNodes = new HashSet();
+    node2.collectUnshared(unsharedNodes);
+    node1.collectUnshared(unsharedNodes);
+
+    if ((unsharedNodes.isEmpty()) && (node1.getNumAreas() == node2.getNumAreas())) {
+      if (node1.children.size() < node2.children.size()) {
+        Node toAdd = new Node(null, node1.getVenn());
+        result.add(toAdd);
+        return result;
+      }if (node2.children.size() < node1.children.size()) {
+        Node toAdd = new Node(null, node2.getVenn());
+        result.add(toAdd);
+        return result;
+      }
+
+    }
+
+    if (largest.size() > 0)
+    {
+      Iterator itLargest = largest.iterator();
+
+      Nodes flargest = (Nodes)itLargest.next();
+
+      HashSet lg1sibs = flargest.node1.getSiblings();
+      HashSet lg2sibs = flargest.node2.getSiblings();
+
+      HashSet newSibs = new HashSet();
+      HashSet otherSibs1 = new HashSet();
+      HashSet otherSibs2 = new HashSet();
+
+      Iterator lg1It = lg1sibs.iterator();
+      Iterator lg2It = lg2sibs.iterator();
+
+      sortLargestSibs(lg1It, flargest.node2, newSibs, otherSibs1);
+      sortLargestSibs(lg2It, flargest.node1, newSibs, otherSibs2);
+
+      Iterator nsIt = newSibs.iterator();
+
+      if ((otherSibs1.isEmpty()) && (otherSibs2.isEmpty())) {
+        while (nsIt.hasNext()) {
+          unsharedNodes.add((Node)nsIt.next());
+        }
+
+      }
+
+    }
+
+    HashSet newLinkedNodes = getLinkedNodes(unsharedNodes);
+
+    HashSet polys = reviewPolys(newLinkedNodes);
+    if (!polys.isEmpty())
+    {
+      Iterator polyIt = polys.iterator();
+      if (polyIt.hasNext()) {
+        HashSet polyresult = addPolys((HashSet)polyIt.next());
+        Iterator prIt = polyresult.iterator();
+        if (prIt.hasNext()) {
+          Nodes curr = (Nodes)prIt.next();
+          if ((curr.node1.parent != null) && (curr.node1.linkNode.parent != null)) {
+            String polyString1 = buildUp(curr.node1.parent, curr.node2.getVenn());
+            String polyString2 = buildUp(curr.node1.linkNode.parent, curr.node2.getVenn());
+            Node polyOne = new Node(null, polyString1);
+            Node polyTwo = new Node(null, polyString2);
+            HashSet polyresults = polyOne.combine(polyTwo);
+            Iterator prsIt = polyresults.iterator();
+            while (prsIt.hasNext())
+              result.add((Node)prsIt.next());
+          }
+          else if (curr.node1.parent != null) {
+            String polyString1 = buildUp(curr.node1.parent, curr.node2.getVenn());
+            Node polyOne = new Node(null, polyString1);
+            result.add(polyOne);
+          } else if (curr.node1.linkNode.parent != null) {
+            String polyString2 = buildUp(curr.node1.linkNode.parent, curr.node2.getVenn());
+            Node polyTwo = new Node(null, polyString2);
+            result.add(polyTwo);
+          } else {
+            System.out.println("What's up Houston?");
+          }
+        }
+      }
+      return result;
+    }
+
+    Iterator nLNit = newLinkedNodes.iterator();
+    if (nLNit.hasNext()) {
+      HashSet combined = ((Nodes)nLNit.next()).combineNew();
+      Iterator cIt = combined.iterator();
+      while (cIt.hasNext()) {
+        Nodes curr = (Nodes)cIt.next();
+        HashSet newResults = curr.node1.combine(curr.node2);
+        Iterator nRit = newResults.iterator();
+        while (nRit.hasNext())
+        {
+          result.add((Node)nRit.next());
+        }
+      }
+
+      return result;
+    }
+
+    if (Nodes.setNodesSize(largest) == 1)
+    {
+      HashSet unique = getUniqueNodes(sResult);
+      Iterator uIt = unique.iterator();
+
+      while (uIt.hasNext()) {
+        Node curr = (Node)uIt.next();
+        HashSet dupres = curr.getDuplicates(sResult);
+        if (dupres.size() > 1) {
+          HashSet newDupNodes = getLinkedNodes(dupres);
+          Iterator ndIt = newDupNodes.iterator();
+          while (ndIt.hasNext()) {
+            HashSet combined = ((Nodes)ndIt.next()).combineNew();
+            Iterator cIt = combined.iterator();
+            while (cIt.hasNext()) {
+              Nodes curr2 = (Nodes)cIt.next();
+              HashSet newResults = curr2.node1.combine(curr2.node2);
+              Iterator nRit = newResults.iterator();
+              while (nRit.hasNext())
+              {
+                result.add((Node)nRit.next());
+              }
+            }
+          }
+        }
+        else {
+          result.add(node2.combineAtRoot(node1));
+        }
+      }
+      return result;
+    }
+
+    if (Nodes.setNodesSize(largest) > 1)
+    {
+      Iterator largeIt = largest.iterator();
+      while (largeIt.hasNext()) {
+        Nodes currL = (Nodes)largeIt.next();
+        HashSet largeResult = subTrees(currL);
+        Iterator lrIt = largeResult.iterator();
+        while (lrIt.hasNext()) {
+          result.add((Node)lrIt.next());
+        }
+      }
+      return result;
+    }
+
+    return result;
+  }
+
+  private void sortLargestSibs(Iterator<Node> lgsibs, Node lg, HashSet<Node> ns, HashSet<Node> os) {
+    while (lgsibs.hasNext()) {
+      Node curr = (Node)lgsibs.next();
+      Iterator linkIt = curr.linkNodes.iterator();
+      boolean linkFlargest = false;
+      if (linkIt.hasNext()) {
+        linkFlargest = true;
+      }
+      while (linkIt.hasNext()) {
+        Node currLink = (Node)linkIt.next();
+
+        if (!lg.isIn(currLink)) {
+          linkFlargest = false;
+        }
+      }
+      if (linkFlargest) {
+        ns.add(curr);
+      }
+      else if (!curr.linkNodes.isEmpty())
+        os.add(curr);
+    }
+  }
+
+  public HashSet<Node> subTrees(Nodes largest)
+  {
+    HashSet result = new HashSet();
+    HashSet n = largest.combineSubTrees();
+
+    Iterator nit = n.iterator();
+
+    while (nit.hasNext()) {
+      String curr = ((Node)nit.next()).getVenn();
+
+      if ((curr.length() > 1) && (
+        (largest.node1.getSiblings().size() != 1) || 
+        (largest.node2.getSiblings().size() != 1))) {
+        curr = curr.substring(1, curr.length() - 1);
+      }
+
+      String curr1 = "(" + largest.node1.getVenn() + curr + ")";
+      String curr2 = "(" + largest.node2.getVenn() + curr + ")";
+      if ((largest.node1.parent != null) && 
+        (largest.node2.parent != null)) {
+        String one = buildUp(largest.node1.parent, curr1);
+        String two = buildUp(largest.node2.parent, curr2);
+        Node newone = new Node(null, one);
+        Node newtwo = new Node(null, two);
+        HashSet newresult = newone.combine(newtwo);
+        Iterator newit = newresult.iterator();
+        while (newit.hasNext()) {
+          result.add((Node)newit.next());
+        }
+        return result;
+      }if (largest.node1.parent != null) {
+        String one = buildUp(largest.node1.parent, curr1);
+        Node newone = new Node(null, one);
+        result.add(newone);
+        return result;
+      }if (largest.node2.parent != null) {
+        String two = buildUp(largest.node2.parent, curr2);
+        Node newtwo = new Node(null, two);
+        result.add(newtwo);
+        return result;
+      }
+      System.out.println("Houston we have a problem.");
+    }
+
+    return result;
+  }
+
+  private void search(Node node, HashSet<Nodes> h)
+  {
+    if (equals(node)) {
+      this.linkNodes.add(node);
+      node.linkNodes.add(this);
+
+      h.add(new Nodes(this, node));
+    } else {
+      Iterator it = this.children.iterator();
+      while (it.hasNext())
+        ((Node)(Node)it.next()).search(node, h);
+    }
+  }
+
+  private HashSet<Nodes> searchBothNodes(Node node)
+  {
+    HashSet sResult = new HashSet();
+    search(node, sResult);
+
+    HashSet nsResult = new HashSet();
+    node.search(this, nsResult);
+
+    Iterator it = nsResult.iterator();
+    while (it.hasNext()) {
+      Nodes curr = (Nodes)it.next();
+
+      Iterator currIt = sResult.iterator();
+      boolean inSet = inSet(curr, currIt);
+      if (!inSet) {
+        sResult.add(curr);
+      }
+    }
+    return sResult;
+  }
+
+  public void deepSearch(Node node, HashSet<Nodes> h)
+  {
+    HashSet first_result = searchBothNodes(node);
+    Iterator first_it = first_result.iterator();
+    while (first_it.hasNext()) {
+      h.add((Nodes)first_it.next());
+    }
+
+    Iterator it = node.children.iterator();
+    while (it.hasNext())
+      deepSearch((Node)it.next(), h);
+  }
+
+  private boolean inSet(Nodes n, Iterator<Nodes> it)
+  {
+    boolean inSet = false;
+    while (it.hasNext()) {
+      if (n.equals(it.next())) {
+        inSet = true;
+        break;
+      }
+    }
+    return inSet;
+  }
+
+  private void resetSearch() {
+    this.linkNodes = new HashSet();
+    this.linkNode = null;
+
+    Iterator it = this.children.iterator();
+    while (it.hasNext())
+      ((Node)it.next()).resetSearch();
+  }
+
+  private HashSet<Nodes> getLNodes(HashSet<Nodes> h)
+  {
+    HashSet result = new HashSet();
+    int size = 0;
+
+    Iterator it = h.iterator();
+    if (it.hasNext()) {
+      Nodes curr = (Nodes)it.next();
+      size = curr.nodesSize();
+      result.add(curr);
+    }
+    while (it.hasNext()) {
+      Nodes curr = (Nodes)it.next();
+      if (size < curr.nodesSize()) {
+        size = curr.nodesSize();
+        result.clear();
+        result.add(curr);
+      } else if (size == curr.nodesSize()) {
+        result.add(curr);
+      }
+    }
+    return result;
+  }
+
+  private Node combineAtRoot(Node node)
+  {
+    HashSet uniqueNodes = new HashSet();
+    Iterator thisIt = this.children.iterator();
+    Iterator nodeIt = node.children.iterator();
+    boolean reduced = false;
+    while (thisIt.hasNext()) {
+      Node curr = (Node)thisIt.next();
+      if (!contains(curr, uniqueNodes))
+        uniqueNodes.add(curr);
+      else {
+        reduced = true;
+      }
+    }
+    while (nodeIt.hasNext()) {
+      Node curr = (Node)nodeIt.next();
+      if (!contains(curr, uniqueNodes))
+        uniqueNodes.add(curr);
+      else {
+        reduced = true;
+      }
+    }
+    Iterator unIt = uniqueNodes.iterator();
+    String result = "(";
+    while (unIt.hasNext()) {
+      result = result + unIt.next();
+    }
+    result = result + ")";
+
+    if (!reduced) {
+      return new Node(null, "(" + this.venn + node.getVenn() + ")");
+    }
+    return new Node(null, result);
+  }
+
+  private boolean unshared()
+  {
+    if (this.linkNodes.isEmpty())
+    {
+      Iterator it = this.children.iterator();
+      if (!it.hasNext()) {
+        return true;
+      }
+      while (it.hasNext()) {
+        if (!((Node)it.next()).unshared()) {
+          return false;
+        }
+      }
+      return true;
+    }
+    return false;
+  }
+
+  private void collectUnshared(HashSet<Node> h) {
+    if (unshared()) {
+      h.add(this);
+    }
+    Iterator it = this.children.iterator();
+    while (it.hasNext())
+      ((Node)it.next()).collectUnshared(h);
+  }
+
+  private HashSet<Nodes> getLinkedNodes(HashSet<Node> h)
+  {
+    HashSet result = new HashSet();
+    Iterator hit = h.iterator();
+    while (hit.hasNext()) {
+      Node curr = (Node)hit.next();
+      if (curr.hasLink() != null)
+        result.add(new Nodes(curr, curr.hasLink()));
+    }
+    return result;
+  }
+
+  private Node hasLink() {
+    HashSet sibs = getSiblings();
+    Iterator it = sibs.iterator();
+    while (it.hasNext()) {
+      Node curr = (Node)it.next();
+
+      if (!curr.linkNodes.isEmpty()) {
+        return curr;
+      }
+    }
+    return null;
+  }
+
+  public Node root() {
+    if (getSiblings().isEmpty()) {
+      return this;
+    }
+    return this.parent.root();
+  }
+
+  private String buildUp(Node n, String s) {
+    if (n.parent == null) {
+      return s;
+    }
+    String result = "(" + s;
+    if (n.parent != null) {
+      HashSet sibs = n.getSiblings();
+      Iterator it = sibs.iterator();
+      while (it.hasNext()) {
+        result = result + ((Node)it.next()).getVenn();
+      }
+    }
+    result = result + ")";
+    return buildUp(n.parent, result);
+  }
+
+  private HashSet<HashSet<Nodes>> reviewPolys(HashSet<Nodes> h)
+  {
+    HashSet result = new HashSet();
+    Iterator it = h.iterator();
+    while (it.hasNext()) {
+      HashSet curresult = new HashSet();
+      Nodes curr = (Nodes)it.next();
+      Iterator currIt = h.iterator();
+      while (currIt.hasNext()) {
+        Nodes inCurr = (Nodes)currIt.next();
+        if (inCurr == curr) {
+          continue;
+        }
+        HashSet linkedNodes = inCurr.node2.linkNodes;
+        Iterator lnIt = linkedNodes.iterator();
+        boolean found = false;
+        while (lnIt.hasNext()) {
+          Node currln = (Node)lnIt.next();
+          if (currln == curr.node2) {
+            found = true;
+            inCurr.node2.linkNode = currln;
+            break;
+          }
+        }
+
+        if (found) {
+          curresult.add(inCurr);
+        }
+      }
+
+      if (!curresult.isEmpty()) {
+        curresult.add(curr);
+        result.add(curresult);
+      }
+    }
+    return result;
+  }
+
+  private HashSet<Nodes> addPolys(HashSet<Nodes> h) {
+    HashSet setresult = new HashSet();
+    Iterator it = h.iterator();
+    Nodes first = (Nodes)it.next();
+    String result = first.node2.getVenn();
+    result = result + first.node1.getVenn();
+    while (it.hasNext()) {
+      result = result + ((Nodes)it.next()).node1.getVenn();
+    }
+    Iterator itr = h.iterator();
+    while (itr.hasNext()) {
+      Nodes toAdd = new Nodes(((Nodes)itr.next()).node2, new Node(null, "(" + result + ")"));
+      setresult.add(toAdd);
+    }
+    return setresult;
+  }
+
+  private boolean isIn(Node node) {
+    if (this == node) {
+      return true;
+    }
+    Iterator it = this.children.iterator();
+    while (it.hasNext()) {
+      if (((Node)it.next()).isIn(node)) {
+        return true;
+      }
+    }
+
+    return false;
+  }
+
+  private HashSet<Node> getUniqueNodes(HashSet<Nodes> h)
+  {
+    HashSet result = new HashSet();
+    Iterator it = h.iterator();
+    while (it.hasNext()) {
+      Nodes curr = (Nodes)it.next();
+      if (!result.contains(curr.node1)) {
+        result.add(curr.node1);
+      }
+      if (!result.contains(curr.node2)) {
+        result.add(curr.node2);
+      }
+    }
+    return result;
+  }
+
+  private HashSet<Node> getDuplicates(HashSet<Nodes> h) {
+    HashSet result = new HashSet();
+    Iterator it = h.iterator();
+    while (it.hasNext()) {
+      Nodes curr = (Nodes)it.next();
+      if (curr.node1 == this)
+        result.add(curr.node2);
+      else if (curr.node2 == this) {
+        result.add(curr.node1);
+      }
+    }
+    return result;
+  }
+
+  protected Node reduce()
+  {
+    HashSet c = this.children;
+    HashSet uniqueC = new HashSet();
+    HashSet dupC = new HashSet();
+    Iterator it = c.iterator();
+    while (it.hasNext()) {
+      Node curr = (Node)it.next();
+      if (contains(curr, uniqueC))
+        dupC.add(curr);
+      else {
+        uniqueC.add(curr);
+      }
+    }
+    if ((dupC.isEmpty()) || (this.venn.length() == 1)) {
+      return this;
+    }
+    Iterator uIt = uniqueC.iterator();
+    String result = "";
+    if (uIt.hasNext()) {
+      result = result + ((Node)uIt.next()).getVenn();
+    }
+    if (uIt.hasNext()) {
+      result = result + "(" + result;
+      while (uIt.hasNext()) {
+        result = result + ((Node)uIt.next()).getVenn();
+      }
+      result = result + ")";
+    }
+    return new Node(null, result);
+  }
+
+  private boolean contains(Node n, HashSet<Node> h)
+  {
+    Iterator it = h.iterator();
+    while (it.hasNext()) {
+      Node curr = (Node)it.next();
+      if (curr.equals(n)) {
+        return true;
+      }
+    }
+    return false;
+  }
+}

+ 153 - 0
Nodes.java

@@ -0,0 +1,153 @@
+import java.util.HashSet;
+import java.util.Iterator;
+
+public class Nodes
+{
+  Node node1;
+  Node node2;
+
+  public Nodes(Node n1, Node n2)
+  {
+    this.node1 = n1;
+    this.node2 = n2;
+  }
+
+  public static int setNodesSize(HashSet<Nodes> h)
+  {
+    Iterator it = h.iterator();
+    return ((Nodes)it.next()).nodesSize();
+  }
+
+  public int nodesSize()
+  {
+    return this.node1.getVenn().length();
+  }
+
+  public String toString() {
+    return "<" + this.node1 + ", " + this.node2 + ">";
+  }
+
+  public boolean equals(Object o) {
+    Nodes input = (Nodes)o;
+
+    return ((input.node1 == this.node1) && (input.node2 == this.node2)) || (
+      (input.node1 == this.node2) && (input.node2 == this.node1));
+  }
+
+  public HashSet<Node> combineSubTrees()
+  {
+    HashSet comsibs = new HashSet();
+
+    HashSet n1sibs = this.node1.getSiblings();
+    Iterator it2 = n1sibs.iterator();
+    HashSet n2sibs = this.node2.getSiblings();
+    Iterator it1 = n2sibs.iterator();
+
+    String sibs1 = "";
+
+    if (it2.hasNext()) {
+      sibs1 = sibs1 + ((Node)it2.next()).getVenn();
+    }
+    else
+    {
+      if (it1.hasNext()) {
+        sibs1 = sibs1 + ((Node)it1.next()).getVenn();
+        if (it1.hasNext()) {
+          String temp = sibs1;
+          sibs1 = "(" + temp;
+          while (it1.hasNext()) {
+            sibs1 = sibs1 + ((Node)it1.next()).getVenn();
+          }
+          sibs1 = sibs1 + ")";
+        }
+        comsibs.add(new Node(null, sibs1));
+        return comsibs;
+      }
+
+      return comsibs;
+    }
+
+    if (it2.hasNext())
+    {
+      String temp = sibs1;
+      sibs1 = "(" + temp;
+      while (it2.hasNext()) {
+        sibs1 = sibs1 + ((Node)it2.next()).getVenn();
+      }
+      sibs1 = sibs1 + ")";
+    }
+
+    Node nodesibs1 = new Node(null, sibs1);
+    if (!it1.hasNext()) {
+      comsibs.add(nodesibs1);
+      return comsibs;
+    }
+    String sibs2 = "";
+    if (it1.hasNext()) {
+      sibs2 = sibs2 + ((Node)it1.next()).getVenn();
+      if (it1.hasNext()) {
+        String temp = sibs2;
+        sibs2 = "(" + temp;
+        while (it1.hasNext()) {
+          sibs2 = sibs2 + ((Node)it1.next()).getVenn();
+        }
+        sibs2 = sibs2 + ")";
+      }
+    }
+
+    Node nodesibs2 = new Node(null, sibs2);
+    return nodesibs1.combine(nodesibs2);
+  }
+
+  private boolean inSet(Node n, Iterator<Node> it)
+  {
+    boolean inSet = false;
+    while (it.hasNext()) {
+      if (n.equals((Node)it.next())) {
+        inSet = true;
+        break;
+      }
+    }
+    return inSet;
+  }
+
+  protected HashSet<Nodes> combineNew()
+  {
+    HashSet setResult = new HashSet();
+    Node rnode1 = new Node(null, this.node2.root().getVenn());
+    String added = "(" + this.node2.getVenn() + this.node1.getVenn() + ")";
+
+    HashSet ln = this.node2.linkNodes;
+
+    Iterator lnIt = ln.iterator();
+    while (lnIt.hasNext()) {
+      String result = buildUp((Node)lnIt.next(), added);
+      Node rnode2 = new Node(null, result);
+      setResult.add(new Nodes(rnode1, rnode2));
+    }
+
+    return setResult;
+  }
+
+  private String buildUp(Node n, String s)
+  {
+    if (n.parent == null) {
+      return s;
+    }
+    String result = s;
+    if (n.parent != null) {
+      HashSet sibs = n.getSiblings();
+      Iterator it = sibs.iterator();
+      if (it.hasNext()) {
+        String temp = "(" + result;
+        result = temp;
+
+        while (it.hasNext()) {
+          result = result + ((Node)it.next()).getVenn();
+        }
+        result = result + ")";
+      }
+    }
+    return buildUp(n.parent, result);
+  }
+}

+ 325 - 0
SwingApplication.java

@@ -0,0 +1,325 @@
+import java.awt.Component;
+import java.awt.Container;
+import java.awt.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Vector;
+import javax.swing.BorderFactory;
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JFileChooser;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+
+public class SwingApplication extends JPanel
+  implements ActionListener
+{
+  protected JTextField textField;
+  protected JTextField inputField;
+  private static String labelPrefix = "Status: Idle";
+
+  final JLabel label = new JLabel(labelPrefix);
+  final JLabel label1 = new JLabel("Input File: ");
+  final JLabel label2 = new JLabel("Output File: ");
+  final JFileChooser fc = new JFileChooser();
+  JButton button2;
+  JButton button;
+  String fileName = "";
+  JButton savebutton;
+  HashSet<Node> output;
+  String outFileName = "";
+  JButton resetbutton;
+  JButton viewbutton;
+  File outFile;
+  ImageIcon icon = new ImageIcon("sr.JPG");
+  final JLabel labelicon = new JLabel(this.icon);
+
+  static final String LOOKANDFEEL = null;
+
+  public Component createComponents() {
+    this.button = new JButton("Start PACT");
+    this.button.setMnemonic(73);
+    this.button.addActionListener(this);
+    this.label.setLabelFor(this.button);
+
+    this.button2 = new JButton("Open Input File...");
+    this.button2.setMnemonic(73);
+    this.button2.addActionListener(this);
+    this.label.setLabelFor(this.button2);
+
+    this.savebutton = new JButton("Save Output As...");
+    this.savebutton.setMnemonic(73);
+    this.savebutton.addActionListener(this);
+    this.label.setLabelFor(this.savebutton);
+
+    this.resetbutton = new JButton("Reset PACT");
+    this.resetbutton.setMnemonic(73);
+    this.resetbutton.addActionListener(this);
+    this.label.setLabelFor(this.resetbutton);
+
+    this.viewbutton = new JButton("View Output");
+    this.viewbutton.setMnemonic(73);
+    this.viewbutton.addActionListener(this);
+    this.label.setLabelFor(this.viewbutton);
+
+    this.textField = new JTextField(15);
+    this.textField.setEnabled(false);
+    this.inputField = new JTextField(20);
+
+    JPanel pane = new JPanel(new GridLayout(6, 2));
+
+    pane.add(this.label1);
+    pane.add(new JLabel(""));
+    pane.add(this.textField);
+    pane.add(this.button2);
+    pane.add(new JLabel(""));
+    pane.add(this.button);
+    pane.add(new JLabel(""));
+    pane.add(this.label);
+    pane.add(new JLabel(""));
+    pane.add(new JLabel(""));
+    pane.add(this.resetbutton);
+    pane.add(this.savebutton);
+
+    pane.setBorder(BorderFactory.createEmptyBorder(
+      30, 
+      30, 
+      10, 
+      30));
+
+    return pane;
+  }
+
+  public void actionPerformed(ActionEvent e) {
+    this.resetbutton.setEnabled(true);
+    if (e.getSource() == this.button2)
+    {
+      int returnVal = this.fc.showOpenDialog(this);
+
+      if (returnVal == 0) {
+        File file = this.fc.getSelectedFile();
+        this.fileName = file.getName();
+        this.textField.setText(this.fileName);
+      }
+    } else if (e.getSource() == this.button)
+    {
+      String outName = "output.txt";
+      try
+      {
+        this.output = process(this.fileName, outName);
+      } catch (FileNotFoundException io) {
+        this.label.setText("Status: Fail, file error");
+      } catch (IOException fe) {
+        this.label.setText("Status: Fail, read error");
+      } catch (BracketException be) {
+        this.label.setText("Status: Input error, try again");
+        this.resetbutton.setEnabled(false);
+        throw new BracketException();
+      } catch (Exception ge) {
+        this.label.setText("Status: Fail, general error");
+        this.resetbutton.setEnabled(false);
+        throw new RuntimeException();
+      }
+      if (this.fileName.equals(""))
+        this.label.setText("Status: Fail, file error");
+      else {
+        this.label.setText("Status: Success");
+      }
+    }
+    else if (e.getSource() == this.savebutton)
+    {
+      int returnVal = this.fc.showSaveDialog(this);
+      if (returnVal == 0) {
+        File file = this.fc.getSelectedFile();
+        this.outFileName = file.getName();
+        try {
+          PrintWriter summary = new PrintWriter(new FileWriter(
+            this.outFileName));
+          Iterator pIt = this.output.iterator();
+          while (pIt.hasNext()) {
+            summary.println(((Node)pIt.next()).getVenn());
+          }
+          summary.close();
+        } catch (IOException io) {
+          this.label.setText("Status: Fail, write error");
+        }
+      }
+    } else if (e.getSource() == this.resetbutton) {
+      this.output.clear();
+      this.fileName = "";
+      this.outFileName = "";
+      this.label.setText("Status: Idle");
+      this.textField.setText("");
+    }
+  }
+
+  private HashSet<Node> process(String in, String out)
+    throws IOException, FileNotFoundException, BracketException
+  {
+    BufferedReader inB = new BufferedReader(new FileReader(in));
+    String line = inB.readLine();
+    Vector inputs = new Vector();
+    while (line != null) {
+      inputs.add(line);
+      line = inB.readLine();
+    }
+    inB.close();
+    Iterator it = inputs.iterator();
+    HashSet printResult = new HashSet();
+    HashSet currres = new HashSet();
+    HashSet subres = new HashSet();
+    if (!it.hasNext()) {
+      System.out.println("Input file empty!");
+    }
+    Node curr1 = null;
+    if (it.hasNext()) {
+      curr1 = new Node(null, (String)it.next());
+      currres.add(curr1);
+    }
+    if (!it.hasNext()) {
+      printResult.add(curr1);
+    }
+
+    while (it.hasNext()) {
+      subres.clear();
+      Node curr2 = new Node(null, (String)it.next());
+
+      Iterator cIt = currres.iterator();
+
+      while (cIt.hasNext()) {
+        HashSet com = ((Node)cIt.next()).reducedCombine(curr2);
+        System.out.println("com: " + com);
+        Iterator comIt = com.iterator();
+        while (comIt.hasNext()) {
+          subres.add((Node)comIt.next());
+        }
+      }
+      Iterator sIt = subres.iterator();
+      currres.clear();
+      while (sIt.hasNext()) {
+        currres.add((Node)sIt.next());
+      }
+    }
+
+    Iterator itC = currres.iterator();
+    printResult.clear();
+    while (itC.hasNext()) {
+      printResult.add((Node)itC.next());
+    }
+    HashSet temp = removeDuplicates(printResult);
+
+    PrintWriter summary = new PrintWriter(new FileWriter(
+      out));
+    Iterator pIt = temp.iterator();
+    while (pIt.hasNext()) {
+      summary.println(((Node)pIt.next()).getVenn());
+    }
+    summary.close();
+    return temp;
+  }
+
+  private HashSet<Node> removeDuplicates(HashSet<Node> h)
+  {
+    Iterator it = h.iterator();
+    HashSet result = new HashSet();
+    while (it.hasNext()) {
+      Node curr = (Node)it.next();
+      if (!contains(curr, result)) {
+        result.add(curr);
+      }
+    }
+    return result;
+  }
+
+  private boolean contains(Node n, HashSet<Node> h) {
+    Iterator it = h.iterator();
+    while (it.hasNext()) {
+      Node curr = (Node)it.next();
+      if (curr.equals(n)) {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  private static void initLookAndFeel() {
+    String lookAndFeel = null;
+
+    if (LOOKANDFEEL != null) {
+      if (LOOKANDFEEL.equals("Metal")) {
+        lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
+      } else if (LOOKANDFEEL.equals("System")) {
+        lookAndFeel = UIManager.getSystemLookAndFeelClassName();
+      } else if (LOOKANDFEEL.equals("Motif")) {
+        lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
+      } else if (LOOKANDFEEL.equals("GTK+")) {
+        lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
+      } else {
+        System.err.println("Unexpected value of LOOKANDFEEL specified: " + 
+          LOOKANDFEEL);
+        lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
+      }
+      try
+      {
+        UIManager.setLookAndFeel(lookAndFeel);
+      } catch (ClassNotFoundException e) {
+        System.err.println("Couldn't find class for specified look and feel:" + 
+          lookAndFeel);
+        System.err.println("Did you include the L&F library in the class path?");
+        System.err.println("Using the default look and feel.");
+      } catch (UnsupportedLookAndFeelException e) {
+        System.err.println("Can't use the specified look and feel (" + 
+          lookAndFeel + 
+          ") on this platform.");
+        System.err.println("Using the default look and feel.");
+      } catch (Exception e) {
+        System.err.println("Couldn't get specified look and feel (" + 
+          lookAndFeel + 
+          "), for some reason.");
+        System.err.println("Using the default look and feel.");
+        e.printStackTrace();
+      }
+    }
+  }
+
+  private static void createAndShowGUI()
+  {
+    initLookAndFeel();
+
+    JFrame.setDefaultLookAndFeelDecorated(true);
+
+    JFrame frame = new JFrame("PACT 2.0");
+    frame.setDefaultCloseOperation(3);
+
+    SwingApplication app = new SwingApplication();
+    Component contents = app.createComponents();
+    frame.getContentPane().add(contents, "Center");
+
+    frame.pack();
+    frame.setVisible(true);
+  }
+
+  public static void main(String[] args)
+  {
+    SwingUtilities.invokeLater(new Runnable() {
+      public void run() {
+        SwingApplication.access$0();
+      }
+    });
+  }
+}

+ 210 - 0
TestNode.java

@@ -0,0 +1,210 @@
+import java.io.PrintStream;
+import java.util.HashSet;
+
+public class TestNode
+{
+  public static void main(String[] args)
+  {
+    System.out.println("**Equals Test**");
+    Node node1 = new Node(null, "(ABC)");
+    Node node2 = new Node(null, "(AB)");
+    try {
+      if (node1.equals(node2))
+        System.out.println("Equals test failed.");
+      else
+        System.out.println("Equals test passed.");
+    }
+    catch (Exception e) {
+      System.out.println("Equals test failed: + e");
+    }
+
+    System.out.println("**Equals Test**");
+    Node node3 = new Node(null, "(ABC)");
+    Node node4 = new Node(null, "(AB)");
+    try {
+      if (node4.equals(node3))
+        System.out.println("Equals test failed.");
+      else
+        System.out.println("Equals test passed.");
+    }
+    catch (Exception e) {
+      System.out.println("Equals test failed: + e");
+    }
+
+    System.out.println("**Equals Test**");
+    Node node5 = new Node(null, "((BC)A)");
+    Node node6 = new Node(null, "(A(CB))");
+    try {
+      if (!node5.equals(node6))
+        System.out.println("Equals test failed.");
+      else
+        System.out.println("Equals test passed.");
+    }
+    catch (Exception e) {
+      System.out.println("Equals test failed: + e");
+    }
+
+    System.out.println("**Equals Test**");
+    Node node7 = new Node(null, "(A(BC))");
+    Node node8 = new Node(null, "(A(BCD))");
+    try {
+      if (node7.equals(node8))
+        System.out.println("Equals test failed.");
+      else
+        System.out.println("Equals test passed.");
+    }
+    catch (Exception e) {
+      System.out.println("Equals test failed: " + e);
+    }
+
+    System.out.println("**Search Test**");
+    Node node11 = new Node(null, "(A(B(C(DA))))");
+    Node node12 = new Node(null, "(A(B(CD)))");
+    try {
+      HashSet result = new HashSet();
+      node12.deepSearch(node11, result);
+      System.out.println(result);
+    } catch (Exception e) {
+      System.out.println("Search test failed: " + e);
+    }
+
+    System.out.println("**reducedCombine Test 1**");
+    System.out.println("Totally different.");
+    Node node13 = new Node(null, "(DB)");
+    Node node14 = new Node(null, "(AC)");
+    try {
+      HashSet result = node13.reducedCombine(node14);
+      System.out.println(result);
+    } catch (Exception e) {
+      System.out.println("reducedCombine test failed: " + e);
+    }
+
+    System.out.println("**reducedCombine Test 2**");
+    System.out.println("Totally the same.");
+    Node node15 = new Node(null, "(A(BDC))");
+    Node node16 = new Node(null, "((CDB)A)");
+    try {
+      HashSet result = node16.reducedCombine(node15);
+      System.out.println(result);
+    } catch (Exception e) {
+      System.out.println("reducedCombine test failed: " + e);
+    }
+
+    System.out.println("**reducedCombine Test 3**");
+    Node node17 = new Node(null, "(A(CD))");
+    Node node18 = new Node(null, "(A(B(CD)))");
+
+    HashSet result5 = node17.reducedCombine(node18);
+    System.out.println(result5);
+
+    System.out.println("**reducedCombine Test 4**");
+    Node node19 = new Node(null, "(A((BE)(CD)))");
+    Node node20 = new Node(null, "(A(B(CD)))");
+
+    HashSet result4 = node19.reducedCombine(node20);
+    System.out.println(result4);
+
+    System.out.println("**reducedCombine Test 5**");
+    Node node21 = new Node(null, "(A(A(B(CD))))");
+    Node node22 = new Node(null, "(A((BE)(C(DA))))");
+
+    HashSet result2 = node21.reducedCombine(node22);
+    System.out.println(result2);
+
+    System.out.println("**reducedCombine Test 6**");
+    Node node23 = new Node(null, "(A(B(CD)))");
+    Node node24 = new Node(null, "((A(B(CD)))(A(B(CD))))");
+
+    HashSet result1 = node24.reducedCombine(node23);
+    System.out.println(result1);
+
+    System.out.println("**reducedCombine Test 7**");
+    Node node25 = new Node(null, "(A(CD))");
+    Node node26 = new Node(null, "(A(B(C(DA))))");
+
+    HashSet result = node25.reducedCombine(node26);
+    System.out.println(result);
+
+    System.out.println("**reducedCombine Test 8**");
+    Node node27 = new Node(null, "(A(B(CD)))");
+    Node node28 = new Node(null, "(A(B(C(DA))))");
+
+    HashSet resultA = node27.reducedCombine(node28);
+    System.out.println(resultA);
+
+    System.out.println("**reducedCombine Test 9**");
+    Node node29 = new Node(null, "A");
+    Node node30 = new Node(null, "B");
+
+    HashSet resultB = node29.reducedCombine(node30);
+    System.out.println(resultB);
+
+    System.out.println("**reducedCombine Test 10**");
+    Node node31 = new Node(null, "(A(B(D((CE)(CD)))))");
+    Node node32 = new Node(null, "(F(C(DB)((D(BE))(CD))))");
+
+    HashSet resultC = node32.reducedCombine(node31);
+    System.out.println(resultC);
+
+    System.out.println("**reducedCombine Test 10b**");
+
+    HashSet resultD = node31.reducedCombine(node32);
+    System.out.println(resultD);
+
+    System.out.println("**reducedCombine Test 11**");
+    Node node33 = new Node(null, "((AB)(CD))");
+    Node node34 = new Node(null, "(E(F(GA)))");
+
+    HashSet result6 = node33.reducedCombine(node34);
+    System.out.println(result6);
+
+    System.out.println("**reducedCombine Test 12**");
+    Node node35 = new Node(null, "(A(B(CD)))");
+    Node node36 = new Node(null, "(D(C(BA)))");
+
+    HashSet result7 = node35.reducedCombine(node36);
+    System.out.println(result7);
+
+    System.out.println("**reducedCombine Test 13**");
+    Node node37 = new Node(null, "(A(CD))");
+    Node node38 = new Node(null, "(A(BE))");
+
+    HashSet result8 = node37.reducedCombine(node38);
+    System.out.println(result8);
+
+    System.out.println("**reducedCombine Test 14**");
+    Node node39 = new Node(null, "(A(CD)(BE))");
+    Node node40 = new Node(null, "(A((CD)(BE)))");
+
+    HashSet result9 = node39.reducedCombine(node40);
+    System.out.println(result9);
+
+    System.out.println("**reducedCombine Test 15**");
+    Node node41 = new Node(null, "((AC)B)");
+    Node node42 = new Node(null, "(A(AB))");
+
+    HashSet result10 = node42.reducedCombine(node41);
+    System.out.println(result10);
+
+    System.out.println("**reducedCombine Test 16**");
+    Node node43 = new Node(null, "((AC)BA)");
+    Node node44 = new Node(null, "(A(CB))");
+
+    HashSet result11 = node43.reducedCombine(node44);
+    System.out.println(result11);
+
+    System.out.println("**reducedCombine Test 17**");
+    Node node45 = new Node(null, "(A(BC))");
+    Node node46 = new Node(null, "(DB)");
+
+    HashSet result12 = node45.reducedCombine(node46);
+    System.out.println(result12);
+
+    System.out.println("**reducedCombine Test 18**");
+    Node node47 = new Node(null, "(((TH)(IS))(R(E(AL))))");
+    Node node48 = new Node(null, "(((TH)(IS))(T(RE)))");
+
+    HashSet result14 = node47.reducedCombine(node48);
+    System.out.println(result14);
+  }
+}