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.rdf.model.impl.*; 15 import com.hp.hpl.jena.mem.GraphMem; 16 import com.hp.hpl.jena.reasoner.*; 17 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable; 18 import com.hp.hpl.jena.vocabulary.RDF; 19 20 import com.hp.hpl.jena.shared.*; 21 22 import junit.framework.TestCase; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import java.util.*; 27 import java.io.*; 28 29 50 public class ReasonerTester { 51 52 53 public static final String NS = "http://www.hpl.hp.com/semweb/2003/query_tester#"; 54 55 56 public static final String BASE_URI = "http://www.hpl.hp.com/semweb/2003/query_tester/"; 57 58 59 public static final Resource testClass; 60 61 62 public static final Property descriptionP; 63 64 65 public static final Property tboxP; 66 67 68 public static final Property dataP; 69 70 71 public static final Property queryP; 72 73 74 public static final Property resultP; 75 76 77 public static final String baseDir = "testing/reasoners/"; 78 79 static { 81 descriptionP = new PropertyImpl(NS, "description"); 82 tboxP = new PropertyImpl(NS, "tbox"); 83 dataP = new PropertyImpl(NS, "data"); 84 queryP = new PropertyImpl(NS, "query"); 85 resultP = new PropertyImpl(NS, "result"); 86 testClass = new ResourceImpl(NS, "Test"); 87 } 88 89 90 protected Model testManifest; 91 92 93 protected Map sourceCache = new HashMap(); 94 95 protected static Log logger = LogFactory.getLog(ReasonerTester.class); 96 97 102 public ReasonerTester(String manifest) throws IOException { 103 testManifest = loadFile(manifest, false); 104 } 105 106 112 public Model loadFile(String file, boolean cache) throws IOException { 113 if (cache && sourceCache.keySet().contains(file)) { 114 return (Model)sourceCache.get(file); 115 } 116 String langType = "RDF/XML"; 117 if (file.endsWith(".nt")) { 118 langType = "N-TRIPLE"; 119 } else if (file.endsWith("n3")) { 120 langType = "N3"; 121 } 122 Model result = ModelFactory.createDefaultModel(); 123 Reader reader = new BufferedReader(new FileReader(baseDir + file)); 124 result.read(reader, BASE_URI + file, langType); 125 if (cache) { 126 sourceCache.put(file, result); 127 } 128 return result; 129 } 130 131 139 public Graph loadTestFile(Resource test, Property predicate) throws IOException { 140 if (test.hasProperty(predicate)) { 141 String fileName = test.getRequiredProperty(predicate).getObject().toString(); 142 boolean cache = predicate.equals(tboxP) || predicate.equals(dataP); 143 return loadFile(fileName, cache).getGraph(); 144 } else { 145 return new GraphMem(); 146 } 147 } 148 149 153 public static TriplePattern tripleToPattern(Triple t) { 154 return new TriplePattern( 155 nodeToPattern(t.getSubject()), 156 nodeToPattern(t.getPredicate()), 157 nodeToPattern(t.getObject())); 158 } 159 160 164 public static Node nodeToPattern(Node n) { 165 if (n.isURI() && n.toString().startsWith("var:")) { 166 return Node_RuleVariable.WILD; 167 } else { 169 return n; 170 } 171 } 172 173 182 public boolean runTests(ReasonerFactory reasonerF, TestCase testcase, Resource configuration) throws IOException { 183 for (Iterator i = listTests().iterator(); i.hasNext(); ) { 184 String test = (String )i.next(); 185 if (!runTest(test, reasonerF, testcase, configuration)) return false; 186 } 187 return true; 188 } 189 190 198 public boolean runTests(Reasoner reasoner, TestCase testcase) throws IOException { 199 for (Iterator i = listTests().iterator(); i.hasNext(); ) { 200 String test = (String )i.next(); 201 if (!runTest(test, reasoner, testcase)) return false; 202 } 203 return true; 204 } 205 206 209 public List listTests() { 210 List testList = new ArrayList(); 211 ResIterator tests = testManifest.listSubjectsWithProperty(RDF.type, testClass); 212 while (tests.hasNext()) { 213 testList.add(tests.next().toString()); 214 } 215 return testList; 216 } 217 218 219 229 public boolean runTest(String uri, ReasonerFactory reasonerF, TestCase testcase, Resource configuration) throws IOException { 230 Reasoner reasoner = reasonerF.create(configuration); 231 return runTest(uri, reasoner, testcase); 232 } 233 234 243 public boolean runTest(String uri, Reasoner reasoner, TestCase testcase) throws IOException { 244 Resource test = testManifest.getResource(uri); 246 if (!test.hasProperty(RDF.type, testClass)) { 247 throw new JenaException("Can't find test: " + uri); 248 } 249 250 String description = test.getRequiredProperty(descriptionP).getObject().toString(); 251 logger.debug("Reasoner test " + test.getURI() + " - " + description); 252 253 Graph tbox = loadTestFile(test, tboxP); 255 Graph data = loadTestFile(test, dataP); 256 InfGraph graph = reasoner.bindSchema(tbox).bind(data); 257 258 Graph queryG = loadTestFile(test, queryP); 260 Graph resultG = new GraphMem(); 261 262 Iterator queries = queryG.find(null, null, null); 263 while (queries.hasNext()) { 264 TriplePattern query = tripleToPattern((Triple)queries.next()); 265 logger.debug("Query: " + query); 266 Iterator answers = graph.find(query.asTripleMatch()); 267 while (answers.hasNext()) { 268 Triple ans = (Triple)answers.next(); 269 logger.debug("ans: " + TriplePattern.simplePrintString(ans)); 270 resultG.add(ans); 271 } 272 } 273 274 Graph correctG = loadTestFile(test, resultP); 276 boolean correct = correctG.isIsomorphicWith(resultG); 277 300 if (testcase != null) { 302 TestCase.assertTrue(description, correct); 303 } 304 return correct; 305 } 306 307 } 308 309 338 339 | Popular Tags |