KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: TestFBRules.java
3  * Created by: Dave Reynolds
4  * Created on: 29-May-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: TestFBRules.java,v 1.40 2005/04/12 10:59:02 der Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.test;
11
12 import com.hp.hpl.jena.mem.GraphMem;
13 import com.hp.hpl.jena.reasoner.*;
14 import com.hp.hpl.jena.reasoner.rulesys.*;
15 import com.hp.hpl.jena.reasoner.test.TestUtil;
16 import com.hp.hpl.jena.datatypes.xsd.*;
17 import com.hp.hpl.jena.graph.*;
18 import com.hp.hpl.jena.graph.impl.LiteralLabel;
19 import com.hp.hpl.jena.rdf.model.*;
20 import com.hp.hpl.jena.shared.ClosedException;
21 import com.hp.hpl.jena.shared.impl.JenaParameters;
22 import com.hp.hpl.jena.util.FileManager;
23 import com.hp.hpl.jena.util.PrintUtil;
24 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
25 import com.hp.hpl.jena.vocabulary.*;
26
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import java.io.IOException JavaDoc;
31 import java.util.*;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * Test suite for the hybrid forward/backward rule system.
38  *
39  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
40  * @version $Revision: 1.40 $ on $Date: 2005/04/12 10:59:02 $
41  */

