[Mulgara-svn] r1609 - branches/consistency/src/jar/content-rlog/java/org/mulgara/krule/rlog/ast

pag at mulgara.org pag at mulgara.org
Fri Mar 13 01:31:25 UTC 2009


Author: pag
Date: 2009-03-12 18:31:24 -0700 (Thu, 12 Mar 2009)
New Revision: 1609

Added:
   branches/consistency/src/jar/content-rlog/java/org/mulgara/krule/rlog/ast/CanonicalStatement.java
Log:
Added a structure forc anonical statements, so avoid the minor possibility of the head being confused with the body of a consistency check

Added: branches/consistency/src/jar/content-rlog/java/org/mulgara/krule/rlog/ast/CanonicalStatement.java
===================================================================
--- branches/consistency/src/jar/content-rlog/java/org/mulgara/krule/rlog/ast/CanonicalStatement.java	                        (rev 0)
+++ branches/consistency/src/jar/content-rlog/java/org/mulgara/krule/rlog/ast/CanonicalStatement.java	2009-03-13 01:31:24 UTC (rev 1609)
@@ -0,0 +1,111 @@
+/*
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+
+package org.mulgara.krule.rlog.ast;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Represents a canonicalized form of a statement. Used for comparing statements
+ * to check them for redundancy.
+ *
+ * @created Mar 12, 2009
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class CanonicalStatement {
+
+  /** A sorted list of predicates */
+  private List<CanonicalPredicate> body;
+
+  /** The head of the rule. May be empty. */
+  private CanonicalPredicate head;
+
+
+  /**
+   * Creates a canonical statement to represent an axiom.
+   * @param h The predicate in the axiom.
+   */
+  CanonicalStatement(CanonicalPredicate h) {
+    this(h, null);
+  }
+
+
+  /**
+   * Creates a canonical statement to represent a rule.
+   * @param h The predicate in the head of the rule.
+   * @param b The <em>sorted</em> predicates in the body of the rule.
+   */
+  CanonicalStatement(CanonicalPredicate h, List<CanonicalPredicate> b) {
+    if (b == null) body = Collections.emptyList();
+    else body = b;
+    head = h;
+    renameVariables();
+  }
+
+
+  /**
+   * Test is this statement is the same as another.
+   * @see java.lang.Object#equals(java.lang.Object)
+   */
+  public boolean equals(Object o) {
+    if (!(o instanceof CanonicalStatement)) return false;
+    CanonicalStatement s = (CanonicalStatement)o;
+    return equal(head, s.head) && body.equals(s.body);
+  }
+
+
+  /**
+   * Generates a hashcode which merges the hashcodes of the body and head.
+   * @see java.lang.Object#hashCode()
+   */
+  public int hashCode() {
+    int hhc = head.hashCode();
+    return (hhc >>> 16 | hhc << 16) ^ body.hashCode();
+  }
+
+
+  public String toString() {
+    StringBuilder s = new StringBuilder();
+    s.append(head);
+    s.append(" :- ");
+    for (int p = 0; p < body.size(); p++) {
+      if (p == 0) s.append(", ");
+      s.append(body.get(p));
+    }
+    s.append(".");
+    return s.toString();
+  }
+
+  /**
+   * Renames the variables into a canonical form.
+   */
+  private void renameVariables() {
+    VariableCanonicalizer vc = new VariableCanonicalizer();
+    for (CanonicalPredicate p: body) p.renameVariables(vc);
+    head.renameVariables(vc);
+  }
+
+
+  /**
+   * Performs an equality comparison for values that may be null.
+   * @param a The first object to be compared. May be null.
+   * @param b The second object to be compared. May be null.
+   * @return <code>true</code> if both a and b are null, or if they are equal.
+   *         <code>false</code> otherwise.
+   */
+  private static final boolean equal(Object a, Object b) {
+    return a == null ? b == null : a.equals(b);
+  }
+}




More information about the Mulgara-svn mailing list