KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 package com.hp.hpl.jena.reasoner.rulesys.test;
11
12 import java.util.*;
13
14 import com.hp.hpl.jena.graph.*;
15 import com.hp.hpl.jena.mem.GraphMem;
16 import com.hp.hpl.jena.reasoner.*;
17 import com.hp.hpl.jena.reasoner.rulesys.*;
18 import com.hp.hpl.jena.reasoner.test.TestUtil;
19 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
20 import com.hp.hpl.jena.vocabulary.*;
21
22 import java.io.*;
23
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 /**
28  * Early test cases for the LP version of the backward chaining system.
29  * <p>
30  * To be moved to a test directory once the code is working.
31  * </p>
32  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
33  * @version $Revision: 1.10 $ on $Date: 2005/02/21 12:18:09 $
34  */

35 public class TestBasicLP extends TestCase {
36     
37     // Useful constants
38
Node p = Node.createURI("p");
39     Node q = Node.createURI("q");
40     Node r = Node.createURI("r");
41     Node s = Node.createURI("s");
42     Node t = Node.createURI("t");
43     Node u = Node.createURI("u");
44     Node a = Node.createURI("a");
45     Node b = Node.createURI("b");
46     Node c = Node.createURI("c");
47     Node d = Node.createURI("d");
48     Node e = Node.createURI("e");
49     Node C1 = Node.createURI("C1");
50     Node C2 = Node.createURI("C2");
51     Node C3 = Node.createURI("C3");
52     Node C4 = Node.createURI("C4");
53     Node D1 = Node.createURI("D1");
54     Node D2 = Node.createURI("D2");
55     Node D3 = Node.createURI("D3");
56     Node sP = RDFS.subPropertyOf.getNode();
57     Node sC = RDFS.subClassOf.getNode();
58     Node ty = RDF.type.getNode();
59
60     /**
61      * Boilerplate for junit
62      */

63     public TestBasicLP( String JavaDoc name ) {
64         super( name );
65     }
66     
67     /**
68      * Boilerplate for junit.
69      * This is its own test suite
70      */

71     public static TestSuite suite() {
72         return new TestSuite( TestBasicLP.class );
73         
74 // TestSuite suite = new TestSuite();
75
// suite.addTest(new TestBasicLP( "testRuleDerivations" ));
76
// return suite;
77
}
78    
79     /**
80      * Return an inference graph working over the given rule set and raw data.
81      * Can be overridden by subclasses of this test class.
82      * @param rules the rule set to use
83      * @param data the graph of triples to process
84      */

85     public InfGraph makeInfGraph(List rules, Graph data) {
86         FBRuleReasoner reasoner = new FBRuleReasoner(rules);
87         FBRuleInfGraph infgraph = (FBRuleInfGraph) reasoner.bind(data);
88 // infgraph.setTraceOn(true);
89
return infgraph;
90     }
91    
92     /**
93      * Return an inference graph working over the given rule set and raw data.
94      * Can be overridden by subclasses of this test class.
95      * @param rules the rule set to use
96      * @param data the graph of triples to process
97      * @param tabled an array of predicates that should be tabled
98      */

99     public InfGraph makeInfGraph(List rules, Graph data, Node[] tabled) {
100         FBRuleReasoner reasoner = new FBRuleReasoner(rules);
101         FBRuleInfGraph infgraph = (FBRuleInfGraph) reasoner.bind(data);
102         for (int i = 0; i < tabled.length; i++) {
103             infgraph.setTabled(tabled[i]);
104         }
105         return infgraph;
106     }
107     
108     /**
109      * Test basic rule operations - lookup, no matching rules
110      */

111     public void testBaseRules1() {
112         doBasicTest("[r1: (?x r c) <- (?x p b)]",
113                      new Triple(Node.ANY, p, b),
114                      new Object JavaDoc[] {
115                         new Triple(a, p, b)
116                      } );
117     }
118    
119     /**
120      * Test basic rule operations - simple chain rule
121      */

122     public void testBaseRules2() {
123         doBasicTest("[r1: (?x r c) <- (?x p b)]",
124                      new Triple(Node.ANY, r, c),
125                      new Object JavaDoc[] {
126                         new Triple(a, r, c)
127                      } );
128     }
129    
130     /**
131      * Test basic rule operations - chain rule with head unification
132      */

133     public void testBaseRules3() {
134         doBasicTest("[r1: (?x r ?x) <- (?x p b)]",
135                      new Triple(Node.ANY, r, a),
136                      new Object JavaDoc[] {
137                         new Triple(a, r, a)
138                      } );
139     }
140     
141     /**
142      * Test basic rule operations - rule with head unification, non-temp var
143      */

144     public void testBaseRules4() {
145         doBasicTest("[r1: (?x r ?x) <- (?y p b), (?x p b)]",
146                      new Triple(Node.ANY, r, a),
147                      new Object JavaDoc[] {
148                         new Triple(a, r, a)
149                      } );
150     }
151     
152     /**
153      * Test basic rule operations - simple cascade
154      */

155     public void testBaseRules5() {
156         doBasicTest("[r1: (?x q ?y) <- (?x r ?y)(?y s ?x)]" +
157                     "[r2: (?x r ?y) <- (?x p ?y)]" +
158                     "[r3: (?x s ?y) <- (?y p ?x)]",
159                      new Triple(Node.ANY, q, Node.ANY),
160                      new Object JavaDoc[] {
161                         new Triple(a, q, b)
162                      } );
163     }
164    
165     /**
166      * Test basic rule operations - chain rule which will fail at head time
167      */

168     public void testBaseRules6() {
169         doBasicTest("[r1: (?x r ?x) <- (?x p b)]",
170                      new Triple(a, r, b),
171                      new Object JavaDoc[] {
172                      } );
173     }
174    
175     /**
176      * Test basic rule operations - chain rule which will fail in search
177      */

178     public void testBaseRules7() {
179         doBasicTest("[r1: (?x r ?y) <- (?x p c)]",
180                      new Triple(a, r, b),
181                      new Object JavaDoc[] {
182                      } );
183     }
184     
185     /**
186      * Test basic rule operations - simple chain
187      */

188     public void testBaseRules8() {
189         doBasicTest("[r1: (?x q ?y) <- (?x r ?y)]" +
190                     "[r2: (?x r ?y) <- (?x p ?y)]",
191                      new Triple(Node.ANY, q, Node.ANY),
192                      new Object JavaDoc[] {
193                         new Triple(a, q, b)
194                      } );
195     }
196     
197     /**
198      * Test basic rule operations - simple chain
199      */

200     public void testBaseRules9() {
201         doBasicTest("[r1: (?x q ?y) <- (?x r ?y)]" +
202                     "[r2: (?x r ?y) <- (?y p ?x)]",
203                      new Triple(Node.ANY, q, Node.ANY),
204                      new Object JavaDoc[] {
205                         new Triple(b, q, a)
206                      } );
207     }
208     
209     /**
210      * Test backtracking - simple triple query.
211      */

212     public void testBacktrack1() {
213         doTest("[r1: (?x r ?y) <- (?x p ?y)]",
214                 new Triple[] {
215                     new Triple(a, p, b),
216                     new Triple(a, p, c),
217                     new Triple(a, p, d)
218                 },
219                 new Triple(a, p, Node.ANY),
220                 new Object JavaDoc[] {
221                     new Triple(a, p, b),
222                     new Triple(a, p, c),
223                     new Triple(a, p, d)
224                 } );
225     }
226     
227     /**
228      * Test backtracking - chain to simple triple query.
229      */

230     public void testBacktrack2() {
231         doTest("[r1: (?x r ?y) <- (?x p ?y)]",
232                 new Triple[] {
233                     new Triple(a, p, b),
234                     new Triple(a, p, c),
235                     new Triple(a, p, d)
236                 },
237                 new Triple(a, r, Node.ANY),
238                 new Object JavaDoc[] {
239                     new Triple(a, r, b),
240                     new Triple(a, r, c),
241                     new Triple(a, r, d)
242                 } );
243     }
244     
245     /**
246      * Test backtracking - simple choice point
247      */

248     public void testBacktrack3() {
249         doTest("[r1: (?x r C1) <- (?x p b)]" +
250                "[r2: (?x r C2) <- (?x p b)]" +
251                "[r3: (?x r C3) <- (?x p b)]",
252                 new Triple[] {
253                     new Triple(a, p, b)
254                 },
255                 new Triple(a, r, Node.ANY),
256                 new Object JavaDoc[] {
257                     new Triple(a, r, C1),
258                     new Triple(a, r, C2),
259                     new Triple(a, r, C3)
260                 } );
261     }
262     
263     /**
264      * Test backtracking - nested choice point
265      */

266     public void testBacktrack4() {
267         doTest("[r1: (?x r C1) <- (?x p b)]" +
268                "[r2: (?x r C2) <- (?x p b)]" +
269                "[r3: (?x r C3) <- (?x p b)]" +
270                "[r4: (?x s ?z) <- (?x p ?w), (?x r ?y) (?y p ?z)]",
271                 new Triple[] {
272                     new Triple(a, p, b),
273                     new Triple(C1, p, D1),
274                     new Triple(C2, p, D2),
275                     new Triple(C3, p, D3)
276                 },
277                 new Triple(a, s, Node.ANY),
278                 new Object JavaDoc[] {
279                     new Triple(a, s, D1),
280                     new Triple(a, s, D2),
281                     new Triple(a, s, D3)
282                 } );
283     }
284     
285     /**
286      * Test backtracking - nested choice point with multiple triple matches
287      */

288     public void testBacktrack5() {
289         doTest("[r1: (?x r C3) <- (C1 p ?x)]" +
290                "[r2: (?x r C2) <- (C2 p ?x)]" +
291                "[r4: (?x s ?y) <- (?x r ?y)]",
292                 new Triple[] {
293                     new Triple(C1, p, D1),
294                     new Triple(C1, p, a),
295                     new Triple(C2, p, D2),
296                     new Triple(C2, p, b)
297                 },
298                 new Triple(Node.ANY, s, Node.ANY),
299                 new Object JavaDoc[] {
300                     new Triple(D1, s, C3),
301                     new Triple(a, s, C3),
302                     new Triple(D2, s, C2),
303                     new Triple(b, s, C2)
304                 } );
305     }
306     
307     /**
308      * Test backtracking - nested choice point with multiple triple matches, and
309      * checking temp v. permanent variable usage
310      */

311     public void testBacktrack6() {
312         doTest("[r1: (?x r C1) <- (?x p a)]" +
313                "[r2: (?x r C2) <- (?x p b)]" +
314                "[r3: (?x q C1) <- (?x p b)]" +
315                "[r4: (?x q C2) <- (?x p a)]" +
316                "[r5: (?x s ?y) <- (?x r ?y) (?x q ?y)]",
317                 new Triple[] {
318                     new Triple(D1, p, a),
319                     new Triple(D2, p, a),
320                     new Triple(D2, p, b),
321                     new Triple(D3, p, b)
322                 },
323                 new Triple(Node.ANY, s, Node.ANY),
324                 new Object JavaDoc[] {
325                     new Triple(D2, s, C1),
326                     new Triple(D2, s, C2),
327                 } );
328     }
329     
330     /**
331      * Test backtracking - nested choice point with simple triple matches
332      */

333     public void testBacktrack7() {
334         doTest( "[r1: (?x r C1) <- (?x p b)]" +
335                 "[r2: (?x r C2) <- (?x p b)]" +
336                 "[r3: (?x r C3) <- (?x p b)]" +
337                 "[r3: (?x r D1) <- (?x p b)]" +
338                 "[r4: (?x q C2) <- (?x p b)]" +
339                 "[r5: (?x q C3) <- (?x p b)]" +
340                 "[r5: (?x q D1) <- (?x p b)]" +
341                 "[r6: (?x t C1) <- (?x p b)]" +
342                 "[r7: (?x t C2) <- (?x p b)]" +
343                 "[r8: (?x t C3) <- (?x p b)]" +
344                 "[r9: (?x s ?y) <- (?x r ?y) (?x q ?y) (?x t ?y)]",
345                 new Triple[] {
346                     new Triple(a, p, b),
347                 },
348                 new Triple(Node.ANY, s, Node.ANY),
349                 new Object JavaDoc[] {
350                     new Triple(a, s, C2),
351                     new Triple(a, s, C3),
352                 } );
353     }
354     
355     /**
356      * Test backtracking - nested choice point with simple triple matches,
357      * permanent vars but used just once in body
358      */

359     public void testBacktrack8() {
360         doTest( "[r1: (?x r C1) <- (?x p b)]" +
361                 "[r2: (?x r C2) <- (?x p b)]" +
362                 "[r3: (?x r C3) <- (?x p b)]" +
363                 "[r3: (?x r D1) <- (?x p b)]" +
364                 "[r4: (?x q C2) <- (?x p b)]" +
365                 "[r5: (?x q C3) <- (?x p b)]" +
366                 "[r5: (?x q D1) <- (?x p b)]" +
367                 "[r6: (?x t C1) <- (?x p b)]" +
368                 "[r7: (?x t C2) <- (?x p b)]" +
369                 "[r8: (?x t C3) <- (?x p b)]" +
370                 "[r9: (?x s ?y) <- (?w r C1) (?x q ?y) (?w t C1)]",
371                 new Triple[] {
372                     new Triple(a, p, b),
373                 },
374                 new Triple(Node.ANY, s, Node.ANY),
375                 new Object JavaDoc[] {
376                     new Triple(a, s, D1),
377                     new Triple(a, s, C2),
378                     new Triple(a, s, C3),
379                 } );
380     }
381    
382     /**
383      * Test backtracking - multiple triple matches
384      */

385     public void testBacktrack9() {
386         doTest("[r1: (?x s ?y) <- (?x r ?y) (?x q ?y)]",
387                 new Triple[] {
388                     new Triple(a, r, D1),
389                     new Triple(a, r, D2),
390                     new Triple(a, r, D3),
391                     new Triple(b, r, D2),
392                     new Triple(a, q, D2),
393                     new Triple(b, q, D2),
394                     new Triple(b, q, D3),
395                 },
396                 new Triple(Node.ANY, s, Node.ANY),
397                 new Object JavaDoc[] {
398                     new Triple(a, s, D2),
399                     new Triple(b, s, D2),
400                 } );
401     }
402    
403     /**
404      * Test backtracking - multiple triple matches
405      */

406     public void testBacktrack10() {
407         doTest("[r1: (?x s ?y) <- (?x r ?y) (?x q ?z), equal(?y, ?z)(?x, p, ?y)]" +
408         "[(a p D1) <- ]" +
409         "[(a p D2) <- ]" +
410         "[(b p D1) <- ]",
411                 new Triple[] {
412                     new Triple(a, r, D1),
413                     new Triple(a, r, D2),
414                     new Triple(a, r, D3),
415                     new Triple(b, r, D2),
416                     new Triple(a, q, D2),
417                     new Triple(b, q, D2),
418                     new Triple(b, q, D3),
419                 },
420                 new Triple(Node.ANY, s, Node.ANY),
421                 new Object JavaDoc[] {
422                     new Triple(a, s, D2),
423                 } );
424     }
425     
426     /**
427      * Test clause order is right
428      */

429     public void testClauseOrder() {
430         List rules = Rule.parseRules(
431             "[r1: (?x r C1) <- (?x p b)]" +
432             "[r1: (?x r C2) <- (?x p b)]" +
433             "[r2: (?x r C3) <- (?x r C3) (?x p b)]");
434         Graph data = new GraphMem();
435         data.add(new Triple(a, p, b));
436         InfGraph infgraph = makeInfGraph(rules, data);
437         ExtendedIterator i = infgraph.find(Node.ANY, r, Node.ANY);
438         assertTrue(i.hasNext());
439         assertEquals(i.next(), new Triple(a, r, C1));
440         i.close();
441     }
442     
443     /**
444      * Test axioms work.
445      */

446     public void testAxioms() {
447         doTest("[a1: -> (a r C1) ]" +
448                "[a2: -> (a r C2) ]" +
449                "[a3: (b r C1) <- ]" +
450                "[r1: (?x s ?y) <- (?x r ?y)]",
451                 new Triple[] {
452                 },
453                 new Triple(Node.ANY, s, Node.ANY),
454                 new Object JavaDoc[] {
455                     new Triple(a, s, C1),
456                     new Triple(a, s, C2),
457                     new Triple(b, s, C1),
458                 } );
459     }
460
461     /**
462      * Test nested invocation of rules with permanent vars
463      */

464     public void testNestedPvars() {
465         doTest("[r1: (?x r ?y) <- (?x p ?z) (?z q ?y)]" +
466                "[r1: (?y t ?x) <- (?x p ?z) (?z q ?y)]" +
467                "[r3: (?x s ?y) <- (?x r ?y) (?y t ?x)]",
468                 new Triple[] {
469                     new Triple(a, p, C1),
470                     new Triple(a, p, C2),
471                     new Triple(a, p, C3),
472                     new Triple(C2, q, b),
473                     new Triple(C3, q, c),
474                     new Triple(D1, q, D2),
475                 },
476                 new Triple(Node.ANY, s, Node.ANY),
477                 new Object JavaDoc[] {
478                     new Triple(a, s, b),
479                     new Triple(a, s, c),
480                 } );
481     }
482     
483     /**
484      * Test simple invocation of a builtin
485      */

486     public void testBuiltin1() {
487         doTest("[r1: (?x r ?y) <- (?x p ?v), sum(?v 2 ?y)]",
488                 new Triple[] {
489                     new Triple(a, p, Util.makeIntNode(3)),
490                     new Triple(b, p, Util.makeIntNode(4))
491                 },
492                 new Triple(Node.ANY, r, Node.ANY),
493                 new Object JavaDoc[] {
494                     new Triple(a, r, Util.makeIntNode(5)),
495                     new Triple(b, r, Util.makeIntNode(6)),
496                 } );
497     }
498     
499     
500     /**
501      * Test simple invocation of a builtin
502      */

503     public void testBuiltin2() {
504         doTest("[r1: (?x r C1) <- (?x p ?v), lessThan(?v 3)]",
505                 new Triple[] {
506                     new Triple(a, p, Util.makeIntNode(1)),
507                     new Triple(b, p, Util.makeIntNode(2)),
508                     new Triple(c, p, Util.makeIntNode(3))
509                 },
510                 new Triple(Node.ANY, r, Node.ANY),
511                 new Object JavaDoc[] {
512                     new Triple(a, r, C1),
513                     new Triple(b, r, C1),
514                 } );
515     }
516     
517     /**
518      * Test wildcard predicate usage - simple triple search.
519      * Rules look odd because we have to hack around the recursive loops.
520      */

521     public void testWildPredicate1() {
522         doTest("[r1: (b r ?y) <- (a ?y ?v)]",
523                 new Triple[] {
524                     new Triple(a, p, C1),
525                     new Triple(a, q, C2),
526                     new Triple(a, q, C3),
527                 },
528                 new Triple(b, r, Node.ANY),
529                 new Object JavaDoc[] {
530                     new Triple(b, r, p),
531                     new Triple(b, r, q)
532                 } );
533     }
534     
535     /**
536      * Test wildcard predicate usage - combind triple search and multiclause matching.
537      * Rules look odd because we have to hack around the recursive loops.
538      */

539     public void testWildPredicate2() {
540         doTest("[r1: (a r ?y) <- (b ?y ?v)]" +
541                 "[r2: (?x q ?y) <- (?x p ?y)]" +
542                 "[r3: (?x s C1) <- (?x p C1)]" +
543                 "[r4: (?x t C2) <- (?x p C2)]",
544                 new Triple[] {
545                     new Triple(b, p, C1),
546                     new Triple(b, q, C2),
547                     new Triple(b, q, C3),
548                     new Triple(a, p, C1),
549                     new Triple(a, p, C2),
550                     new Triple(c, p, C1),
551                 },
552                 new Triple(a, Node.ANY, Node.ANY),
553                 new Object JavaDoc[] {
554                     new Triple(a, r, p),
555                     new Triple(a, r, q),
556                     new Triple(a, q, C1),
557                     new Triple(a, q, C2),
558                     new Triple(a, s, C1),
559                     new Triple(a, t, C2),
560                     new Triple(a, p, C1),
561                     new Triple(a, p, C2),
562                     new Triple(a, r, s),
563                 } );
564     }
565     
566     /**
567      * Test wildcard predicate usage - combined triple search and multiclause matching.
568      * Rules look odd because we have to hack around the recursive loops.
569      */

570     public void testWildPredicate3() {
571         String JavaDoc rules = "[r1: (a r ?y) <- (b ?y ?v)]" +
572                 "[r2: (?x q ?y) <- (?x p ?y)]" +
573                 "[r3: (?x s C1) <- (?x p C1)]" +
574                 "[r4: (?x t ?y) <- (?x ?y C1)]";
575         Triple[] data =
576                 new Triple[] {
577                     new Triple(b, p, C1),
578                     new Triple(b, q, C2),
579                     new Triple(b, q, C3),
580                     new Triple(a, p, C1),
581                     new Triple(a, p, C2),
582                     new Triple(c, p, C1),
583                 };
584         doTest(rules, data,
585                 new Triple(a, Node.ANY, C1),
586                 new Object JavaDoc[] {
587                     new Triple(a, q, C1),
588                     new Triple(a, s, C1),
589                     new Triple(a, p, C1),
590                 } );
591         doTest(rules, data,
592                 new Triple(a, t, Node.ANY),
593                 new Object JavaDoc[] {
594                     new Triple(a, t, q),
595                     new Triple(a, t, s),
596                     new Triple(a, t, p),
597                 } );
598         doTest(rules, data,
599                 new Triple(Node.ANY, t, q),
600                 new Object JavaDoc[] {
601                     new Triple(a, t, q),
602                     new Triple(b, t, q),
603                     new Triple(c, t, q)
604                 } );
605     }
606     
607     /**
608      * Test wildcard predicate usage - wildcard in head as well
609      */

610     public void testWildPredicate4() {
611         doTest("[r1: (a ?p ?x) <- (b ?p ?x)]",
612                 new Triple[] {
613                     new Triple(b, p, C1),
614                     new Triple(b, q, C2),
615                     new Triple(b, q, C3),
616                     new Triple(c, q, d),
617                 },
618                 new Triple(a, Node.ANY, Node.ANY),
619                 new Object JavaDoc[] {
620                     new Triple(a, p, C1),
621                     new Triple(a, q, C2),
622                     new Triple(a, q, C3),
623                 } );
624     }
625
626     /**
627      * Test functor usage.
628      */

629     public void testFunctors1() {
630         String JavaDoc ruleSrc = "[r1: (?x s ?y) <- (?x p foo(?z, ?y))] ";
631         Triple[] triples =
632             new Triple[] {
633                 new Triple(a, p, Functor.makeFunctorNode("foo", new Node[] {C1, C2})),
634                 new Triple(a, p, Functor.makeFunctorNode("bar", new Node[] {C1, D1})),
635                 new Triple(b, p, Functor.makeFunctorNode("foo", new Node[] {C1, C2})),
636                 new Triple(a, p, Functor.makeFunctorNode("foo", new Node[] {C1, C3})),
637                 new Triple(a, p, D1),
638             };
639         doTest(ruleSrc, triples, new Triple(Node.ANY, s, Node.ANY),
640             new Object JavaDoc[] {
641                 new Triple(a, s, C2),
642                 new Triple(b, s, C2),
643                 new Triple(a, s, C3)
644             } );
645     }
646
647     /**
648      * Test functor usage.
649      */

650     public void testFunctors2() {
651         String JavaDoc ruleSrc = "[r1: (?x r foo(?y,?z)) <- (?x p ?y), (?x q ?z)]" +
652                "[r2: (?x s ?y) <- (?x r foo(?z, ?y))] ";
653         Triple[] triples =
654             new Triple[] {
655                 new Triple(a, p, C1),
656                 new Triple(a, p, C3),
657                 new Triple(a, q, C2),
658                 new Triple(b, p, D1),
659                 new Triple(b, q, D2),
660                 new Triple(b, q, D3),
661             };
662         doTest(ruleSrc, triples, new Triple(Node.ANY, s, Node.ANY),
663             new Object JavaDoc[] {
664                 new Triple(a, s, C2),
665                 new Triple(b, s, D2),
666                 new Triple(b, s, D3)
667             } );
668     }
669
670     /**
671      * Test functor usage.
672      */

673     public void testFunctors3() {
674         String JavaDoc ruleSrc = "[r1: (?x r foo(p,?y)) <- (?x p ?y)]" +
675                          "[r2: (?x r foo(q,?y)) <- (?x q ?y)]" +
676                         "[r3: (?x r ?y) <- (?x t ?y)] " +
677                         "[r4: (?x s ?y) <- (?x r ?y), notFunctor(?y)] " +
678                         "[r5: (?x s ?y) <- (?x r foo(?y, ?z))] ";
679         Triple[] triples =
680             new Triple[] {
681                 new Triple(a, p, C1),
682                 new Triple(b, q, D1),
683                 new Triple(b, p, D2),
684                 new Triple(c, t, d)
685             };
686         doTest(ruleSrc, triples, new Triple(Node.ANY, s, Node.ANY),
687             new Object JavaDoc[] {
688                 new Triple(a, s, p),
689                 new Triple(b, s, p),
690                 new Triple(b, s, q),
691                 new Triple(c, s, d)
692             } );
693     }
694     
695     /**
696      * Test tabled predicates. Simple chain call case.
697      */

698     public void testTabled1() {
699         doTest("[r1: (?a q ?b) <- (?a p ?b)]" +
700                "[r2: (?x r ?y) <- (?x q ?y)]",
701                 new Node[] { q },
702                 new Triple[] {
703                     new Triple(a, p, b),
704                     new Triple(b, p, c),
705                 },
706                 new Triple(Node.ANY, r, Node.ANY),
707                 new Object JavaDoc[] {
708                     new Triple(a, r, b),
709                     new Triple(b, r, c)
710                 } );
711     }
712     
713     /**
714      * Test tabled predicates. Simple transitive closure case.
715      */

716     public void testTabled2() {
717         doTest("[r1: (?a p ?c) <- (?a p ?b)(?b p ?c)]",
718                 new Node[] { p },
719                 new Triple[] {
720                     new Triple(a, p, b),
721                     new Triple(b, p, c),
722                     new Triple(b, p, d),
723                 },
724                 new Triple(Node.ANY, p, Node.ANY),
725                 new Object JavaDoc[] {
726                     new Triple(a, p, b),
727                     new Triple(b, p, c),
728                     new Triple(a, p, c),
729                     new Triple(b, p, d),
730                     new Triple(a, p, d),
731                 } );
732     }
733     
734     /**
735      * Test tabled predicates. Simple transitive closure over normal predicates
736      */

737     public void testTabled3() {
738         doTest("[r1: (?x p ?z) <- (?x p ?y), (?y p ?z)]" +
739                "[r2: (?x p ?z) <- (?x e ?z), (?z q ?z)]",
740                 new Node[] { p },
741                 new Triple[] {
742                     new Triple(a, e, b),
743                     new Triple(a, e, d),
744                     new Triple(b, e, c),
745                     new Triple(a, q, a),
746                     new Triple(b, q, b),
747                     new Triple(c, q, c),
748                 },
749                 new Triple(a, p, Node.ANY),
750                 new Object JavaDoc[] {
751                     new Triple(a, p, b),
752 // new Triple(b, p, c),
753
new Triple(a, p, c)
754                 } );
755     }
756     
757     /**
758      * Test tabled predicates. Co-routining example.
759      */

760     public void testTabled4() {
761         doTest("[r1: (?x a ?y) <- (?x c ?y)]" +
762                "[r2: (?x a ?y) <- (?x b ?z), (?z c ?y)]" +
763                "[r3: (?x b ?y) <- (?x d ?y)]" +
764                "[r4: (?x b ?y) <- (?x a ?z) (?z c ?y)]",
765                 new Node[] { a, b },
766                 new Triple[] {
767                     new Triple(p, c, q),
768                     new Triple(q, c, r),
769                     new Triple(p, d, q),
770                     new Triple(q, d, r),
771                 },
772                 new Triple(p, a, Node.ANY),
773                 new Object JavaDoc[] {
774                     new Triple(p, a, q),
775                     new Triple(p, a, r)
776                 } );
777     }
778     
779     /**
780      * Test tabled predicates. Simple transitive closure case.
781      */

782     public void testTabled5() {
783         doTest("[r1: (?a p ?c) <- (?a p ?b)(?b p ?c)]" +
784                "[r2: (?a r ?b) <- (?a q ?b)]",
785                 new Node[] { p },
786                 new Triple[] {
787                     new Triple(a, p, b),
788                     new Triple(b, p, c),
789                     new Triple(a, q, d),
790                     new Triple(c, q, d),
791                 },
792                 new Triple(a, Node.ANY, Node.ANY),
793                 new Object JavaDoc[] {
794                     new Triple(a, p, b),
795                     new Triple(a, p, c),
796                     new Triple(a, q, d),
797                     new Triple(a, r, d),
798                 } );
799     }
800    
801     /**
802      * Test tabled predicates. Simple transitive closure case, tabling set
803      * by rule base.
804      */

805     public void testTabled6() {
806         doTest("[-> table(p)] [r1: (?a p ?c) <- (?a p ?b)(?b p ?c)]",
807                 new Triple[] {
808                     new Triple(a, p, b),
809                     new Triple(b, p, c),
810                     new Triple(b, p, d),
811                 },
812                 new Triple(Node.ANY, p, Node.ANY),
813                 new Object JavaDoc[] {
814                     new Triple(a, p, b),
815                     new Triple(b, p, c),
816                     new Triple(a, p, c),
817                     new Triple(b, p, d),
818                     new Triple(a, p, d),
819                 } );
820     }
821
822     /**
823      * Test tabled calls with aliased local vars in the call.
824      */

825     public void testTabled7() {
826         doTest("[r1: (?a q ?b) <- (?a p ?b)]" +
827                "[r2: (?a q ?a) <- (?a s ?a)]" +
828                "[r2: (?a r ?z) <- (?a q ?a)]",
829                 new Node[] { },
830                 new Triple[] {
831                     new Triple(a, p, b),
832                     new Triple(c, p, c),
833                     new Triple(a, p, a),
834                     new Triple(b, s, e),
835                     new Triple(d, s, d),
836                 },
837                 new Triple(Node.ANY, r, C1),
838                 new Object JavaDoc[] {
839                     new Triple(a, r, C1),
840                     new Triple(c, r, C1),
841                     new Triple(d, r, C1),
842                 } );
843     }
844     
845     /**
846      * Test RDFS example.
847      */

848     public void testRDFS1() {
849         doTest(
850     "[ (?a rdf:type C1) <- (?a rdf:type C2) ]" +
851     "[ (?a rdf:type C2) <- (?a rdf:type C3) ]" +
852     "[ (?a rdf:type C3) <- (?a rdf:type C4) ]",
853                 new Node[] { ty },
854                 new Triple[] {
855                     new Triple(a, ty, C1),
856                     new Triple(b, ty, C2),
857                     new Triple(c, ty, C3),
858                     new Triple(d, ty, C4),
859                 },
860                 new Triple(Node.ANY, ty, C1),
861                 new Object JavaDoc[] {
862                     new Triple(a, ty, C1),
863                     new Triple(b, ty, C1),
864                     new Triple(c, ty, C1),
865                     new Triple(d, ty, C1),
866                 } );
867     }
868    
869     /**
870      * Test RDFS example - branched version
871      */

872     public void testRDFS2() {
873         doTest(
874     "[ (?a rdf:type C1) <- (?a rdf:type C2) ]" +
875     "[ (?a rdf:type C1) <- (?a rdf:type C3) ]" +
876     "[ (?a rdf:type C1) <- (?a rdf:type C4) ]",
877                 new Node[] { ty },
878                 new Triple[] {
879                     new Triple(a, ty, C1),
880                     new Triple(b, ty, C2),
881                     new Triple(c, ty, C3),
882                     new Triple(d, ty, C4),
883                 },
884                 new Triple(Node.ANY, ty, C1),
885                 new Object JavaDoc[] {
886                     new Triple(a, ty, C1),
887                     new Triple(b, ty, C1),
888                     new Triple(c, ty, C1),
889                     new Triple(d, ty, C1),
890                 } );
891     }
892
893     /**
894      * A problem from the original backchainer tests - interaction
895      * of tabling and functor expansion.
896      */

897     public void testProblem1() {
898         doTest(
899                "[r1: (a q f(?x,?y)) <- (a s ?x), (a t ?y)]" +
900                "[r2: (a p ?x) <- (a q ?x)]" +
901                "[r3: (a r ?y) <- (a p f(?x, ?y))]",
902                 new Node[] { p },
903                 new Triple[] {
904                     new Triple(a, s, b),
905                     new Triple(a, t, c)
906                 },
907                 new Triple(a, r, Node.ANY),
908                 new Object JavaDoc[] {
909                     new Triple(a, r, c)
910                 } );
911
912     }
913
914     /**
915      * A problem from the original backchainer tests - tabled closure operation.
916      */

917     public void testProblem2() {
918         String JavaDoc ruleSrc =
919         "[rdfs8: (?a rdfs:subClassOf ?c) <- (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c)]" +
920         "[rdfs7: (?a rdfs:subClassOf ?a) <- (?a rdf:type rdfs:Class)]";
921         doTest( ruleSrc,
922                 new Node[] { ty, sC },
923                 new Triple[] {
924                     new Triple(C1, sC, C2),
925                     new Triple(C2, sC, C3),
926                     new Triple(C1, ty, RDFS.Class.asNode()),
927                     new Triple(C2, ty, RDFS.Class.asNode()),
928                     new Triple(C3, ty, RDFS.Class.asNode())
929                 },
930                 new Triple(Node.ANY, sC, Node.ANY),
931                 new Object JavaDoc[] {
932                     new Triple(C1, sC, C2),
933                     new Triple(C1, sC, C3),
934                     new Triple(C1, sC, C1),
935                     new Triple(C2, sC, C3),
936                     new Triple(C2, sC, C2),
937                     new Triple(C3, sC, C3)
938                 } );
939     }
940
941     /**
942      * A problem from the original backchainer tests - bound/unbound primitives
943      */

944     public void testProblem3() {
945         String JavaDoc rules = "[r1: (?x r ?y ) <- bound(?x), (?x p ?y) ]" +
946         "[r2: (?x r ?y) <- unbound(?x), (?x q ?y)]";
947         doTest(rules,
948                 new Triple[] {
949                     new Triple(a, p, b),
950                     new Triple(a, q, c)
951                 },
952                 new Triple(a, r, Node.ANY),
953                 new Object JavaDoc[] {
954                     new Triple(a, r, b)
955                 } );
956         doTest(rules,
957                 new Triple[] {
958                     new Triple(a, p, b),
959                     new Triple(a, q, c)
960                 },
961                 new Triple(Node.ANY, r, Node.ANY),
962                 new Object JavaDoc[] {
963                     new Triple(a, r, c)
964                 } );
965     }
966
967     /**
968      * A problem from the original backchainer tests - head unification test
969      */

970     public void testProblem4() {
971         String JavaDoc rules = "[r1: (c r ?x) <- (?x p ?x)]" +
972         "[r2: (?x p ?y) <- (a q ?x), (b q ?y)]";
973         doTest(rules,
974                 new Node[] { r, p },
975                 new Triple[] {
976                     new Triple(a, q, a),
977                     new Triple(a, q, b),
978                     new Triple(a, q, c),
979                     new Triple(b, q, b),
980                     new Triple(b, q, d),
981                 },
982                 new Triple(c, r, Node.ANY),
983                 new Object JavaDoc[] {
984                     new Triple(c, r, b)
985                 } );
986     }
987
988     /**
989      * A problem from the original backchainer tests - RDFS example which threw an NPE
990      */

991     public void testProblem5() {
992         String JavaDoc ruleSrc =
993         "[rdfs8: (?a rdfs:subClassOf ?c) <- (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c)]" +
994         "[rdfs9: (?a rdf:type ?y) <- (?x rdfs:subClassOf ?y), (?a rdf:type ?x)]" +
995         "[(rdf:type rdfs:range rdfs:Class) <-]" +
996         "[rdfs3: (?y rdf:type ?c) <- (?x ?p ?y), (?p rdfs:range ?c)]" +
997         "[rdfs7: (?a rdfs:subClassOf ?a) <- (?a rdf:type rdfs:Class)]";
998         doTest( ruleSrc,
999                 new Node[] { ty, sC },
1000                new Triple[] {
1001                    new Triple(p, sP, q),
1002                    new Triple(q, sP, r),
1003                    new Triple(C1, sC, C2),
1004                    new Triple(C2, sC, C3),
1005                    new Triple(a, ty, C1)
1006                },
1007                new Triple(a, ty, Node.ANY),
1008                new Object JavaDoc[] {
1009                    new Triple(a, ty, C1),
1010                    new Triple(a, ty, C2),
1011                    new Triple(a, ty, C3)
1012                } );
1013    }
1014
1015    /**
1016     * A problem from the original backchainer tests - RDFS example which threw an NPE
1017     */

1018    public void testProblem6() {
1019        String JavaDoc ruleSrc =
1020        "[rdfs9: (?a rdf:type ?y) <- (?x rdfs:subClassOf ?y), (?a rdf:type ?x)]" +
1021        "[restriction2: (?C owl:equivalentClass all(?P, ?D)) <- (?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:allValuesFrom ?D)]" +
1022        "[rs2: (?X rdf:type all(?P,?C)) <- (?D owl:equivalentClass all(?P,?C)), (?X rdf:type ?D)]" +
1023        "[rp4: (?Y rdf:type ?C) <- (?X rdf:type all(?P, ?C)), (?X ?P ?Y)]";
1024        doTest( ruleSrc,
1025                new Node[] { ty, sC, OWL.equivalentClass.asNode() },
1026                new Triple[] {
1027                    new Triple(a, ty, r),
1028                    new Triple(a, p, b),
1029                    new Triple(r, sC, C1),
1030                    new Triple(C1, ty, OWL.Restriction.asNode()),
1031                    new Triple(C1, OWL.onProperty.asNode(), p),
1032                    new Triple(C1, OWL.allValuesFrom.asNode(), c)
1033                },
1034                new Triple(b, ty, c),
1035                new Object JavaDoc[] {
1036                    new Triple(b, ty, c)
1037                } );
1038    }
1039
1040    /**
1041     * A problem from the original backchainer tests - incorrect additional deduction.
1042     * Was due to interpeter setup failing to clone input variables.
1043     */

1044    public void testProblem7() {
1045        String JavaDoc ruleSrc =
1046        "[rdfs8: (?a rdfs:subClassOf ?c) <- (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c)]" +
1047        "[rdfs9: (?a rdf:type ?y) <- (?x rdfs:subClassOf ?y), (?a rdf:type ?x)]" +
1048// "[(rdf:type rdfs:range rdfs:Class) <-]" +
1049
// "[rdfs3: (?y rdf:type ?c) <- (?x ?p ?y), (?p rdfs:range ?c)]" +
1050
"[rdfs3: (?y rdf:type rdfs:Class) <- (?x rdf:type ?y)]" +
1051        "[rdfs7: (?a rdfs:subClassOf ?a) <- (?a rdf:type rdfs:Class)]";
1052        List rules = Rule.parseRules(ruleSrc);
1053        Node[] tabled = new Node[] { ty, sC };
1054        Triple[] triples = new Triple[] {
1055                    new Triple(C1, sC, C2),
1056                    new Triple(C2, sC, C3),
1057                    new Triple(a, ty, C1)
1058                };
1059        Graph data = new GraphMem();
1060        for (int i = 0; i < triples.length; i++) {
1061            data.add(triples[i]);
1062        }
1063        InfGraph infgraph = makeInfGraph(rules, data, tabled);
1064        ExtendedIterator it = infgraph.find(a, ty, null);
1065        Triple result = (Triple)it.next();
1066        assertEquals(result.getSubject(), a);
1067        assertEquals(result.getPredicate(), ty);
1068        it.close();
1069        // Make sure if we start again we get the full listing.
1070
TestUtil.assertIteratorValues(this,
1071            infgraph.find(a, ty, null),
1072            new Object JavaDoc[] {
1073                new Triple(a, ty, C1),
1074                new Triple(a, ty, C2),
1075                new Triple(a, ty, C3)
1076            } );
1077    }
1078
1079    /**
1080     * A problem from the original backchainer tests - RDFS example which failed.
1081     * Was due to unsupported multi-head statement.
1082     */

1083    public void testProblem8() {
1084        String JavaDoc ruleSrc =
1085        "[rdfs9: (?a rdf:type ?y) <- bound(?y) (?x rdfs:subClassOf ?y) (?a rdf:type ?x)]" +
1086        "[restriction4: (?C owl:equivalentClass max(?P, ?X)) <- (?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:maxCardinality ?X)]" +
1087        "[restrictionProc11: (?X rdf:type max(?P, 1)) <- (?P rdf:type owl:FunctionalProperty), (?X rdf:type owl:Thing)]" +
1088        "[equivalentClass1: (?Q rdfs:subClassOf ?P) <- (?P owl:equivalentClass ?Q) ]" +
1089        "[equivalentClass1: (?P rdfs:subClassOf ?Q) <- (?P owl:equivalentClass ?Q) ]" +
1090        "[restrictionSubclass1: (?X rdf:type ?D) <- bound(?D) (?D owl:equivalentClass ?R), isFunctor(?R) (?X rdf:type ?R)]";
1091        doTest( ruleSrc,
1092                new Node[] { ty, sC, OWL.equivalentClass.asNode() },
1093                new Triple[] {
1094                    new Triple(a, ty, OWL.Thing.asNode()),
1095                    new Triple(p, ty, OWL.FunctionalProperty.asNode()),
1096                    new Triple(c, OWL.equivalentClass.asNode(), C1),
1097                    new Triple(C1, ty, OWL.Restriction.asNode()),
1098                    new Triple(C1, OWL.onProperty.asNode(), p),
1099                    new Triple(C1, OWL.maxCardinality.asNode(), Util.makeIntNode(1)),
1100                },
1101                new Triple(a, ty, c),
1102                new Object JavaDoc[] {
1103                    new Triple(a, ty, c)
1104                } );
1105    }
1106      
1107    /**
1108     * Test derivation machinery
1109     */

1110    public void testRuleDerivations() {
1111        String JavaDoc rules = "[testRule1: (C2, p, ?a) <- (C1 p ?a)]" +
1112                       "[testRule2: (C2, q, ?a) <- (C1 q ?a)]" +
1113                       "[testRule3: (a p ?a) <- (C2 p ?a), (C2 q ?a)]";
1114        List ruleList = Rule.parseRules(rules);
1115        Graph data = new GraphMem();
1116        data.add(new Triple(C1, p, C3));
1117        data.add(new Triple(C1, q, C4));
1118        data.add(new Triple(C1, q, C3));
1119        InfGraph infgraph = makeInfGraph(ruleList, data, new Node[]{p, q});
1120        infgraph.setDerivationLogging(true);
1121
1122        TestUtil.assertIteratorValues(this, infgraph.find(a, null, null),
1123            new Triple[] {
1124                new Triple(a, p, C3)
1125            });
1126        
1127        Iterator derivs = infgraph.getDerivation(new Triple(a, p, C3));
1128        StringWriter outString = new StringWriter(250);
1129        PrintWriter out = new PrintWriter(outString);
1130        while (derivs.hasNext()) {
1131            Derivation d = (Derivation) derivs.next();
1132            d.printTrace(out, true);
1133        }
1134        out.flush();
1135
1136        String JavaDoc testString = TestUtil.normalizeWhiteSpace("Rule testRule3 concluded (a p C3) <-\n" +
1137                " Rule testRule1 concluded (C2 p C3) <-\n" +
1138                " Fact (C1 p C3)\r\n" +
1139                " Rule testRule2 concluded (C2 q C3) <-\n" +
1140                " Fact (C1 q C3)\r\n");
1141        assertEquals(testString, TestUtil.normalizeWhiteSpace(outString.getBuffer().toString()));
1142    }
1143
1144    /**
1145     * A suspect problem, originally derived from the OWL rules - risk of unbound variables escaping.
1146     * Not managed to isolate or reproduce the problem yet.
1147     */

1148    public void testProblem9() {
1149        String JavaDoc ruleSrc =
1150        "[test: (?x owl:sameAs ?x) <- (?x rdf:type owl:Thing) ]" +
1151        "[sameIndividualAs6: (?X rdf:type owl:Thing) <- (?X owl:sameAs ?Y) ]" +
1152        "[ans: (?x p C1) <- (?y owl:sameAs ?x)]";
1153        Node sI = OWL.sameAs.asNode();
1154        doTest( ruleSrc,
1155                new Node[] { ty, sI }, // Tabled predicates
1156
new Triple[] { // init data
1157
new Triple(a, ty, OWL.Thing.asNode()),
1158                    new Triple(b, sI, c),
1159                },
1160        new Triple(Node.ANY, p, Node.ANY), // query
1161
new Object JavaDoc[] { // result
1162
new Triple(a, p, C1),
1163            new Triple(b, p, C1),
1164            new Triple(c, p, C1),
1165        } );
1166// new Triple(Node.ANY, ty, Node.ANY), // query
1167
// new Object[] { // result
1168
// new Triple(a, ty, OWL.Thing.asNode()),
1169
// new Triple(b, ty, OWL.Thing.asNode())
1170
// } );
1171
}
1172    
1173    /**
1174     * Test 3-arg builtins such as arithmetic.
1175     */

1176    public void testArithBuiltins() {
1177        doBuiltinTest(
1178            "[(a,r,0) <- (a,p,?x), (a,q,?y), lessThan(?x,?y)]" +
1179            "[(a,r,1) <- (a,p,?x), (a,q,?y), ge(?x, ?y)]",
1180            Util.makeIntNode(2),Util.makeIntNode(3), Util.makeIntNode(0)
1181        );
1182        doBuiltinTest(
1183            "[(a,r,0) <- (a,p,?x), (a,q,?y), lessThan(?x,?y)]" +
1184            "[(a,r,1) <- (a,p,?x), (a,q,?y), ge(?x, ?y)]",
1185            Util.makeIntNode(3),Util.makeIntNode(3), Util.makeIntNode(1)
1186        );
1187        doBuiltinTest(
1188            "[(a,r,0) <- (a,p,?x), (a,q,?y), le(?x,?y)]" +
1189            "[(a,r,1) <- (a,p,?x), (a,q,?y), greaterThan(?x, ?y)]",
1190            Util.makeIntNode(3),Util.makeIntNode(3), Util.makeIntNode(0)
1191        );
1192        doBuiltinTest(
1193            "[(a,r,?z) <- (a,p,?x), (a,q,?y), min(?x,?y,?z)]",
1194            Util.makeIntNode(2),Util.makeIntNode(3), Util.makeIntNode(2)
1195        );
1196        doBuiltinTest(
1197            "[(a,r,?z) <- (a,p,?x), (a,q,?y), min(?x,?y,?z)]",
1198            Util.makeIntNode(4),Util.makeIntNode(3), Util.makeIntNode(3)
1199        );
1200        doBuiltinTest(
1201            "[(a,r,?z) <- (a,p,?x), (a,q,?y), max(?x,?y,?z)]",
1202            Util.makeIntNode(2),Util.makeIntNode(3), Util.makeIntNode(3)
1203        );
1204        doBuiltinTest(
1205            "[(a,r,?z) <- (a,p,?x), (a,q,?y), max(?x,?y,?z)]",
1206            Util.makeIntNode(4),Util.makeIntNode(3), Util.makeIntNode(4)
1207        );
1208    }
1209    
1210    /**
1211     * Test the temporary list builtins
1212     */

1213    public void testListBuiltins() {
1214        String JavaDoc ruleSrc = "[(a r ?n) <- (a p ?l), listLength(?l, ?n)]";
1215        List rules = Rule.parseRules(ruleSrc);
1216        Graph data = new GraphMem();
1217        data.add(new Triple(a, p, Util.makeList(new Node[]{C1,C2,C3},data)));
1218        InfGraph infgraph = makeInfGraph(rules, data);
1219        TestUtil.assertIteratorValues(this,
1220            infgraph.find(new Triple(a, r, Node.ANY)),
1221            new Triple[] {
1222                new Triple(a, r, Util.makeIntNode(3))
1223            });
1224
1225        rules = Rule.parseRules(
1226        "[(a s b) <- (a p ?l), (a, q, ?j) listEqual(?l, ?j)]" +
1227        "[(a s c) <- (a p ?l), (a, q, ?j) listNotEqual(?l, ?j)]" +
1228        "[(a s d) <- (a p ?l), (a, r, ?j) listEqual(?l, ?j)]" +
1229        "[(a s e) <- (a p ?l), (a, r, ?j) listNotEqual(?l, ?j)]"
1230            );
1231        data = new GraphMem();
1232        data.add(new Triple(a, p,
1233            Util.makeList( new Node[]{C1, Util.makeIntNode(3), C3}, data) ));
1234        data.add(new Triple(a, q,
1235            Util.makeList( new Node[]{C3, C1, Util.makeLongNode(3)}, data) ));
1236        data.add(new Triple(a, r,
1237            Util.makeList( new Node[]{C3, C1, Util.makeLongNode(2)}, data) ));
1238        infgraph = makeInfGraph(rules, data);
1239        TestUtil.assertIteratorValues(this,
1240            infgraph.find(new Triple(a, s, Node.ANY)),
1241            new Triple[] {
1242                new Triple(a, s, b),
1243                new Triple(a, s, e),
1244            });
1245
1246        rules = Rule.parseRules(
1247        "[(b r ?j) <- (a p ?l), (a, q, ?j) listContains(?l, ?j)]" +
1248        "[(b s ?j) <- (a p ?l), (a, q, ?j) listNotContains(?l, ?j)]"
1249            );
1250        data = new GraphMem();
1251        data.add(new Triple(a, p,
1252            Util.makeList( new Node[]{C1, Util.makeIntNode(3), C3}, data) ));
1253        data.add(new Triple(a, q, C1));
1254        data.add(new Triple(a, q, Util.makeLongNode(3)));;
1255        data.add(new Triple(a, q, C2));
1256        infgraph = makeInfGraph(rules, data);
1257        TestUtil.assertIteratorValues(this,
1258            infgraph.find(new Triple(b, Node.ANY, Node.ANY)),
1259            new Triple[] {
1260                new Triple(b, r, C1),
1261                new Triple(b, r, Util.makeIntNode(3)),
1262                new Triple(b, s, C2),
1263            });
1264    }
1265    
1266    /**
1267     * Generic test operation.
1268     * @param ruleSrc the source of the rules
1269     * @param triples a set of triples to insert in the graph before the query
1270     * @param query the TripleMatch to search for
1271     * @param results the array of expected results
1272     */

1273    private void doTest(String JavaDoc ruleSrc, Triple[] triples, TripleMatch query, Object JavaDoc[] results) {
1274        List rules = Rule.parseRules(ruleSrc);
1275        Graph data = new GraphMem();
1276        for (int i = 0; i < triples.length; i++) {
1277            data.add(triples[i]);
1278        }
1279        InfGraph infgraph = makeInfGraph(rules, data);
1280        TestUtil.assertIteratorValues(this, infgraph.find(query), results);
1281    }
1282
1283    /**
1284     * Generic test operation.
1285     * @param ruleSrc the source of the rules
1286     * @param tabled the predicates that should be tabled
1287     * @param triples a set of triples to insert in the graph before the query
1288     * @param query the TripleMatch to search for
1289     * @param results the array of expected results
1290     */

1291    private void doTest(String JavaDoc ruleSrc, Node[] tabled, Triple[] triples, TripleMatch query, Object JavaDoc[] results) {
1292        List rules = Rule.parseRules(ruleSrc);
1293        Graph data = new GraphMem();
1294        for (int i = 0; i < triples.length; i++) {
1295            data.add(triples[i]);
1296        }
1297        InfGraph infgraph = makeInfGraph(rules, data, tabled);
1298        TestUtil.assertIteratorValues(this, infgraph.find(query), results);
1299
1300    }
1301    
1302    /**
1303     * Generic base test operation on a graph with the single triple (a, p, b)
1304     * @param ruleSrc the source of the rules
1305     * @param query the TripleMatch to search for
1306     * @param results the array of expected results
1307     */

1308    private void doBasicTest(String JavaDoc ruleSrc, TripleMatch query, Object JavaDoc[] results) {
1309        doTest(ruleSrc, new Triple[]{new Triple(a,p,b)}, query, results);
1310    }
1311    
1312    /**
1313     * Generic test operation.
1314     * @param rule to test a simple builtin operation
1315     * @param param1 value to bind to first parameter by (a,p,_)
1316     * @param param2 value to bind to first parameter by (a,q,_)
1317     * @param result the expected result to be found by (a,r,_)
1318     */

1319    private void doBuiltinTest(String JavaDoc ruleSrc, Node param1, Node param2, Node result) {
1320        doTest(ruleSrc,
1321               new Triple[] {
1322                   new Triple(a, p, param1),
1323                   new Triple(a, q, param2)
1324                },
1325                new Triple(a, r, Node.ANY),
1326                new Triple[] {
1327                    new Triple(a, r, result)
1328                });
1329    }
1330    
1331}
1332
1333
1334/*
1335    (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
1336    All rights reserved.
1337
1338    Redistribution and use in source and binary forms, with or without
1339    modification, are permitted provided that the following conditions
1340    are met:
1341
1342    1. Redistributions of source code must retain the above copyright
1343       notice, this list of conditions and the following disclaimer.
1344
1345    2. Redistributions in binary form must reproduce the above copyright
1346       notice, this list of conditions and the following disclaimer in the
1347       documentation and/or other materials provided with the distribution.
1348
1349    3. The name of the author may not be used to endorse or promote products
1350       derived from this software without specific prior written permission.
1351
1352    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1353    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1354    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1355    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1356    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1357    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1358    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1359    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1360    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1361    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1362*/
Popular Tags