42 public class TestFBRules extends TestCase {
43     
44     protected static Log logger = LogFactory.getLog(TestFBRules.class);
45     
46     // Useful constants
47
protected Node p = Node.createURI("p");
48     protected Node q = Node.createURI("q");
49     protected Node n1 = Node.createURI("n1");
50     protected Node n2 = Node.createURI("n2");
51     protected Node n3 = Node.createURI("n3");
52     protected Node n4 = Node.createURI("n4");
53     protected Node n5 = Node.createURI("n5");
54     protected Node res = Node.createURI("res");
55     protected Node r = Node.createURI("r");
56     protected Node s = Node.createURI("s");
57     protected Node t = Node.createURI("t");
58     protected Node u = Node.createURI("u");
59     protected Node a = Node.createURI("a");
60     protected Node b = Node.createURI("b");
61     protected Node c = Node.createURI("c");
62     protected Node d = Node.createURI("d");
63     protected Node C1 = Node.createURI("C1");
64     protected Node C2 = Node.createURI("C2");
65     protected Node C3 = Node.createURI("C3");
66     protected Node sP = RDFS.subPropertyOf.getNode();
67     protected Node sC = RDFS.subClassOf.getNode();
68     protected Node ty = RDF.type.getNode();
69      
70     /**
71      * Boilerplate for junit
72      */

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

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

91     public Reasoner createReasoner(List rules) {
92         FBRuleReasoner reasoner = new FBRuleReasoner(rules);
93         reasoner.tablePredicate(RDFS.Nodes.subClassOf);
94         reasoner.tablePredicate(RDF.Nodes.type);
95         reasoner.tablePredicate(p);
96         return reasoner;
97     }
98
99     /**
100      * Check parser extension for f/b distinction.
101      */

102     public void testParser() {
103         String JavaDoc rf = "(?a rdf:type ?t) -> (?t rdf:type rdfs:Class).";
104         String JavaDoc rb = "(?t rdf:type rdfs:Class) <- (?a rdf:type ?t).";
105         assertTrue( ! Rule.parseRule(rf).isBackward() );
106         assertTrue( Rule.parseRule(rb).isBackward() );
107     }
108      
109     /**
110      * Minimal rule tester to check basic pattern match, forward style.
111      */

112     public void testRuleMatcher() {
113         String JavaDoc rules = "[r1: (?a p ?b), (?b q ?c) -> (?a, q, ?c)]" +
114                        "[r2: (?a p ?b), (?b p ?c) -> (?a, p, ?c)]" +
115                        "[r3: (?a p ?a), (n1 p ?c), (n1, p, ?a) -> (?a, p, ?c)]" +
116                        "[r4: (n4 ?p ?a) -> (n4, ?a, ?p)]";
117         List ruleList = Rule.parseRules(rules);
118         
119         InfGraph infgraph = createReasoner(ruleList).bind(new GraphMem());
120         infgraph.add(new Triple(n1, p, n2));
121         infgraph.add(new Triple(n2, p, n3));
122         infgraph.add(new Triple(n2, q, n3));
123         infgraph.add(new Triple(n4, p, n4));
124         
125         TestUtil.assertIteratorValues(this, infgraph.find(null, null, null),
126             new Triple[] {
127                 new Triple(n1, p, n2),
128                 new Triple(n2, p, n3),
129                 new Triple(n2, q, n3),
130                 new Triple(n4, p, n4),
131                 new Triple(n1, p, n3),
132                 new Triple(n1, q, n3),
133                 new Triple(n4, n4, p),
134             });
135     }
136     
137     /**
138      * Test functor handling
139      */

140     public void testEmbeddedFunctors() {
141         String JavaDoc rules = "(?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:allValuesFrom ?D) -> (?C rb:restriction all(?P, ?D))." +
142                         "(?C rb:restriction all(eg:p, eg:D)) -> (?C rb:restriction 'allOK')." +
143                        "[ -> (eg:foo eg:prop functor(eg:bar, 1)) ]" +
144                        "[ (?x eg:prop functor(eg:bar, ?v)) -> (?x eg:propbar ?v) ]" +
145                        "[ (?x eg:prop functor(?v, ?*)) -> (?x eg:propfunc ?v) ]" +
146                        "";
147         List ruleList = Rule.parseRules(rules);
148         
149         Model data = ModelFactory.createDefaultModel();
150         Resource R1 = data.createResource(PrintUtil.egNS + "R1");
151         Resource D = data.createResource(PrintUtil.egNS + "D");
152         Property p = data.createProperty(PrintUtil.egNS, "p");
153         Property prop = data.createProperty(PrintUtil.egNS, "prop");
154         Property propbar = data.createProperty(PrintUtil.egNS, "propbar");
155         Property propfunc = data.createProperty(PrintUtil.egNS, "propfunc");
156         Property rbr = data.createProperty(ReasonerVocabulary.RBNamespace, "restriction");
157         R1.addProperty(RDF.type, OWL.Restriction)
158           .addProperty(OWL.onProperty, p)
159           .addProperty(OWL.allValuesFrom, D);
160         
161         Reasoner reasoner = createReasoner(ruleList);
162         InfGraph infgraph = reasoner.bind(data.getGraph());
163         Model infModel = ModelFactory.createModelForGraph(infgraph);
164         Resource foo = infModel.createResource(PrintUtil.egNS + "foo");
165         Resource bar = infModel.createResource(PrintUtil.egNS + "bar");
166         
167         RDFNode flit = infModel.getResource(R1.getURI()).getRequiredProperty(rbr).getObject();
168         assertNotNull(flit);
169         assertEquals(flit.toString(), "allOK");
170 // assertTrue(flit instanceof Literal);
171
// Functor func = (Functor)((Literal)flit).getValue();
172
// assertEquals("all", func.getName());
173
// assertEquals(p.getNode(), func.getArgs()[0]);
174
// assertEquals(D.getNode(), func.getArgs()[1]);
175

176         Literal one = (Literal)foo.getRequiredProperty(propbar).getObject();
177         assertEquals(new Integer JavaDoc(1), one.getValue());
178     }
179     
180     /**
181      * The the minimal machinery for supporting builtins
182      */

183     public void testBuiltins() {
184         String JavaDoc rules = //"[testRule1: (n1 ?p ?a) -> print('rule1test', ?p, ?a)]" +
185
"[r1: (n1 p ?x), addOne(?x, ?y) -> (n1 q ?y)]" +
186                        "[r2: (n1 p ?x), lessThan(?x, 3) -> (n2 q ?x)]" +
187                        "[axiom1: -> (n1 p 1)]" +
188                        "[axiom2: -> (n1 p 4)]" +
189                        "";
190         List ruleList = Rule.parseRules(rules);
191         
192         InfGraph infgraph = createReasoner(ruleList).bind(new GraphMem());
193         TestUtil.assertIteratorValues(this, infgraph.find(n1, q, null),
194             new Triple[] {
195                 new Triple(n1, q, Util.makeIntNode(2)),
196                 new Triple(n1, q, Util.makeIntNode(5))
197             });
198         TestUtil.assertIteratorValues(this, infgraph.find(n2, q, null),
199             new Triple[] {
200                 new Triple(n2, q, Util.makeIntNode(1))
201             });
202         
203     }
204          
205     /**
206      * Test schmea partial binding machinery, forward subset.
207      */

208     public void testSchemaBinding() {
209         String JavaDoc rules = "[testRule1: (n1 p ?a) -> (n2, p, ?a)]" +
210                        "[testRule2: (n1 q ?a) -> (n2, q, ?a)]" +
211                        "[testRule3: (n2 p ?a), (n2 q ?a) -> (res p ?a)]" +
212                        "[testBRule4: (n3 p ?a) <- (n1, p, ?a)]";
213         List ruleList = Rule.parseRules(rules);
214         Graph schema = new GraphMem();
215         schema.add(new Triple(n1, p, n3));
216         Graph data = new GraphMem();
217         data.add(new Triple(n1, q, n4));
218         data.add(new Triple(n1, q, n3));
219         
220         Reasoner reasoner = createReasoner(ruleList);
221         Reasoner boundReasoner = reasoner.bindSchema(schema);
222         InfGraph infgraph = boundReasoner.bind(data);
223
224         TestUtil.assertIteratorValues(this, infgraph.find(null, null, null),
225             new Triple[] {
226                 new Triple(n1, p, n3),
227                 new Triple(n2, p, n3),
228                 new Triple(n3, p, n3),
229                 new Triple(n1, q, n4),
230                 new Triple(n2, q, n4),
231                 new Triple(n1, q, n3),
232                 new Triple(n2, q, n3),
233                 new Triple(res, p, n3)
234             });
235     }
236     
237     /**
238      * The the "remove" builtin
239      */

240     public void testRemoveBuiltin() {
241         String JavaDoc rules =
242                        "[rule1: (?x p ?y), (?x q ?y) -> remove(0)]" +
243                        "";
244         List ruleList = Rule.parseRules(rules);
245
246         InfGraph infgraph = createReasoner(ruleList).bind(new GraphMem());
247         infgraph.add(new Triple(n1, p, Util.makeIntNode(1)));
248         infgraph.add(new Triple(n1, p, Util.makeIntNode(2)));
249         infgraph.add(new Triple(n1, q, Util.makeIntNode(2)));
250         
251         TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null),
252             new Triple[] {
253                 new Triple(n1, p, Util.makeIntNode(1)),
254                 new Triple(n1, q, Util.makeIntNode(2))
255             });
256         
257     }
258     
259     /**
260      * Test the rebind operation.
261      */

