KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > test > TestBackchainer


1 /******************************************************************
2  * File: TestBackchainer.java
3  * Created by: Dave Reynolds
4  * Created on: 04-May-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: TestBackchainer.java,v 1.31 2005/04/08 16:36:04 der Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.test;
11
12 import com.hp.hpl.jena.graph.*;
13 import com.hp.hpl.jena.mem.GraphMem;
14 import com.hp.hpl.jena.reasoner.*;
15 import com.hp.hpl.jena.reasoner.rulesys.*;
16 import com.hp.hpl.jena.reasoner.rulesys.impl.*;
17 import com.hp.hpl.jena.reasoner.test.TestUtil;
18 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
19 import com.hp.hpl.jena.vocabulary.OWL;
20 import com.hp.hpl.jena.vocabulary.RDFS;
21 import com.hp.hpl.jena.vocabulary.RDF;
22
23 import java.io.IOException JavaDoc;
24 import java.util.*;
25
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29 /**
30  * Test harness for the backward chainer.
31  * Parameterizable in subclasses by overriding createReasoner.
32  * The original version was developed for the original backchaining interpeter.
33  * That has now been obsoleted at this is now used to double check the
34  * LP engine, though the bulk of such tests are really done by TestBasicLP.
35  *
36  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
37  * @version $Revision: 1.31 $ on $Date: 2005/04/08 16:36:04 $
38  */

