KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: OWLRuleReasoner.java
3  * Created by: Dave Reynolds
4  * Created on: 11-Apr-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: OWLRuleReasoner.java,v 1.6 2005/04/08 16:37:52 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.BasicForwardRuleInfGraph;
18 import com.hp.hpl.jena.reasoner.rulesys.BasicForwardRuleReasoner;
19 import com.hp.hpl.jena.reasoner.rulesys.ClauseEntry;
20 import com.hp.hpl.jena.reasoner.rulesys.Functor;
21 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable;
22 import com.hp.hpl.jena.reasoner.rulesys.Rule;
23 import com.hp.hpl.jena.reasoner.rulesys.Util;
24 import com.hp.hpl.jena.shared.WrappedIOException;
25 import com.hp.hpl.jena.vocabulary.*;
26 import com.hp.hpl.jena.graph.*;
27
28 /**
29  * @deprecated Obsoleted post jena2.0, replaced by
30  * {@link com.hp.hpl.jena.reasoner.rulesys.OWLFBRuleReasoner}.
31  *
32  * An pure forward chaining implementation of the experimental OWL closure rules
33  * based upon the basic forward rule interpreter. This is not a serious or
34  * usable OWL implementation, it is a tool for developing the rules.
35  * <p>
36  * TODO: Replace intersection-translation step by rule based alternative (or failing that
37  * figure out what should be done at the bindSchema stage).
38  *
39  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
40  * @version $Revision: 1.6 $ on $Date: 2005/04/08 16:37:52 $
41  */