262     public void testRebind() {
263         String JavaDoc rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
264         List ruleList = Rule.parseRules(rules);
265         Graph data = new GraphMem();
266         data.add(new Triple(n1, p, n2));
267         InfGraph infgraph = createReasoner(ruleList).bind(data);
268         TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null),
269             new Triple[] {
270                 new Triple(n1, p, n2),
271                 new Triple(n1, q, n2)
272             });
273         Graph ndata = new GraphMem();
274         ndata.add(new Triple(n1, p, n3));
275         infgraph.rebind(ndata);
276         TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null),
277             new Triple[] {
278                 new Triple(n1, p, n3),
279                 new Triple(n1, q, n3)
280             });
281     }
282     
283    
284     /**
285      * Test the close operation.
286      */

287     public void testClose() {
288         String JavaDoc rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
289         List ruleList = Rule.parseRules(rules);
290         Graph data = new GraphMem();
291         data.add(new Triple(n1, p, n2));
292         InfGraph infgraph = createReasoner(ruleList).bind(data);
293         TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null),
294             new Triple[] {
295                 new Triple(n1, p, n2),
296                 new Triple(n1, q, n2)
297             });
298         infgraph.close();
299         boolean foundException = false;
300         try {
301             infgraph.find(n1, null, null);
302         } catch (ClosedException e) {
303             foundException = true;
304         }
305         assertTrue("Close detected", foundException);
306     }
307
308     /**
309      * Test example pure backchaining rules
310      */

311     public void testBackchain1() {
312         Graph data = new GraphMem();
313         data.add(new Triple(p, sP, q));
314         data.add(new Triple(q, sP, r));
315         data.add(new Triple(C1, sC, C2));
316         data.add(new Triple(C2, sC, C3));
317         data.add(new Triple(a, ty, C1));
318         List rules = Rule.parseRules(
319         "[rdfs8: (?a rdfs:subClassOf ?c) <- (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c)]" +
320         "[rdfs9: (?a rdf:type ?y) <- (?x rdfs:subClassOf ?y), (?a rdf:type ?x)]" +
321         "[-> (rdf:type rdfs:range rdfs:Class)]" +
322         "[rdfs3: (?y rdf:type ?c) <- (?x ?p ?y), (?p rdfs:range ?c)]" +
323         "[rdfs7: (?a rdfs:subClassOf ?a) <- (?a rdf:type rdfs:Class)]"
324                         );
325         Reasoner reasoner = createReasoner(rules);
326         InfGraph infgraph = reasoner.bind(data);
327         TestUtil.assertIteratorValues(this,
328             infgraph.find(a, ty, null),
329             new Object JavaDoc[] {
330                 new Triple(a, ty, C1),
331                 new Triple(a, ty, C2),
332                 new Triple(a, ty, C3)
333             } );
334         TestUtil.assertIteratorValues(this,
335             infgraph.find(C1, sC, a),
336             new Object JavaDoc[] {
337             } );
338     }
339
340     /**
341      * Test complex rule head unification
342      */

343     public void testBackchain2() {
344         Graph data = new GraphMem();
345         data.add(new Triple(c, q, d));
346         List rules = Rule.parseRules(
347             "[r1: (c r ?x) <- (?x p f(?x b))]" +
348             "[r2: (?y p f(a ?y)) <- (c q ?y)]"
349                           );
350         Reasoner reasoner = createReasoner(rules);
351         InfGraph infgraph = reasoner.bind(data);
352         TestUtil.assertIteratorValues(this,
353               infgraph.find(c, r, null), new Object JavaDoc[] { } );
354               
355         data.add(new Triple(c, q, a));
356         rules = Rule.parseRules(
357         "[r1: (c r ?x) <- (?x p f(?x a))]" +
358         "[r2: (?y p f(a ?y)) <- (c q ?y)]"
359                           );
360         reasoner = createReasoner(rules);
361         infgraph = reasoner.bind(data);
362         TestUtil.assertIteratorValues(this,
363               infgraph.find(c, r, null),
364               new Object JavaDoc[] {
365                   new Triple(c, r, a)
366               } );
367             
368         data = new GraphMem();
369         data.add(new Triple(a, q, a));
370         data.add(new Triple(a, q, b));
371         data.add(new Triple(a, q, c));
372         data.add(new Triple(b, q, d));
373         data.add(new Triple(b, q, b));
374         rules = Rule.parseRules(
375           "[r1: (c r ?x) <- (?x p ?x)]" +
376           "[r2: (?x p ?y) <- (a q ?x), (b q ?y)]"
377                           );
378         reasoner = createReasoner(rules);
379         infgraph = reasoner.bind(data);
380         TestUtil.assertIteratorValues(this,
381               infgraph.find(c, r, null),
382               new Object JavaDoc[] {
383                   new Triple(c, r, b)
384               } );
385               
386         rules = Rule.parseRules(
387           "[r1: (c r ?x) <- (?x p ?x)]" +
388           "[r2: (a p ?x) <- (a q ?x)]"
389                           );
390         reasoner = createReasoner(rules);
391         infgraph = reasoner.bind(data);
392         TestUtil.assertIteratorValues(this,
393               infgraph.find(c, r, null),
394               new Object JavaDoc[] {
395                   new Triple(c, r, a)
396               } );
397     }
398
399     /**
400      * Test restriction example
401      */

