KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rdfsReasoner1 > RDFSReasoner


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

10 package com.hp.hpl.jena.reasoner.rdfsReasoner1;
11
12 import com.hp.hpl.jena.reasoner.*;
13 import com.hp.hpl.jena.reasoner.rulesys.Util;
14 import com.hp.hpl.jena.reasoner.transitiveReasoner.*;
15 import com.hp.hpl.jena.rdf.model.*;
16 import com.hp.hpl.jena.graph.*;
17 import com.hp.hpl.jena.vocabulary.RDFS;
18 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
19
20 /**
21  * @deprecated Obsoleted at jena2p4, replaced by
22  * {@link com.hp.hpl.jena.reasoner.rulesys.RDFSRuleReasoner}.
23  *
24  * An RDFS reasoner suited to modest vocabularies but large instance
25  * data. It does eager processing on the class and property declarations
26  * and caches the results. This means that the initial creation can
27  * be slow. However, if the vocabulary and instance data can be
28  * separated then at least the class lattice results can be reused.
29  * <p>
30  * Instance related rules are implemented using a very simple rewrite
31  * system. Triple queries that match a rule are rewritten and reapplied
32  * as queries. This is reasonably efficient for fairly ground queries,
33  * especially where the predicate is ground. It performs redundant
34  * passes over the data for unground queries, especially any that
35  * need that might match (*, type, Resource) or (*, type, Property)!</p>
36  *
37  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
38  * @version $Revision: 1.20 $ on $Date: 2005/02/21 12:16:50 $
39  */

