1 10 package com.hp.hpl.jena.reasoner.rulesys; 11 12 import com.hp.hpl.jena.rdf.model.*; 13 import com.hp.hpl.jena.reasoner.*; 14 import com.hp.hpl.jena.shared.WrappedIOException; 15 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary; 16 import com.hp.hpl.jena.graph.*; 17 18 import java.util.*; 19 20 28 public class FBRuleReasoner implements RuleReasoner { 29 30 31 protected ReasonerFactory factory; 32 33 34 protected List rules = new ArrayList(); 35 36 37 protected Graph schemaGraph; 38 39 40 protected boolean recordDerivations = false; 41 42 43 boolean traceOn = false; 44 46 47 protected static final boolean cachePreload = true; 48 49 50 protected InfGraph preload; 51 52 53 protected Resource configuration; 54 55 56 protected Capabilities capabilities; 57 58 63 public FBRuleReasoner(List rules) { 64 if (rules == null) throw new NullPointerException ( "null rules" ); 65 this.rules = rules; 66 } 67 68 72 public FBRuleReasoner(ReasonerFactory factory) { 73 this( new ArrayList(), factory); 74 } 75 76 81 public FBRuleReasoner(ReasonerFactory factory, Resource configuration) { 82 this( new ArrayList(), factory); 83 this.configuration = configuration; 84 if (configuration != null) loadConfiguration( configuration ); 85 } 86 87 90 protected void loadConfiguration( Resource configuration ) 91 { 92 StmtIterator i = configuration.listProperties(); 93 while (i.hasNext()) { 94 Statement st = i.nextStatement(); 95 doSetRDFNodeParameter( st.getPredicate(), st.getObject() ); 96 } 97 } 98 99 104 public FBRuleReasoner(List rules, ReasonerFactory factory) { 105 this(rules); 106 this.factory = factory; 107 } 108 109 113 protected FBRuleReasoner(List rules, Graph schemaGraph, ReasonerFactory factory) { 114 this(rules, factory); 115 this.schemaGraph = schemaGraph; 116 } 117 118 124 public FBRuleReasoner addRules(List rules) { 125 List combined = new ArrayList( this.rules ); 126 combined.addAll( rules ); 127 setRules( combined ); 128 return this; 129 } 130 131 137 public Model getReasonerCapabilities() { 138 if (factory != null) { 139 return factory.getCapabilities(); 140 } else { 141 return null; 142 } 143 } 144 145 151 public void addDescription(Model configSpec, Resource base) { 152 if (configuration != null) { 153 StmtIterator i = configuration.listProperties(); 154 while (i.hasNext()) { 155 Statement st = i.nextStatement(); 156 configSpec.add(base, st.getPredicate(), st.getObject()); 157 } 158 } 159 } 160 161 168 public boolean supportsProperty(Property property) { 169 if (factory == null) return false; 170 Model caps = factory.getCapabilities(); 171 Resource root = caps.getResource(factory.getURI()); 172 return caps.contains(root, ReasonerVocabulary.supportsP, property); 173 } 174 175 179 public Reasoner bindSchema(Graph tbox) throws ReasonerException { 180 if (schemaGraph != null) { 181 throw new ReasonerException("Can only bind one schema at a time to an OWLRuleReasoner"); 182 } 183 FBRuleInfGraph graph = new FBRuleInfGraph(this, rules, getPreload(), tbox); 184 graph.prepare(); 185 FBRuleReasoner fbr = new FBRuleReasoner(rules, graph, factory); 186 fbr.setDerivationLogging(recordDerivations); 187 fbr.setTraceOn(traceOn); 188 return fbr; 189 } 190 191 195 public Reasoner bindSchema(Model tbox) throws ReasonerException { 196 return bindSchema(tbox.getGraph()); 197 } 198 199 210 public InfGraph bind(Graph data) throws ReasonerException { 211 Graph schemaArg = schemaGraph == null ? getPreload() : (FBRuleInfGraph)schemaGraph; 212 FBRuleInfGraph graph = new FBRuleInfGraph(this, rules, schemaArg); 213 graph.setDerivationLogging(recordDerivations); 214 graph.setTraceOn(traceOn); 215 graph.rebind(data); 216 return graph; 217 } 218 219 223 public void setRules(List rules) { 224 this.rules = rules; 225 preload = null; 226 } 227 228 232 public List getRules() { 233 return rules; 234 } 235 236 240 public static List loadRules( String fileName ) { 241 try 242 { return Rule.parseRules(Util.loadRuleParserFromResourceFile( fileName ) ); } 243 catch (WrappedIOException e) 244 { throw new ReasonerException("Can't load rules file: " + fileName, e.getCause() ); } 245 } 246 247 251 public synchronized void tablePredicate(Node predicate) { 252 Rule tablePredicateRule = new Rule("", 254 new ClauseEntry[]{ 255 new Functor("table", new Node[] { predicate }) 256 }, 257 new ClauseEntry[]{}); 258 rules.add(tablePredicateRule); 260 } 261 262 265 protected synchronized InfGraph getPreload() { 266 if (cachePreload && preload == null) { 267 preload = (new FBRuleInfGraph(this, rules, null)); 268 preload.prepare(); 269 } 270 return preload; 271 } 272 273 281 public void setDerivationLogging(boolean logOn) { 282 recordDerivations = logOn; 283 } 284 285 289 public void setTraceOn(boolean state) { 290 traceOn = state; 291 } 292 293 297 public boolean isTraceOn() { 298 return traceOn; 299 } 300 301 314 public void setParameter(Property parameter, Object value) { 315 if (!doSetParameter(parameter, value)) { 316 throw new IllegalParameterException("RuleReasoner does not recognize configuration parameter " + parameter); 317 } else { 318 if (configuration == null) { 320 Model configModel = ModelFactory.createDefaultModel(); 321 configuration = configModel.createResource(); 322 } 323 Util.updateParameter(configuration, parameter, value); 324 } 325 } 326 327 338 protected boolean doSetRDFNodeParameter( Property parameter, RDFNode value ) 339 { 340 return 341 (value instanceof Resource && doSetResourceParameter( parameter, (Resource) value )) 342 || doSetParameter( parameter, value.toString() ) 343 ; 344 } 345 346 350 protected boolean doSetResourceParameter( Property parameter, Resource value ) 351 { return false; } 352 353 365 protected boolean doSetParameter(Property parameter, Object value) { 366 if (parameter.equals(ReasonerVocabulary.PROPderivationLogging)) { 367 recordDerivations = Util.convertBooleanPredicateArg(parameter, value); 368 return true; 369 } else if (parameter.equals(ReasonerVocabulary.PROPtraceOn)) { 370 traceOn = Util.convertBooleanPredicateArg(parameter, value); 371 return true; 372 } else { 373 return false; 374 } 375 } 376 377 381 public Capabilities getGraphCapabilities() { 382 if (capabilities == null) { 383 capabilities = new BaseInfGraph.InfCapabilities(); 384 } 385 return capabilities; 386 } 387 388 } 389 390 391 | Popular Tags |