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