KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > transitiveReasoner > TransitiveInfGraph


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

10 package com.hp.hpl.jena.reasoner.transitiveReasoner;
11
12 import com.hp.hpl.jena.reasoner.BaseInfGraph;
13 import com.hp.hpl.jena.reasoner.*;
14 import com.hp.hpl.jena.graph.*;
15 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
16 import com.hp.hpl.jena.util.iterator.UniqueExtendedIterator;
17
18 /**
19  * Implementation of InfGraph used by the TransitiveReasoner.
20  * This is returned by the TransitiveReasoner when a data graph
21  * (together with an optional schema) has been bound.
22  *
23  * <p>The cached property and class graphs are calculated by the
24  * reasoner when the schema is bound. If the data graph does not
25  * include schema information then the caches generated at
26  * schema binding stage are reused here. Otherwise the caches
27  * are regenerated.</p>
28  *
29  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
30  * @version $Revision: 1.21 $ on $Date: 2005/02/21 12:18:37 $
31  */

32 public class TransitiveInfGraph extends BaseInfGraph {
33
34     /** The paire of subclass and subproperty lattices */
35     protected TransitiveEngine transitiveEngine;
36     
37     /** The graph registered as the schema, if any */
38     protected Finder tbox = null;
39     
40     /** The combined data and schema finder */
41     protected Finder dataFind;
42     
43     /**
44      * Constructor. Called by the TransitiveReasoner when it
45      * is bound to a data graph.
46      * @param reasoner the parent instance of the transitive reasoner,
47      * including any precomputed class and property caches
48      * @param data the data graph being bound in.
49      */

50     public TransitiveInfGraph(Graph data, TransitiveReasoner reasoner) {
51         super(data, reasoner);
52     }
53     
54     /**
55      * Perform any initial processing and caching. This call is optional. Most
56      * engines either have negligable set up work or will perform an implicit
57      * "prepare" if necessary. The call is provided for those occasions where
58      * substantial preparation work is possible (e.g. running a forward chaining
59      * rule system) and where an application might wish greater control over when
60      * this prepration is done.
61      */

62     public void prepare() {
63         tbox = ((TransitiveReasoner)reasoner).getTbox();
64         // Initially just point to the reasoner's precached information
65
transitiveEngine = new TransitiveEngine(((TransitiveReasoner)reasoner).getSubClassCache(),
66                                                  ((TransitiveReasoner)reasoner).getSubPropertyCache());
67
68         // But need to check if the data graph defines schema data as well
69
dataFind = transitiveEngine.insert(tbox, fdata);
70         transitiveEngine.setCaching(true, true);
71         
72         isPrepared = true;
73     }
74
75     /**
76      * Return the schema graph, if any, bound into this inference graph.
77      */

78     public Graph getSchemaGraph() {
79         if (tbox == null) return null;
80         if (tbox instanceof FGraph) {
81             return ((FGraph)tbox).getGraph();
82         } else {
83             throw new ReasonerException("Transitive reasoner got into an illegal state");
84         }
85     }
86     
87     /**
88      * Extended find interface used in situations where the implementator
89      * may or may not be able to answer the complete query. It will
90      * attempt to answer the pattern but if its answers are not known
91      * to be complete then it will also pass the request on to the nested
92      * Finder to append more results.
93      * @param pattern a TriplePattern to be matched against the data
94      * @param continuation either a Finder or a normal Graph which
95      * will be asked for additional match results if the implementor
96      * may not have completely satisfied the query.
97      */

98     public ExtendedIterator findWithContinuation(TriplePattern pattern, Finder continuation) {
99         checkOpen();
100         if (!isPrepared) prepare();
101         Finder cascade = transitiveEngine.getFinder(pattern, FinderUtil.cascade(tbox, continuation));
102         return new UniqueExtendedIterator(cascade.find(pattern));
103     }
104    
105     /**
106      * Returns an iterator over Triples.
107      */

108     public ExtendedIterator graphBaseFind(Node subject, Node property, Node object) {
109         return findWithContinuation(new TriplePattern(subject, property, object), fdata);
110     }
111
112     /**
113      * Basic pattern lookup interface.
114      * @param pattern a TriplePattern to be matched against the data
115      * @return a ExtendedIterator over all Triples in the data set
116      * that match the pattern
117      */

118     public ExtendedIterator find(TriplePattern pattern) {
119         return findWithContinuation(pattern, fdata);
120     }
121         
122     /**
123      * Add one triple to the data graph, run any rules triggered by
124      * the new data item, recursively adding any generated triples.
125      */

126     public synchronized void performAdd(Triple t) {
127         if (!isPrepared) prepare();
128         fdata.getGraph().add(t);
129         transitiveEngine.add(t);
130     }
131
132     /**
133      * Removes the triple t (if possible) from the set belonging to this graph.
134      */

135     public void performDelete(Triple t) {
136         fdata.getGraph().delete(t);
137         if (isPrepared) {
138             transitiveEngine.delete(t);
139         }
140     }
141     /**
142     Answer the InfCapabilities of this InfGraph.
143  */

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

178
179
Popular Tags