KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > test > TestReasoners


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

10 package com.hp.hpl.jena.reasoner.test;
11
12 import com.hp.hpl.jena.reasoner.transitiveReasoner.*;
13 import com.hp.hpl.jena.reasoner.rdfsReasoner1.*;
14 import com.hp.hpl.jena.reasoner.rulesys.RDFSRuleReasonerFactory;
15 import com.hp.hpl.jena.reasoner.*;
16 import com.hp.hpl.jena.rdf.model.*;
17 import com.hp.hpl.jena.graph.*;
18 import com.hp.hpl.jena.mem.GraphMem;
19 import com.hp.hpl.jena.ontology.*;
20 import com.hp.hpl.jena.vocabulary.*;
21
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24 import java.io.IOException JavaDoc;
25
26 /**
27  * Unit tests for initial experimental reasoners
28  *
29  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
30  * @version $Revision: 1.27 $ on $Date: 2005/02/21 12:18:17 $
31  */

32 public class TestReasoners extends TestCase {
33     
34     /**
35      * Boilerplate for junit
36      */

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

45     public static TestSuite suite() {
46         return new TestSuite(TestReasoners.class);
47     }
48
49     /**
50      * Test the basic functioning of a Transitive closure cache
51      */

52     public void testTransitiveReasoner() throws IOException JavaDoc {
53         ReasonerTester tester = new ReasonerTester("transitive/manifest.rdf");
54         ReasonerFactory rf = TransitiveReasonerFactory.theInstance();
55         assertTrue("transitive reasoner tests", tester.runTests(rf, this, null));
56     }
57
58     /**
59      * Test rebind operation for the transitive reasoner
60      */

61     public void testTransitiveRebind() {
62         Graph data = new GraphMem();
63         Node C1 = Node.createURI("C1");
64         Node C2 = Node.createURI("C2");
65         Node C3 = Node.createURI("C3");
66         Node C4 = Node.createURI("C4");
67         data.add( new Triple(C1, RDFS.subClassOf.asNode(), C2) );
68         data.add( new Triple(C2, RDFS.subClassOf.asNode(), C3) );
69         Reasoner reasoner = TransitiveReasonerFactory.theInstance().create(null);
70         assertTrue(reasoner.supportsProperty(RDFS.subClassOf));
71         assertTrue(! reasoner.supportsProperty(RDFS.domain));
72         InfGraph infgraph = reasoner.bind(data);
73         TestUtil.assertIteratorValues(this,
74             infgraph.find(C1, null, null),
75             new Object JavaDoc[] {
76                 new Triple(C1, RDFS.subClassOf.asNode(), C1),
77                 new Triple(C1, RDFS.subClassOf.asNode(), C2),
78                 new Triple(C1, RDFS.subClassOf.asNode(), C3)
79             } );
80         Graph data2 = new GraphMem();
81         data2.add( new Triple(C1, RDFS.subClassOf.asNode(), C2) );
82         data2.add( new Triple(C2, RDFS.subClassOf.asNode(), C4) );
83         infgraph.rebind(data2);
84             
85         // Incremental additions
86
Node a = Node.createURI("a");
87         Node b = Node.createURI("b");
88         Node c = Node.createURI("c");
89         infgraph.add(new Triple(a, RDFS.subClassOf.asNode(), b));
90         infgraph.add(new Triple(b, RDFS.subClassOf.asNode(), c));
91         TestUtil.assertIteratorValues(this,
92             infgraph.find(b, RDFS.subClassOf.asNode(), null),
93             new Object JavaDoc[] {
94                 new Triple(b, RDFS.subClassOf.asNode(), c),
95                 new Triple(b, RDFS.subClassOf.asNode(), b)
96             } );
97         TestUtil.assertIteratorValues(this,
98             infgraph.find(a, RDFS.subClassOf.asNode(), null),
99             new Object JavaDoc[] {
100                 new Triple(a, RDFS.subClassOf.asNode(), a),
101                 new Triple(a, RDFS.subClassOf.asNode(), b),
102                 new Triple(a, RDFS.subClassOf.asNode(), c)
103             } );
104         Node p = Node.createURI("p");
105         Node q = Node.createURI("q");
106         Node r = Node.createURI("r");
107         infgraph.add(new Triple(p, RDFS.subPropertyOf.asNode(), q));
108         infgraph.add(new Triple(q, RDFS.subPropertyOf.asNode(), r));
109         TestUtil.assertIteratorValues(this,
110             infgraph.find(q, RDFS.subPropertyOf.asNode(), null),
111             new Object JavaDoc[] {
112                 new Triple(q, RDFS.subPropertyOf.asNode(), q),
113                 new Triple(q, RDFS.subPropertyOf.asNode(), r)
114             } );
115         TestUtil.assertIteratorValues(this,
116             infgraph.find(p, RDFS.subPropertyOf.asNode(), null),
117             new Object JavaDoc[] {
118                 new Triple(p, RDFS.subPropertyOf.asNode(), p),
119                 new Triple(p, RDFS.subPropertyOf.asNode(), q),
120                 new Triple(p, RDFS.subPropertyOf.asNode(), r)
121             } );
122     }
123     
124     /**
125      * Test delete operation for Transtive reasoner.
126      */

127     public void testTransitiveRemove() {
128         Graph data = new GraphMem();
129         Node a = Node.createURI("a");
130         Node b = Node.createURI("b");
131         Node c = Node.createURI("c");
132         Node d = Node.createURI("d");
133         Node e = Node.createURI("e");
134         Node closedP = RDFS.subClassOf.asNode();
135         data.add( new Triple(a, RDFS.subClassOf.asNode(), b) );
136         data.add( new Triple(a, RDFS.subClassOf.asNode(), c) );
137         data.add( new Triple(b, RDFS.subClassOf.asNode(), d) );
138         data.add( new Triple(c, RDFS.subClassOf.asNode(), d) );
139         data.add( new Triple(d, RDFS.subClassOf.asNode(), e) );
140         Reasoner reasoner = TransitiveReasonerFactory.theInstance().create(null);
141         InfGraph infgraph = reasoner.bind(data);
142         TestUtil.assertIteratorValues(this, infgraph.find(a, RDFS.subClassOf.asNode(), null),
143             new Object JavaDoc[] {
144                 new Triple(a, closedP, a),
145                 new Triple(a, closedP, b),
146                 new Triple(a, closedP, b),
147                 new Triple(a, closedP, c),
148                 new Triple(a, closedP, d),
149                 new Triple(a, closedP, e)
150             });
151         TestUtil.assertIteratorValues(this, infgraph.find(b, RDFS.subClassOf.asNode(), null),
152             new Object JavaDoc[] {
153                 new Triple(b, closedP, b),
154                 new Triple(b, closedP, d),
155                 new Triple(b, closedP, e)
156             });
157         infgraph.delete(new Triple(b, closedP, d));
158         TestUtil.assertIteratorValues(this, infgraph.find(a, RDFS.subClassOf.asNode(), null),
159             new Object JavaDoc[] {
160                 new Triple(a, closedP, a),
161                 new Triple(a, closedP, b),
162                 new Triple(a, closedP, b),
163                 new Triple(a, closedP, c),
164                 new Triple(a, closedP, d),
165                 new Triple(a, closedP, e)
166             });
167         TestUtil.assertIteratorValues(this, infgraph.find(b, RDFS.subClassOf.asNode(), null),
168             new Object JavaDoc[] {
169                 new Triple(b, closedP, b),
170             });
171         infgraph.delete(new Triple(a, closedP, c));
172         TestUtil.assertIteratorValues(this, infgraph.find(a, RDFS.subClassOf.asNode(), null),
173             new Object JavaDoc[] {
174                 new Triple(a, closedP, a),
175                 new Triple(a, closedP, b)
176             });
177         TestUtil.assertIteratorValues(this, infgraph.find(b, RDFS.subClassOf.asNode(), null),
178             new Object JavaDoc[] {
179                 new Triple(b, closedP, b)
180             });
181         TestUtil.assertIteratorValues(this, data.find(null, RDFS.subClassOf.asNode(), null),
182             new Object JavaDoc[] {
183                 new Triple(a, closedP, b),
184                 new Triple(c, closedP, d),
185                 new Triple(d, closedP, e)
186             });
187     }
188   
189     /**
190      * Test metalevel add/remove subproperty operations for transitive reasoner.
191      */

192     public void testTransitiveMetaLevel() {
193         doTestMetaLevel(TransitiveReasonerFactory.theInstance());
194     }
195   
196     /**
197      * Test metalevel add/remove subproperty operations for rdsf reasoner.
198      */

199     public void testRDFSMetaLevel() {
200         doTestMetaLevel(RDFSRuleReasonerFactory.theInstance());
201     }
202     
203     /**
204      * Test metalevel add/remove subproperty operations for a reasoner.
205      */

206     public void doTestMetaLevel(ReasonerFactory rf) {
207         Graph data = new GraphMem();
208         Node c1 = Node.createURI("C1");
209         Node c2 = Node.createURI("C2");
210         Node c3 = Node.createURI("C3");
211         Node p = Node.createURI("p");
212         Node q = Node.createURI("q");
213         Node sC = RDFS.subClassOf.asNode();
214         Node sP = RDFS.subPropertyOf.asNode();
215         Node ty = RDF.type.asNode();
216         data.add( new Triple(c2, sC, c3));
217         data.add( new Triple(c1, p, c2));
218         Reasoner reasoner = rf.create(null);
219         InfGraph infgraph = reasoner.bind(data);
220         TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null),
221             new Object JavaDoc[] {
222             });
223         infgraph.add(new Triple(p, q, sC));
224         TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null),
225             new Object JavaDoc[] {
226             });
227         infgraph.add(new Triple(q, sP, sP));
228         TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null),
229             new Object JavaDoc[] {
230                 new Triple(c1, sC, c1),
231                 new Triple(c1, sC, c2),
232                 new Triple(c1, sC, c3)
233             });
234         infgraph.delete(new Triple(p, q, sC));
235         TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null),
236             new Object JavaDoc[] {
237             });
238     }
239  
240     /**
241      * Test rebind operation for the RDFS1 reasoner
242      */