402     public void testBackchain3() {
403         Graph data = new GraphMem();
404         data.add(new Triple(a, ty, r));
405         data.add(new Triple(a, p, b));
406         data.add(new Triple(r, sC, C1));
407         data.add(new Triple(C1, ty, OWL.Restriction.asNode()));
408         data.add(new Triple(C1, OWL.onProperty.asNode(), p));
409         data.add(new Triple(C1, OWL.allValuesFrom.asNode(), c));
410         List rules = Rule.parseRules(
411     "[rdfs9: (?a rdf:type ?y) <- (?x rdfs:subClassOf ?y) (?a rdf:type ?x)]" +
412     "[restriction2: (?C owl:equivalentClass all(?P, ?D)) <- (?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:allValuesFrom ?D)]" +
413     "[rs2: (?X rdf:type all(?P,?C)) <- (?D owl:equivalentClass all(?P,?C)), (?X rdf:type ?D)]" +
414     "[rp4: (?Y rdf:type ?C) <- (?X rdf:type all(?P, ?C)), (?X ?P ?Y)]"
415                           );
416         Reasoner reasoner = createReasoner(rules);
417         InfGraph infgraph = reasoner.bind(data);
418         TestUtil.assertIteratorValues(this,
419               infgraph.find(b, ty, c), new Object JavaDoc[] {
420                   new Triple(b, ty, c)
421               } );
422     }
423     
424     /**
425      * Test example hybrid rule.
426      */

427     public void testHybrid1() {
428         Graph data = new GraphMem();
429         data.add(new Triple(a, p, b));
430         data.add(new Triple(p, ty, s));
431         List rules = Rule.parseRules(
432         "[r1: (?p rdf:type s) -> [r1b: (?x ?p ?y) <- (?y ?p ?x)]]"
433                           );
434         Reasoner reasoner = createReasoner(rules);
435         InfGraph infgraph = reasoner.bind(data);
436         TestUtil.assertIteratorValues(this,
437               infgraph.find(null, p, null), new Object JavaDoc[] {
438                   new Triple(a, p, b),
439                   new Triple(b, p, a)
440               } );
441     }
442     
443     /**
444      * Test example hybrid rule.
445      */

446     public void testHybrid2() {
447         Graph data = new GraphMem();
448         data.add(new Triple(a, r, b));
449         data.add(new Triple(p, ty, s));
450         List rules = Rule.parseRules(
451         "[a1: -> (a rdf:type t)]" +
452         "[r0: (?x r ?y) -> (?x p ?y)]" +
453         "[r1: (?p rdf:type s) -> [r1b: (?x ?p ?y) <- (?y ?p ?x)]]" +
454         "[r2: (?p rdf:type s) -> [r2b: (?x ?p ?x) <- (?x rdf:type t)]]"
455                           );
456         Reasoner reasoner = createReasoner(rules);
457         FBRuleInfGraph infgraph = (FBRuleInfGraph) reasoner.bind(data);
458         infgraph.setDerivationLogging(true);
459         infgraph.prepare();
460         assertTrue("Forward rule count", infgraph.getNRulesFired() == 3);
461         TestUtil.assertIteratorValues(this,
462               infgraph.find(null, p, null), new Object JavaDoc[] {
463                   new Triple(a, p, a),
464                   new Triple(a, p, b),
465                   new Triple(b, p, a)
466               } );
467         // Suppressed until LP engine implements rule counting, if ever
468
// assertTrue("Backward rule count", infgraph.getNRulesFired() == 8);
469

470         // Check derivation tracing as well
471
// Suppressed until LP engine implements derivation tracing
472
Iterator di = infgraph.getDerivation(new Triple(b, p, a));
473         assertTrue(di.hasNext());
474         RuleDerivation d = (RuleDerivation)di.next();
475         assertTrue(d.getRule().getName().equals("r1b"));
476         TestUtil.assertIteratorValues(this, d.getMatches().iterator(), new Object JavaDoc[] { new Triple(a, p, b) });
477         assertTrue(! di.hasNext());
478     }
479     
480     /**
481      * Test example hybrid rules for rdfs.
482      */

483     public void testHybridRDFS() {
484         Graph data = new GraphMem();
485         data.add(new Triple(a, p, b));
486         data.add(new Triple(p, RDFS.range.asNode(), C1));
487         List rules = Rule.parseRules(
488     "[rdfs2: (?p rdfs:domain ?c) -> [(?x rdf:type ?c) <- (?x ?p ?y)] ]" +
489     "[rdfs3: (?p rdfs:range ?c) -> [(?y rdf:type ?c) <- (?x ?p ?y)] ]" +
490     "[rdfs5a: (?a rdfs:subPropertyOf ?b), (?b rdfs:subPropertyOf ?c) -> (?a rdfs:subPropertyOf ?c)]" +
491     "[rdfs5b: (?a rdf:type rdf:Property) -> (?a rdfs:subPropertyOf ?a)]" +
492     "[rdfs6: (?p rdfs:subPropertyOf ?q) -> [ (?a ?q ?b) <- (?a ?p ?b)] ]" +
493     "[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]" +
494     "[rdfs8: (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
495     "[rdfs9: (?x rdfs:subClassOf ?y) -> [ (?a rdf:type ?y) <- (?a rdf:type ?x)] ]" +
496                           "" );
497         Reasoner reasoner = createReasoner(rules);
498         InfGraph infgraph = reasoner.bind(data);
499 // ((FBRuleInfGraph)infgraph).setTraceOn(true);
500
TestUtil.assertIteratorValues(this,
501               infgraph.find(b, ty, null), new Object JavaDoc[] {
502                   new Triple(b, ty, C1)
503               } );
504     }
505     
506     /**
507      * Test example hybrid rules for rdfs.
508      */

