KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > compose > Polyadic


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 4 Mar 2003
9  * Filename $RCSfile: Polyadic.java,v $
10  * Revision $Revision: 1.14 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/02/21 11:52:04 $
14  * by $Author: andy_seaborne $
15  *
16  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * (see footer for full conditions)
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.graph.compose;
23
24
25 // Imports
26
///////////////
27
import com.hp.hpl.jena.graph.*;
28 import com.hp.hpl.jena.graph.impl.WrappedBulkUpdateHandler;
29 import com.hp.hpl.jena.shared.*;
30 import com.hp.hpl.jena.util.iterator.*;
31
32 import java.util.*;
33
34
35 /**
36  * <p>
37  * A base class for composition graphs that are composed from zero or more
38  * sub-graphs (thus providing a basis for polyadic composition operators).
39  * A distinguished graph is the designated graph for additions to the union.
40  * By default, this is the first sub-graph of the composition, however any
41  * of the graphs in the composition can be nominated to be the distinguished
42  * graph.
43  * </p>
44  *
45  * @author Ian Dickinson, HP Labs
46  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
47  * @version CVS $Id: Polyadic.java,v 1.14 2005/02/21 11:52:04 andy_seaborne Exp $
48  */

49 public abstract class Polyadic
50     extends CompositionBase
51 {
52     // Constants
53
//////////////////////////////////
54

55
56     // Static variables
57
//////////////////////////////////
58

59
60     // Instance variables
61
//////////////////////////////////
62

63     /** A list of the sub-graphs that this composition contains */
64     protected List m_subGraphs = new ArrayList();
65     
66     /** The distinguished graph for adding to. If null, use the 0'th graph in the list. */
67     protected Graph m_baseGraph = null;
68     
69     
70     // Constructors
71
//////////////////////////////////
72

73     /**
74      * <p>
75      * Construct a composition of exactly no sub graphs.
76      * </p>
77      */

78     public Polyadic() {
79     }
80
81     
82     /**
83      * <p>
84      * Construct a composition of all of the given graphs
85      * </p>
86      *
87      * @param graphs An array of the sub-graphs of this composition
88      */

89     public Polyadic( Graph[] graphs) {
90         for (int i = 0; i < graphs.length; i++) {
91             m_subGraphs.add( graphs[i] );
92         }
93     }
94     
95     private PrefixMapping pm;
96     
97     public PrefixMapping getPrefixMapping()
98         {
99         if (pm == null) pm = new PolyadicPrefixMappingImpl( this );
100         return pm;
101         }
102     
103     /**
104      * <p>
105      * Construct a composition of all of the given graphs.
106      * </p>
107      *
108      * @param graphs An iterator of the sub-graphs of this composition. If graphs is
109      * a closable iterator, it will be automatically closed.
110      */

111     public Polyadic( Iterator graphs ) {
112         while (graphs.hasNext()) {
113             m_subGraphs.add( graphs.next() );
114         }
115         
116         if (graphs instanceof ClosableIterator) {
117             ((ClosableIterator) graphs).close();
118         }
119     }
120     
121     
122     // External signature methods
123
//////////////////////////////////
124

125     /**
126      * <p>
127      * Close the graph by closing all of the sub-graphs.
128      * </p>
129      *
130      * @see com.hp.hpl.jena.graph.Graph#close()
131      */

132     public void close() {
133         for (Iterator i = m_subGraphs.iterator(); i.hasNext(); ) {
134             ((Graph) i.next()).close();
135         }
136     }
137  
138  
139     /**
140      * <p>
141      * Answer true if this graph contains the given graph as a sub-component.
142      * </p>
143      *
144      * @param graph A graph to test
145      * @return True if the graph is this graph, or is a sub-graph of this one.
146      * @see com.hp.hpl.jena.graph.Graph#dependsOn(Graph)
147      */

148     public boolean dependsOn( Graph graph ) {
149         return (graph == this) || m_subGraphs.contains( graph );
150     }
151     
152     
153     /**
154      * <p>
155      * Add the given graph to this composition.
156      * </p>
157      *
158      * @param graph A sub-graph to add to this composition
159      */

160     public void addGraph( Graph graph ) {
161         m_subGraphs.add( graph );
162     }
163     
164
165     /**
166      * <p>
167      * Remove the given graph from this composition. If the removed graph is the
168      * designated updateable graph, the updatable graph goes back to the default
169      * for this composition.
170      * </p>
171      *
172      * @param graph A sub-graph to remove from this composition
173      */

174     public void removeGraph( Graph graph ) {
175         m_subGraphs.remove( graph );
176         
177         if (m_baseGraph == graph) {
178             m_baseGraph = null;
179         }
180     }
181     
182
183     /**
184      * <p>
185      * Answer the distinguished graph for the composition, which will be the graph
186      * that receives triple adds and deletes.
187      * </p>
188      *
189      * @return The distinguished updateable graph, or null if there are no graphs
190      * in this composition
191      */

192     public Graph getBaseGraph() {
193         if (m_baseGraph == null) {
194             // no designated graph, so default to the first graph on the list
195
return (m_subGraphs.size() == 0) ? null : ((Graph) m_subGraphs.get( 0 ));
196         }
197         else {
198             return m_baseGraph;
199         }
200     }
201
202
203     /**
204      * <p>
205      * Set the designated updateable graph for this composition.
206      * </p>
207      *
208      * @param graph One of the graphs currently in this composition to be the
209      * designated graph to receive udpates
210      * @exception IllegalArgumentException if graph is not one of the members of
211      * the composition
212      */

213     public void setBaseGraph( Graph graph ) {
214         if (m_subGraphs.contains( graph )) {
215             m_baseGraph = graph;
216             bulkHandler = null;
217         }
218         else {
219             throw new IllegalArgumentException JavaDoc( "The updateable graph must be one of the graphs from the composition" );
220         }
221     }
222     
223     
224     /**
225      * <p>
226      * Answer a list of the graphs other than the updateable (base) graph
227      * </p>
228      *
229      * @return A list of all of the sub-graphs, excluding the base graph.
230      */

231     public List getSubGraphs() {
232         List sg = new ArrayList( m_subGraphs );
233         
234         if (getBaseGraph() != null) {
235             sg.remove( getBaseGraph() );
236         }
237        
238         return sg;
239     }
240
241     public BulkUpdateHandler getBulkUpdateHandler() {
242         if (getBaseGraph() == null)
243             throw new RuntimeException JavaDoc(); // return super.getBulkUpdateHandler();
244
if (bulkHandler == null)
245             bulkHandler = new WrappedBulkUpdateHandler( this, getBaseGraph().getBulkUpdateHandler() );
246         return bulkHandler;
247     }
248
249     // the following methods all delegate handling capabilities to the base graph
250
// TODO: this needs to be integrated with WrappedGraph, but we don't have time to do so before Jena 2.0 release
251

252     public TransactionHandler getTransactionHandler() {
253         return (getBaseGraph() == null) ? super.getTransactionHandler() : getBaseGraph().getTransactionHandler();
254         }
255
256     public Capabilities getCapabilities() {
257         return (getBaseGraph() == null) ? super.getCapabilities() : getBaseGraph().getCapabilities();
258     }
259
260 }
261
262
263 /*
264     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
265     All rights reserved.
266
267     Redistribution and use in source and binary forms, with or without
268     modification, are permitted provided that the following conditions
269     are met:
270
271     1. Redistributions of source code must retain the above copyright
272        notice, this list of conditions and the following disclaimer.
273
274     2. Redistributions in binary form must reproduce the above copyright
275        notice, this list of conditions and the following disclaimer in the
276        documentation and/or other materials provided with the distribution.
277
278     3. The name of the author may not be used to endorse or promote products
279        derived from this software without specific prior written permission.
280
281     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
282     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
283     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
284     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
285     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
286     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
287     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
288     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
289     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
290     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291 */

292
293
Popular Tags