243     public void testRDFSRebind() {
244         Graph data = new GraphMem();
245         Node C1 = Node.createURI("C1");
246         Node C2 = Node.createURI("C2");
247         Node C3 = Node.createURI("C3");
248         Node C4 = Node.createURI("C4");
249         data.add( new Triple(C1, RDFS.subClassOf.asNode(), C2) );
250         data.add( new Triple(C2, RDFS.subClassOf.asNode(), C3) );
251         Reasoner reasoner = RDFSReasonerFactory.theInstance().create(null);
252         InfGraph infgraph = reasoner.bind(data);
253         TestUtil.assertIteratorValues(this,
254             infgraph.find(C1, RDFS.subClassOf.asNode(), null),
255             new Object JavaDoc[] {
256                 new Triple(C1, RDFS.subClassOf.asNode(), C1),
257                 new Triple(C1, RDFS.subClassOf.asNode(), C2),
258                 new Triple(C1, RDFS.subClassOf.asNode(), C3)
259             } );
260         Graph data2 = new GraphMem();
261         data2.add( new Triple(C1, RDFS.subClassOf.asNode(), C2) );
262         data2.add( new Triple(C2, RDFS.subClassOf.asNode(), C4) );
263         infgraph.rebind(data2);
264         TestUtil.assertIteratorValues(this,
265             infgraph.find(C1, RDFS.subClassOf.asNode(), null),
266             new Object JavaDoc[] {
267                 new Triple(C1, RDFS.subClassOf.asNode(), C1),
268                 new Triple(C1, RDFS.subClassOf.asNode(), C2),
269                 new Triple(C1, RDFS.subClassOf.asNode(), C4)
270             } );
271     }
272  
273     /**
274      * Test the ModelFactory interface
275      */