509     public void testHybridRDFS2() {
510         Graph data = new GraphMem();
511         data.add(new Triple(a, p, b));
512         data.add(new Triple(p, sP, r));
513         data.add(new Triple(r, RDFS.range.asNode(), C1));
514         List rules = Rule.parseRules(
515     "[rdfs3: (?p rdfs:range ?c) -> [(?y rdf:type ?c) <- (?x ?p ?y)] ]" +
516     "[rdfs6: (?p rdfs:subPropertyOf ?q) -> [ (?a ?q ?b) <- (?a ?p ?b)] ]" +
517                           "" );
518         Reasoner reasoner = createReasoner(rules);
519         InfGraph infgraph = reasoner.bind(data);
520 // ((FBRuleInfGraph)infgraph).setTraceOn(true);
521
TestUtil.assertIteratorValues(this,
522               infgraph.find(b, ty, C1), new Object JavaDoc[] {
523                   new Triple(b, ty, C1)
524               } );
525     }
526
527     /**
528      * Test access to makeInstance machinery from a Brule.
529      */

530     public void testMakeInstance() {
531         Graph data = new GraphMem();
532         data.add(new Triple(a, ty, C1));
533         List rules = Rule.parseRules(
534         "[r1: (?x p ?t) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]" +
535         "[r2: (?t rdf:type C2) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]" +
536                           "" );
537         Reasoner reasoner = createReasoner(rules);
538         InfGraph infgraph = reasoner.bind(data);
539         
540         Node valueInstance = getValue(infgraph, a, p);
541         assertNotNull(valueInstance);
542         Node valueInstance2 = getValue(infgraph, a, p);
543         assertEquals(valueInstance, valueInstance2);
544         Node valueType = getValue(infgraph, valueInstance, RDF.type.asNode());
545         assertEquals(valueType, C2);
546     }
547
548     /**
549      * Test access to makeInstance machinery from a Brule.
550      */

551     public void testMakeInstances() {
552         Graph data = new GraphMem();
553         data.add(new Triple(a, ty, C1));
554         List rules = Rule.parseRules(
555         "[r1: (?x p ?t) <- (?x rdf:type C1), makeInstance(?x, p, ?t)]" +
556                           "" );
557         Reasoner reasoner = createReasoner(rules);
558         InfGraph infgraph = reasoner.bind(data);
559         
560         Node valueInstance = getValue(infgraph, a, p);
561         assertNotNull(valueInstance);
562         Node valueInstance2 = getValue(infgraph, a, p);
563         assertEquals(valueInstance, valueInstance2);
564     }
565     
566     /**
567      * Test case for makeInstance which failed during development.
568      */

569     public void testMakeInstanceBug() {
570         Graph data = new GraphMem();
571         data.add(new Triple(a, ty, r));
572         data.add(new Triple(r, sC, Functor.makeFunctorNode("some", new Node[] {p, C1})));
573         List rules = Rule.parseRules(
574         "[some1: (?C rdfs:subClassOf some(?P, ?D)) ->"
575         + "[some1b: (?X ?P ?T) <- (?X rdf:type ?C), unbound(?T), noValue(?X, ?P), makeInstance(?X, ?P, ?D, ?T) ]"
576         + "[some1b2: (?T rdf:type ?D) <- (?X rdf:type ?C), bound(?T), makeInstance(?X, ?P, ?D, ?T) ]"
577         + "]");
578         Reasoner reasoner = createReasoner(rules);
579         InfGraph infgraph = reasoner.bind(data);
580         
581         Node valueInstance = getValue(infgraph, a, p);
582         assertNotNull(valueInstance);
583         Node valueType = getValue(infgraph, valueInstance, ty);
584         assertEquals(valueType, C1);
585         
586     }
587     
588     /**
589      * Test numeric functors
590      */

591     public void testNumericFunctors() {
592         String JavaDoc rules =
593         "[r1: (?x p f(a, ?x)) -> (?x q f(?x)) ]" +
594         "[r1: (?x p f(a, 0)) -> (?x s res) ]" +
595                        "";
596         List ruleList = Rule.parseRules(rules);
597         Graph data = new GraphMem();
598         data.add(new Triple(n1, p, Util.makeIntNode(2)) );
599         data.add(new Triple(n2, p, Functor.makeFunctorNode("f", new Node[] {
600                                         a, Util.makeIntNode(0) })));
601         data.add(new Triple(n3, p, Functor.makeFunctorNode("f", new Node[] {
602                a, Node.createLiteral(new LiteralLabel("0", "", XSDDatatype.XSDnonNegativeInteger)) } )));
603         InfGraph infgraph = createReasoner(ruleList).bind(data);
604         
605         TestUtil.assertIteratorValues(this, infgraph.find(null, s, null),
606             new Triple[] {
607                 new Triple(n2, s, res),
608                 new Triple(n3, s, res),
609             });
610     }
611     
612     /**
613      * Test the builtins themselves
614      */