42 public class OWLRuleReasoner extends BasicForwardRuleReasoner {
43     
44     /** The location of the OWL rule definitions on the class path */
45     protected static final String JavaDoc RULE_FILE = "etc/owl.rules";
46     
47     /** The parsed rules */
48     protected static List ruleSet;
49     
50     protected static Log logger = LogFactory.getLog(OWLRuleReasoner.class);
51     
52     /** Performance statistics - the total number of rule firings used during data bind operations so far. */
53     protected static long nRulesFired = 0;
54     
55     /** Performance statistics - the total elapsed time spent during data bind operations so far, in ms. */
56     protected static long timeCost = 0;
57     
58     /**
59      * Constructor
60      */

61     public OWLRuleReasoner() {
62         super(loadRules(), OWLRuleReasonerFactory.theInstance());
63         
64     }
65     
66     /**
67      * Internal constructor, used to generated a partial binding of a schema
68      * to a rule reasoner instance.
69      */

70     private OWLRuleReasoner(List rules, InfGraph schemaGraph) {
71         super(rules, OWLRuleReasonerFactory.theInstance());
72         this.rules = rules;
73         this.schemaGraph = schemaGraph;
74     }
75     
76     /**
77      * Return the RDFS rule set, loading it in if necessary
78      */

79     public static List loadRules() {
80         if (ruleSet == null) {
81             try {
82                 ruleSet = Rule.parseRules(Util.loadRuleParserFromResourceFile(RULE_FILE));
83             } catch (WrappedIOException e) {
84                 throw new ReasonerException("Can't load rules file: " + RULE_FILE, e);
85             }
86         }
87         return ruleSet;
88     }
89     
90     /**
91      * Precompute the implications of a schema graph.
92      * The practical benefit of this has not yet been fully checked out.
93      */

94     public Reasoner bindSchema(Graph tbox) throws ReasonerException {
95         InfGraph graph = new BasicForwardRuleInfGraph(this, rules, tbox);
96         // Process the scehma looking for any intersection declarations
97
// that we translate into addtiional rules procedurally (for now at least)
98
Iterator i = tbox.find(null, OWL.intersectionOf.asNode(), null);
99         ArrayList rules = (ArrayList) ruleSet;
100         if (i.hasNext()) {
101             rules = (ArrayList) rules.clone();
102             while(i.hasNext()) {
103                 translateIntersectionOf((Triple)i.next(), rules, tbox);
104             }
105         }
106         return new OWLRuleReasoner(rules, graph);
107     }
108         
109     /**
110      * Attach the reasoner to a set of RDF data to process.
111      * The reasoner may already have been bound to specific rules or ontology
112      * axioms (encoded in RDF) through earlier bindRuleset calls.
113      *
114      * @param data the RDF data to be processed, some reasoners may restrict
115      * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
116      * @return an inference graph through which the data+reasoner can be queried.
117      * @throws ReasonerException if the data is ill-formed according to the
118      * constraints imposed by this reasoner.
119      */

120     public InfGraph bind(Graph data) throws ReasonerException {
121         // First process the data looking for any intersection declarations
122
// that we translate into addtiional rules procedurally (for now at least)
123
long startTime = System.currentTimeMillis();
124         Iterator i = data.find(null, OWL.intersectionOf.asNode(), null);
125         ArrayList rules = (ArrayList) ruleSet;
126         if (i.hasNext()) {
127             rules = (ArrayList) rules.clone();
128             while(i.hasNext()) {
129                 translateIntersectionOf((Triple)i.next(), rules, data);
130             }
131         }
132
133         // Now create the inference graph
134
BasicForwardRuleInfGraph graph = new BasicForwardRuleInfGraph(this, rules, schemaGraph);
135         graph.setDerivationLogging(recordDerivations);
136         graph.setTraceOn(traceOn);
137         graph.rebind(data);
138         long endTime = System.currentTimeMillis();
139         timeCost += (double)(endTime - startTime);
140         nRulesFired += graph.getNRulesFired();
141         
142         return graph;
143     }
144     
145     /**
146      * Print (to the default logging channel at Info level) a summary of the
147      * total number of rules fired and the time taken by
148      * the reasoner instances created thus far.
149      */

150     public static void printStats() {
151         logger.info("Fired " + nRulesFired + " over " + (timeCost/1000.0) + " s = "
152                      + (nRulesFired*1000/timeCost) + " r/s");
153     }
154     
155     /**
156      * Translation code to compile intersectionOf declarations.
157      * In the future this will be replaced by a rule-baesd translator which will cope
158      * with dynamic expressions.
159      * @param decl a triple of the form (C owl:intersectionOf [..])
160      * @param rules a list of rules to be extended
161      * @param data the source data to use as a context for this processing
162      */

163     private void translateIntersectionOf(Triple decl, List rules, Graph data) {
164         Node className = decl.getSubject();
165         List elements = new ArrayList();
166         translateIntersectionList(decl.getObject(), data, elements);
167         // Generate the corresponding ruleset
168
List recognitionBody = new ArrayList();
169         Node var = new Node_RuleVariable("?x", 0);
170         for (Iterator i = elements.iterator(); i.hasNext(); ) {
171             Node description = (Node)i.next();
172             // Implication rule
173
Rule ir = new Rule("intersectionImplication", new ClauseEntry[] {
174                                 new TriplePattern(className, RDFS.subClassOf.asNode(), description)
175                                 }, new ClauseEntry[0]);
176            rules.add(ir);
177            //System.out.println("Adding rule: " + ir.toString());
178
// Recognition rule elements
179
recognitionBody.add(new TriplePattern(var, RDF.type.asNode(), description));
180         }
181         List recognitionHead = new ArrayList(1);
182         recognitionHead.add(new TriplePattern(var, RDF.type.asNode(), className));
183         Rule rr = new Rule("intersectionRecognition", recognitionHead, recognitionBody);
184         //System.out.println("Adding rule: " + rr.toString());
185
rules.add(rr);
186     }
187     
188     /**
189      * Translation code to translate a list of intersection elements into a
190      * Java list of corresponding class names or restriction functors.
191      * @param node the list node currently being processed
192      * @param data the source data to use as a context for this processing
193      * @param elements the list of elements found so far
194      */

195     private void translateIntersectionList(Node node, Graph data, List elements) {
196         if (node.equals(RDF.nil.asNode())) {
197             return; // end of list
198
}
199         Node description = Util.getPropValue(node, RDF.first.asNode(), data);
200         // Translate the first description element
201
if (data.contains(description, RDF.type.asNode(), OWL.Restriction.asNode())) {
202             // Process a restriction element
203
Node onprop = Util.getPropValue(description, OWL.onProperty.asNode(), data);
204             Node value;
205             if ((value = Util.getPropValue(description, OWL.allValuesFrom.asNode(), data)) != null) {
206                 elements.add(Functor.makeFunctorNode("all", new Node[] {onprop, value}));
207             } else if ((value = Util.getPropValue(description, OWL.someValuesFrom.asNode(), data)) != null) {
208                 elements.add(Functor.makeFunctorNode("some", new Node[] {onprop, value}));
209             } else if ((value = Util.getPropValue(description, OWL.minCardinality.asNode(), data)) != null) {
210                 elements.add(Functor.makeFunctorNode("min", new Node[] {onprop, value}));
211             } else if ((value = Util.getPropValue(description, OWL.maxCardinality.asNode(), data)) != null) {
212                 elements.add(Functor.makeFunctorNode("max", new Node[] {onprop, value}));
213             } else if ((value = Util.getPropValue(description, OWL.cardinality.asNode(), data)) != null) {
214                 elements.add(Functor.makeFunctorNode("max", new Node[] {onprop, value}));
215                 elements.add(Functor.makeFunctorNode("min", new Node[] {onprop, value}));
216             }
217         } else {
218             // Assume its a class name
219
elements.add(description);
220         }
221         // Process the list tail
222
Node next = Util.getPropValue(node, RDF.rest.asNode(), data);
223         translateIntersectionList(next, data, elements);
224     }
225      
226 }
227
228 /*
229     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
230     All rights reserved.
231
232     Redistribution and use in source and binary forms, with or without
233     modification, are permitted provided that the following conditions
234     are met:
235
236     1. Redistributions of source code must retain the above copyright
237        notice, this list of conditions and the following disclaimer.
238
239     2. Redistributions in binary form must reproduce the above copyright
240        notice, this list of conditions and the following disclaimer in the
241        documentation and/or other materials provided with the distribution.
242
243     3. The name of the author may not be used to endorse or promote products
244        derived from this software without specific prior written permission.
245
246     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
247     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
248     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
249     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
250     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
251     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
252     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
253     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
255     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
256 */

257
Popular Tags