1 10 package com.hp.hpl.jena.reasoner.rulesys.test; 11 12 import java.util.Iterator ; 13 14 import com.hp.hpl.jena.graph.*; 15 import com.hp.hpl.jena.graph.compose.Union; 16 import com.hp.hpl.jena.mem.GraphMem; 17 import com.hp.hpl.jena.util.FileManager; 19 import com.hp.hpl.jena.util.PrintUtil; 20 import com.hp.hpl.jena.vocabulary.*; 21 import com.hp.hpl.jena.reasoner.*; 22 import com.hp.hpl.jena.reasoner.rulesys.*; 23 import com.hp.hpl.jena.reasoner.rulesys.impl.oldCode.*; 24 import com.hp.hpl.jena.shared.WrappedIOException; 25 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import java.util.*; 30 31 39 public class DebugOWL { 40 41 42 Reasoner reasoner; 43 44 45 Graph testdata; 46 47 48 Graph schema; 49 50 51 InfGraph infgraph; 52 53 54 Node[] concepts; 55 56 57 Node[] instances; 58 59 60 Node[] properties; 61 62 static Log logger = LogFactory.getLog(DebugOWL.class); 63 64 65 public static final int EXPT = 1; 66 67 68 public static final int OWLFB = 2; 69 70 71 public static final int OWL = 3; 72 73 74 public static final int RDFSFB = 4; 75 76 77 public static final int RDFSFinal = 5; 78 79 80 public static final int OWLExpt = 6; 81 82 83 public static final int RDFSLPExpt = 7; 84 85 86 89 public DebugOWL(int config) { 90 testdata = new GraphMem(); 91 schema = null; 92 93 switch(config) { 94 95 case EXPT: 96 reasoner = GenericRuleReasonerFactory.theInstance().create(null); 97 GenericRuleReasoner grr = (GenericRuleReasoner)reasoner; 98 grr.setMode(GenericRuleReasoner.HYBRID); 99 try { 100 grr.setRules(Rule.parseRules(Util.loadRuleParserFromResourceFile("etc/expt.rules"))); 101 } catch (WrappedIOException e) { 102 System.out.println("Failed to open rules file: " + e.getCause() ); 103 System.exit(1); 104 } 105 break; 109 110 case OWLFB: 111 reasoner = OWLFBRuleReasonerFactory.theInstance().create(null); 112 break; 114 115 case OWL: 116 reasoner = OWLRuleReasonerFactory.theInstance().create(null); 117 break; 119 120 case RDFSFB: 121 reasoner = RDFSFBRuleReasonerFactory.theInstance().create(null); 122 break; 123 124 case RDFSFinal: 125 reasoner = RDFSRuleReasonerFactory.theInstance().create(null); 126 break; 127 128 case OWLExpt: 129 reasoner = OWLExptRuleReasonerFactory.theInstance().create(null); 130 break; 132 133 case RDFSLPExpt: 134 try { 135 List rules = Rule.parseRules(Util.loadRuleParserFromResourceFile("etc/expt.rules")); 136 reasoner = new FBRuleReasoner(rules); 137 } catch (WrappedIOException e) { 138 System.out.println("Failed to open rules file: " + e.getCause()); 139 System.exit(1); 140 } 141 break; 142 143 } 144 145 } 146 147 150 public void load(String testFile) { 151 testdata = FileManager.get().loadModel(testFile).getGraph(); 152 schema = null; 153 } 154 155 158 public void load(String schemaFile, String testFile) { 159 testdata = FileManager.get().loadModel(testFile).getGraph(); 160 schema = FileManager.get().loadModel(schemaFile).getGraph(); 161 } 162 163 171 public void createTest(int depth, int NS, int NI, boolean withProps) { 172 int numClasses = 0; 174 int levelSize = 1; 175 for (int i = 0; i < depth; i++) { 176 levelSize *= NS; 177 numClasses += levelSize; 178 } 179 concepts = new Node[numClasses]; 180 properties = new Node[numClasses]; 181 instances = new Node[numClasses * NI]; 182 logger.info("Classes: " + numClasses +" Instances: " + (numClasses * NI) 183 + (withProps ? " with properties" : "")); 184 185 testdata = new GraphMem(); 187 int conceptPtr = 0; 189 int levelStart = 0; 190 int levelEnd = 0; 191 int instancePtr = 0; 192 for (int i = 0; i < depth; i++) { 193 Node property = null; 195 if (i == 0) { 196 for (int j = 0; j < NS; j++) { 197 Node concept = Node.createURI("concept" + conceptPtr); 198 if (withProps) { 199 property = Node.createURI("prop" + conceptPtr); 200 properties[conceptPtr] = property; 201 } 202 concepts[conceptPtr++] = concept; 203 } 204 } else { 205 for (int j = levelStart; j < levelEnd; j++) { 206 Node superConcept = concepts[j]; 207 for (int k = 0; k < NS; k++) { 208 Node concept = Node.createURI("concept" + conceptPtr); 209 if (withProps) { 210 property = Node.createURI("prop" + conceptPtr); 211 properties[conceptPtr] = property; 212 } 213 concepts[conceptPtr++] = concept; 214 testdata.add(new Triple(concept, RDFS.subClassOf.asNode(), superConcept)); 215 } 216 } 217 } 218 levelStart = levelEnd; 219 levelEnd = conceptPtr; 220 for (int j = levelStart; j < levelEnd; j++) { 222 Node concept = concepts[j]; 223 for (int k = 0; k < NI; k++) { 224 Node instance = Node.createURI("instance"+instancePtr); 225 testdata.add(new Triple(instance, RDF.type.asNode(), concept)); 226 if (withProps && (k-1)%3 == 0) { 227 testdata.add(new Triple(instances[instancePtr-1], property, instance)); 228 } 229 instances[instancePtr++] = instance; 230 } 231 } 232 } 233 } 234 235 238 public void init() { 239 if (schema == null) { 240 infgraph = reasoner.bind(testdata); 241 } else { 242 infgraph = reasoner.bind(new Union(schema, testdata)); 244 } 245 if (infgraph instanceof FBRuleInfGraph) { 249 System.out.println("Starting prepare"); 250 ((FBRuleInfGraph)infgraph).prepare(); 251 System.out.println(".. finished"); 252 } 253 } 254 255 258 long listC0(boolean print) { 259 return list(null, RDF.type.asNode(), concepts[0], print); 260 } 261 262 265 long list(Node s, Node p, Node o, boolean print) { 266 long t1 = System.currentTimeMillis(); 267 init(); 268 int count = 0; 269 for (Iterator i = infgraph.find(s,p,o); i.hasNext(); ) { 270 Triple t = (Triple)i.next(); 271 count++; 272 if (print) { 273 logger.info(PrintUtil.print(t)); 274 } 275 } 276 long t2 = System.currentTimeMillis(); 277 System.out.println("Found " + count + " results"); 278 return (t2 - t1); 279 } 280 281 284 public void runListClassesTest(int depth, int NS, int NI, boolean withProps) { 285 createTest(depth, NS, NI, withProps); 286 long t = list(null, RDF.type.asNode(), RDFS.Class.asNode(), false); 287 System.out.println("Took " + t + "ms"); 288 } 289 290 293 public void runVolz(int depth, int NS, int NI, boolean withProps) { 294 createTest(depth, NS, NI, withProps); 295 long t = listC0(false); 296 System.out.println("Took " + t + "ms"); 297 if (infgraph instanceof FBRuleInfGraph) { 298 ((FBRuleInfGraph)infgraph).printLPProfile(); 299 } 300 } 301 302 305 public void runVolz() { 306 runVolz(3,5,10, false); 307 runVolz(3,5,10, false); 308 runVolz(4,5,10, false); 309 runVolz(5,5,10, false); 310 311 } 318 319 322 public void listClassesOn(String filename) { 323 load(filename); 324 System.out.println("Testing: " + filename); 325 long t = list(null, RDF.type.asNode(), RDFS.Class.asNode(), false); 326 System.out.println("Took " + t + "ms"); 327 } 328 329 public static void main(String [] args) { 330 try { 331 String dataFile = "file:testing/ontology/owl/list-syntax/test-with-import.rdf"; 332 String schemaFile = "file:vocabularies/owl.owl"; 333 String schemaFile2 = "file:testing/reasoners/bugs/owl-partial.owl"; 334 String dataFile2 = "file:testing/reasoners/bugs/test.owl"; 335 String food = "file:testing/reasoners/bugs/food.owl"; 336 337 340 344 348 System.out.println("Volz tests on normal RDFS, tgc + type rules"); 350 new DebugOWL(RDFSFinal).runVolz(); 351 354 361 367 375 } catch (Exception e) { 376 System.out.println("Problem: " + e); 377 e.printStackTrace(); 378 } 379 } 380 381 } 382 383 384 | Popular Tags |