615     public void testBuiltins2() {
616         // Numeric comparisions
617
Node lt = Node.createURI("lt");
618         Node gt = Node.createURI("gt");
619         Node le = Node.createURI("le");
620         Node ge = Node.createURI("ge");
621         Node eq = Node.createURI("eq");
622         Node ne = Node.createURI("ne");
623         String JavaDoc rules =
624         "[r1: (?x q ?vx), (?y q ?vy), lessThan(?vx, ?vy) -> (?x lt ?y)]" +
625         "[r2: (?x q ?vx), (?y q ?vy), greaterThan(?vx, ?vy) -> (?x gt ?y)]" +
626         "[r3: (?x q ?vx), (?y q ?vy), le(?vx, ?vy) -> (?x le ?y)]" +
627         "[r4: (?x q ?vx), (?y q ?vy), ge(?vx, ?vy) -> (?x ge ?y)]" +
628         "[r5: (?x q ?vx), (?y q ?vy), notEqual(?vx, ?vy) -> (?x ne ?y)]" +
629         "[r6: (?x q ?vx), (?y q ?vy), equal(?vx, ?vy) -> (?x eq ?y)]" +
630                        "";
631         List ruleList = Rule.parseRules(rules);
632         Graph data = new GraphMem();
633         data.add(new Triple(n1, q, Util.makeIntNode(2)) );
634         data.add(new Triple(n2, q, Util.makeIntNode(2)) );
635         data.add(new Triple(n3, q, Util.makeIntNode(3)) );
636         InfGraph infgraph = createReasoner(ruleList).bind(data);
637         
638         TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n2),
639             new Triple[] {
640                 new Triple(n1, eq, n2),
641                 new Triple(n1, le, n2),
642                 new Triple(n1, ge, n2),
643             });
644         TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n3),
645             new Triple[] {
646                 new Triple(n1, ne, n3),
647                 new Triple(n1, lt, n3),
648                 new Triple(n1, le, n3),
649             });
650         TestUtil.assertIteratorValues(this, infgraph.find(n3, null, n1),
651             new Triple[] {
652                 new Triple(n3, ne, n1),
653                 new Triple(n3, gt, n1),
654                 new Triple(n3, ge, n1),
655             });
656         
657         // Floating point comparisons
658
data = new GraphMem();
659         data.add(new Triple(n1, q, Util.makeIntNode(2)) );
660         data.add(new Triple(n2, q, Util.makeDoubleNode(2.2)) );
661         data.add(new Triple(n3, q, Util.makeDoubleNode(2.3)) );
662         infgraph = createReasoner(ruleList).bind(data);
663         
664         TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n2),
665             new Triple[] {
666                 new Triple(n1, ne, n2),
667                 new Triple(n1, le, n2),
668                 new Triple(n1, lt, n2),
669             });
670         TestUtil.assertIteratorValues(this, infgraph.find(n2, null, n3),
671             new Triple[] {
672                 new Triple(n2, ne, n3),
673                 new Triple(n2, le, n3),
674                 new Triple(n2, lt, n3),
675             });
676             
677         // XSD timeDate point comparisons
678
data = new GraphMem();
679         XSDDatatype dt = new XSDDatatype("dateTime");
680         data.add(new Triple(n1, q, Node.createLiteral("2000-03-04T20:00:00Z", "", XSDDatatype.XSDdateTime)));
681         data.add(new Triple(n2, q, Node.createLiteral("2001-03-04T20:00:00Z", "", XSDDatatype.XSDdateTime)));
682         data.add(new Triple(n3, q, Node.createLiteral("2002-03-04T20:00:00Z", "", XSDDatatype.XSDdateTime)));
683         infgraph = createReasoner(ruleList).bind(data);
684                
685         TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n2),
686             new Triple[] {
687                 new Triple(n1, ne, n2),
688                 new Triple(n1, le, n2),
689                 new Triple(n1, lt, n2),
690             });
691         TestUtil.assertIteratorValues(this, infgraph.find(n2, null, n3),
692             new Triple[] {
693                 new Triple(n2, ne, n3),
694                 new Triple(n2, le, n3),
695                 new Triple(n2, lt, n3),
696             });
697         TestUtil.assertIteratorValues(this, infgraph.find(n2, null, n1),
698             new Triple[] {
699                 new Triple(n2, ne, n1),
700                 new Triple(n2, ge, n1),
701                 new Triple(n2, gt, n1),
702             });
703         TestUtil.assertIteratorValues(this, infgraph.find(n3, null, n2),
704             new Triple[] {
705                 new Triple(n3, ne, n2),
706                 new Triple(n3, ge, n2),
707                 new Triple(n3, gt, n2),
708             });
709                     
710         // Arithmetic
711
rules =
712         "[r1: (?x p ?a), (?x q ?b), sum(?a, ?b, ?c) -> (?x s ?c)]" +
713         "[r2: (?x p ?a), (?x q ?b), product(?a, ?b, ?c) -> (?x t ?c)]" +
714         "[r3: (?x p ?a), (?x q ?b), difference(?b, ?a, ?c) -> (?x u ?c)]" +
715                        "";
716         ruleList = Rule.parseRules(rules);
717         data = new GraphMem();
718         data.add(new Triple(n1, p, Util.makeIntNode(3)) );
719         data.add(new Triple(n1, q, Util.makeIntNode(5)) );
720         infgraph = createReasoner(ruleList).bind(data);
721         
722         TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null),
723             new Triple[] {
724                 new Triple(n1, p, Util.makeIntNode(3)),
725                 new Triple(n1, q, Util.makeIntNode(5)),
726                 new Triple(n1, s, Util.makeIntNode(8)),
727                 new Triple(n1, t, Util.makeIntNode(15)),
728                 new Triple(n1, u, Util.makeIntNode(2)),
729             });
730          
731         // Note type checking
732
rules =
733         "[r1: (?x p ?y), isLiteral(?y) -> (?x s 'literal')]" +
734         "[r1: (?x p ?y), notLiteral(?y) -> (?x s 'notLiteral')]" +
735         "[r1: (?x p ?y), isBNode(?y) -> (?x s 'bNode')]" +
736         "[r1: (?x p ?y), notBNode(?y) -> (?x s 'notBNode')]" +
737                        "";
738         ruleList = Rule.parseRules(rules);
739         data = new GraphMem();
740         data.add(new Triple(n1, p, Util.makeIntNode(3)) );
741         data.add(new Triple(n2, p, res));
742         data.add(new Triple(n3, p, Node.createAnon()));
743         infgraph = createReasoner(ruleList).bind(data);
744         
745         TestUtil.assertIteratorValues(this, infgraph.find(n1, s, null),
746             new Triple[] {
747                 new Triple(n1, s, Node.createLiteral("literal", "", null)),
748                 new Triple(n1, s, Node.createLiteral("notBNode", "", null)),
749             });
750         TestUtil.assertIteratorValues(this, infgraph.find(n2, s, null),
751             new Triple[] {
752                 new Triple(n2, s, Node.createLiteral("notLiteral", "", null)),
753                 new Triple(n2, s, Node.createLiteral("notBNode", "", null)),
754             });
755         TestUtil.assertIteratorValues(this, infgraph.find(n3, s, null),
756             new Triple[] {
757                 new Triple(n3, s, Node.createLiteral("notLiteral", "", null)),
758                 new Triple(n3, s, Node.createLiteral("bNode", "", null)),
759             });
760          
761         // Data type checking
762
rules =
763         "[r1: (?x p ?y), isDType(?y, rdfs:Literal) -> (?x s 'isLiteral')]" +
764         "[r1: (?x p ?y), isDType(?y, http://www.w3.org/2001/XMLSchema#int) -> (?x s 'isXSDInt')]" +
765         "[r1: (?x p ?y), isDType(?y, http://www.w3.org/2001/XMLSchema#string) -> (?x s 'isXSDString')]" +
766         "[r1: (?x p ?y), notDType(?y, rdfs:Literal) -> (?x s 'notLiteral')]" +
767         "[r1: (?x p ?y), notDType(?y, http://www.w3.org/2001/XMLSchema#int) -> (?x s 'notXSDInt')]" +
768         "[r1: (?x p ?y), notDType(?y, http://www.w3.org/2001/XMLSchema#string) -> (?x s 'notXSDString')]" +
769                        "";
770         ruleList = Rule.parseRules(rules);
771         data = new GraphMem();
772         data.add(new Triple(n1, p, Util.makeIntNode(3)) );
773         data.add(new Triple(n2, p, Node.createLiteral("foo", "", null)) );
774         data.add(new Triple(n3, p, Node.createLiteral("foo", "", XSDDatatype.XSDstring)) );
775         data.add(new Triple(n4, p, n4));
776         data.add(new Triple(n5, p, Node.createLiteral("-1", "", XSDDatatype.XSDnonNegativeInteger)) );
777         infgraph = createReasoner(ruleList).bind(data);
778         
779         TestUtil.assertIteratorValues(this, infgraph.find(null, s, null),
780             new Triple[] {
781                 new Triple(n1, s, Node.createLiteral("isLiteral", "", null)),
782                 new Triple(n1, s, Node.createLiteral("isXSDInt", "", null)),
783                 new Triple(n1, s, Node.createLiteral("notXSDString", "", null)),
784
785                 new Triple(n2, s, Node.createLiteral("isLiteral", "", null)),
786                 new Triple(n2, s, Node.createLiteral("notXSDInt", "", null)),
787                 new Triple(n2, s, Node.createLiteral("isXSDString", "", null)),
788
789                 new Triple(n3, s, Node.createLiteral("isLiteral", "", null)),
790                 new Triple(n3, s, Node.createLiteral("notXSDInt", "", null)),
791                 new Triple(n3, s, Node.createLiteral("isXSDString", "", null)),
792
793                 new Triple(n4, s, Node.createLiteral("notLiteral", "", null)),
794                 new Triple(n4, s, Node.createLiteral("notXSDInt", "", null)),
795                 new Triple(n4, s, Node.createLiteral("notXSDString", "", null)),
796
797                 new Triple(n5, s, Node.createLiteral("notLiteral", "", null)),
798                 new Triple(n5, s, Node.createLiteral("notXSDInt", "", null)),
799                 new Triple(n5, s, Node.createLiteral("notXSDString", "", null)),
800             });
801             
802         // Literal counting
803
rules = "[r1: (?x p ?y), countLiteralValues(?x, p, ?c) -> (?x s ?c)]";
804         ruleList = Rule.parseRules(rules);
805         data = new GraphMem();
806         data.add(new Triple(n1, p, Util.makeIntNode(2)) );
807         data.add(new Triple(n1, p, Util.makeIntNode(2)) );
808         data.add(new Triple(n1, p, Util.makeIntNode(3)) );
809         data.add(new Triple(n1, p, n2) );
810         infgraph = createReasoner(ruleList).bind(data);
811         TestUtil.assertIteratorValues(this, infgraph.find(n1, s, null),
812             new Triple[] {
813                 new Triple(n1, s, Util.makeIntNode(2)),
814             });
815         
816         // Map list operation
817
rules = "[r1: (n1 p ?l) -> listMapAsSubject(?l, q, C1)]" +
818                 "[r2: (n1 p ?l) -> listMapAsObject ( a, q, ?l)]";
819         ruleList = Rule.parseRules(rules);
820         data = new GraphMem();
821         data.add(new Triple(n1, p, Util.makeList(new Node[]{b, c, d}, data) ));
822         infgraph = createReasoner(ruleList).bind(data);
823         TestUtil.assertIteratorValues(this, infgraph.find(null, q, null),
824             new Triple[] {
825                 new Triple(b, q, C1),
826                 new Triple(c, q, C1),
827                 new Triple(d, q, C1),
828                 new Triple(a, q, b),
829                 new Triple(a, q, c),
830                 new Triple(a, q, d),
831             });
832     }
833          
834     
835     /**
836      * Helper - returns the single object value for an s/p pair, asserts an error
837      * if there is more than one.
838      */

