1 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 ; 25 26 32 public class TestReasoners extends TestCase { 33 34 37 public TestReasoners( String name ) { 38 super( name ); 39 } 40 41 45 public static TestSuite suite() { 46 return new TestSuite(TestReasoners.class); 47 } 48 49 52 public void testTransitiveReasoner() throws IOException { 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 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 [] { 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 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 [] { 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 [] { 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 [] { 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 [] { 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 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 [] { 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 [] { 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 [] { 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 [] { 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 [] { 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 [] { 179 new Triple(b, closedP, b) 180 }); 181 TestUtil.assertIteratorValues(this, data.find(null, RDFS.subClassOf.asNode(), null), 182 new Object [] { 183 new Triple(a, closedP, b), 184 new Triple(c, closedP, d), 185 new Triple(d, closedP, e) 186 }); 187 } 188 189 192 public void testTransitiveMetaLevel() { 193 doTestMetaLevel(TransitiveReasonerFactory.theInstance()); 194 } 195 196 199 public void testRDFSMetaLevel() { 200 doTestMetaLevel(RDFSRuleReasonerFactory.theInstance()); 201 } 202 203 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 [] { 222 }); 223 infgraph.add(new Triple(p, q, sC)); 224 TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), 225 new Object [] { 226 }); 227 infgraph.add(new Triple(q, sP, sP)); 228 TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), 229 new Object [] { 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 [] { 237 }); 238 } 239 240 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 [] { 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 [] { 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 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 [] { 287 data.createStatement(b, RDF.type, RDFS.Resource ), 288 data.createStatement(b, RDF.type, C ) 289 }); 290 291 } 292 293 296 public void testTransitiveFindWithPremises() { 297 doTestFindWithPremises(TransitiveReasonerFactory.theInstance()); 298 } 299 300 303 public void testRDFSFindWithPremises() { 304 doTestFindWithPremises(RDFSRuleReasonerFactory.theInstance()); 305 } 306 307 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 [] { 324 }); 325 TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null, premise), 326 new Object [] { 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 [] { 333 }); 334 335 } 336 337 340 public void testDuplicateStatements() { 341 String 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 396 397 | Popular Tags |