KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > RDFSRuleReasoner


1 /******************************************************************
2  * File: RDFSRuleReasoner.java
3  * Created by: Dave Reynolds
4  * Created on: 16-Jun-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: RDFSRuleReasoner.java,v 1.16 2005/02/21 12:17:04 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys;
11
12 import java.util.*;
13
14 import com.hp.hpl.jena.graph.*;
15 import com.hp.hpl.jena.rdf.model.*;
16 import com.hp.hpl.jena.reasoner.*;
17 import com.hp.hpl.jena.reasoner.rulesys.impl.RDFSCMPPreprocessHook;
18 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
19
20 /**
21  * A full implemention of RDFS reasoning using a hybrid rule system, together
22  * with optimized subclass/subproperty closure using the transitive graph caches.
23  * Implements the container membership property rules using an optional
24  * data scanning hook. Implements datatype range validation.
25  *
26  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
27  * @version $Revision: 1.16 $ on $Date: 2005/02/21 12:17:04 $
28  */

29 public class RDFSRuleReasoner extends GenericRuleReasoner {
30     
31     /** Constant: used to indicate default RDFS processing level */
32     public static final String JavaDoc DEFAULT_RULES = "default";
33     
34     /** Constant: used to indicate full RDFS processing level */
35     public static final String JavaDoc FULL_RULES = "full";
36     
37     /** Constant: used to indicate minimal RDFS processing level */
38     public static final String JavaDoc SIMPLE_RULES = "simple";
39     
40     /** The location of the default RDFS rule definitions on the class path */
41     protected static final String JavaDoc RULE_FILE = "etc/rdfs-fb-tgc-noresource.rules";
42     
43     /** The location of the full RDFS rule definitions on the class path */
44     protected static final String JavaDoc FULL_RULE_FILE = "etc/rdfs-fb-tgc.rules";
45     
46     /** The location of the simple RDFS rule definitions on the class path */
47     protected static final String JavaDoc SIMPLE_RULE_FILE = "etc/rdfs-fb-tgc-simple.rules";
48     
49     /** The cached rule sets, indexed by processing level */
50     protected static HashMap ruleSets = new HashMap();
51     
52     /** The rule file names, indexed by processing level */
53     protected static HashMap ruleFiles;
54     
55     /** The (stateless) preprocessor for container membership properties */
56     protected static RulePreprocessHook cmpProcessor = new RDFSCMPPreprocessHook();
57     
58     static {
59         ruleFiles = new HashMap();
60         ruleFiles.put(DEFAULT_RULES, RULE_FILE);
61         ruleFiles.put(FULL_RULES, FULL_RULE_FILE);
62         ruleFiles.put(SIMPLE_RULES, SIMPLE_RULE_FILE);
63     }
64     
65     /**
66      * Constructor
67      */

68     public RDFSRuleReasoner(ReasonerFactory parent) {
69         super(loadRulesLevel(DEFAULT_RULES), parent);
70         setMode(HYBRID);
71         setTransitiveClosureCaching(true);
72         //addPreprocessingHook(new RDFSCMPPreprocessHook());
73
}
74     
75     /**
76      * Constructor
77      * @param factory the parent reasoner factory which is consulted to answer capability questions
78      * @param configuration RDF information to configure the rule set and mode, can be null
79      */

80     public RDFSRuleReasoner(ReasonerFactory factory, Resource configuration) {
81         this(factory);
82         if (configuration != null) {
83             StmtIterator i = configuration.listProperties();
84             while (i.hasNext()) {
85                 Statement st = i.nextStatement();
86                 doSetParameter(st.getPredicate(), st.getObject().toString());
87             }
88         }
89     }
90    
91     /**
92      * Internal constructor, used to generated a partial binding of a schema
93      * to a rule reasoner instance.
94      */

95     protected RDFSRuleReasoner(FBRuleInfGraph schemaGraph, ReasonerFactory factory) {
96         super(schemaGraph.getRules(), factory);
97         this.schemaGraph = schemaGraph;
98     }
99     
100     /**
101      * Internal version of setParameter that does not directly raise an
102      * exception on parameters it does not reconize.
103      * @return false if the parameter was not recognized
104      */

105     protected boolean doSetParameter(Property parameter, Object JavaDoc value) {
106         if (parameter.equals(ReasonerVocabulary.PROPenableCMPScan)) {
107             boolean scanProperties = Util.convertBooleanPredicateArg(parameter, value);
108             if (scanProperties) {
109                 addPreprocessingHook(cmpProcessor);
110             } else {
111                 removePreprocessingHook(cmpProcessor);
112             }
113             return true;
114         } else if (parameter.equals(ReasonerVocabulary.PROPsetRDFSLevel)) {
115             String JavaDoc level = ((String JavaDoc)value).toLowerCase();
116             setRules(loadRulesLevel(level));
117             if (level.equals(FULL_RULES)) {
118                 addPreprocessingHook(cmpProcessor);
119             } else {
120                 removePreprocessingHook(cmpProcessor);
121             }
122             return true;
123         } else {
124             return super.doSetParameter(parameter, value);
125         }
126     }
127     
128     /**
129      * Attach the reasoner to a set of RDF data to process.
130      * The reasoner may already have been bound to specific rules or ontology
131      * axioms (encoded in RDF) through earlier bindRuleset calls.
132      *
133      * @param data the RDF data to be processed, some reasoners may restrict
134      * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
135      * @return an inference graph through which the data+reasoner can be queried.
136      * @throws ReasonerException if the data is ill-formed according to the
137      * constraints imposed by this reasoner.
138      */

139     public InfGraph bind(Graph data) throws ReasonerException {
140         Graph schemaArg = schemaGraph == null ? getPreload() : schemaGraph;
141         InfGraph graph = null;
142         List ruleSet = ((FBRuleInfGraph)schemaArg).getRules();
143         FBRuleInfGraph fbgraph = new RDFSRuleInfGraph(this, ruleSet, schemaArg);
144         graph = fbgraph;
145         if (enableTGCCaching) fbgraph.setUseTGCCache();
146         fbgraph.setTraceOn(traceOn);
147         if (preprocessorHooks!= null) {
148             for (Iterator i = preprocessorHooks.iterator(); i.hasNext(); ) {
149                 fbgraph.addPreprocessingHook((RulePreprocessHook)i.next());
150             }
151         }
152         graph.setDerivationLogging(recordDerivations);
153         graph.rebind(data);
154         return graph;
155     }
156     
157     /**
158      * Precompute the implications of a schema graph. The statements in the graph
159      * will be combined with the data when the final InfGraph is created.
160      */

161     public Reasoner bindSchema(Graph tbox) throws ReasonerException {
162         if (schemaGraph != null) {
163             throw new ReasonerException("Can only bind one schema at a time to an RDFSRuleReasoner");
164         }
165         FBRuleInfGraph graph = new FBRuleInfGraph(this, rules, getPreload(), tbox);
166         if (enableTGCCaching) ((FBRuleInfGraph)graph).setUseTGCCache();
167         graph.prepare();
168         RDFSRuleReasoner grr = new RDFSRuleReasoner(graph, factory);
169         grr.setDerivationLogging(recordDerivations);
170         grr.setTraceOn(traceOn);
171         grr.setTransitiveClosureCaching(enableTGCCaching);
172         grr.setFunctorFiltering(filterFunctors);
173         if (preprocessorHooks != null) {
174             for (Iterator i = preprocessorHooks.iterator(); i.hasNext(); ) {
175                 grr.addPreprocessingHook((RulePreprocessHook)i.next());
176             }
177         }
178         return grr;
179     }
180     
181     /**
182      * Return the RDFS rule set, loading it in if necessary.
183      * @param level a string defining the processing level required
184      */

185     public static List loadRulesLevel(String JavaDoc level) {
186         List ruleSet = (List)ruleSets.get(level);
187         if (ruleSet == null) {
188             String JavaDoc file = (String JavaDoc)ruleFiles.get(level);
189             if (file == null) {
190                 throw new ReasonerException("Illegal RDFS conformance level: " + level);
191             }
192             ruleSet = loadRules( file );
193             ruleSets.put(level, ruleSet);
194         }
195         return ruleSet;
196     }
197
198     /**
199      * Return the Jena Graph Capabilties that the inference graphs generated
200      * by this reasoner are expected to conform to.
201      */

202     public Capabilities getGraphCapabilities() {
203         if (capabilities == null) {
204             capabilities = new BaseInfGraph.InfFindSafeCapabilities();
205         }
206         return capabilities;
207     }
208         
209 }
210
211 /*
212     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
213     All rights reserved.
214
215     Redistribution and use in source and binary forms, with or without
216     modification, are permitted provided that the following conditions
217     are met:
218
219     1. Redistributions of source code must retain the above copyright
220        notice, this list of conditions and the following disclaimer.
221
222     2. Redistributions in binary form must reproduce the above copyright
223        notice, this list of conditions and the following disclaimer in the
224        documentation and/or other materials provided with the distribution.
225
226     3. The name of the author may not be used to endorse or promote products
227        derived from this software without specific prior written permission.
228
229     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
230     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
231     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
232     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
233     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
234     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
238     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
239 */
Popular Tags