839     private Node getValue(Graph g, Node s, Node p) {
840         ExtendedIterator i = g.find(s, p, null);
841         assertTrue(i.hasNext());
842         Node result = ((Triple)i.next()).getObject();
843         if (i.hasNext()) {
844             assertTrue("multiple values not expected", false);
845             i.close();
846         }
847         return result;
848     }
849
850     /**
851      * Investigate a suspicious case in the OWL ruleset, is the backchainer
852      * returning duplicate values?
853      */

854     public void testDuplicatesEC4() throws IOException JavaDoc {
855         boolean prior = JenaParameters.enableFilteringOfHiddenInfNodes;
856         try {
857             JenaParameters.enableFilteringOfHiddenInfNodes = false;
858             Model premisesM = FileManager.get().loadModel("file:testing/wg/equivalentClass/premises004.rdf");
859             Graph data = premisesM.getGraph();
860             Reasoner reasoner = new OWLFBRuleReasoner(OWLFBRuleReasonerFactory.theInstance());
861             InfGraph infgraph = reasoner.bind(data);
862             Node rbPrototypeProp = Node.createURI(ReasonerVocabulary.RBNamespace+"prototype");
863             int count = 0;
864             for (Iterator i = infgraph.find(null, rbPrototypeProp, null); i.hasNext(); ) {
865                 Object JavaDoc t = i.next();
866     // System.out.println(" - " + PrintUtil.print(t));
867
count++;
868             }
869     // listFBGraph("direct databind case", (FBRuleInfGraph)infgraph);
870
assertEquals(5, count);
871             
872             infgraph = reasoner.bindSchema(data).bind(new GraphMem());
873             count = 0;
874             for (Iterator i = infgraph.find(null, rbPrototypeProp, null); i.hasNext(); ) {
875                 Object JavaDoc t = i.next();
876     // System.out.println(" - " + PrintUtil.print(t));
877
count++;
878             }
879     // listFBGraph("bindSchema case", (FBRuleInfGraph)infgraph);
880
assertEquals(5, count);
881         } finally {
882             JenaParameters.enableFilteringOfHiddenInfNodes = prior;
883         }
884     }
885     
886     /**
887      * Check cost of creating an empty OWL closure.
888      */

