001: /******************************************************************
002: * File: TestTrail.java
003: * Created by: Dave Reynolds
004: * Created on: 20-May-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: TestTrail.java,v 1.7 2008/01/02 12:09:44 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.reasoner.*;
013: import com.hp.hpl.jena.reasoner.rulesys.*;
014:
015: import junit.framework.TestCase;
016: import junit.framework.TestSuite;
017:
018: /**
019: * Test harness for the prototype binding trail implementation.
020: *
021: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
022: * @version $Revision: 1.7 $ on $Date: 2008/01/02 12:09:44 $
023: */
024: public class TestTrail extends TestCase {
025:
026: Node a = Node.createURI("a");
027: Node b = Node.createURI("b");
028: Node c = Node.createURI("c");
029: Node p = Node.createURI("p");
030: Node q = Node.createURI("q");
031:
032: /**
033: * Boilerplate for junit
034: */
035: public TestTrail(String name) {
036: super (name);
037: }
038:
039: /**
040: * Boilerplate for junit.
041: * This is its own test suite
042: */
043: public static TestSuite suite() {
044: return new TestSuite(TestTrail.class);
045: }
046:
047: /**
048: * Test unification support.
049: */
050: public void testUnify() {
051: Node_RuleVariable X = new Node_RuleVariable("x", 0);
052: Node_RuleVariable Y = new Node_RuleVariable("y", 1);
053: Node_RuleVariable Z = new Node_RuleVariable("z", 2);
054:
055: Trail trail = new Trail();
056: assertTrue(trail.unify(new TriplePattern(X, p, Y),
057: new TriplePattern(a, p, b)));
058: assertEquals(X.deref(), a);
059: assertEquals(Y.deref(), b);
060: assertTrue(Z.isUnbound());
061: trail.unwindAndClear();
062: assertTrue(X.isUnbound());
063: assertTrue(Y.isUnbound());
064:
065: assertTrue(trail.unify(new TriplePattern(X, p, X),
066: new TriplePattern(Z, p, a)));
067: assertEquals(X.deref(), a);
068: assertEquals(Z.deref(), a);
069: trail.unwindAndClear();
070:
071: TriplePattern gf = new TriplePattern(X, p, Functor
072: .makeFunctorNode("f", new Node[] { X, b }));
073: TriplePattern hf1 = new TriplePattern(Y, p, Functor
074: .makeFunctorNode("f", new Node[] { Z, b }));
075: TriplePattern hf2 = new TriplePattern(Y, p, Functor
076: .makeFunctorNode("f", new Node[] { a, Y }));
077: TriplePattern hf3 = new TriplePattern(Y, p, Functor
078: .makeFunctorNode("f", new Node[] { b, Y }));
079: assertTrue(trail.unify(gf, hf1));
080: assertEquals(X.deref(), Y.deref());
081: assertEquals(X.deref(), Z.deref());
082: trail.unwindAndClear();
083:
084: assertTrue(!trail.unify(gf, hf2));
085: assertTrue(X.isUnbound());
086: assertTrue(Y.isUnbound());
087: assertTrue(Z.isUnbound());
088: trail.unwindAndClear();
089:
090: assertTrue(trail.unify(gf, hf3));
091: assertEquals(X.deref(), b);
092: assertEquals(Y.deref(), b);
093: trail.unwindAndClear();
094:
095: }
096:
097: /**
098: * Check a few triple pattern invariants. These are not directly
099: * part of the trail system but the trail machinery depends on them.
100: */
101: public void testMatching() {
102: Node_RuleVariable X = new Node_RuleVariable("x", 0);
103: Node_RuleVariable Y = new Node_RuleVariable("y", 1);
104: Node_RuleVariable Z = new Node_RuleVariable("z", 2);
105: Node_RuleVariable X1 = new Node_RuleVariable("x1", 0);
106: Node_RuleVariable Y1 = new Node_RuleVariable("y1", 1);
107: Node_RuleVariable Z1 = new Node_RuleVariable("z1", 2);
108:
109: assertTrue(X.sameValueAs(Y));
110: TriplePattern f1 = new TriplePattern(X, p, Functor
111: .makeFunctorNode("f", new Node[] { X, b }));
112: TriplePattern f2 = new TriplePattern(Y, p, Functor
113: .makeFunctorNode("f", new Node[] { Z, b }));
114: TriplePattern f3 = new TriplePattern(Y1, p, Functor
115: .makeFunctorNode("f", new Node[] { Y1, b }));
116: TriplePattern f4 = new TriplePattern(X1, p, Functor
117: .makeFunctorNode("f", new Node[] { Z1, b }));
118: assertEquals(f1, f2);
119: assertEquals(f1.hashCode(), f2.hashCode());
120: assertTrue(f1.variantOf(f3));
121: assertTrue(f2.variantOf(f4));
122: assertTrue(!f1.variantOf(f2));
123: assertTrue(!f3.variantOf(f4));
124: }
125:
126: }
127:
128: /*
129: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
130: All rights reserved.
131:
132: Redistribution and use in source and binary forms, with or without
133: modification, are permitted provided that the following conditions
134: are met:
135:
136: 1. Redistributions of source code must retain the above copyright
137: notice, this list of conditions and the following disclaimer.
138:
139: 2. Redistributions in binary form must reproduce the above copyright
140: notice, this list of conditions and the following disclaimer in the
141: documentation and/or other materials provided with the distribution.
142:
143: 3. The name of the author may not be used to endorse or promote products
144: derived from this software without specific prior written permission.
145:
146: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
148: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
149: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
150: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
151: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
152: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
153: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
154: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
155: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
156: */
|