001: /******************************************************************
002: * File: OWLBRuleReasoner.java
003: * Created by: Dave Reynolds
004: * Created on: 12-May-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: OWLFBRuleReasoner.java,v 1.19 2008/01/02 12:07:47 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys;
010:
011: import java.util.*;
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: import com.hp.hpl.jena.reasoner.*;
016: import com.hp.hpl.jena.reasoner.rulesys.impl.OWLRuleTranslationHook;
017: import com.hp.hpl.jena.shared.impl.JenaParameters;
018: import com.hp.hpl.jena.graph.*;
019:
020: /**
021: * A hybrid forward/backward implementation of the OWL closure rules.
022: *
023: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
024: * @version $Revision: 1.19 $ on $Date: 2008/01/02 12:07:47 $
025: */
026: public class OWLFBRuleReasoner extends FBRuleReasoner {
027:
028: /** The location of the OWL rule definitions on the class path */
029: protected static final String RULE_FILE = "etc/owl-fb.rules";
030:
031: /** The parsed rules */
032: protected static List ruleSet;
033:
034: /** The precomputed axiom closure and compiled rule set */
035: protected static FBRuleInfGraph staticPreload;
036:
037: protected static Log logger = LogFactory
038: .getLog(OWLFBRuleReasoner.class);
039:
040: /**
041: * Constructor
042: */
043: public OWLFBRuleReasoner(ReasonerFactory factory) {
044: super (loadRules(), factory);
045:
046: }
047:
048: /**
049: * Internal constructor, used to generated a partial binding of a schema
050: * to a rule reasoner instance.
051: */
052: private OWLFBRuleReasoner(OWLFBRuleReasoner parent,
053: InfGraph schemaGraph) {
054: super (parent.rules, schemaGraph, parent.factory);
055: }
056:
057: /**
058: * Return the rule set, loading it in if necessary
059: */
060: public static List loadRules() {
061: if (ruleSet == null)
062: ruleSet = loadRules(RULE_FILE);
063: return ruleSet;
064: }
065:
066: /**
067: * Precompute the implications of a schema graph. The statements in the graph
068: * will be combined with the data when the final InfGraph is created.
069: */
070: public Reasoner bindSchema(Graph tbox) throws ReasonerException {
071: checkArgGraph(tbox);
072: if (schemaGraph != null) {
073: throw new ReasonerException(
074: "Can only bind one schema at a time to an OWLRuleReasoner");
075: }
076: FBRuleInfGraph graph = new FBRuleInfGraph(this , rules,
077: getPreload(), tbox);
078: graph.addPreprocessingHook(new OWLRuleTranslationHook());
079: graph.prepare();
080: return new OWLFBRuleReasoner(this , graph);
081: }
082:
083: /**
084: * Attach the reasoner to a set of RDF data to process.
085: * The reasoner may already have been bound to specific rules or ontology
086: * axioms (encoded in RDF) through earlier bindRuleset calls.
087: *
088: * @param data the RDF data to be processed, some reasoners may restrict
089: * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
090: * @return an inference graph through which the data+reasoner can be queried.
091: * @throws ReasonerException if the data is ill-formed according to the
092: * constraints imposed by this reasoner.
093: */
094: public InfGraph bind(Graph data) throws ReasonerException {
095: checkArgGraph(data);
096: FBRuleInfGraph graph = null;
097: InfGraph schemaArg = schemaGraph == null ? getPreload()
098: : (FBRuleInfGraph) schemaGraph;
099: List baseRules = ((FBRuleInfGraph) schemaArg).getRules();
100: graph = new FBRuleInfGraph(this , baseRules, schemaArg);
101: graph.addPreprocessingHook(new OWLRuleTranslationHook());
102: graph.setDerivationLogging(recordDerivations);
103: graph.setTraceOn(traceOn);
104: graph.rebind(data);
105:
106: return graph;
107: }
108:
109: /**
110: * Get the single static precomputed rule closure.
111: */
112: public InfGraph getPreload() {
113: synchronized (OWLFBRuleReasoner.class) {
114: if (staticPreload == null) {
115: boolean prior = JenaParameters.enableFilteringOfHiddenInfNodes;
116: try {
117: JenaParameters.enableFilteringOfHiddenInfNodes = true;
118: staticPreload = new FBRuleInfGraph(this , rules,
119: null);
120: staticPreload.prepare();
121: } finally {
122: JenaParameters.enableFilteringOfHiddenInfNodes = prior;
123: }
124: }
125: return staticPreload;
126: }
127: }
128:
129: /**
130: * Check an argument graph to make sure it is not an OWL rule graph
131: * already and if so log a warning message.
132: */
133: private void checkArgGraph(Graph g) {
134: if (JenaParameters.enableOWLRuleOverOWLRuleWarnings) {
135: if (g instanceof InfGraph) {
136: if (((InfGraph) g).getReasoner() instanceof OWLFBRuleReasoner) {
137: logger
138: .warn("Creating OWL rule reasoner working over another OWL rule reasoner");
139: }
140: }
141: }
142: }
143: }
144:
145: /*
146: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
147: All rights reserved.
148:
149: Redistribution and use in source and binary forms, with or without
150: modification, are permitted provided that the following conditions
151: are met:
152:
153: 1. Redistributions of source code must retain the above copyright
154: notice, this list of conditions and the following disclaimer.
155:
156: 2. Redistributions in binary form must reproduce the above copyright
157: notice, this list of conditions and the following disclaimer in the
158: documentation and/or other materials provided with the distribution.
159:
160: 3. The name of the author may not be used to endorse or promote products
161: derived from this software without specific prior written permission.
162:
163: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
164: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
165: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
166: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
167: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
168: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
169: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
170: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
171: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
172: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
173: */
|