/** * Test a global boolean property of the graph. This might included * properties like consistency, OWLSyntacticValidity etc. * It remains to be seen what level of generality is needed here. We could * replace this by a small number of specific tests for common concepts. * @param property the URI of the property to be tested * @return a Node giving the value of the global property, this may * be a boolean literal, some other literal value (e.g. a size). */ @Override public Node getGlobalProperty(Node property) { throw new ReasonerException("Global property not implemented: " + property); }
/** * Process a call to a builtin predicate * @param clause the Functor representing the call * @param env the BindingEnvironment for this call * @param rule the rule which is invoking this call * @return true if the predicate succeeds */ @Override public boolean processBuiltin(ClauseEntry clause, Rule rule, BindingEnvironment env) { throw new ReasonerException("Internal error in FBLP rule engine, incorrect invocation of building in rule " + rule); }
/** * Set (or change) the rule set that this reasoner should execute. * @param rules a list of Rule objects */ @Override public void setRules(List<Rule> rules) { this.rules = rules; preload = null; if (schemaGraph != null) { // The change of rules invalidates the existing precomputed schema graph // This might be recoverable but for now simply flag the error and let the // user reorder their code to set the rules before doing a bind! throw new ReasonerException("Cannot change the rule set for a bound rule reasoner.\nSet the rules before calling bindSchema"); } }
/** * Set the direction of rule inference desired. * If set to a pure mode (FORWARD, BACKWARD) then the rules will be * interpreted as operating in that direction which ever direction * they were written in. In HYBRID mode then the direction of the rule * itself which control whether it is used in forward or backward mode. * In addition, HYBRID mode allows forward rules to generate addition * backward rules. */ public void setMode(RuleMode mode) { if (schemaGraph != null) { throw new ReasonerException("Can't change mode of a reasoner bound to a schema"); } this.mode = mode; preload = null; bRuleStore = null; }
/** * Process a call to a builtin predicate * @param clause the Functor representing the call * @param env the BindingEnvironment for this call * @param rule the rule which is invoking this call * @return true if the predicate succeeds */ @Override public boolean processBuiltin(ClauseEntry clause, Rule rule, BindingEnvironment env) { throw new ReasonerException("Internal error in FBLP rule engine, incorrect invocation of builtin in rule " + rule); // TODO: Remove // if (clause instanceof Functor) { // context.setEnv(env); // context.setRule(rule); // return((Functor)clause).evalAsBodyClause(context); // } else { // throw new ReasonerException("Illegal builtin predicate: " + clause + " in rule " + rule); // } }
/** * Adds a new Backward rule as a rules of a forward rule process. Only some * infgraphs support this. */ @Override public void addBRule(Rule brule) { throw new ReasonerException("Forward reasoner does not support hybrid rules - " + brule.toShortString()); }
/** * Return the schema graph, if any, bound into this inference graph. */ @Override public Graph getSchemaGraph() { if (tbox == null) return null; if (tbox instanceof FGraph) { return ((FGraph)tbox).getGraph(); } else { throw new ReasonerException("Transitive reasoner got into an illegal state"); } }
/** * Deletes a new Backward rule as a rules of a forward rule process. Only some * infgraphs support this. */ @Override public void deleteBRule(Rule brule) { throw new ReasonerException("Forward reasoner does not support hybrid rules - " + brule.toShortString()); }
/** * Return the RDFS rule set, loading it in if necessary. * @param level a string defining the processing level required */ public static List<Rule> loadRulesLevel(String level) { List<Rule> ruleSet = ruleSets.get(level); if (ruleSet == null) { String file = ruleFiles.get(level); if (file == null) { throw new ReasonerException("Illegal RDFS conformance level: " + level); } ruleSet = loadRules( file ); ruleSets.put(level, ruleSet); } return ruleSet; }
/** * Set to true to enable translation of selected parts of an OWL schema * to additional rules. At present only intersction statements are handled this way. * The translation is only applicable in HYBRID mode. */ public void setOWLTranslation(boolean enableOWLTranslation) { if (enableOWLTranslation && (mode != HYBRID)) { throw new ReasonerException("Can only enable OWL rule translation in HYBRID mode"); } this.enableOWLTranslation = enableOWLTranslation; if (enableOWLTranslation) { addPreprocessingHook(owlTranslator); } else { removePreprocessingHook(owlTranslator); } }
/** * Create and return a new instance of the reasoner identified by * the given uri. * @param uri the uri of the reasoner to be created, expressed as a simple string * @param configuration an optional set of configuration information encoded in RDF this * parameter can be null if no configuration information is required. * @return a reaoner instance * @throws ReasonerException if there is not such reasoner or if there is * some problem during instantiation */ public Reasoner create(String uri, Resource configuration) throws ReasonerException { ReasonerFactory factory = getFactory(uri); if (factory != null) { return factory.create(configuration); } else { throw new ReasonerException("Attempted to instantiate an unknown reasoner: " + uri); } }
/** * A convenience version of getGlobalProperty which can only return * a boolean result. */ @Override public boolean testGlobalProperty(Node property) { Node resultNode = getGlobalProperty(property); if (resultNode.isLiteral()) { Object result = resultNode.getLiteralValue(); if (result instanceof Boolean) { return ((Boolean)result).booleanValue(); } } throw new ReasonerException("Global property test returned non-boolean value" + "\nTest was: " + property + "\nResult was: " + resultNode); }
/** * Add a single rule to the store. * N.B. This will invalidate current partial results and the engine * should be reset() before future queries. */ public synchronized void addRule(Rule rule) { checkSafeToUpdate(); if (rule.headLength() > 1) { throw new ReasonerException("Backward rules only allowed one head clause"); } ruleStore.addRule(rule); }
/** Answer the list of rules loaded from the given filename. May throw a ReasonerException wrapping an IOException. */ public static List<Rule> loadRules( String fileName ) { try { return Rule.parseRules(Util.loadRuleParserFromResourceFile( fileName ) ); } catch (WrappedIOException e) { throw new ReasonerException("Can't load rules file: " + fileName, e.getCause() ); } }
private boolean allMonotonic(ClauseEntry[] elts) { for (int i = 0; i < elts.length; i++) { ClauseEntry elt = elts[i]; if (elt instanceof Functor) { Builtin b = ((Functor)elt).getImplementor(); if (b != null) { if (! b.isMonotonic() ) return false; } else { throw new ReasonerException("Undefined Functor " + ((Functor)elt).getName() +" in " + toShortString()); } } } return true; }
/** * Extracts all of the subClass and subProperty declarations from * the given schema/tbox and caches the resultant graphs. * It can only be used once, can't stack up multiple tboxes this way. * This limitation could be lifted - the only difficulty is the need to * reprocess all the earlier tboxes if a new subPropertyOf subPropertyOf * subClassOf is discovered. * @param tbox schema containing the property and class declarations */ Reasoner bindSchema(Finder tbox) throws ReasonerException { if (this.tbox != null) { throw new ReasonerException("Attempt to bind multiple rulesets - disallowed for now"); } TransitiveGraphCache sCc = new TransitiveGraphCache(directSubClassOf, subClassOf); TransitiveGraphCache sPc = new TransitiveGraphCache(directSubPropertyOf, subPropertyOf); TransitiveEngine.cacheSubPropUtility(tbox, sPc); TransitiveEngine.cacheSubClassUtility(tbox, sPc, sCc); return new TransitiveReasoner(tbox, sCc, sPc); }
/** * Precompute the implications of a schema graph. The statements in the graph * will be combined with the data when the final InfGraph is created. */ @Override public Reasoner bindSchema(Graph tbox) throws ReasonerException { if (schemaGraph != null) { throw new ReasonerException("Can only bind one schema at a time to an OWLRuleReasoner"); } FBRuleInfGraph graph = new FBRuleInfGraph(this, rules, getPreload(), tbox); graph.prepare(); FBRuleReasoner fbr = new FBRuleReasoner(rules, graph, factory); fbr.setDerivationLogging(recordDerivations); fbr.setTraceOn(traceOn); return fbr; }
/** * Precompute the implications of a schema graph. The statements in the graph * will be combined with the data when the final InfGraph is created. */ @Override public Reasoner bindSchema(Graph tbox) throws ReasonerException { checkArgGraph(tbox); if (schemaGraph != null) { throw new ReasonerException("Can only bind one schema at a time to an OWLRuleReasoner"); } FBRuleInfGraph graph = new FBRuleInfGraph(this, rules, getPreload(), tbox); graph.addPreprocessingHook(new OWLRuleTranslationHook()); graph.prepare(); return new OWLFBRuleReasoner(this, graph); }
/** * Scan the rules for any run actions and run those */ protected void findAndProcessActions() { RETERuleContext tempContext = new RETERuleContext(infGraph, this); for (Iterator<Rule> i = rules.iterator(); i.hasNext(); ) { Rule r = i.next(); if (r.bodyLength() == 0) { for (int j = 0; j < r.headLength(); j++) { Object head = r.getHeadElement(j); if (head instanceof Functor) { Functor f = (Functor)head; Builtin imp = f.getImplementor(); if (imp != null) { tempContext.setRule(r); tempContext.setEnv(new BindingVector( r.getNumVars() )); imp.headAction(f.getArgs(), f.getArgLength(), tempContext); } else { throw new ReasonerException("Invoking undefined Functor " + f.getName() +" in " + r.toShortString()); } } } } } }
/** * Precompute the implications of a schema graph. The statements in the graph * will be combined with the data when the final InfGraph is created. */ @Override public Reasoner bindSchema(Graph tbox) throws ReasonerException { if (schemaGraph != null) { throw new ReasonerException("Can only bind one schema at a time to an RDFSRuleReasoner"); } FBRuleInfGraph graph = new FBRuleInfGraph(this, rules, getPreload(), tbox); if (enableTGCCaching) (graph).setUseTGCCache(); graph.prepare(); RDFSRuleReasoner grr = new RDFSRuleReasoner(graph, factory); grr.setDerivationLogging(recordDerivations); grr.setTraceOn(traceOn); grr.setTransitiveClosureCaching(enableTGCCaching); grr.setFunctorFiltering(filterFunctors); if (preprocessorHooks != null) { for (RulePreprocessHook rulePreprocessHook : preprocessorHooks) { grr.addPreprocessingHook(rulePreprocessHook); } } return grr; }