40 public class RDFSReasoner extends TransitiveReasoner implements Reasoner {
41     /** The domain property */
42     public static Node domainP;
43     
44     /** The range property */
45     public static Node rangeP;
46     
47     /** Note if the reasoner is configured to scan for member properties */
48     protected boolean scanProperties = true;
49     
50     // Static initializer
51
static {
52         domainP = RDFS.domain.getNode();
53         rangeP = RDFS.range.getNode();
54     }
55     
56     /** Constructor */
57     public RDFSReasoner() {
58         super();
59     }
60      
61     /**
62      * Constructor
63      * @param configuration a set of arbitrary configuration information to be
64      * passed the reasoner, encoded as RDF properties of a base configuration resource,
65      * can be null in no custom configuration is required. The
66      * only meaningful configuration property at present is scanProperties.
67      */

68     public RDFSReasoner(Resource configuration) {
69         super();
70         if (configuration != null) {
71             Boolean JavaDoc flag = checkBinaryPredicate(RDFSReasonerFactory.scanProperties, configuration);
72             if (flag != null) scanProperties = flag.booleanValue();
73         }
74     }
75     
76     /**
77      * Private constructor used by bindSchema when
78      * returning a partially bound reasoner instance.
79      */

80     protected RDFSReasoner(Finder tbox,
81                     TransitiveGraphCache subClassCache,
82                     TransitiveGraphCache subPropertyCache,
83                     boolean scanProperties) {
84         super(tbox, subClassCache, subPropertyCache);
85         this.scanProperties = scanProperties;
86     }
87
88     /**
89      * Determine whether the given property is recognized and treated specially
90      * by this reasoner. This is a convenience packaging of a special case of getCapabilities.
91      * @param property the property which we want to ask the reasoner about, given as a Node since
92      * this is part of the SPI rather than API
93      * @return true if the given property is handled specially by the reasoner.
94      */

95     public boolean supportsProperty(Property property) {
96         ReasonerFactory rf = RDFSReasonerFactory.theInstance();
97         Model caps = rf.getCapabilities();
98         Resource root = caps.getResource(rf.getURI());
99         return caps.contains(root, ReasonerVocabulary.supportsP, property);
100     }
101      
102     /**
103      * Helper method - extracts the truth of a boolean configuration
104      * predicate.
105      * @param pred the predicate to be tested
106      * @param configuration the configuration node
107      * @return null if there is no setting otherwise a Boolean giving the setting value
108      */

109     private Boolean JavaDoc checkBinaryPredicate(Property predicate, Resource configuration) {
110         StmtIterator i = configuration.listProperties(predicate);
111         if (i.hasNext()) {
112             return new Boolean JavaDoc(i.nextStatement().getObject().toString().equalsIgnoreCase("true"));
113         } else {
114             return null;
115         }
116     }
117
118     /**
119      * Extracts all of the subClass and subProperty declarations from
120      * the given schema/tbox and caches the resultant graphs.
121      * It can only be used once, can't stack up multiple tboxes this way.
122      * This limitation could be lifted - the only difficulty is the need to
123      * reprocess all the earlier tboxes if a new subPropertyOf subPropertyOf
124      * subClassOf is discovered.
125      * @param tbox schema containing the property and class declarations
126      */

127     public Reasoner bindSchema(Graph tbox) throws ReasonerException {
128         if (this.tbox != null) {
129             throw new ReasonerException("Attempt to bind multiple rulesets - disallowed for now");
130         }
131         FGraph ftbox = new FGraph(tbox);
132         TransitiveGraphCache sCc = new TransitiveGraphCache(directSubClassOf, subClassOf);
133         TransitiveGraphCache sPc = new TransitiveGraphCache(directSubPropertyOf, subPropertyOf);
134         TransitiveEngine.cacheSubPropUtility(ftbox, sPc);
135         TransitiveEngine.cacheSubClassUtility(ftbox, sPc, sCc);
136         sPc.setCaching(true);
137         return new RDFSReasoner(ftbox, sCc, sPc, scanProperties);
138     }
139     
140      
141     /**
142      * Attach the reasoner to a set of RDF ddata to process.
143      * The reasoner may already have been bound to specific rules or ontology
144      * axioms (encoded in RDF) through earlier bindRuleset calls.
145      * @param data the RDF data to be processed, some reasoners may restrict
146      * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
147      * @return an inference graph through which the data+reasoner can be queried.
148      * @throws ReasonerException if the data is ill-formed according to the
149      * constraints imposed by this reasoner.
150      */

151     public InfGraph bind(Graph data) throws ReasonerException {
152         return new RDFSInfGraph(this, data);
153     }
154     
155     /**
156      * Switch on/off drivation logging.
157      * If set to true then the InfGraph created from the bind operation will start
158      * life with recording of derivations switched on. This is currently only of relevance
159      * to rule-based reasoners.
160      * <p>
161      * Default - false.
162      */

163     public void setDerivationLogging(boolean logOn) {
164         // Irrelevant to this reasoner
165
}
166     
167     /**
168      * Set a configuration parameter for the reasoner. The only supported parameter at present is:
169      * are:
170      * <ul>
171      * <li>RDFSReasonerFactory.scanProperties - set this to Boolean true to
172      * enable scanning of all properties looking for container membership properties, default on. </li>
173      * </ul>
174      *
175      * @param parameter the property identifying the parameter to be changed
176      * @param value the new value for the parameter, typically this is a wrapped
177      * java object like Boolean or Integer.
178      */

179     public void setParameter(Property parameter, Object JavaDoc value) {
180         if (parameter.equals(RDFSReasonerFactory.scanProperties)) {
181             scanProperties = Util.convertBooleanPredicateArg(parameter, value);
182         } else {
183             throw new IllegalParameterException(parameter.toString());
184         }
185     }
186     
187 }
188
189 /*
190     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
191     All rights reserved.
192
193     Redistribution and use in source and binary forms, with or without
194     modification, are permitted provided that the following conditions
195     are met:
196
197     1. Redistributions of source code must retain the above copyright
198        notice, this list of conditions and the following disclaimer.
199
200     2. Redistributions in binary form must reproduce the above copyright
201        notice, this list of conditions and the following disclaimer in the
202        documentation and/or other materials provided with the distribution.
203
204     3. The name of the author may not be used to endorse or promote products
205        derived from this software without specific prior written permission.
206
207     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
208     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
209     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
210     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
211     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
213     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
214     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
215     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
216     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
217 */

218
219
Popular Tags