276     public void testModelFactoryRDFS() {
277         Model data = ModelFactory.createDefaultModel();
278         Property p = data.createProperty("urn:x-hp:ex/p");
279         Resource a = data.createResource("urn:x-hp:ex/a");
280         Resource b = data.createResource("urn:x-hp:ex/b");
281         Resource C = data.createResource("urn:x-hp:ex/c");
282         data.add(p, RDFS.range, C)
283             .add(a, p, b);
284         Model result = ModelFactory.createRDFSModel(data);
285         StmtIterator i = result.listStatements( b, RDF.type, (RDFNode)null );
286         TestUtil.assertIteratorValues(this, i, new Object JavaDoc[] {
287             data.createStatement(b, RDF.type, RDFS.Resource ),
288             data.createStatement(b, RDF.type, C )
289         });
290         
291     }
292
293     /**
294      * Run test on findWithPremies for Transitive reasoner.
295      */

296     public void testTransitiveFindWithPremises() {
297         doTestFindWithPremises(TransitiveReasonerFactory.theInstance());
298     }
299
300     /**
301      * Run test on findWithPremies for RDFS reasoner.
302      */

303     public void testRDFSFindWithPremises() {
304         doTestFindWithPremises(RDFSRuleReasonerFactory.theInstance());
305     }
306     
307     /**
308      * Test a reasoner's ability to implement find with premises.
309      * Assumes the reasoner can at least implement RDFS subClassOf.
310      */

