KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > impl > InfModelImpl


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

10 package com.hp.hpl.jena.rdf.model.impl;
11
12 import com.hp.hpl.jena.graph.Graph;
13 import com.hp.hpl.jena.rdf.model.*;
14 import com.hp.hpl.jena.reasoner.*;
15 import java.util.Iterator JavaDoc;
16
17 /**
18  * Default implementation of the InfModel interface which simply wraps up
19  * an InfGraph.
20
21  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
22  * @version $Revision: 1.7 $ on $Date: 2005/02/21 12:14:32 $
23  */

24 public class InfModelImpl extends ModelCom implements InfModel {
25
26     /**
27      * Constructor.
28      * @param infgraph the InfGraph, as generated by a reasoner.bind operation, to be packaged up.
29      */

30     public InfModelImpl(InfGraph infgraph) {
31         super(infgraph);
32     }
33
34     /**
35      * Return the underlying inference graph for this model.
36      */

37     public InfGraph getInfGraph() {
38         return (InfGraph)getGraph();
39     }
40     
41     /**
42      * Return the raw RDF model being processed (i.e. the argument
43      * to the Reasonder.bind call that created this InfModel).
44      */

45     public Model getRawModel() {
46         return new ModelCom(getInfGraph().getRawGraph());
47     }
48     
49     /**
50      * Return the Reasoner which is being used to answer queries to this graph.
51      */

52     public Reasoner getReasoner() {
53         return getInfGraph().getReasoner();
54     }
55
56     /**
57      * Cause the inference model to reconsult the underlying data to take
58      * into account changes. Normally changes are made through the InfModel's add and
59      * remove calls are will be handled appropriately. However, in some cases changes
60      * are made "behind the InfModels's back" and this forces a full reconsult of
61      * the changed data.
62      */

63     public void rebind() {
64         getInfGraph().rebind();
65     }
66     
67     /**
68      * Perform any initial processing and caching. This call is optional. Most
69      * engines either have negligable set up work or will perform an implicit
70      * "prepare" if necessary. The call is provided for those occasions where
71      * substantial preparation work is possible (e.g. running a forward chaining
72      * rule system) and where an application might wish greater control over when
73      * this prepration is done rather than just leaving to be done at first query time.
74      */

75     public void prepare() {
76         getInfGraph().prepare();
77     }
78     
79     /**
80      * Reset any internal caches. Some systems, such as the tabled backchainer,
81      * retain information after each query. A reset will wipe this information preventing
82      * unbounded memory use at the expense of more expensive future queries. A reset
83      * does not cause the raw data to be reconsulted and so is less expensive than a rebind.
84      */

85     public void reset() {
86         getInfGraph().reset();
87     }
88     
89     /**
90      * Test the consistency of the underlying data. This normally tests
91      * the validity of the bound instance data against the bound
92      * schema data.
93      * @return a ValidityReport structure
94      */

95     public ValidityReport validate() {
96         return getInfGraph().validate();
97     }
98     
99     /** Find all the statements matching a pattern.
100      * <p>Return an iterator over all the statements in a model
101      * that match a pattern. The statements selected are those
102      * whose subject matches the <code>subject</code> argument,
103      * whose predicate matches the <code>predicate</code> argument
104      * and whose object matchesthe <code>object</code> argument.
105      * If an argument is <code>null</code> it matches anything.</p>
106      * <p>
107      * The s/p/o terms may refer to resources which are temporarily defined in the "posit" model.
108      * This allows one, for example, to query what resources are of type CE where CE is a
109      * class expression rather than a named class - put CE in the posit arg.</p>
110      *
111      * @return an iterator over the subjects
112      * @param subject The subject sought
113      * @param predicate The predicate sought
114      * @param object The value sought
115      */

116     public StmtIterator listStatements( Resource subject, Property predicate, RDFNode object, Model posit ) {
117         Iterator JavaDoc iter = getInfGraph().find(subject.asNode(), predicate.asNode(), object.asNode(), posit.getGraph());
118         return IteratorFactory.asStmtIterator(iter,this);
119     }
120     
121     /**
122      * Switch on/off drivation logging. If this is switched on then every time an inference
123      * is a made that fact is recorded and the resulting record can be access through a later
124      * getDerivation call. This may consume a lot of space!
125      */

126     public void setDerivationLogging(boolean logOn) {
127         getInfGraph().setDerivationLogging(logOn);
128     }
129    
130     /**
131      * Return the derivation of the given statement (which should be the result of
132      * some previous list operation).
133      * Not all reasoneers will support derivations.
134      * @return an iterator over Derivation records or null if there is no derivation information
135      * available for this triple.
136      */

137     public Iterator JavaDoc getDerivation(Statement statement) {
138         return getInfGraph().getDerivation(statement.asTriple());
139     }
140     
141     /**
142      * Returns a derivations model. The rule reasoners typically create a
143      * graph containing those triples added to the base graph due to rule firings.
144      * In some applications it can useful to be able to access those deductions
145      * directly, without seeing the raw data which triggered them. In particular,
146      * this allows the forward rules to be used as if they were rewrite transformation
147      * rules.
148      * @return the deductions model, if relevant for this class of inference
149      * engine or null if not.
150      */

151     public Model getDeductionsModel() {
152         if (deductionsModel == null) {
153             Graph deductionsGraph = getInfGraph().getDeductionsGraph();
154             if (deductionsGraph != null) {
155                 deductionsModel = new ModelCom(deductionsGraph);
156             }
157         }
158         return deductionsModel;
159     }
160     
161     /** Cached deductions model */
162     private Model deductionsModel = null;
163 }
164
165
166 /*
167     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
168     All rights reserved.
169
170     Redistribution and use in source and binary forms, with or without
171     modification, are permitted provided that the following conditions
172     are met:
173
174     1. Redistributions of source code must retain the above copyright
175        notice, this list of conditions and the following disclaimer.
176
177     2. Redistributions in binary form must reproduce the above copyright
178        notice, this list of conditions and the following disclaimer in the
179        documentation and/or other materials provided with the distribution.
180
181     3. The name of the author may not be used to endorse or promote products
182        derived from this software without specific prior written permission.
183
184     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
185     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
186     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
188     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
189     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
191     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
192     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
193     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
194 */
Popular Tags