1 10 package com.hp.hpl.jena.reasoner.rulesys.test; 11 12 import com.hp.hpl.jena.graph.query.*; 13 import com.hp.hpl.jena.graph.*; 14 import com.hp.hpl.jena.ontology.OntModelSpec; 15 import com.hp.hpl.jena.rdf.model.*; 16 import com.hp.hpl.jena.reasoner.*; 17 import com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph; 18 import com.hp.hpl.jena.reasoner.test.WGReasonerTester; 19 import com.hp.hpl.jena.vocabulary.*; 20 21 import java.io.*; 22 import java.util.*; 23 24 32 public class WebOntTestHarness { 33 34 37 38 public static boolean includeModified = false; 39 40 41 public static boolean approvedOnly = true; 42 43 44 public static boolean printProfile = false; 45 46 49 50 Reasoner reasoner; 51 52 53 Model testDefinitions; 54 55 56 int testCount = 0; 57 58 59 long lastTestDuration = 0; 60 61 62 int passCount = 0; 63 64 65 Model testResults; 66 67 68 Resource jena2; 69 70 73 74 public static final String BASE_TESTDIR = "testing/wg/"; 75 76 77 public static String BASE_URI = "http://www.w3.org/2002/03owlt/"; 78 79 80 public static String BASE_RESULTS_URI = "http://jena.sourceforge.net/data/owl-results.rdf"; 81 82 83 public static final String [] TEST_DIRS= {"AllDifferent", "AllDistinct", 84 "AnnotationProperty", "DatatypeProperty", "FunctionalProperty", 85 "I3.2", "I3.4", "I4.1", "I4.5", "I4.6", "I5.1", "I5.2", "I5.21", "I5.24", 86 "I5.26", "I5.3", "I5.5", "I5.8", "InverseFunctionalProperty", "Nothing", 87 "Restriction", "SymmetricProperty", "Thing", "TransitiveProperty", 88 "allValuesFrom", "amp-in-url", "cardinality", "complementOf", "datatypes", 89 "differentFrom", "disjointWith", "distinctMembers", 90 "equivalentClass", "equivalentProperty", "imports", 91 "intersectionOf", "inverseOf", "localtests", "maxCardinality", "miscellaneous", 92 "oneOf", "oneOfDistinct", "sameAs", "sameClassAs", "sameIndividualAs", 93 "samePropertyAs", "someValuesFrom", "statement-entailment", "unionOf", 94 "xmlbase", 95 "description-logic", 96 }; 98 99 100 public static final String [] BLOCKED_TESTS = { 101 "http://www.w3.org/2002/03owlt/complementOf/Manifest001#test", 103 "http://www.w3.org/2002/03owlt/description-logic/Manifest901#test", 104 "http://www.w3.org/2002/03owlt/description-logic/Manifest903#test", 105 "http://www.w3.org/2002/03owlt/description-logic/Manifest902#test", 106 "http://www.w3.org/2002/03owlt/description-logic/Manifest904#test", 107 "http://www.w3.org/2002/03owlt/oneOf/Manifest004#test", 110 "http://www.w3.org/2002/03owlt/unionOf/Manifest001#test", 111 "http://www.w3.org/2002/03owlt/unionOf/Manifest002#test", 112 "http://www.w3.org/2002/03owlt/unionOf/Manifest003#test", 113 "http://www.w3.org/2002/03owlt/unionOf/Manifest004#test", 114 "http://www.w3.org/2002/03owlt/equivalentClass/Manifest006#test", 115 "http://www.w3.org/2002/03owlt/equivalentClass/Manifest007#test", 116 "http://www.w3.org/2002/03owlt/description-logic/Manifest201#test", 117 "http://www.w3.org/2002/03owlt/I5.8/Manifest004#test", 118 "http://www.w3.org/2002/03owlt/I5.2/Manifest004#test", 119 120 "http://www.w3.org/2002/03owlt/description-logic/Manifest008#test", 121 "http://www.w3.org/2002/03owlt/description-logic/Manifest011#test", 122 "http://www.w3.org/2002/03owlt/description-logic/Manifest015#test", 123 "http://www.w3.org/2002/03owlt/description-logic/Manifest019#test", 124 "http://www.w3.org/2002/03owlt/description-logic/Manifest023#test", 125 "http://www.w3.org/2002/03owlt/description-logic/Manifest026#test", 126 "http://www.w3.org/2002/03owlt/description-logic/Manifest027#test", 127 "http://www.w3.org/2002/03owlt/description-logic/Manifest029#test", 128 "http://www.w3.org/2002/03owlt/description-logic/Manifest030#test", 129 "http://www.w3.org/2002/03owlt/description-logic/Manifest032#test", 130 "http://www.w3.org/2002/03owlt/description-logic/Manifest033#test", 131 "http://www.w3.org/2002/03owlt/description-logic/Manifest035#test", 132 133 "http://www.w3.org/2002/03owlt/description-logic/Manifest101#test", 134 "http://www.w3.org/2002/03owlt/description-logic/Manifest102#test", 135 "http://www.w3.org/2002/03owlt/description-logic/Manifest103#test", 136 "http://www.w3.org/2002/03owlt/description-logic/Manifest104#test", 137 "http://www.w3.org/2002/03owlt/description-logic/Manifest105#test", 138 "http://www.w3.org/2002/03owlt/description-logic/Manifest106#test", 139 "http://www.w3.org/2002/03owlt/description-logic/Manifest107#test", 140 "http://www.w3.org/2002/03owlt/description-logic/Manifest108#test", 141 "http://www.w3.org/2002/03owlt/description-logic/Manifest109#test", 142 "http://www.w3.org/2002/03owlt/description-logic/Manifest110#test", 143 "http://www.w3.org/2002/03owlt/description-logic/Manifest111#test", 144 "http://www.w3.org/2002/03owlt/description-logic/Manifest502#test", 145 "http://www.w3.org/2002/03owlt/description-logic/Manifest504#test", 146 147 "http://www.w3.org/2002/03owlt/description-logic/Manifest202#test", 148 "http://www.w3.org/2002/03owlt/description-logic/Manifest203#test", 149 "http://www.w3.org/2002/03owlt/description-logic/Manifest204#test", 150 "http://www.w3.org/2002/03owlt/description-logic/Manifest205#test", 151 "http://www.w3.org/2002/03owlt/description-logic/Manifest206#test", 152 "http://www.w3.org/2002/03owlt/description-logic/Manifest207#test", 153 "http://www.w3.org/2002/03owlt/description-logic/Manifest208#test", 154 155 "http://www.w3.org/2002/03owlt/description-logic/Manifest209#test", 156 157 "http://www.w3.org/2002/03owlt/miscellaneous/Manifest010#test", 158 "http://www.w3.org/2002/03owlt/miscellaneous/Manifest011#test", 159 "http://www.w3.org/2002/03owlt/SymmetricProperty/Manifest002#test", 160 161 "http://www.w3.org/2002/03owlt/Thing/Manifest005#test", 162 163 "http://www.w3.org/2002/03owlt/TransitiveProperty/Manifest002#test", 165 "http://www.w3.org/2002/03owlt/description-logic/Manifest661#test", 166 "http://www.w3.org/2002/03owlt/description-logic/Manifest662#test", 167 "http://www.w3.org/2002/03owlt/description-logic/Manifest663#test", 168 169 "http://www.w3.org/2002/03owlt/description-logic/Manifest608#test", 170 "http://www.w3.org/2002/03owlt/description-logic/Manifest611#test", 171 "http://www.w3.org/2002/03owlt/description-logic/Manifest615#test", 172 "http://www.w3.org/2002/03owlt/description-logic/Manifest623#test", 173 "http://www.w3.org/2002/03owlt/description-logic/Manifest626#test", 174 "http://www.w3.org/2002/03owlt/description-logic/Manifest627#test", 175 "http://www.w3.org/2002/03owlt/description-logic/Manifest630#test", 176 "http://www.w3.org/2002/03owlt/description-logic/Manifest668#test", 177 "http://www.w3.org/2002/03owlt/description-logic/Manifest668#test", 178 179 }; 180 181 183 public static final String [] STATUS_FLAGS = { "APPROVED", "PROPOSED" }; 184 185 188 public WebOntTestHarness() { 189 testDefinitions = loadAllTestDefinitions(); 190 reasoner = ReasonerRegistry.getOWLReasoner(); 191 initResults(); 192 } 193 194 195 public static Model loadAllTestDefinitions() { 196 System.out.print("Loading manifests "); System.out.flush(); 197 Model testDefs = ModelFactory.createDefaultModel(); 198 int count = 0; 199 for (int idir = 0; idir < TEST_DIRS.length; idir++) { 200 File dir = new File(BASE_TESTDIR + TEST_DIRS[idir]); 201 String [] manifests = dir.list(new FilenameFilter() { 202 public boolean accept(File df, String name) { 203 if (name.startsWith("Manifest") && name.endsWith(".rdf")) { 204 return includeModified || ! name.endsWith("-mod.rdf"); 205 } else { 206 return false; 207 } 208 } 209 }); 210 for (int im = 0; im < manifests.length; im++) { 211 String manifest = manifests[im]; 212 File mf = new File(dir, manifest); 213 try { 214 testDefs.read(new FileInputStream(mf), "file:" + mf); 215 count ++; 216 if (count % 8 == 0) { 217 System.out.print("."); System.out.flush(); 218 } 219 } catch (FileNotFoundException e) { 220 System.out.println("File not readable - " + e); 221 } 222 } 223 } 224 System.out.println("loaded"); 225 return testDefs; 226 } 227 228 231 public void initResults() { 232 testResults = ModelFactory.createDefaultModel(); 233 jena2 = testResults.createResource(BASE_RESULTS_URI + "#jena2"); 234 jena2.addProperty(RDFS.comment, 235 testResults.createLiteral( 236 "<a xmlns=\"http://www.w3.org/1999/xhtml\" HREF=\"http://jena.sourceforce.net/\">Jena2</a> includes a rule-based inference engine for RDF processing, " + 237 "supporting both forward and backward chaining rules. Its OWL rule set is designed to provide sound " + 238 "but not complete instance resasoning for that fragment of OWL/Full limited to the OWL/lite vocabulary. In" + 239 "particular it does not support unionOf/complementOf.", 240 true) 241 ); 242 jena2.addProperty(RDFS.label, "Jena2"); 243 testResults.setNsPrefix("results", OWLResults.NS); 244 } 245 246 249 public static void main(String [] args) throws IOException { 250 String resultFile = "owl-results.rdf"; 251 if (args.length >= 1) { 252 resultFile = args[0]; 253 } 254 WebOntTestHarness harness = new WebOntTestHarness(); 255 harness.runTests(); 256 RDFWriter writer = harness.testResults.getWriter("RDF/XML-ABBREV"); 264 OutputStream stream = new FileOutputStream(resultFile); 265 writer.setProperty("showXmlDeclaration", "true"); 266 harness.testResults.setNsPrefix("", "http://www.w3.org/1999/xhtml"); 267 writer.write(harness.testResults, stream, BASE_RESULTS_URI); 268 } 269 270 273 public void runTests() { 274 System.out.println("Testing " + (approvedOnly ? "only APPROVED" : "APPROVED and PROPOSED") ); 275 System.out.println("Positive entailment: "); 276 runTests(findTestsOfType(OWLTest.PositiveEntailmentTest)); 277 System.out.println("\nTrue tests: "); 280 runTests(findTestsOfType(OWLTest.TrueTest)); 281 System.out.println("\nOWL for OWL tests: "); 282 runTests(findTestsOfType(OWLTest.OWLforOWLTest)); 283 System.out.println("\nImport entailment tests: "); 284 runTests(findTestsOfType(OWLTest.ImportEntailmentTest)); 285 System.out.println("\nInconsistency tests: "); 286 runTests(findTestsOfType(OWLTest.InconsistencyTest)); 287 System.out.println("\nPassed " + passCount + " out of " + testCount); 288 } 289 290 293 public void runTests(List tests) { 294 for (Iterator i = tests.iterator(); i.hasNext(); ) { 295 runTest( (Resource) i.next() ); 296 } 297 } 298 299 303 public void runTest(String test) { 304 runTest(testDefinitions.getResource(test)); 305 } 306 307 311 public void runTest(Resource test) { 312 System.out.println("Running " + test); 313 boolean success = false; 314 boolean fail = false; 315 try { 316 success = doRunTest(test); 317 } catch (Exception e) { 318 fail = true; 319 System.out.print("\nException: " + e); 320 e.printStackTrace(); 321 } 322 testCount++; 323 if (success) { 324 System.out.print( (testCount % 40 == 0) ? ".\n" : "."); 325 System.out.flush(); 326 passCount++; 327 } else { 328 System.out.println("\nFAIL: " + test); 329 } 330 Resource resultType = null; 331 if (fail) { 332 resultType = OWLResults.FailingRun; 333 } else { 334 if (test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest) 335 || test.hasProperty(RDF.type, OWLTest.ConsistencyTest)) { 336 resultType = success ? OWLResults.PassingRun : OWLResults.FailingRun; 337 } else { 338 resultType = success ? OWLResults.PassingRun : OWLResults.IncompleteRun; 339 } 340 } 341 Resource result = testResults.createResource() 343 .addProperty(RDF.type, OWLResults.TestRun) 344 .addProperty(RDF.type, resultType) 345 .addProperty(OWLResults.test, test) 346 .addProperty(OWLResults.system, jena2); 347 } 348 349 352 public boolean doRunTest(Resource test) throws IOException { 353 if (test.hasProperty(RDF.type, OWLTest.PositiveEntailmentTest) 354 || test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest) 355 || test.hasProperty(RDF.type, OWLTest.OWLforOWLTest) 356 || test.hasProperty(RDF.type, OWLTest.ImportEntailmentTest) 357 || test.hasProperty(RDF.type, OWLTest.TrueTest) ) { 358 boolean processImports = test.hasProperty(RDF.type, OWLTest.ImportEntailmentTest); 360 Model premises = getDoc(test, RDFTest.premiseDocument, processImports); 361 Model conclusions = getDoc(test, RDFTest.conclusionDocument); 362 comprehensionAxioms(premises, conclusions); 363 long t1 = System.currentTimeMillis(); 364 InfGraph graph = reasoner.bind(premises.getGraph()); 365 if (printProfile) { 366 ((FBRuleInfGraph)graph).resetLPProfile(true); 367 } 368 Model result = ModelFactory.createModelForGraph(graph); 369 boolean correct = testEntailment(conclusions.getGraph(), result.getGraph()); 370 long t2 = System.currentTimeMillis(); 371 lastTestDuration = t2 - t1; 372 if (printProfile) { 373 ((FBRuleInfGraph)graph).printLPProfile(); 374 } 375 if (test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest)) { 376 correct = !correct; 377 } 378 return correct; 379 } else if (test.hasProperty(RDF.type, OWLTest.InconsistencyTest)) { 380 Model input = getDoc(test, RDFTest.inputDocument); 382 long t1 = System.currentTimeMillis(); 383 InfGraph graph = reasoner.bind(input.getGraph()); 384 boolean correct = ! graph.validate().isValid(); 385 long t2 = System.currentTimeMillis(); 386 lastTestDuration = t2 - t1; 387 return correct; 388 } else if (test.hasProperty(RDF.type, OWLTest.ConsistencyTest)) { 389 Model input = getDoc(test, RDFTest.inputDocument); 392 long t1 = System.currentTimeMillis(); 393 InfGraph graph = reasoner.bind(input.getGraph()); 394 boolean correct = graph.validate().isValid(); 395 long t2 = System.currentTimeMillis(); 396 lastTestDuration = t2 - t1; 397 return correct; 398 } else { 399 for (StmtIterator i = test.listProperties(RDF.type); i.hasNext(); ) { 400 System.out.println("Test type = " + i.nextStatement().getObject()); 401 } 402 throw new ReasonerException("Unknown test type"); 403 } 404 } 405 406 410 public Model getDoc(Resource test, Property docType, boolean processImports) throws IOException { 411 if (processImports) { 412 Model result = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null); 413 StmtIterator si = test.listProperties(docType); 414 while ( si.hasNext() ) { 415 String fname = si.nextStatement().getObject().toString() + ".rdf"; 416 loadFile(fname, result); 417 } 418 return result; 419 } else { 420 return getDoc(test, docType); 421 } 422 } 423 424 427 public Model getDoc(Resource test, Property docType) throws IOException { 428 Model result = ModelFactory.createDefaultModel(); 429 StmtIterator si = test.listProperties(docType); 430 while ( si.hasNext() ) { 431 String fname = si.nextStatement().getObject().toString() + ".rdf"; 432 loadFile(fname, result); 433 } 434 return result; 435 } 436 437 443 public static Model loadFile(String file, Model model) throws IOException { 444 String langType = "RDF/XML"; 445 if (file.endsWith(".nt")) { 446 langType = "N-TRIPLE"; 447 } else if (file.endsWith("n3")) { 448 langType = "N3"; 449 } 450 String fname = file; 451 if (fname.startsWith(BASE_URI)) { 452 fname = fname.substring(BASE_URI.length()); 453 } 454 Reader reader = new BufferedReader(new FileReader(BASE_TESTDIR + fname)); 455 model.read(reader, BASE_URI + fname, langType); 456 return model; 457 } 458 459 464 public boolean testEntailment(Graph conclusions, Graph result) { 465 QueryHandler qh = result.queryHandler(); 466 Query query = WGReasonerTester.graphToQuery(conclusions); 467 Iterator i = qh.prepareBindings(query, new Node[] {}).executeBindings(); 468 return i.hasNext(); 469 } 470 471 476 public void comprehensionAxioms(Model premises, Model conclusions) { 477 Map comprehension = new HashMap(); 479 StmtIterator ri = conclusions.listStatements(null, RDF.type, OWL.Restriction); 480 while (ri.hasNext()) { 481 Resource restriction = ri.nextStatement().getSubject(); 482 StmtIterator pi = restriction.listProperties(OWL.onProperty); 483 while (pi.hasNext()) { 484 Resource prop = (Resource)pi.nextStatement().getObject(); 485 StmtIterator vi = restriction.listProperties(); 486 while (vi.hasNext()) { 487 Statement rs = vi.nextStatement(); 488 if ( ! rs.getPredicate().equals(OWL.onProperty)) { 489 Resource comp = premises.createResource() 492 .addProperty(RDF.type, OWL.Restriction) 493 .addProperty(OWL.onProperty, prop) 494 .addProperty(rs.getPredicate(), rs.getObject()); 495 comprehension.put(restriction, comp); 496 } 497 } 498 } 499 } 500 StmtIterator ii = conclusions.listStatements(null, OWL.intersectionOf, (RDFNode)null); 505 List intersections = new ArrayList(); 506 while (ii.hasNext()) { 507 intersections.add(ii.next()); 508 } 509 for (Iterator i = intersections.iterator(); i.hasNext(); ) { 510 Statement is = (Statement)i.next(); 511 Resource comp = premises.createResource() 513 .addProperty(RDF.type, OWL.Class) 514 .addProperty(OWL.intersectionOf, mapList(premises, (Resource)is.getObject(), comprehension)); 515 conclusions.remove(is); 518 conclusions.add(is.getSubject(), OWL.equivalentClass, comp); 519 } 520 StmtIterator io = conclusions.listStatements(null, OWL.oneOf, (RDFNode)null); 522 while (io.hasNext()) { 523 Statement s = io.nextStatement(); 524 Resource comp = premises.createResource() 525 .addProperty(OWL.oneOf, s.getObject()); 526 } 527 } 528 529 533 private Resource mapList(Model target, Resource list, Map map) { 534 if (list.equals(RDF.nil)) { 535 return RDF.nil; 536 } else { 537 Resource head = (Resource) list.getRequiredProperty(RDF.first).getObject(); 538 Resource rest = (Resource) list.getRequiredProperty(RDF.rest).getObject(); 539 Resource mapElt = target.createResource(); 540 Resource mapHead = (Resource) map.get(head); 541 if (mapHead == null) mapHead = head; 542 mapElt.addProperty(RDF.first, mapHead); 543 mapElt.addProperty(RDF.rest, mapList(target, rest, map)); 544 return mapElt; 545 } 546 } 547 548 551 552 public List findTestsOfType(Resource testType) { 553 ArrayList result = new ArrayList(); 554 StmtIterator si = testDefinitions.listStatements(null, RDF.type, testType); 555 while (si.hasNext()) { 556 Resource test = si.nextStatement().getSubject(); 557 boolean accept = true; 558 Literal status = (Literal) test.getProperty(RDFTest.status).getObject(); 560 if (approvedOnly) { 561 accept = status.getString().equals(STATUS_FLAGS[0]); 562 } else { 563 accept = false; 564 for (int i = 0; i < STATUS_FLAGS.length; i++) { 565 if (status.getString().equals(STATUS_FLAGS[i])) { 566 accept = true; 567 break; 568 } 569 } 570 } 571 for (int i = 0; i < BLOCKED_TESTS.length; i++) { 573 if (BLOCKED_TESTS[i].equals(test.toString())) { 574 accept = false; 575 } 576 } 577 if (accept) { 579 result.add(test); 580 } 581 } 582 return result; 583 } 584 } 585 586 587 | Popular Tags |