KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > impl > oldCode > TestTrail


1 /******************************************************************
2  * File: TestTrail.java
3  * Created by: Dave Reynolds
4  * Created on: 20-May-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: TestTrail.java,v 1.4 2005/02/21 12:18:06 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode;
11
12 import com.hp.hpl.jena.graph.*;
13 import com.hp.hpl.jena.reasoner.*;
14 import com.hp.hpl.jena.reasoner.rulesys.*;
15
16 import junit.framework.TestCase;
17 import junit.framework.TestSuite;
18
19 /**
20  * Test harness for the prototype binding trail implementation.
21  *
22  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
23  * @version $Revision: 1.4 $ on $Date: 2005/02/21 12:18:06 $
24  */

25 public class TestTrail extends TestCase {
26
27     Node a = Node.createURI("a");
28     Node b = Node.createURI("b");
29     Node c = Node.createURI("c");
30     Node p = Node.createURI("p");
31     Node q = Node.createURI("q");
32     
33     /**
34      * Boilerplate for junit
35      */

36     public TestTrail( String JavaDoc name ) {
37         super( name );
38     }
39     
40     /**
41      * Boilerplate for junit.
42      * This is its own test suite
43      */

44     public static TestSuite suite() {
45         return new TestSuite( TestTrail.class );
46     }
47
48     /**
49      * Test unification support.
50      */

51     public void testUnify() {
52         Node_RuleVariable X = new Node_RuleVariable("x", 0);
53         Node_RuleVariable Y = new Node_RuleVariable("y", 1);
54         Node_RuleVariable Z = new Node_RuleVariable("z", 2);
55  
56         Trail trail = new Trail();
57         assertTrue(trail.unify(new TriplePattern(X, p, Y), new TriplePattern(a, p, b)));
58         assertEquals(X.deref(), a);
59         assertEquals(Y.deref(), b);
60         assertTrue(Z.isUnbound());
61         trail.unwindAndClear();
62         assertTrue(X.isUnbound());
63         assertTrue(Y.isUnbound());
64         
65         assertTrue(trail.unify(new TriplePattern(X, p, X), new TriplePattern(Z, p, a)));
66         assertEquals(X.deref(), a);
67         assertEquals(Z.deref(), a);
68         trail.unwindAndClear();
69         
70         TriplePattern gf = new TriplePattern(X, p,
71                                 Functor.makeFunctorNode("f", new Node[]{X, b}));
72         TriplePattern hf1 = new TriplePattern(Y, p,
73                                 Functor.makeFunctorNode("f", new Node[]{Z, b}));
74         TriplePattern hf2 = new TriplePattern(Y, p,
75                                 Functor.makeFunctorNode("f", new Node[]{a, Y}));
76         TriplePattern hf3 = new TriplePattern(Y, p,
77                                 Functor.makeFunctorNode("f", new Node[]{b, Y}));
78         assertTrue(trail.unify(gf, hf1));
79         assertEquals(X.deref(), Y.deref());
80         assertEquals(X.deref(), Z.deref());
81         trail.unwindAndClear();
82
83         assertTrue(! trail.unify(gf, hf2));
84         assertTrue(X.isUnbound());
85         assertTrue(Y.isUnbound());
86         assertTrue(Z.isUnbound());
87         trail.unwindAndClear();
88
89         assertTrue(trail.unify(gf, hf3));
90         assertEquals(X.deref(), b);
91         assertEquals(Y.deref(), b);
92         trail.unwindAndClear();
93         
94     }
95     
96     /**
97      * Check a few triple pattern invariants. These are not directly
98      * part of the trail system but the trail machinery depends on them.
99      */

100     public void testMatching() {
101         Node_RuleVariable X = new Node_RuleVariable("x", 0);
102         Node_RuleVariable Y = new Node_RuleVariable("y", 1);
103         Node_RuleVariable Z = new Node_RuleVariable("z", 2);
104         Node_RuleVariable X1 = new Node_RuleVariable("x1", 0);
105         Node_RuleVariable Y1 = new Node_RuleVariable("y1", 1);
106         Node_RuleVariable Z1 = new Node_RuleVariable("z1", 2);
107  
108         assertTrue(X.sameValueAs(Y));
109         TriplePattern f1 = new TriplePattern(X, p,
110                                 Functor.makeFunctorNode("f", new Node[]{X, b}));
111         TriplePattern f2 = new TriplePattern(Y, p,
112                                 Functor.makeFunctorNode("f", new Node[]{Z, b}));
113         TriplePattern f3 = new TriplePattern(Y1, p,
114                                 Functor.makeFunctorNode("f", new Node[]{Y1, b}));
115         TriplePattern f4 = new TriplePattern(X1, p,
116                                 Functor.makeFunctorNode("f", new Node[]{Z1, b}));
117         assertEquals(f1, f2);
118         assertEquals(f1.hashCode(), f2.hashCode());
119         assertTrue(f1.variantOf(f3));
120         assertTrue(f2.variantOf(f4));
121         assertTrue( ! f1.variantOf(f2));
122         assertTrue( ! f3.variantOf(f4));
123     }
124     
125 }
126
127 /*
128     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
129     All rights reserved.
130
131     Redistribution and use in source and binary forms, with or without
132     modification, are permitted provided that the following conditions
133     are met:
134
135     1. Redistributions of source code must retain the above copyright
136        notice, this list of conditions and the following disclaimer.
137
138     2. Redistributions in binary form must reproduce the above copyright
139        notice, this list of conditions and the following disclaimer in the
140        documentation and/or other materials provided with the distribution.
141
142     3. The name of the author may not be used to endorse or promote products
143        derived from this software without specific prior written permission.
144
145     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
146     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
147     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
148     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
149     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
150     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
151     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
152     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
153     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
154     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
155 */
Popular Tags