889     public void temp() {
890         Graph data = new GraphMem();
891         Graph data2 = new GraphMem();
892         Reasoner reasoner = new OWLFBRuleReasoner(OWLFBRuleReasonerFactory.theInstance());
893         FBRuleInfGraph infgraph = (FBRuleInfGraph)reasoner.bind(data);
894         FBRuleInfGraph infgraph2 = (FBRuleInfGraph)reasoner.bind(data2);
895         long t1 = System.currentTimeMillis();
896         infgraph.prepare();
897         long t2 = System.currentTimeMillis();
898         System.out.println("Prepare on empty graph = " + (t2-t1) +"ms");
899         t1 = System.currentTimeMillis();
900         infgraph2.prepare();
901         t2 = System.currentTimeMillis();
902         System.out.println("Prepare on empty graph = " + (t2-t1) +"ms");
903     }
904     
905     /**
906      * Helper function to list a graph out to logger.info
907      */

908     public void listGraph(Graph g) {
909         for (Iterator i = g.find(null,null,null); i.hasNext();) {
910             Triple t = (Triple)i.next();
911             logger.info(PrintUtil.print(t));
912         }
913         logger.info(" -------- ");
914     }
915     
916     /**
917      * Helper function to list the interesting parts of an FBInfGraph.
918      */

919     public void listFBGraph(String JavaDoc message, FBRuleInfGraph graph) {
920         logger.info(message);
921         logger.info("Raw graph data");
922         listGraph(graph.getRawGraph());
923         logger.info("Static deductions");
924         listGraph(graph.getDeductionsGraph());
925     }
926     
927 }
928
929
930 /*
931     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
932     All rights reserved.
933
934     Redistribution and use in source and binary forms, with or without
935     modification, are permitted provided that the following conditions
936     are met:
937
938     1. Redistributions of source code must retain the above copyright
939        notice, this list of conditions and the following disclaimer.
940
941     2. Redistributions in binary form must reproduce the above copyright
942        notice, this list of conditions and the following disclaimer in the
943        documentation and/or other materials provided with the distribution.
944
945     3. The name of the author may not be used to endorse or promote products
946        derived from this software without specific prior written permission.
947
948     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
949     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
950     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
951     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
952     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
953     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
954     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
955     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
956     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
957     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
958 */
Popular Tags