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

pag at mulgara.org pag at mulgara.org
Thu Mar 12 22:53:59 UTC 2009


Author: pag
Date: 2009-03-12 15:53:58 -0700 (Thu, 12 Mar 2009)
New Revision: 1608

Added:
   branches/consistency/src/jar/content-rlog/java/org/mulgara/krule/rlog/ast/CanonicalPredicateTest.java
Log:
Test for canonical predicates

Added: branches/consistency/src/jar/content-rlog/java/org/mulgara/krule/rlog/ast/CanonicalPredicateTest.java
===================================================================
--- branches/consistency/src/jar/content-rlog/java/org/mulgara/krule/rlog/ast/CanonicalPredicateTest.java	                        (rev 0)
+++ branches/consistency/src/jar/content-rlog/java/org/mulgara/krule/rlog/ast/CanonicalPredicateTest.java	2009-03-12 22:53:58 UTC (rev 1608)
@@ -0,0 +1,179 @@
+/*
+ * 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 org.apache.log4j.Logger;
+import org.mulgara.krule.rlog.ParseContext;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * 
+ *
+ * @created Mar 10, 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 CanonicalPredicateTest extends TestCase {
+
+  @SuppressWarnings("unused")
+  private static final Logger logger = Logger.getLogger(CanonicalPredicateTest.class.getName());
+
+  ParseContext context = new ParseContext();
+
+  /**
+   * Create the test
+   * @param name The name of the test.
+   */
+  public CanonicalPredicateTest(String name) {
+    super(name);
+  }
+
+
+  /**
+   * Hook from which the test runner can obtain a test suite.
+   *
+   * @return the test suite
+   */
+  public static Test suite() {
+    TestSuite suite = new TestSuite(CanonicalPredicateTest.class);
+    return suite;
+  }
+
+
+  /**
+   * This is a hook for initializing a test.
+   */
+  public void setup() {
+  }
+
+
+  /**
+   * Test that a predicate can be canonicalized.
+   *
+   * @throws Exception Due to any kind of internal failure.
+   */
+  public void testBinaryLiteral() throws Exception {
+    PredicateParam left = new BPredicateLiteral("foo", "bar", context);
+    PredicateParam right = new BPredicateLiteral("foo", "baz", context);
+    BPredicateLabel label = new BPredicateLiteral("foo", "pred", context);
+    Predicate p = new BPredicate(label, left, right, context);
+    CanonicalPredicate cp = p.getCanonical();
+    assertEquals("[foo:bar, foo:pred, foo:baz]", cp.toString());
+    cp.invert();
+    assertEquals("~[foo:bar, foo:pred, foo:baz]", cp.toString());
+  }
+
+  /**
+   * Test that a type predicate can be canonicalized.
+   *
+   * @throws Exception Due to any kind of internal failure.
+   */
+  public void testTypeLiteral() throws Exception {
+    PredicateParam left = new BPredicateLiteral("foo", "bar", context);
+    TypeLabel label = new TypeLiteral("foo", "Type", context);
+    Predicate p = new TypeStatement(label, left, context);
+    CanonicalPredicate cp = p.getCanonical();
+    assertEquals("[foo:Type, foo:bar]", cp.toString());
+    cp.invert();
+    assertEquals("~[foo:Type, foo:bar]", cp.toString());
+  }
+
+  /**
+   * Test that a binary predicate with variables can be canonicalized.
+   *
+   * @throws Exception Due to any kind of internal failure.
+   */
+  public void testBinaryVar() throws Exception {
+    PredicateParam left = new BPredicateLiteral("foo", "bar", context);
+    PredicateParam right = new Variable("X");
+    BPredicateLabel label = new BPredicateLiteral("foo", "pred", context);
+    Predicate p = new BPredicate(label, left, right, context);
+    CanonicalPredicate cp = p.getCanonical();
+    assertEquals("[foo:bar, foo:pred, ?X]", cp.toString());
+    VariableCanonicalizer con = new VariableCanonicalizer();
+    cp.renameVariables(con);
+    assertEquals("[foo:bar, foo:pred, ?V1]", cp.toString());
+
+    left = new Variable("A");
+    right = new Variable("B");
+    label = new Variable("C");
+    p = new BPredicate(label, left, right, context);
+    cp = p.getCanonical();
+    assertEquals("[?A, ?C, ?B]", cp.toString());
+    con = new VariableCanonicalizer();
+    cp.renameVariables(con);
+    assertEquals("[?V1, ?V2, ?V3]", cp.toString());
+    cp.renameVariables(con);
+    assertEquals("[?V4, ?V5, ?V6]", cp.toString());
+
+    cp = p.getCanonical();
+    cp.renameVariables(con);
+    assertEquals("[?V1, ?V2, ?V3]", cp.toString());
+  }
+
+  /**
+   * Test that a variable type predicate can be canonicalized.
+   *
+   * @throws Exception Due to any kind of internal failure.
+   */
+  public void testTypeVariable() throws Exception {
+    PredicateParam left = new BPredicateLiteral("foo", "bar", context);
+    TypeLabel label = new Variable("X");
+    Predicate p = new TypeStatement(label, left, context);
+    CanonicalPredicate cp = p.getCanonical();
+    assertEquals("[?X, foo:bar]", cp.toString());
+    VariableCanonicalizer con = new VariableCanonicalizer();
+    cp.renameVariables(con);
+    assertEquals("[?V1, foo:bar]", cp.toString());
+
+    left = new Variable("B");
+    label = new Variable("A");
+    p = new TypeStatement(label, left, context);
+    cp = p.getCanonical();
+    assertEquals("[?A, ?B]", cp.toString());
+    con = new VariableCanonicalizer();
+    cp.renameVariables(con);
+    assertEquals("[?V1, ?V2]", cp.toString());
+
+    cp.renameVariables(con);
+    assertEquals("[?V3, ?V4]", cp.toString());
+
+    cp = p.getCanonical();
+    cp.renameVariables(con);
+    assertEquals("[?V1, ?V2]", cp.toString());
+  }
+
+  /**
+   * Test that a variable type predicate can be equal.
+   *
+   * @throws Exception Due to any kind of internal failure.
+   */
+  public void testTypeEqual() throws Exception {
+    PredicateParam left = new BPredicateLiteral("foo", "bar", context);
+    TypeLabel label = new Variable("X");
+    Predicate p = new TypeStatement(label, left, context);
+    CanonicalPredicate cp = p.getCanonical();
+    VariableCanonicalizer con = new VariableCanonicalizer();
+    cp.renameVariables(con);
+    assertEquals("[?V1, foo:bar]", cp.toString());
+
+    Predicate p2 = new TypeStatement(new Variable("V1"), left, context);
+    assertNotSame(p, p2);
+    assertEquals(cp, p2.getCanonical());
+  }
+
+}




More information about the Mulgara-svn mailing list