001: /******************************************************************
002: * File: InfModelImpl.java
003: * Created by: Dave Reynolds
004: * Created on: 08-May-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: InfModelImpl.java,v 1.12 2008/01/02 12:05:00 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.rdf.model.impl;
010:
011: import com.hp.hpl.jena.graph.Graph;
012: import com.hp.hpl.jena.rdf.model.*;
013: import com.hp.hpl.jena.reasoner.*;
014: import java.util.Iterator;
015:
016: /**
017: * Default implementation of the InfModel interface which simply wraps up
018: * an InfGraph.
019:
020: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
021: * @version $Revision: 1.12 $ on $Date: 2008/01/02 12:05:00 $
022: */
023: public class InfModelImpl extends ModelCom implements InfModel {
024:
025: /**
026: * Constructor.
027: * @param infgraph the InfGraph, as generated by a reasoner.bind operation, to be packaged up.
028: */
029: public InfModelImpl(InfGraph infgraph) {
030: super (infgraph);
031: }
032:
033: /**
034: * Return the underlying inference graph for this model.
035: */
036: public InfGraph getInfGraph() {
037: return (InfGraph) getGraph();
038: }
039:
040: /**
041: * Return the raw RDF model being processed (i.e. the argument
042: * to the Reasonder.bind call that created this InfModel).
043: */
044: public Model getRawModel() {
045: return new ModelCom(getInfGraph().getRawGraph());
046: }
047:
048: /**
049: * Return the Reasoner which is being used to answer queries to this graph.
050: */
051: public Reasoner getReasoner() {
052: return getInfGraph().getReasoner();
053: }
054:
055: /**
056: * Cause the inference model to reconsult the underlying data to take
057: * into account changes. Normally changes are made through the InfModel's add and
058: * remove calls are will be handled appropriately. However, in some cases changes
059: * are made "behind the InfModels's back" and this forces a full reconsult of
060: * the changed data.
061: */
062: public void rebind() {
063: getInfGraph().rebind();
064: }
065:
066: /**
067: * Perform any initial processing and caching. This call is optional. Most
068: * engines either have negligable set up work or will perform an implicit
069: * "prepare" if necessary. The call is provided for those occasions where
070: * substantial preparation work is possible (e.g. running a forward chaining
071: * rule system) and where an application might wish greater control over when
072: * this prepration is done rather than just leaving to be done at first query time.
073: */
074: public void prepare() {
075: getInfGraph().prepare();
076: }
077:
078: /**
079: * Reset any internal caches. Some systems, such as the tabled backchainer,
080: * retain information after each query. A reset will wipe this information preventing
081: * unbounded memory use at the expense of more expensive future queries. A reset
082: * does not cause the raw data to be reconsulted and so is less expensive than a rebind.
083: */
084: public void reset() {
085: getInfGraph().reset();
086: }
087:
088: /**
089: * Test the consistency of the underlying data. This normally tests
090: * the validity of the bound instance data against the bound
091: * schema data.
092: * @return a ValidityReport structure
093: */
094: public ValidityReport validate() {
095: return getInfGraph().validate();
096: }
097:
098: /** Find all the statements matching a pattern.
099: * <p>Return an iterator over all the statements in a model
100: * that match a pattern. The statements selected are those
101: * whose subject matches the <code>subject</code> argument,
102: * whose predicate matches the <code>predicate</code> argument
103: * and whose object matchesthe <code>object</code> argument.
104: * If an argument is <code>null</code> it matches anything.</p>
105: * <p>
106: * The s/p/o terms may refer to resources which are temporarily defined in the "posit" model.
107: * This allows one, for example, to query what resources are of type CE where CE is a
108: * class expression rather than a named class - put CE in the posit arg.</p>
109: *
110: * @return an iterator over the subjects
111: * @param subject The subject sought
112: * @param predicate The predicate sought
113: * @param object The value sought
114: */
115: public StmtIterator listStatements(Resource subject,
116: Property predicate, RDFNode object, Model posit) {
117: Iterator iter = getInfGraph().find(asNode(subject),
118: asNode(predicate), asNode(object), posit.getGraph());
119: return IteratorFactory.asStmtIterator(iter, this );
120: }
121:
122: /**
123: * Switch on/off drivation logging. If this is switched on then every time an inference
124: * is a made that fact is recorded and the resulting record can be access through a later
125: * getDerivation call. This may consume a lot of space!
126: */
127: public void setDerivationLogging(boolean logOn) {
128: getInfGraph().setDerivationLogging(logOn);
129: }
130:
131: /**
132: * Return the derivation of the given statement (which should be the result of
133: * some previous list operation).
134: * Not all reasoneers will support derivations.
135: * @return an iterator over Derivation records or null if there is no derivation information
136: * available for this triple.
137: */
138: public Iterator getDerivation(Statement statement) {
139: return getInfGraph().getDerivation(statement.asTriple());
140: }
141:
142: /**
143: * Returns a derivations model. The rule reasoners typically create a
144: * graph containing those triples added to the base graph due to rule firings.
145: * In some applications it can useful to be able to access those deductions
146: * directly, without seeing the raw data which triggered them. In particular,
147: * this allows the forward rules to be used as if they were rewrite transformation
148: * rules.
149: * @return the deductions model, if relevant for this class of inference
150: * engine or null if not.
151: */
152: public Model getDeductionsModel() {
153: Graph deductionsGraph = getInfGraph().getDeductionsGraph();
154: if (deductionsGraph != null) {
155: if (deductionsModel == null
156: || (deductionsModel.getGraph() != deductionsGraph)) {
157: deductionsModel = new ModelCom(deductionsGraph);
158: }
159: }
160: return deductionsModel;
161: }
162:
163: /** Cached deductions model */
164: private Model deductionsModel = null;
165: }
166:
167: /*
168: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
169: All rights reserved.
170:
171: Redistribution and use in source and binary forms, with or without
172: modification, are permitted provided that the following conditions
173: are met:
174:
175: 1. Redistributions of source code must retain the above copyright
176: notice, this list of conditions and the following disclaimer.
177:
178: 2. Redistributions in binary form must reproduce the above copyright
179: notice, this list of conditions and the following disclaimer in the
180: documentation and/or other materials provided with the distribution.
181:
182: 3. The name of the author may not be used to endorse or promote products
183: derived from this software without specific prior written permission.
184:
185: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
189: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
192: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
193: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
194: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195: */
|