1 10 package com.hp.hpl.jena.reasoner.test; 11 12 import com.hp.hpl.jena.rdf.model.*; 13 import com.hp.hpl.jena.graph.*; 14 import com.hp.hpl.jena.graph.query.*; 15 import com.hp.hpl.jena.rdf.model.impl.PropertyImpl; 16 import com.hp.hpl.jena.rdf.model.impl.ResourceImpl; 17 import com.hp.hpl.jena.mem.GraphMem; 18 import com.hp.hpl.jena.reasoner.*; 19 import com.hp.hpl.jena.vocabulary.RDF; 20 import com.hp.hpl.jena.rdf.arp.test.ARPTests; 21 22 import com.hp.hpl.jena.shared.*; 23 24 import junit.framework.TestCase; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import java.io.*; 29 import java.util.*; 30 import java.net.*; 31 32 49 public class WGReasonerTester { 50 51 52 public static final String NS = "http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#"; 53 54 55 public static final String BASE_URI = "http://www.w3.org/2000/10/rdf-tests/rdfcore/"; 56 57 58 public static final String DEFAULT_BASE_DIR = "testing/wg/"; 59 60 61 protected String baseDir = DEFAULT_BASE_DIR; 62 63 64 public static final Resource PositiveEntailmentTest; 65 66 67 public static final Resource NegativeEntailmentTest; 68 69 70 public static final Resource FalseDocument; 71 72 73 public static final Property descriptionP; 74 75 76 public static final Property statusP; 77 78 79 public static final Property entailmentRulesP; 80 81 82 public static final Property premiseDocumentP; 83 84 85 public static final Property conclusionDocumentP; 86 87 88 Resource testType; 89 90 91 public static final String [] blockedTests = { 92 BASE_URI + "datatypes/Manifest.rdf#language-important-for-non-dt-entailment-1", 93 BASE_URI + "datatypes/Manifest.rdf#language-important-for-non-dt-entailment-2", 94 BASE_URI + "pfps-10/Manifest.rdf#non-well-formed-literal-1", 96 BASE_URI + "xmlsch-02/Manifest.rdf#whitespace-facet-3", 97 }; 101 102 static { 104 PositiveEntailmentTest = new ResourceImpl(NS, "PositiveEntailmentTest"); 105 NegativeEntailmentTest = new ResourceImpl(NS, "NegativeEntailmentTest"); 106 FalseDocument = new ResourceImpl(NS, "False-Document"); 107 descriptionP = new PropertyImpl(NS, "description"); 108 statusP = new PropertyImpl(NS, "status"); 109 entailmentRulesP = new PropertyImpl(NS, "entailmentRules"); 110 premiseDocumentP = new PropertyImpl(NS, "premiseDocument"); 111 conclusionDocumentP = new PropertyImpl(NS, "conclusionDocument"); 112 } 113 114 115 protected Model testManifest; 116 117 protected static Log logger = LogFactory.getLog(WGReasonerTester.class); 118 119 125 public WGReasonerTester(String manifest, String baseDir) throws IOException { 126 this.baseDir = baseDir; 127 testManifest = loadFile(manifest); 128 } 129 130 135 public WGReasonerTester(String manifest) throws IOException { 136 testManifest = loadFile(manifest); 137 } 138 139 145 public Model loadFile(String file) throws IOException { 146 String langType = "RDF/XML"; 147 if (file.endsWith(".nt")) { 148 langType = "N-TRIPLE"; 149 } else if (file.endsWith("n3")) { 150 langType = "N3"; 151 } 152 Model result = ModelFactory.createNonreifyingModel(); 153 String fname = file; 154 if (fname.startsWith(BASE_URI)) { 155 fname = fname.substring(BASE_URI.length()); 156 } 157 158 162 InputStream in; 163 if ( baseDir.startsWith("http:")) { 164 in = new URL(baseDir+fname).openStream(); 165 } else { 166 in = new FileInputStream(baseDir + fname); 167 } 168 in = new BufferedInputStream(in); 169 170 171 result.read(in, BASE_URI + fname, langType); 172 return result; 173 } 174 175 183 private Graph loadTestFile(Resource test, Property predicate) throws IOException { 184 if (test.hasProperty(predicate)) { 185 String fileName = test.getRequiredProperty(predicate).getObject().toString(); 186 return loadFile(fileName).getGraph(); 187 } else { 188 return new GraphMem(); 189 } 190 } 191 192 201 public boolean runTests(ReasonerFactory reasonerF, TestCase testcase, Resource configuration) throws IOException { 202 for (Iterator i = listTests().iterator(); i.hasNext(); ) { 203 String test = (String )i.next(); 204 if (!runTest(test, reasonerF, testcase, configuration)) return false; 205 } 206 return true; 207 } 208 209 212 public List listTests() { 213 List testList = new ArrayList(); 214 ResIterator tests = testManifest.listSubjectsWithProperty(RDF.type, PositiveEntailmentTest); 215 while (tests.hasNext()) { 216 testList.add(tests.next().toString()); 217 } 218 tests = testManifest.listSubjectsWithProperty(RDF.type, NegativeEntailmentTest); 219 while (tests.hasNext()) { 220 testList.add(tests.next().toString()); 221 } 222 return testList; 223 } 224 225 229 public Resource getTypeOfLastTest() { 230 return testType; 231 } 232 233 243 public boolean runTest(String uri, ReasonerFactory reasonerF, TestCase testcase, Resource configuration) throws IOException { 244 return runTestDetailedResponse(uri,reasonerF,testcase,configuration) != FAIL; 245 } 246 static final public int FAIL = -1; 247 static final public int NOT_APPLICABLE = 0; 248 static final public int INCOMPLETE = 1; 249 static final public int PASS = 2; 250 251 261 262 263 public int runTestDetailedResponse(String uri, ReasonerFactory reasonerF, TestCase testcase, Resource configuration) throws IOException { 264 265 Resource test = testManifest.getResource(uri); 267 testType = (Resource)test.getRequiredProperty(RDF.type).getObject(); 268 if (!(testType.equals(NegativeEntailmentTest) || 269 testType.equals(PositiveEntailmentTest) ) ) { 270 throw new JenaException("Can't find test: " + uri); 271 } 272 273 Statement descriptionS = test.getProperty(descriptionP); 274 String description = (descriptionS == null) ? "no description" : descriptionS.getObject().toString(); 275 String status = test.getRequiredProperty(statusP).getObject().toString(); 276 logger.debug("WG test " + test.getURI() + " - " + status); 277 if (! status.equals("APPROVED")) { 278 return NOT_APPLICABLE; 279 } 280 281 for (int i = 0; i < blockedTests.length; i++) { 283 if (test.getURI().equals(blockedTests[i])) return NOT_APPLICABLE; 284 } 285 286 Model premises = ModelFactory.createNonreifyingModel(); 288 for (StmtIterator premisesI = test.listProperties(premiseDocumentP); premisesI.hasNext(); ) { 289 premises.add(loadFile(premisesI.nextStatement().getObject().toString())); 290 } 291 292 Model conclusions = null; 294 Resource conclusionsRes = (Resource) test.getRequiredProperty(conclusionDocumentP).getObject(); 295 Resource conclusionsType = (Resource) conclusionsRes.getRequiredProperty(RDF.type).getObject(); 296 if (!conclusionsType.equals(FalseDocument)) { 297 conclusions = loadFile(conclusionsRes.toString()); 298 } 299 300 Reasoner reasoner = reasonerF.create(configuration); 302 InfGraph graph = reasoner.bind(premises.getGraph()); 303 Model result = ModelFactory.createModelForGraph(graph); 304 305 boolean correct = true; 307 int goodResult = PASS; 308 boolean noisy = !(baseDir.equals(DEFAULT_BASE_DIR) 309 || ARPTests.internet ); 310 if (testType.equals(PositiveEntailmentTest)) { 311 if (conclusions == null) { 312 correct = ! graph.validate().isValid(); 314 if (noisy) { 315 System.out.println("PositiveEntailmentTest of FalseDoc " + test.getURI() + (correct ? " - OK" : " - FAIL")); 316 } 317 } else { 318 correct = testConclusions(conclusions.getGraph(), result.getGraph()); 319 if (!graph.validate().isValid()) { 320 correct = false; 321 } 322 if (noisy) { 323 System.out.println("PositiveEntailmentTest " + test.getURI() + (correct ? " - OK" : " - FAIL")); 324 } 325 } 326 } else { 327 goodResult = INCOMPLETE; 328 if (conclusions == null) { 330 correct = graph.validate().isValid(); 332 if (noisy) { 333 System.out.println("NegativentailmentTest of FalseDoc " + test.getURI() + (correct ? " - OK" : " - FAIL")); 334 } 335 } else { 336 correct = !testConclusions(conclusions.getGraph(), result.getGraph()); 337 if (noisy) { 338 System.out.println("NegativeEntailmentTest " + test.getURI() + (correct ? " - OK" : " - FAIL")); 339 } 340 } 341 } 342 343 if (!correct) { 345 logger.debug("Premises: " ); 346 for (StmtIterator i = premises.listStatements(); i.hasNext(); ) { 347 logger.debug(" - " + i.nextStatement()); 348 } 349 logger.debug("Conclusions: " ); 350 if (conclusions != null) { 351 for (StmtIterator i = conclusions.listStatements(); i.hasNext(); ) { 352 logger.debug(" - " + i.nextStatement()); 353 } 354 } 355 } 356 357 if (testcase != null) { 359 TestCase.assertTrue("Test: " + test + "\n" + description, correct); 360 } 361 return correct?goodResult:FAIL; 362 } 363 364 369 private boolean testConclusions(Graph conclusions, Graph result) { 370 QueryHandler qh = result.queryHandler(); 371 Query query = graphToQuery(conclusions); 372 Iterator i = qh.prepareBindings(query, new Node[] {}).executeBindings(); 373 return i.hasNext(); 374 } 375 376 377 380 public static Query graphToQuery(Graph graph) { 381 HashMap bnodeToVar = new HashMap(); 382 Query query = new Query(); 383 for (Iterator i = graph.find(null, null, null); i.hasNext(); ) { 384 Triple triple = (Triple)i.next(); 385 query.addMatch( 386 translate(triple.getSubject(), bnodeToVar), 387 translate(triple.getPredicate(), bnodeToVar), 388 translate(triple.getObject(), bnodeToVar) ); 389 } 390 return query; 391 } 392 393 399 private static Node translate(Node node, HashMap bnodeToVar) { 400 String varnames = "abcdefghijklmnopqrstuvwxyz"; 401 if (node.isBlank()) { 402 Node t = (Node)bnodeToVar.get(node); 403 if (t == null) { 404 int i = bnodeToVar.size(); 405 if (i > varnames.length()) { 406 throw new ReasonerException("Too many bnodes in query"); 407 } 408 t = Node.createVariable(varnames.substring(i, i+1)); 409 bnodeToVar.put(node, t); 410 } 411 return t; 412 } else { 413 return node; 414 } 415 } 416 417 } 418 419 448 449 | Popular Tags |