1 10 package com.hp.hpl.jena.reasoner.rulesys.test; 11 12 import com.hp.hpl.jena.reasoner.*; 13 import com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph; 14 import com.hp.hpl.jena.reasoner.test.WGReasonerTester; 15 import com.hp.hpl.jena.util.FileManager; 16 import com.hp.hpl.jena.rdf.model.*; 17 import com.hp.hpl.jena.vocabulary.*; 18 import com.hp.hpl.jena.graph.*; 19 import com.hp.hpl.jena.graph.query.*; 20 21 import com.hp.hpl.jena.shared.*; 22 23 import junit.framework.TestCase; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import java.io.*; 28 import java.util.*; 29 30 31 43 public class OWLWGTester { 44 45 public static String BASE_URI = "http://www.w3.org/2002/03owlt/"; 46 47 48 public static String baseDir = "testing/wg/"; 49 50 51 public static final String NS_OTEST = "http://www.w3.org/2002/03owlt/testOntology#"; 52 53 54 public static final String NS_RTEST = "http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#"; 55 56 57 public static final Resource PositiveEntailmentTest; 58 59 60 public static final Resource NegativeEntailmentTest; 61 62 63 public static final Property descriptionP; 64 65 66 public static final Property premiseDocumentP; 67 68 69 public static final Property conclusionDocumentP; 70 71 72 public static final Property statusP; 73 74 75 protected ReasonerFactory reasonerF; 76 77 78 protected Resource configuration; 79 80 81 protected TestCase testcase; 82 83 84 protected static long timeCost = 0; 85 86 87 protected static int numTests = 0; 88 89 protected static Log logger = LogFactory.getLog(OWLWGTester.class); 90 91 static { 93 PositiveEntailmentTest = ResourceFactory.createProperty(NS_OTEST, "PositiveEntailmentTest"); 94 NegativeEntailmentTest = ResourceFactory.createProperty(NS_OTEST, "NegativeEntailmentTest"); 95 descriptionP = ResourceFactory.createProperty(NS_RTEST, "description"); 96 premiseDocumentP = ResourceFactory.createProperty(NS_RTEST, "premiseDocument"); 97 conclusionDocumentP = ResourceFactory.createProperty(NS_RTEST, "conclusionDocument"); 98 statusP = ResourceFactory.createProperty(NS_RTEST, "status"); 99 } 100 101 107 public OWLWGTester(ReasonerFactory reasonerF, TestCase testcase, Resource configuration) { 108 this.reasonerF = reasonerF; 109 this.testcase = testcase; 110 this.configuration = configuration; 111 } 112 113 121 public boolean runTests(String manifestFile, boolean log, boolean stats) throws IOException { 122 Model manifest = FileManager.get().loadModel(baseDir + manifestFile); 124 ResIterator tests = manifest.listSubjectsWithProperty(RDF.type, PositiveEntailmentTest); 125 while (tests.hasNext()) { 126 Resource test = tests.nextResource(); 127 if (!runTest(test, log, stats)) return false; 128 } 129 tests = manifest.listSubjectsWithProperty(RDF.type, NegativeEntailmentTest); 130 while (tests.hasNext()) { 131 Resource test = tests.nextResource(); 132 if (!runTest(test, log, stats)) return false; 133 } 134 return true; 135 } 136 137 145 public boolean runTest(Resource test, boolean log, boolean stats) throws IOException { 146 RDFNode testType = test.getRequiredProperty(RDF.type).getObject(); 148 if (!(testType.equals(NegativeEntailmentTest) || 149 testType.equals(PositiveEntailmentTest) ) ) { 150 throw new JenaException("Can't find test: " + test); 151 } 152 153 String description = test.getRequiredProperty(descriptionP).getObject().toString(); 154 String status = test.getRequiredProperty(statusP).getObject().toString(); 155 logger.debug("WG test " + test.getURI() + " - " + status); 156 157 Model premises = ModelFactory.createNonreifyingModel(); 159 for (StmtIterator premisesI = test.listProperties(premiseDocumentP); premisesI.hasNext(); ) { 160 premises.add(loadFile(premisesI.nextStatement().getObject().toString() + ".rdf")); 161 } 162 163 Resource conclusionsRes = (Resource) test.getRequiredProperty(conclusionDocumentP).getObject(); 165 Model conclusions = loadFile(conclusionsRes.toString() + ".rdf"); 166 167 if (log) { 170 if (configuration == null) { 171 Model m = ModelFactory.createDefaultModel(); 172 configuration = m.createResource(); 173 } 174 configuration.addProperty(ReasonerVocabulary.PROPtraceOn, "true") 175 .addProperty(ReasonerVocabulary.PROPderivationLogging, "true"); 176 } 177 Reasoner reasoner = reasonerF.create(configuration); 178 long t1 = System.currentTimeMillis(); 179 InfGraph graph = reasoner.bind(premises.getGraph()); 180 Model result = ModelFactory.createModelForGraph(graph); 181 182 if (stats && graph instanceof FBRuleInfGraph) { 183 } 185 186 boolean correct = true; 188 if (testType.equals(PositiveEntailmentTest)) { 189 correct = testConclusions(conclusions.getGraph(), result.getGraph()); 190 } else { 191 correct = !testConclusions(conclusions.getGraph(), result.getGraph()); 193 } 194 long t2 = System.currentTimeMillis(); 195 timeCost += (t2-t1); 196 numTests++; 197 if (stats) { 198 logger.info("Time=" + (t2-t1) + "ms for " + test.getURI()); 199 printStats(); 200 201 if (graph instanceof FBRuleInfGraph) { 202 ((FBRuleInfGraph)graph).printLPProfile(); 203 } 204 } 205 206 if (!correct) { 207 } 215 216 if (testcase != null) { 218 TestCase.assertTrue("Test: " + test + "\n" + reasonerF.getURI() + "\n" + description, correct); 219 } 220 return correct; 221 } 222 223 229 public static Model loadFile(String file) throws IOException { 230 String langType = "RDF/XML"; 231 if (file.endsWith(".nt")) { 232 langType = "N-TRIPLE"; 233 } else if (file.endsWith("n3")) { 234 langType = "N3"; 235 } 236 Model result = ModelFactory.createNonreifyingModel(); 237 String fname = file; 238 if (fname.startsWith(BASE_URI)) { 239 fname = fname.substring(BASE_URI.length()); 240 } 241 Reader reader = new BufferedReader(new FileReader(baseDir + fname)); 242 result.read(reader, BASE_URI + fname, langType); 243 return result; 244 } 245 246 251 private boolean testConclusions(Graph conclusions, Graph result) { 252 QueryHandler qh = result.queryHandler(); 253 Query query = WGReasonerTester.graphToQuery(conclusions); 254 Iterator i = qh.prepareBindings(query, new Node[] {}).executeBindings(); 255 return i.hasNext(); 256 } 257 258 261 public void printStats() { 262 logger.info("Ran " + numTests +" in " + timeCost +"ms = " + (timeCost/numTests) + "ms/test"); 263 } 264 265 } 266 267 293 294 | Popular Tags |