39 public class TestBackchainer extends TestCase {
40
41     // Useful constants
42
protected Node p = Node.createURI("p");
43     protected Node q = Node.createURI("q");
44     protected Node r = Node.createURI("r");
45     protected Node s = Node.createURI("s");
46     protected Node t = Node.createURI("t");
47     protected Node a = Node.createURI("a");
48     protected Node b = Node.createURI("b");
49     protected Node c = Node.createURI("c");
50     protected Node d = Node.createURI("d");
51     protected Node C1 = Node.createURI("C1");
52     protected Node C2 = Node.createURI("C2");
53     protected Node C3 = Node.createURI("C3");
54     protected Node sP = RDFS.subPropertyOf.getNode();
55     protected Node sC = RDFS.subClassOf.getNode();
56     protected Node ty = RDF.type.getNode();
57     
58     String JavaDoc testRules1 =
59         "(?x ?q ?y) <- (?p rdfs:subPropertyOf ?q)(?x ?p ?y). " +
60         "(?a rdfs:subPropertyOf ?c) <- (?a rdfs:subPropertyOf ?b)(?b rdfs:subPropertyOf ?c). ";
61         
62     String JavaDoc testRuleAxioms = "[ -> (p rdfs:subPropertyOf q)]" +
63                             "[ -> (q rdfs:subPropertyOf r) ]" +
64                             "[ -> (a p b) ]";
65                             
66     Triple[] dataElts = new Triple[] {
67                             new Triple(p, sP, q),
68                             new Triple(q, sP, r),
69                             new Triple(a, p, b)
70                             };
71      
72     /**
73      * Boilerplate for junit
74      */

75     public TestBackchainer( String JavaDoc name ) {
76         super( name );
77     }
78     
79     /**
80      * Boilerplate for junit.
81      * This is its own test suite
82      */

83     public static TestSuite suite() {
84         return new TestSuite( TestBackchainer.class );
85 // TestSuite suite = new TestSuite();
86
// suite.addTest(new TestBackchainer( "testRDFSProblemsb" ));
87
// return suite;
88
}
89
90     /**
91      * Override in subclasses to test other reasoners.
92      */

93     public Reasoner createReasoner(List rules) {
94         LPBackwardRuleReasoner reasoner = new LPBackwardRuleReasoner(rules);
95         reasoner.tablePredicate(sP);
96         reasoner.tablePredicate(sC);
97         reasoner.tablePredicate(ty);
98         reasoner.tablePredicate(p);
99         reasoner.tablePredicate(a);
100         reasoner.tablePredicate(b);
101         return reasoner;
102     }
103     
104     /**
105      * Test parser modes to support backarrow notation are working
106      */

107     public void testParse() {
108         List rules = Rule.parseRules(testRules1);
109         assertEquals("BRule parsing",
110                         "[ (?x ?q ?y) <- (?p rdfs:subPropertyOf ?q) (?x ?p ?y) ]",
111                         rules.get(0).toString());
112         assertEquals("BRule parsing",
113                         "[ (?a rdfs:subPropertyOf ?c) <- (?a rdfs:subPropertyOf ?b) (?b rdfs:subPropertyOf ?c) ]",
114                         rules.get(1).toString());
115     }
116     
117     /**
118      * Test goal/head unify operation.
119      */

120     public void testUnify() {
121         Node_RuleVariable xg = new Node_RuleVariable("?x", 0);
122         Node_RuleVariable yg = new Node_RuleVariable("?y", 1);
123         Node_RuleVariable zg = new Node_RuleVariable("?z", 2);
124         
125         Node_RuleVariable xh = new Node_RuleVariable("?x", 0);
126         Node_RuleVariable yh = new Node_RuleVariable("?y", 1);
127         Node_RuleVariable zh = new Node_RuleVariable("?z", 2);
128         
129         TriplePattern g1 = new TriplePattern(xg, p, yg);
130         TriplePattern g2 = new TriplePattern(xg, p, xg);
131         TriplePattern g3 = new TriplePattern( a, p, xg);
132         TriplePattern g4 = new TriplePattern( a, p, b);
133         
134         TriplePattern h1 = new TriplePattern(xh, p, yh);
135         TriplePattern h2 = new TriplePattern(xh, p, xh);
136         TriplePattern h3 = new TriplePattern( a, p, xh);
137         TriplePattern h4 = new TriplePattern( a, p, b);
138         TriplePattern h5 = new TriplePattern(xh, p, a);
139         
140         doTestUnify(g1, h1, true, new Node[] {null, null});
141         doTestUnify(g1, h2, true, new Node[] {null, null});
142         doTestUnify(g1, h3, true, new Node[] {null, null});
143         doTestUnify(g1, h4, true, new Node[] {null, null});
144         doTestUnify(g1, h5, true, new Node[] {null, null});
145         
146         doTestUnify(g2, h1, true, new Node[] {null, xh});
147         doTestUnify(g2, h2, true, new Node[] {null, null});
148         doTestUnify(g2, h3, true, new Node[] {a, null});
149         doTestUnify(g2, h4, false, null);
150         doTestUnify(g2, h5, true, new Node[] {a, null});
151         
152         doTestUnify(g3, h1, true, new Node[] {a, null});
153         doTestUnify(g3, h2, true, new Node[] {a, null});
154         doTestUnify(g3, h3, true, new Node[] {null, null});
155         doTestUnify(g3, h4, true, new Node[] {null, null});
156         doTestUnify(g3, h5, true, new Node[] {a, null});
157         
158         doTestUnify(g4, h1, true, new Node[] {a, b});
159         doTestUnify(g4, h2, false, null);
160         doTestUnify(g4, h3, true, new Node[] {b});
161         doTestUnify(g4, h4, true, null);
162         doTestUnify(g4, h5, false, null);
163         
164         // Recursive case
165
doTestUnify(h1, h1, true, new Node[] {null, null});
166         
167         // Wildcard case
168
doTestUnify(new TriplePattern(null, null, null), h2, true, new Node[] {null, null});
169
170         // Test functor cases as well!
171
TriplePattern gf = new TriplePattern(xg, p,
172                                 Functor.makeFunctorNode("f", new Node[]{xg, b}));
173         TriplePattern hf1 = new TriplePattern(yh, p,
174                                 Functor.makeFunctorNode("f", new Node[]{zh, b}));
175         TriplePattern hf2 = new TriplePattern(yh, p,
176                                 Functor.makeFunctorNode("f", new Node[]{a, yh}));
177         TriplePattern hf3 = new TriplePattern(yh, p,
178                                 Functor.makeFunctorNode("f", new Node[]{b, yh}));
179         doTestUnify(gf, hf1, true, new Node[] {null, null, yh});
180         doTestUnify(gf, hf2, false, null);
181         doTestUnify(gf, hf3, true, new Node[] {null, b});
182         
183         // Check binding environment use
184
BindingVector env = BindingVector.unify(g2, h1);
185         env.bind(xh, c);
186         assertEquals(env.getBinding(yh), c);
187         env = BindingVector.unify(g2, h1);
188         env.bind(yh, c);
189         assertEquals(env.getBinding(xh), c);
190     }
191     
192     /**
193      * Helper for testUnify.
194      * @param goal goal triple pattern
195      * @param head head triple pattern
196      * @param succeed whether match should succeeed or fail
197      * @param env list list of expected environment bindings
198      *
199      */

200     private void doTestUnify(TriplePattern goal, TriplePattern head, boolean succeed, Node[] env) {
201         BindingVector result = BindingVector.unify(goal, head);
202         if (succeed) {
203             assertNotNull(result);
204             if (env != null) {
205                 for (int i = 0; i < env.length; i++) {
206                     Node n = result.getEnvironment()[i];
207                     if (env[i] != null) {
208                         assertEquals(env[i], n);
209                     } else {
210                         assertNull(n);
211                     }
212                 }
213             }
214         } else {
215             assertNull(result);
216         }
217     }
218     
219     /**
220      * Check that a reasoner over an empty rule set accesses
221      * the raw data successfully.
222      */

223     public void testListData() {
224         Graph data = new GraphMem();
225         for (int i = 0; i < dataElts.length; i++) {
226             data.add(dataElts[i]);
227         }
228         Graph schema = new GraphMem();
229         schema.add(new Triple(c, p, c));
230         
231         // Case of schema and data but no rule axioms
232
Reasoner reasoner = createReasoner(new ArrayList());
233         InfGraph infgraph = reasoner.bindSchema(schema).bind(data);
234         TestUtil.assertIteratorValues(this,
235             infgraph.find(null, null, null),
236             new Object JavaDoc[] {
237                 new Triple(p, sP, q),
238                 new Triple(q, sP, r),
239                 new Triple(a, p, b),
240                 new Triple(c, p, c)});
241                 
242         // Case of data and rule axioms but no schema
243
List rules = Rule.parseRules("-> (d p d).");
244         reasoner = createReasoner(rules);
245         infgraph = reasoner.bind(data);
246         TestUtil.assertIteratorValues(this,
247             infgraph.find(null, null, null),
248             new Object JavaDoc[] {
249                 new Triple(p, sP, q),
250                 new Triple(q, sP, r),
251                 new Triple(a, p, b),
252                 new Triple(d, p, d)});
253                 
254         // Case of data and rule axioms and schema
255
infgraph = reasoner.bindSchema(schema).bind(data);
256         TestUtil.assertIteratorValues(this,
257             infgraph.find(null, null, null),
258             new Object JavaDoc[] {
259                 new Triple(p, sP, q),
260                 new Triple(q, sP, r),
261                 new Triple(a, p, b),
262                 new Triple(c, p, c),
263                 new Triple(d, p, d)});
264                 
265     }
266    
267     /**
268      * Test basic rule operations - simple AND rule
269      */

270     public void testBaseRules1() {
271         List rules = Rule.parseRules("[r1: (?a r ?c) <- (?a p ?b),(?b p ?c)]");
272         Graph data = new GraphMem();
273         data.add(new Triple(a, p, b));
274         data.add(new Triple(b, p, c));
275         data.add(new Triple(b, p, d));
276         Reasoner reasoner = createReasoner(rules);
277         InfGraph infgraph = reasoner.bind(data);
278         TestUtil.assertIteratorValues(this,
279             infgraph.find(null, r, null),
280             new Object JavaDoc[] {
281                 new Triple(a, r, c),
282                 new Triple(a, r, d)
283             } );
284     }
285    
286     /**
287      * Test basic rule operations - simple OR rule
288      */

289     public void testBaseRules2() {
290         List rules = Rule.parseRules(
291                 "[r1: (?a r ?b) <- (?a p ?b)]" +
292                 "[r2: (?a r ?b) <- (?a q ?b)]" +
293                 "[r3: (?a r ?b) <- (?a s ?c), (?c s ?b)]"
294         );
295         Graph data = new GraphMem();
296         data.add(new Triple(a, p, b));
297         data.add(new Triple(b, q, c));
298         data.add(new Triple(a, s, b));
299         data.add(new Triple(b, s, d));
300         Reasoner reasoner = createReasoner(rules);
301         InfGraph infgraph = reasoner.bind(data);
302         TestUtil.assertIteratorValues(this,
303             infgraph.find(null, r, null),
304             new Object JavaDoc[] {
305                 new Triple(a, r, b),
306                 new Triple(b, r, c),
307                 new Triple(a, r, d)
308             } );
309     }
310    
311     /**
312      * Test basic rule operations - simple OR rule with chaining
313      */

314     public void testBaseRules2b() {
315         List rules = Rule.parseRules(
316                 "[r1: (?a r ?b) <- (?a p ?b)]" +
317                 "[r2: (?a r ?b) <- (?a q ?b)]" +
318                 "[r3: (?a r ?b) <- (?a t ?c), (?c t ?b)]" +
319                 "[r4: (?a t ?b) <- (?a s ?b)]"
320         );
321         Graph data = new GraphMem();
322         data.add(new Triple(a, p, b));
323         data.add(new Triple(b, q, c));
324         data.add(new Triple(a, s, b));
325         data.add(new Triple(b, s, d));
326         Reasoner reasoner = createReasoner(rules);
327         InfGraph infgraph = reasoner.bind(data);
328         TestUtil.assertIteratorValues(this,
329             infgraph.find(null, r, null),
330             new Object JavaDoc[] {
331                 new Triple(a, r, b),
332                 new Triple(b, r, c),
333                 new Triple(a, r, d)
334             } );
335     }
336     
337     /**
338      * Test basic rule operations - simple AND rule check with tabling.
339      */

340     public void testBaseRules3() {
341         List rules = Rule.parseRules("[rule: (?a rdfs:subPropertyOf ?c) <- (?a rdfs:subPropertyOf ?b),(?b rdfs:subPropertyOf ?c)]");
342         Reasoner reasoner = createReasoner(rules);
343         Graph data = new GraphMem();
344         data.add(new Triple(p, sP, q) );
345         data.add(new Triple(q, sP, r) );
346         data.add(new Triple(p, sP, s) );
347         data.add(new Triple(s, sP, t) );
348         data.add(new Triple(a, p, b) );
349         InfGraph infgraph = reasoner.bind(data);
350         TestUtil.assertIteratorValues(this,
351             infgraph.find(null, RDFS.subPropertyOf.asNode(), null),
352             new Object JavaDoc[] {
353                 new Triple(p, sP, q),
354                 new Triple(q, sP, r),
355                 new Triple(p, sP, s),
356                 new Triple(s, sP, t),
357                 new Triple(p, sP, t),
358                 new Triple(p, sP, r)
359             } );
360     }
361     
362     /**
363      * Test basic rule operations - simple AND rule check with tabling.
364      */

365     public void testBaseRules3b() {
366         List rules = Rule.parseRules("[rule: (?a rdfs:subPropertyOf ?c) <- (?a rdfs:subPropertyOf ?b),(?b rdfs:subPropertyOf ?c)]");
367         Reasoner reasoner = createReasoner(rules);
368         Graph data = new GraphMem();
369         data.add(new Triple(p, sP, q) );
370         data.add(new Triple(q, sP, r) );
371         data.add(new Triple(r, sP, t) );
372         data.add(new Triple(q, sP, s) );
373         InfGraph infgraph = reasoner.bind(data);
374         TestUtil.assertIteratorValues(this,
375             infgraph.find(null, RDFS.subPropertyOf.asNode(), null),
376             new Object JavaDoc[] {
377                 new Triple(p, sP, q),
378                 new Triple(q, sP, r),
379                 new Triple(r, sP, t),
380                 new Triple(q, sP, s),
381                 new Triple(p, sP, s),
382                 new Triple(p, sP, r),
383                 new Triple(p, sP, t),
384                 new Triple(q, sP, t),
385                 new Triple(p, sP, r)
386             } );
387     }
388
389     /**
390      * Test basic rule operations - simple AND/OR with tabling.
391      */

392     public void testBaseRules4() {
393         Graph data = new GraphMem();
394         data.add(new Triple(a, r, b));
395         data.add(new Triple(b, r, c));
396         data.add(new Triple(b, r, b));
397         data.add(new Triple(b, r, d));
398         List rules = Rule.parseRules(
399                         "[r1: (?x p ?y) <- (?x r ?y)]" +
400                         "[r2: (?x p ?z) <- (?x p ?y), (?y r ?z)]"
401                         );
402         Reasoner reasoner = createReasoner(rules);
403         InfGraph infgraph = reasoner.bind(data);
404         TestUtil.assertIteratorValues(this,
405             infgraph.find(a, p, null),
406             new Object JavaDoc[] {
407                 new Triple(a, p, b),
408                 new Triple(a, p, d),
409                 new Triple(a, p, c)
410             } );
411     }
412
413     /**
414      * Test basic rule operations - simple AND/OR with tabling.
415      */

416     public void testBaseRulesXSB1() {
417         Graph data = new GraphMem();
418         data.add(new Triple(p, c, q));
419         data.add(new Triple(q, c, r));
420         data.add(new Triple(p, d, q));
421         data.add(new Triple(q, d, r));
422         List rules = Rule.parseRules(
423             "[r1: (?x a ?y) <- (?x c ?y)]" +
424             "[r2: (?x a ?y) <- (?x b ?z), (?z c ?y)]" +
425             "[r3: (?x b ?y) <- (?x d ?y)]" +
426             "[r4: (?x b ?y) <- (?x a ?z), (?z d ?y)]"
427         );
428         Reasoner reasoner = createReasoner(rules);
429         InfGraph infgraph = reasoner.bind(data);
430         TestUtil.assertIteratorValues(this,
431             infgraph.find(p, a, null),
432             new Object JavaDoc[] {
433                 new Triple(p, a, q),
434                 new Triple(p, a, r)
435             } );
436     }
437     
438     /**
439      * Test basic functor usage.
440      */

441     public void testFunctors1() {
442         Graph data = new GraphMem();
443         data.add(new Triple(a, p, b));
444         data.add(new Triple(a, q, c));
445         List rules = Rule.parseRules(
446             "[r1: (?x r f(?y,?z)) <- (?x p ?y), (?x q ?z)]" +
447             "[r2: (?x s ?y) <- (?x r f(?y, ?z))]"
448         );
449         Reasoner reasoner = createReasoner(rules);
450         InfGraph infgraph = reasoner.bind(data);
451         TestUtil.assertIteratorValues(this,
452             infgraph.find(a, s, null),
453             new Object JavaDoc[] {
454                 new Triple(a, s, b)
455             } );
456     }
457     
458     /**
459      * Test basic functor usage.
460      */

461     public void testFunctors2() {
462         Graph data = new GraphMem();
463         data.add(new Triple(a, p, b));
464         data.add(new Triple(a, q, c));
465         data.add(new Triple(a, t, d));
466         List rules = Rule.parseRules(
467             "[r1: (?x r f(?y,?z)) <- (?x p ?y), (?x q ?z)]" +
468             "[r2: (?x s ?y) <- (?x r f(?y, ?z))]" +
469             "[r3: (?x r g(?y,?z)) <- (?x p ?y), (?x t ?z)]" +
470             "[r4: (?x s ?z) <- (?x r g(?y, ?z))]"
471         );
472         Reasoner reasoner = createReasoner(rules);
473         InfGraph infgraph = reasoner.bind(data);
474         TestUtil.assertIteratorValues(this,
475             infgraph.find(a, s, null),
476             new Object JavaDoc[] {
477                 new Triple(a, s, b),
478                 new Triple(a, s, d)
479             } );
480     }
481     
482     /**
483      * Test basic functor usage.
484      */

485     public void testFunctors3() {
486         Graph data = new GraphMem();
487         data.add(new Triple(a, s, b));
488         data.add(new Triple(a, t, c));
489         List rules = Rule.parseRules(
490             "[r1: (a q f(?x,?y)) <- (a s ?x), (a t ?y)]" +
491             "[r2: (a p ?x) <- (a q ?x)]" +
492             "[r3: (a r ?y) <- (a p f(?x, ?y))]"
493         );
494         Reasoner reasoner = createReasoner(rules);
495         InfGraph infgraph = reasoner.bind(data);
496         TestUtil.assertIteratorValues(this,
497             infgraph.find(a, r, null),
498             new Object JavaDoc[] {
499                 new Triple(a, r, c)
500             } );
501     }
502
503     /**
504      * Test basic builtin usage.
505      */

506     public void testBuiltin1() {
507         Graph data = new GraphMem();
508         List rules = Rule.parseRules(
509             "[a1: -> (a p 2) ]" +
510             "[a2: -> (a q 3) ]" +
511             "[r1: (?x r ?s) <- (?x p ?y), (?x q ?z), sum(?y, ?z, ?s)]"
512         );
513         Reasoner reasoner = createReasoner(rules);
514         InfGraph infgraph = reasoner.bind(data);
515         TestUtil.assertIteratorValues(this,
516             infgraph.find(a, r, null),
517             new Object JavaDoc[] {
518                 new Triple(a, r, Util.makeIntNode(5))
519             } );
520     }
521    
522     /**
523      * Test basic builtin usage.
524      */

525     public void testBuiltin2() {
526         Graph data = new GraphMem();
527         data.add(new Triple(a, p, b));
528         data.add(new Triple(a, q, c));
529         List rules = Rule.parseRules(
530             "[r1: (?x r ?y ) <- bound(?x), (?x p ?y) ]" +
531             "[r2: (?x r ?y) <- unbound(?x), (?x q ?y)]"
532         );
533         Reasoner reasoner = createReasoner(rules);
534         InfGraph infgraph = reasoner.bind(data);
535         TestUtil.assertIteratorValues(this,
536             infgraph.find(a, r, null),
537             new Object JavaDoc[] {
538                 new Triple(a, r, b)
539             } );
540         TestUtil.assertIteratorValues(this,
541             infgraph.find(null, r, null),
542             new Object JavaDoc[] {
543                 new Triple(a, r, c)
544             } );
545     }
546    
547     /**
548      * Test basic builtin usage.
549      */

550     public void testBuiltin3() {
551         Graph data = new GraphMem();
552         List rules = Rule.parseRules(
553             "[r1: (a p b ) <- unbound(?x) ]"
554         );
555         Reasoner reasoner = createReasoner(rules);
556         InfGraph infgraph = reasoner.bind(data);
557         TestUtil.assertIteratorValues(this,
558             infgraph.find(a, null, null),
559             new Object JavaDoc[] {
560                 new Triple(a, p, b)
561             } );
562     }
563   
564     /**
565      * Test basic ground head patterns.
566      */

567     public void testGroundHead() {
568         Graph data = new GraphMem();
569         data.add(new Triple(a, r, b));
570         List rules = Rule.parseRules(
571             "[r1: (a p b ) <- (a r b) ]"
572         );
573         Reasoner reasoner = createReasoner(rules);
574         InfGraph infgraph = reasoner.bind(data);
575         TestUtil.assertIteratorValues(this,
576             infgraph.find(a, null, null),
577             new Object JavaDoc[] {
578                 new Triple(a, p, b),
579                 new Triple(a, r, b)
580             } );
581     }
582   
583 // /**
584
// * Test multiheaded rule.
585
// */
586
// public void testMutliHead() {
587
// Graph data = new GraphMem();
588
// data.add(new Triple(a, p, b));
589
// data.add(new Triple(b, r, c));
590
// List rules = Rule.parseRules(
591
// "[r1: (?x s ?z), (?z s ?x) <- (?x p ?y) (?y r ?z) ]"
592
// );
593
// Reasoner reasoner = createReasoner(rules);
594
// InfGraph infgraph = reasoner.bind(data);
595
// TestUtil.assertIteratorValues(this,
596
// infgraph.find(null, s, null),
597
// new Object[] {
598
// new Triple(a, s, c),
599
// new Triple(c, s, a)
600
// } );
601
// }
602

603     /**
604      * Test rebind operation
605      */

606     public void testRebind() {
607         List rules = Rule.parseRules("[r1: (?a r ?c) <- (?a p ?b),(?b p ?c)]");
608         Graph data = new GraphMem();
609         data.add(new Triple(a, p, b));
610         data.add(new Triple(b, p, c));
611         data.add(new Triple(b, p, d));
612         Reasoner reasoner = createReasoner(rules);
613         InfGraph infgraph = reasoner.bind(data);
614         TestUtil.assertIteratorValues(this,
615             infgraph.find(null, r, null),
616             new Object JavaDoc[] {
617                 new Triple(a, r, c),
618                 new Triple(a, r, d)
619             } );
620         Graph ndata = new GraphMem();
621         ndata.add(new Triple(a, p, d));
622         ndata.add(new Triple(d, p, b));
623         infgraph.rebind(ndata);
624         TestUtil.assertIteratorValues(this,
625             infgraph.find(null, r, null),
626             new Object JavaDoc[] {
627                 new Triple(a, r, b)
628             } );
629
630     }
631
632     /**
633      * Test troublesome rdfs rules
634      */

635     public void testRDFSProblemsb() {
636         Graph data = new GraphMem();
637         data.add(new Triple(C1, sC, C2));
638         data.add(new Triple(C2, sC, C3));
639         data.add(new Triple(C1, ty, RDFS.Class.asNode()));
640         data.add(new Triple(C2, ty, RDFS.Class.asNode()));
641         data.add(new Triple(C3, ty, RDFS.Class.asNode()));
642         List rules = Rule.parseRules(
643         "[rdfs8: (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
644         "[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]"
645                         );
646         Reasoner reasoner = createReasoner(rules);
647         InfGraph infgraph = reasoner.bind(data);
648         TestUtil.assertIteratorValues(this,
649             infgraph.find(null, sC, null),
650             new Object JavaDoc[] {
651                 new Triple(C1, sC, C2),
652                 new Triple(C1, sC, C3),
653                 new Triple(C1, sC, C1),
654                 new Triple(C2, sC, C3),
655                 new Triple(C2, sC, C2),
656                 new Triple(C3, sC, C3),
657             } );
658     }
659
660     /**
661      * Test troublesome rdfs rules
662      */

663     public void testRDFSProblems() {
664         Graph data = new GraphMem();
665         data.add(new Triple(p, sP, q));
666         data.add(new Triple(q, sP, r));
667         data.add(new Triple(C1, sC, C2));
668         data.add(new Triple(C2, sC, C3));
669         data.add(new Triple(a, ty, C1));
670         List rules = Rule.parseRules(
671         "[rdfs8: (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
672         "[rdfs9: (?x rdfs:subClassOf ?y), (?a rdf:type ?x) -> (?a rdf:type ?y)]" +
673 // "[-> (rdf:type rdfs:range rdfs:Class)]" +
674
"[rdfs3: (?x ?p ?y), (?p rdfs:range ?c) -> (?y rdf:type ?c)]" +
675         "[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]"
676                         );
677         Reasoner reasoner = createReasoner(rules);
678         InfGraph infgraph = reasoner.bind(data);
679         TestUtil.assertIteratorValues(this,
680             infgraph.find(a, ty, null),
681             new Object JavaDoc[] {
682                 new Triple(a, ty, C1),
683                 new Triple(a, ty, C2),
684                 new Triple(a, ty, C3)
685             } );
686         TestUtil.assertIteratorValues(this,
687             infgraph.find(C1, sC, a),
688             new Object JavaDoc[] {
689             } );
690     }
691
692     /**
693      * Test complex rule head unification
694      */

695     public void testHeadUnify() {
696         Graph data = new GraphMem();
697         data.add(new Triple(c, q, d));
698         List rules = Rule.parseRules(
699             "[r1: (c r ?x) <- (?x p f(?x b))]" +
700             "[r2: (?y p f(a ?y)) <- (c q ?y)]"
701                           );
702         Reasoner reasoner = createReasoner(rules);
703         InfGraph infgraph = reasoner.bind(data);
704         TestUtil.assertIteratorValues(this,
705               infgraph.find(c, r, null), new Object JavaDoc[] { } );
706               
707         data.add(new Triple(c, q, a));
708         rules = Rule.parseRules(
709         "[r1: (c r ?x) <- (?x p f(?x a))]" +
710         "[r2: (?y p f(a ?y)) <- (c q ?y)]"
711                           );
712         reasoner = createReasoner(rules);
713         infgraph = reasoner.bind(data);
714         TestUtil.assertIteratorValues(this,
715               infgraph.find(c, r, null),
716               new Object JavaDoc[] {
717                   new Triple(c, r, a)
718               } );
719             
720         data = new GraphMem();
721         data.add(new Triple(a, q, a));
722         data.add(new Triple(a, q, b));
723         data.add(new Triple(a, q, c));
724         data.add(new Triple(b, q, d));
725         data.add(new Triple(b, q, b));
726         rules = Rule.parseRules(
727           "[r1: (c r ?x) <- (?x p ?x)]" +
728           "[r2: (?x p ?y) <- (a q ?x), (b q ?y)]"
729                           );
730         reasoner = createReasoner(rules);
731         infgraph = reasoner.bind(data);
732         TestUtil.assertIteratorValues(this,
733               infgraph.find(c, r, null),
734               new Object JavaDoc[] {
735                   new Triple(c, r, b)
736               } );
737               
738         rules = Rule.parseRules(
739           "[r1: (c r ?x) <- (?x p ?x)]" +
740           "[r2: (a p ?x) <- (a q ?x)]"
741                           );
742         reasoner = createReasoner(rules);
743         infgraph = reasoner.bind(data);
744         TestUtil.assertIteratorValues(this,
745               infgraph.find(c, r, null),
746               new Object JavaDoc[] {
747                   new Triple(c, r, a)
748               } );
749     }
750
751     /**
752      * Test restriction example
753      */

754     public void testRestriction1() {
755         Graph data = new GraphMem();
756         data.add(new Triple(a, ty, r));
757         data.add(new Triple(a, p, b));
758         data.add(new Triple(r, sC, C1));
759         data.add(new Triple(C1, ty, OWL.Restriction.asNode()));
760         data.add(new Triple(C1, OWL.onProperty.asNode(), p));
761         data.add(new Triple(C1, OWL.allValuesFrom.asNode(), c));
762         List rules = Rule.parseRules(
763     "[rdfs9: (?x rdfs:subClassOf ?y) (?a rdf:type ?x) -> (?a rdf:type ?y)]" +
764     "[restriction2: (?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:allValuesFrom ?D) -> (?C owl:equivalentClass all(?P, ?D))]" +
765     "[rs2: (?D owl:equivalentClass all(?P,?C)), (?X rdf:type ?D) -> (?X rdf:type all(?P,?C))]" +
766     "[rp4: (?X rdf:type all(?P, ?C)), (?X ?P ?Y) -> (?Y rdf:type ?C)]"
767                           );
768         Reasoner reasoner = createReasoner(rules);
769         InfGraph infgraph = reasoner.bind(data);
770         TestUtil.assertIteratorValues(this,
771               infgraph.find(b, ty, c), new Object JavaDoc[] {
772                   new Triple(b, ty, c)
773               } );
774     }
775     
776
777     /**
778      * Test restriction example. The rules are more than the minimum required
779      * to solve the query and they interact to given run away seaches if there
780      * is a problem.
781      */

782     public void testRestriction2() {
783         Graph data = new GraphMem();
784         data.add(new Triple(a, ty, OWL.Thing.asNode()));
785         data.add(new Triple(p, ty, OWL.FunctionalProperty.asNode()));
786         data.add(new Triple(c, OWL.equivalentClass.asNode(), C1));
787         data.add(new Triple(C1, ty, OWL.Restriction.asNode()));
788         data.add(new Triple(C1, OWL.onProperty.asNode(), p));
789         data.add(new Triple(C1, OWL.maxCardinality.asNode(), Util.makeIntNode(1)));
790         List rules = Rule.parseRules(
791         // these ones are required for the inference.
792
"[rdfs9: bound(?y) (?x rdfs:subClassOf ?y) (?a rdf:type ?x) -> (?a rdf:type ?y)]" +
793         "[restriction4: (?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:maxCardinality ?X) -> (?C owl:equivalentClass max(?P, ?X))]" +
794         "[restrictionProc11: (?P rdf:type owl:FunctionalProperty), (?X rdf:type owl:Thing) -> (?X rdf:type max(?P, 1))]" +
795 // "[equivalentClass1: (?P owl:equivalentClass ?Q) -> (?P rdfs:subClassOf ?Q), (?Q rdfs:subClassOf ?P) ]" +
796
"[equivalentClass1: (?P owl:equivalentClass ?Q) -> (?P rdfs:subClassOf ?Q) ]" +
797         "[equivalentClass1: (?P owl:equivalentClass ?Q) -> (?Q rdfs:subClassOf ?P) ]" +
798         "[restrictionSubclass1: bound(?D) (?D owl:equivalentClass ?R), isFunctor(?R) (?X rdf:type ?R)-> (?X rdf:type ?D)]" +
799          // these ones are noise which can cause run aways or failures if there are bugs
800
"[rdfs8: unbound(?c) (?a rdfs:subClassOf ?b) (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
801         "[rdfs8: bound(?c) (?b rdfs:subClassOf ?c) (?a rdfs:subClassOf ?b) -> (?a rdfs:subClassOf ?c)]" +
802         "[rdfs9: unbound(?y) (?a rdf:type ?x) (?x rdfs:subClassOf ?y) -> (?a rdf:type ?y)]" +
803         "[-> (rdf:type rdfs:range rdfs:Class)]" +
804         "[rdfs3: bound(?c) (?p rdfs:range ?c) (?x ?p ?y) -> (?y rdf:type ?c)]" +
805         "[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]" +
806         "[restrictionProc13: (owl:Thing rdfs:subClassOf all(?P, ?C)) -> (?P rdfs:range ?C)]" +
807         "[restrictionSubclass1: unbound(?D) (?X rdf:type ?R), isFunctor(?R) (?D owl:equivalentClass ?R) -> (?X rdf:type ?D)]" +
808         "[restrictionSubclass2: bound(?R), isFunctor(?R), (?D owl:equivalentClass ?R),(?X rdf:type ?D) -> (?X rdf:type ?R)]" +
809         "[restrictionSubclass2: unbound(?R), (?X rdf:type ?D), (?D owl:equivalentClass ?R) isFunctor(?R) -> (?X rdf:type ?R)]" +
810                        "" );
811         Reasoner reasoner = createReasoner(rules);
812         InfGraph infgraph = reasoner.bind(data);
813         TestUtil.assertIteratorValues(this,
814               infgraph.find(a, ty, C1), new Object JavaDoc[] {
815                   new Triple(a, ty, C1)
816               } );
817         TestUtil.assertIteratorValues(this,
818               infgraph.find(a, ty, c), new Object JavaDoc[] {
819                   new Triple(a, ty, c)
820               } );
821     }
822
823     /**
824      * Test restriction example
825      */

826     public void testRestriction3() {
827         Graph data = new GraphMem();
828         data.add(new Triple(a, ty, r));
829         data.add(new Triple(r, sC, C1));
830         data.add(new Triple(C1, ty, OWL.Restriction.asNode()));
831         data.add(new Triple(C1, OWL.onProperty.asNode(), p));
832         data.add(new Triple(C1, OWL.allValuesFrom.asNode(), c));
833         List rules = Rule.parseRules(
834         "[-> (rdfs:subClassOf rdfs:range rdfs:Class)]" +
835 // "[-> (owl:Class rdfs:subClassOf rdfs:Class)]" +
836
"[rdfs3: bound(?c) (?p rdfs:range ?c) (?x ?p ?y) -> (?y rdf:type ?c)]" +
837         "[rdfs3: unbound(?c) (?x ?p ?y), (?p rdfs:range ?c) -> (?y rdf:type ?c)]" +
838         "[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]" +
839         "[rdfs8: (?a rdfs:subClassOf ?b) (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
840         "[restrictionProc4b: bound(?Y) (?X ?P ?Y), notEqual(?P, rdf:type), (?X rdf:type all(?P, ?C)),-> (?Y rdf:type ?C)]" +
841         "[restrictionProc4b: unbound(?Y), (?X rdf:type all(?P, ?C)), (?X ?P ?Y), notEqual(?P, rdf:type),-> (?Y rdf:type ?C)]" +
842                        "" );
843         Reasoner reasoner = createReasoner(rules);
844         InfGraph infgraph = reasoner.bind(data);
845         TestUtil.assertIteratorValues(this,
846               infgraph.find(null, ty, c), new Object JavaDoc[] {
847               } );
848     }
849
850     /**
851      * Test close and halt operation.
852      */

853     public void testClose() {
854         Graph data = new GraphMem();
855         data.add(new Triple(p, sP, q));
856         data.add(new Triple(q, sP, r));
857         data.add(new Triple(C1, sC, C2));
858         data.add(new Triple(C2, sC, C3));
859         data.add(new Triple(a, ty, C1));
860         data.add(new Triple(ty, RDFS.range.asNode(), RDFS.Class.asNode()));
861         List rules = Rule.parseRules(
862         "[rdfs8: (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
863         "[rdfs9: (?x rdfs:subClassOf ?y), (?a rdf:type ?x) -> (?a rdf:type ?y)]" +
864 // "[-> (rdf:type rdfs:range rdfs:Class)]" +
865
"[rdfs3: (?x ?p ?y), (?p rdfs:range ?c) -> (?y rdf:type ?c)]" +
866         "[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]"
867                         );
868         Reasoner reasoner = createReasoner(rules);
869         InfGraph infgraph = reasoner.bind(data);
870         // Get just one result
871
ExtendedIterator it = infgraph.find(a, ty, null);
872         Triple result = (Triple)it.next();
873         assertEquals(result.getSubject(), a);
874         assertEquals(result.getPredicate(), ty);
875         it.close();
876         // Make sure if we start again we get the full listing.
877
TestUtil.assertIteratorValues(this,
878             infgraph.find(a, ty, null),
879             new Object JavaDoc[] {
880                 new Triple(a, ty, C1),
881                 new Triple(a, ty, C2),
882                 new Triple(a, ty, C3)
883             } );
884     }
885
886     /**
887      * Test problematic rdfs case
888      */

889     public void testBug1() throws IOException JavaDoc {
890         Graph data = new GraphMem();
891         Node p = Node.createURI("http://www.hpl.hp.com/semweb/2003/eg#p");
892         Node r = Node.createURI("http://www.hpl.hp.com/semweb/2003/eg#r");
893         Node C1 = Node.createURI("http://www.hpl.hp.com/semweb/2003/eg#C1");
894         data.add(new Triple(a, p, b));
895         List rules = Rule.parseRules(Util.loadRuleParserFromResourceFile("testing/reasoners/bugs/rdfs-error1.brules"));
896         Reasoner reasoner = createReasoner(rules);
897         InfGraph infgraph = reasoner.bind(data);
898         TestUtil.assertIteratorValues(this,
899             infgraph.find(b, ty, C1),
900             new Object JavaDoc[] {
901                 new Triple(b, ty, C1)
902             } );
903         
904     }
905     
906 }
907
908
909
910 /*
911     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
912     All rights reserved.
913
914     Redistribution and use in source and binary forms, with or without
915     modification, are permitted provided that the following conditions
916     are met:
917
918     1. Redistributions of source code must retain the above copyright
919        notice, this list of conditions and the following disclaimer.
920
921     2. Redistributions in binary form must reproduce the above copyright
922        notice, this list of conditions and the following disclaimer in the
923        documentation and/or other materials provided with the distribution.
924
925     3. The name of the author may not be used to endorse or promote products
926        derived from this software without specific prior written permission.
927
928     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
929     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
930     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
931     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
932     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
933     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
934     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
935     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
936     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
937     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
938 */
Popular Tags