311     public void doTestFindWithPremises(ReasonerFactory rf) {
312         Node c1 = Node.createURI("C1");
313         Node c2 = Node.createURI("C2");
314         Node c3 = Node.createURI("C3");
315         Node sC = RDFS.subClassOf.asNode();
316         Graph data = new GraphMem();
317         data.add( new Triple(c2, sC, c3));
318         Graph premise = new GraphMem();
319         premise.add( new Triple(c1, sC, c2));
320         Reasoner reasoner = rf.create(null);
321         InfGraph infgraph = reasoner.bind(data);
322         TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null),
323             new Object JavaDoc[] {
324             });
325         TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null, premise),
326             new Object JavaDoc[] {
327                 new Triple(c1, sC, c2),
328                 new Triple(c1, sC, c3),
329                 new Triple(c1, sC, c1)
330             });
331         TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null),
332             new Object JavaDoc[] {
333             });
334         
335     }
336         
337     /**
338      * Test for duplicate statements in a constructed ontology.
339      */

340     public void testDuplicateStatements() {
341         String JavaDoc NS = "http://swt/test#";
342          OntModelSpec s = new OntModelSpec(ModelFactory.createMemModelMaker(),
343                                      null, null, ProfileRegistry.DAML_LANG);
344          OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF, null);
345  
346          OntClass documentC = model.createClass(NS + "DOCUMENT");
347          OntClass topicC = model.createClass(NS + "TOPIC");
348  
349          ObjectProperty hasTopicP = model.createObjectProperty(NS + "hasTopic");
350          hasTopicP.addDomain(documentC);
351          hasTopicP.addRange(topicC);
352          ObjectProperty hasDocP = model.createObjectProperty(NS + "hasDocument");
353          hasDocP.addDomain(topicC);
354          hasDocP.addRange(documentC);
355          hasDocP.setInverseOf(hasTopicP);
356  
357          Individual fooTopic = model.createIndividual(NS + "fooTopic", topicC);
358          Individual fooDoc = model.createIndividual(NS + "fooDoc", documentC);
359  
360          fooDoc.addProperty(hasTopicP, fooTopic);
361  
362          TestUtil.assertIteratorLength(fooDoc.listProperties(hasTopicP), 1);
363     }
364     
365 }
366
367 /*
368     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
369     All rights reserved.
370
371     Redistribution and use in source and binary forms, with or without
372     modification, are permitted provided that the following conditions
373     are met:
374
375     1. Redistributions of source code must retain the above copyright
376        notice, this list of conditions and the following disclaimer.
377
378     2. Redistributions in binary form must reproduce the above copyright
379        notice, this list of conditions and the following disclaimer in the
380        documentation and/or other materials provided with the distribution.
381
382     3. The name of the author may not be used to endorse or promote products
383        derived from this software without specific prior written permission.
384
385     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
386     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
387     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
388     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
389     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
390     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
391     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
392     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
393     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
394     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
395 */

396
397
Popular Tags