1 10 package com.hp.hpl.jena.reasoner.rulesys; 11 12 import com.hp.hpl.jena.reasoner.*; 13 import com.hp.hpl.jena.reasoner.rulesys.impl.*; 14 import com.hp.hpl.jena.vocabulary.*; 15 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary; 16 import com.hp.hpl.jena.graph.*; 17 import com.hp.hpl.jena.rdf.model.*; 18 19 import java.util.*; 20 21 32 public class GenericRuleReasoner extends FBRuleReasoner { 33 34 35 protected LPRuleStore bRuleStore; 36 37 38 protected RuleMode mode = HYBRID; 39 40 41 protected static final boolean cachePreload = true; 42 43 44 protected boolean enableTGCCaching = false; 45 46 47 protected boolean enableOWLTranslation = false; 48 49 50 protected HashSet preprocessorHooks; 51 52 53 public boolean filterFunctors = true; 54 55 56 private static final OWLRuleTranslationHook owlTranslator = new OWLRuleTranslationHook(); 57 58 59 public static final RuleMode FORWARD = new RuleMode("forward"); 60 61 62 public static final RuleMode FORWARD_RETE = new RuleMode("forwardRETE"); 63 64 65 public static final RuleMode BACKWARD = new RuleMode("backward"); 66 67 68 public static final RuleMode HYBRID = new RuleMode("hybrid"); 69 70 73 78 public GenericRuleReasoner(List rules) { 79 super(rules); 80 } 81 82 87 public GenericRuleReasoner(ReasonerFactory factory, Resource configuration) { 88 super(factory, configuration); 89 } 90 91 96 public GenericRuleReasoner(List rules, ReasonerFactory factory) { 97 super(rules, factory); 98 } 99 100 104 protected GenericRuleReasoner(List rules, Graph schemaGraph, ReasonerFactory factory, RuleMode mode) { 105 this(rules, factory); 106 this.schemaGraph = schemaGraph; 107 this.mode = mode; 108 } 109 110 113 122 public void setMode(RuleMode mode) { 123 if (schemaGraph != null) { 124 throw new ReasonerException("Can't change mode of a reasoner bound to a schema"); 125 } 126 this.mode = mode; 127 preload = null; 128 bRuleStore = null; 129 } 130 131 136 public void setRules(List rules) { 137 super.setRules(rules); 140 } 141 142 147 public void setOWLTranslation(boolean enableOWLTranslation) { 148 if (enableOWLTranslation && (mode != HYBRID)) { 149 throw new ReasonerException("Can only enable OWL rule translation in HYBRID mode"); 150 } 151 this.enableOWLTranslation = enableOWLTranslation; 152 if (enableOWLTranslation) { 153 addPreprocessingHook(owlTranslator); 154 } else { 155 removePreprocessingHook(owlTranslator); 156 } 157 } 158 159 166 public void setTransitiveClosureCaching(boolean enableTGCCaching) { 167 this.enableTGCCaching = enableTGCCaching; 168 } 169 170 174 public void setFunctorFiltering(boolean param) { 175 filterFunctors = param; 176 } 177 178 184 public void addPreprocessingHook(RulePreprocessHook hook) { 185 if (preprocessorHooks == null) { 186 preprocessorHooks = new HashSet(); 187 } 188 preprocessorHooks.add(hook); 189 } 190 191 194 public void removePreprocessingHook(RulePreprocessHook hook) { 195 if (preprocessorHooks != null) { 196 preprocessorHooks.remove(hook); 197 } 198 } 199 200 protected boolean doSetResourceParameter( Property parameter, Resource value ) 201 { 202 if (parameter.equals( JenaModelSpec.ruleSetURL )) 203 { 204 addRules( Rule.rulesFromURL( value.getURI() ) ); 205 } 206 else if (parameter.equals( JenaModelSpec.ruleSet )) 207 { 208 StmtIterator that = value.listProperties( JenaModelSpec.ruleSetURL ); 209 while (that.hasNext()) 210 { 211 addRules( Rule.rulesFromURL( that.nextStatement().getResource().getURI() ) ); 212 } 213 StmtIterator it = value.listProperties( JenaModelSpec.hasRule ); 214 while (it.hasNext()) 215 { 216 addRules( Rule.parseRules( it.nextStatement().getString() ) ); 217 } 218 } 219 else 220 return false; 221 return true; 222 } 223 224 229 protected boolean doSetParameter(Property parameter, Object value) { 230 if (parameter.equals(ReasonerVocabulary.PROPderivationLogging)) { 231 recordDerivations = Util.convertBooleanPredicateArg(parameter, value); 232 } else if (parameter.equals(ReasonerVocabulary.PROPtraceOn)) { 233 traceOn = Util.convertBooleanPredicateArg(parameter, value); 234 } else if (parameter.equals(ReasonerVocabulary.PROPenableFunctorFiltering)) { 235 filterFunctors = Util.convertBooleanPredicateArg(parameter, value); 236 237 } else if (parameter.equals(ReasonerVocabulary.PROPenableOWLTranslation)) { 238 enableOWLTranslation = Util.convertBooleanPredicateArg(parameter, value); 239 if (enableOWLTranslation) { 240 addPreprocessingHook(owlTranslator); 241 } else { 242 removePreprocessingHook(owlTranslator); 243 } 244 245 } else if (parameter.equals(ReasonerVocabulary.PROPenableTGCCaching)) { 246 enableTGCCaching = Util.convertBooleanPredicateArg(parameter, value); 247 248 } else if (parameter.equals(ReasonerVocabulary.PROPruleMode)) { 249 if (value.equals(FORWARD.name)) { 250 mode = FORWARD; 251 } else if (value.equals(FORWARD_RETE.name)) { 252 mode = FORWARD_RETE; 253 } else if (value.equals(BACKWARD.name)) { 254 mode = BACKWARD; 255 } else if (value.equals(HYBRID.name)) { 256 mode = HYBRID; 257 } else { 258 throw new IllegalParameterException("PROPruleMode can only be 'forward'm 'forwardRETE', 'backward', 'hybrid', not " + value); 259 } 260 261 } else if (parameter.equals(ReasonerVocabulary.PROPruleSet)) { 262 if (value instanceof String ) { 263 addRules( loadRules( (String )value ) ); 264 } else { 265 throw new IllegalParameterException("PROPruleSet value should be a URI string. Was a " + value.getClass()); 266 } 267 } else { 268 return false; 269 } 270 return true; 271 } 272 273 276 280 public Reasoner bindSchema(Graph tbox) throws ReasonerException { 281 if (schemaGraph != null) { 282 throw new ReasonerException("Can only bind one schema at a time to a GenericRuleReasoner"); 283 } 284 Graph graph = null; 285 if (mode == FORWARD) { 286 graph = new BasicForwardRuleInfGraph(this, rules, null, tbox); 287 ((InfGraph)graph).prepare(); 288 } else if (mode == FORWARD_RETE) { 289 graph = new RETERuleInfGraph(this, rules, null, tbox); 290 ((InfGraph)graph).prepare(); 291 } else if (mode == BACKWARD) { 292 graph = tbox; 293 } else { 294 List ruleSet = rules; 295 graph = new FBRuleInfGraph(this, ruleSet, getPreload(), tbox); 296 if (enableTGCCaching) ((FBRuleInfGraph)graph).setUseTGCCache(); 297 ((FBRuleInfGraph)graph).prepare(); 298 } 299 GenericRuleReasoner grr = new GenericRuleReasoner(rules, graph, factory, mode); 300 grr.setDerivationLogging(recordDerivations); 301 grr.setTraceOn(traceOn); 302 grr.setTransitiveClosureCaching(enableTGCCaching); 303 grr.setFunctorFiltering(filterFunctors); 304 if (preprocessorHooks != null) { 305 for (Iterator i = preprocessorHooks.iterator(); i.hasNext(); ) { 306 grr.addPreprocessingHook((RulePreprocessHook)i.next()); 307 } 308 } 309 return grr; 310 } 311 312 323 public InfGraph bind(Graph data) throws ReasonerException { 324 Graph schemaArg = schemaGraph == null ? getPreload() : schemaGraph; 325 InfGraph graph = null; 326 if (mode == FORWARD) { 327 graph = new BasicForwardRuleInfGraph(this, rules, schemaArg); 328 ((BasicForwardRuleInfGraph)graph).setTraceOn(traceOn); 329 } else if (mode == FORWARD_RETE) { 330 graph = new RETERuleInfGraph(this, rules, schemaArg); 331 ((BasicForwardRuleInfGraph)graph).setTraceOn(traceOn); 332 } else if (mode == BACKWARD) { 333 graph = new LPBackwardRuleInfGraph(this, getBruleStore(), data, schemaArg); 334 ((LPBackwardRuleInfGraph)graph).setTraceOn(traceOn); 335 } else { 336 List ruleSet = ((FBRuleInfGraph)schemaArg).getRules(); 337 FBRuleInfGraph fbgraph = new FBRuleInfGraph(this, ruleSet, schemaArg); 338 graph = fbgraph; 339 if (enableTGCCaching) fbgraph.setUseTGCCache(); 340 fbgraph.setTraceOn(traceOn); 341 fbgraph.setFunctorFiltering(filterFunctors); 342 if (preprocessorHooks!= null) { 343 for (Iterator i = preprocessorHooks.iterator(); i.hasNext(); ) { 344 fbgraph.addPreprocessingHook((RulePreprocessHook)i.next()); 345 } 346 } 347 } 348 graph.setDerivationLogging(recordDerivations); 349 graph.rebind(data); 350 return graph; 351 } 352 353 356 protected synchronized InfGraph getPreload() { 357 if (cachePreload && preload == null && mode == HYBRID) { 358 if (mode == HYBRID) { 359 preload = new FBRuleInfGraph(this, rules, null); 360 if (enableTGCCaching) ((FBRuleInfGraph)preload).setUseTGCCache(); 361 } else if (mode == FORWARD) { 362 preload = new BasicForwardRuleInfGraph(this, rules, null); 363 } else if (mode == FORWARD_RETE) { 364 preload = new RETERuleInfGraph(this, rules, null); 365 } 366 preload.prepare(); 367 } 368 return preload; 369 } 370 371 374 protected LPRuleStore getBruleStore() { 375 if (bRuleStore == null) { 376 bRuleStore = new LPRuleStore(rules); 377 } 378 return bRuleStore; 379 } 380 381 384 387 public static class RuleMode { 388 389 String name; 390 391 392 protected RuleMode(String name) { 393 this.name = name; 394 } 395 396 public String toString() { 397 return name; 398 } 399 } 400 401 } 402 403 404 405 | Popular Tags |