KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > impl > oldCode > OWLExptRuleReasoner


1 /******************************************************************
2  * File: OWLExptRuleReasoner.java
3  * Created by: Dave Reynolds
4  * Created on: 10-Jul-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: OWLExptRuleReasoner.java,v 1.8 2005/04/08 16:37:51 der Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode;
11
12 import java.util.*;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import com.hp.hpl.jena.reasoner.*;
17 import com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph;
18 import com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner;
19 import com.hp.hpl.jena.reasoner.rulesys.Rule;
20 import com.hp.hpl.jena.reasoner.rulesys.Util;
21 import com.hp.hpl.jena.reasoner.rulesys.impl.OWLRuleTranslationHook;
22 import com.hp.hpl.jena.shared.WrappedIOException;
23 import com.hp.hpl.jena.graph.*;
24
25 /**
26  * A hybrid forward/backward implementation of the OWL closure rules - experimental variant.
27  *
28  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
29  * @version $Revision: 1.8 $ on $Date: 2005/04/08 16:37:51 $
30  */

31 public class OWLExptRuleReasoner extends FBRuleReasoner {
32     
33     /** The location of the OWL rule definitions on the class path */
34     protected static final String JavaDoc RULE_FILE = "etc/owl-fb.rules";
35     
36     /** The parsed rules */
37     protected static List ruleSet;
38     
39     /** The precomputed axiom closure and compiled rule set */
40     protected static FBRuleInfGraph preload;
41     
42     /** Flag, set to true to use the LP engine */
43     private static final boolean USE_LP = true;
44     
45     protected static Log logger = LogFactory.getLog(OWLExptRuleReasoner.class);
46     
47     /**
48      * Constructor
49      */

50     public OWLExptRuleReasoner(ReasonerFactory factory) {
51         super(loadRules(), factory);
52         
53     }
54     
55     /**
56      * Internal constructor, used to generated a partial binding of a schema
57      * to a rule reasoner instance.
58      */

59     private OWLExptRuleReasoner(OWLExptRuleReasoner parent, InfGraph schemaGraph) {
60         super(parent.rules, schemaGraph, parent.factory);
61     }
62     
63     /**
64      * Return the rule set, loading it in if necessary
65      */

66     public static List loadRules() {
67         if (ruleSet == null) {
68             try {
69                 ruleSet = Rule.parseRules(Util.loadRuleParserFromResourceFile(RULE_FILE));
70             } catch (WrappedIOException e) {
71                 throw new ReasonerException("Can't load rules file: " + RULE_FILE, e);
72             }
73         }
74         return ruleSet;
75     }
76     
77     
78     /**
79      * Precompute the implications of a schema graph. The statements in the graph
80      * will be combined with the data when the final InfGraph is created.
81      */

82     public Reasoner bindSchema(Graph tbox) throws ReasonerException {
83         if (schemaGraph != null) {
84             throw new ReasonerException("Can only bind one schema at a time to an OWLRuleReasoner");
85         }
86         FBRuleInfGraph graph = makeInfGraph(rules, tbox, true);
87         graph.addPreprocessingHook(new OWLRuleTranslationHook());
88         graph.prepare();
89         return new OWLExptRuleReasoner(this, graph);
90     }
91         
92     /**
93      * Attach the reasoner to a set of RDF data to process.
94      * The reasoner may already have been bound to specific rules or ontology
95      * axioms (encoded in RDF) through earlier bindRuleset calls.
96      *
97      * @param data the RDF data to be processed, some reasoners may restrict
98      * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
99      * @return an inference graph through which the data+reasoner can be queried.
100      * @throws ReasonerException if the data is ill-formed according to the
101      * constraints imposed by this reasoner.
102      */

103     public InfGraph bind(Graph data) throws ReasonerException {
104         FBRuleInfGraph graph = null;
105         InfGraph schemaArg = schemaGraph == null ? getPreload() : (FBRuleInfGraph)schemaGraph;
106         List baseRules = ((FBRuleInfGraph)schemaArg).getRules();
107         graph = makeInfGraph(baseRules, schemaArg, false);
108         graph.addPreprocessingHook(new OWLRuleTranslationHook());
109         graph.setDerivationLogging(recordDerivations);
110         graph.setTraceOn(isTraceOn());
111         graph.rebind(data);
112                 
113         return graph;
114     }
115     
116     /**
117      * Get the single static precomputed rule closure.
118      */

119     public InfGraph getPreload() {
120         synchronized (OWLExptRuleReasoner.class) {
121             if (preload == null) {
122                 preload = makeInfGraph(rules, null, false);
123                 preload.prepare();
124             }
125             return preload;
126         }
127     }
128     
129     /**
130      * Construct an FB rule infgraph variant. Allows switching between the normal
131      * and LP implementations during development.
132      */

133     private FBRuleInfGraph makeInfGraph(List rules, Graph schema, boolean doPreload) {
134         if (USE_LP) {
135             if (doPreload) {
136                 return new FBRuleInfGraph(this, rules, getPreload(), schema);
137             } else {
138                 return new FBRuleInfGraph(this, rules, schema);
139             }
140         } else {
141             if (doPreload) {
142                 return new FBRuleInfGraph(this, rules, getPreload(), schema);
143             } else {
144                 return new FBRuleInfGraph(this, rules, schema);
145             }
146         }
147     }
148 }
149
150
151 /*
152     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
153     All rights reserved.
154
155     Redistribution and use in source and binary forms, with or without
156     modification, are permitted provided that the following conditions
157     are met:
158
159     1. Redistributions of source code must retain the above copyright
160        notice, this list of conditions and the following disclaimer.
161
162     2. Redistributions in binary form must reproduce the above copyright
163        notice, this list of conditions and the following disclaimer in the
164        documentation and/or other materials provided with the distribution.
165
166     3. The name of the author may not be used to endorse or promote products
167        derived from this software without specific prior written permission.
168
169     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
170     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
171     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
172     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
173     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
174     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
175     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
176     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
177     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
178     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
179 */
Popular Tags