001: /******************************************************************
002: * File: TransitiveInfGraph.java
003: * Created by: Dave Reynolds
004: * Created on: 02-Feb-03
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: TransitiveInfGraph.java,v 1.25 2008/01/02 12:07:50 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.transitiveReasoner;
010:
011: import com.hp.hpl.jena.reasoner.BaseInfGraph;
012: import com.hp.hpl.jena.reasoner.*;
013: import com.hp.hpl.jena.graph.*;
014: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
015: import com.hp.hpl.jena.util.iterator.UniqueExtendedIterator;
016:
017: /**
018: * Implementation of InfGraph used by the TransitiveReasoner.
019: * This is returned by the TransitiveReasoner when a data graph
020: * (together with an optional schema) has been bound.
021: *
022: * <p>The cached property and class graphs are calculated by the
023: * reasoner when the schema is bound. If the data graph does not
024: * include schema information then the caches generated at
025: * schema binding stage are reused here. Otherwise the caches
026: * are regenerated.</p>
027: *
028: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
029: * @version $Revision: 1.25 $ on $Date: 2008/01/02 12:07:50 $
030: */
031: public class TransitiveInfGraph extends BaseInfGraph {
032:
033: /** The paire of subclass and subproperty lattices */
034: protected TransitiveEngine transitiveEngine;
035:
036: /** The graph registered as the schema, if any */
037: protected Finder tbox = null;
038:
039: /** The combined data and schema finder */
040: protected Finder dataFind;
041:
042: /**
043: * Constructor. Called by the TransitiveReasoner when it
044: * is bound to a data graph.
045: * @param reasoner the parent instance of the transitive reasoner,
046: * including any precomputed class and property caches
047: * @param data the data graph being bound in.
048: */
049: public TransitiveInfGraph(Graph data, TransitiveReasoner reasoner) {
050: super (data, reasoner);
051: }
052:
053: /**
054: * Perform any initial processing and caching. This call is optional. Most
055: * engines either have negligable set up work or will perform an implicit
056: * "prepare" if necessary. The call is provided for those occasions where
057: * substantial preparation work is possible (e.g. running a forward chaining
058: * rule system) and where an application might wish greater control over when
059: * this prepration is done.
060: */
061: public void prepare() {
062: tbox = ((TransitiveReasoner) reasoner).getTbox();
063: // Initially just point to the reasoner's precached information
064: transitiveEngine = new TransitiveEngine(
065: ((TransitiveReasoner) reasoner).getSubClassCache()
066: .deepCopy(), ((TransitiveReasoner) reasoner)
067: .getSubPropertyCache().deepCopy());
068: // The deepCopies reduce the value of precomputing the closure in the reasoner object
069: // but enables people to bind the same reasoner to multiple datasets.
070: // Perhaps need a faster deepcopy
071:
072: // But need to check if the data graph defines schema data as well
073: dataFind = transitiveEngine.insert(tbox, fdata);
074: transitiveEngine.setCaching(true, true);
075:
076: isPrepared = true;
077: }
078:
079: /**
080: * Return the schema graph, if any, bound into this inference graph.
081: */
082: public Graph getSchemaGraph() {
083: if (tbox == null)
084: return null;
085: if (tbox instanceof FGraph) {
086: return ((FGraph) tbox).getGraph();
087: } else {
088: throw new ReasonerException(
089: "Transitive reasoner got into an illegal state");
090: }
091: }
092:
093: /**
094: * Extended find interface used in situations where the implementator
095: * may or may not be able to answer the complete query. It will
096: * attempt to answer the pattern but if its answers are not known
097: * to be complete then it will also pass the request on to the nested
098: * Finder to append more results.
099: * @param pattern a TriplePattern to be matched against the data
100: * @param continuation either a Finder or a normal Graph which
101: * will be asked for additional match results if the implementor
102: * may not have completely satisfied the query.
103: */
104: public ExtendedIterator findWithContinuation(TriplePattern pattern,
105: Finder continuation) {
106: checkOpen();
107: if (!isPrepared)
108: prepare();
109: Finder cascade = transitiveEngine.getFinder(pattern, FinderUtil
110: .cascade(tbox, continuation));
111: return new UniqueExtendedIterator(cascade.find(pattern));
112: }
113:
114: /**
115: * Returns an iterator over Triples.
116: */
117: public ExtendedIterator graphBaseFind(Node subject, Node property,
118: Node object) {
119: return findWithContinuation(new TriplePattern(subject,
120: property, object), fdata);
121: }
122:
123: /**
124: * Basic pattern lookup interface.
125: * @param pattern a TriplePattern to be matched against the data
126: * @return a ExtendedIterator over all Triples in the data set
127: * that match the pattern
128: */
129: public ExtendedIterator find(TriplePattern pattern) {
130: return findWithContinuation(pattern, fdata);
131: }
132:
133: /**
134: * Add one triple to the data graph, run any rules triggered by
135: * the new data item, recursively adding any generated triples.
136: */
137: public synchronized void performAdd(Triple t) {
138: if (!isPrepared)
139: prepare();
140: fdata.getGraph().add(t);
141: transitiveEngine.add(t);
142: }
143:
144: /**
145: * Removes the triple t (if possible) from the set belonging to this graph.
146: */
147: public void performDelete(Triple t) {
148: fdata.getGraph().delete(t);
149: if (isPrepared) {
150: transitiveEngine.delete(t);
151: }
152: }
153:
154: /**
155: Answer the InfCapabilities of this InfGraph.
156: */
157: public Capabilities getCapabilities() {
158: if (capabilities == null)
159: capabilities = new InfFindSafeCapabilities();
160: return capabilities;
161: }
162:
163: }
164:
165: /*
166: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
167: * All rights reserved.
168: *
169: * Redistribution and use in source and binary forms, with or without
170: * modification, are permitted provided that the following conditions
171: * are met:
172: * 1. Redistributions of source code must retain the above copyright
173: * notice, this list of conditions and the following disclaimer.
174: * 2. Redistributions in binary form must reproduce the above copyright
175: * notice, this list of conditions and the following disclaimer in the
176: * documentation and/or other materials provided with the distribution.
177: * 3. The name of the author may not be used to endorse or promote products
178: * derived from this software without specific prior written permission.
179: *
180: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
181: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
183: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
184: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
185: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
186: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
187: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
188: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
189: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
190: */
|