KickJava   Java API By Example, From Geeks To Geeks.

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


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: MultiUnion.java,v $
10  * Revision $Revision: 1.21 $
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.SimpleEventManager;
29 import com.hp.hpl.jena.shared.JenaException;
30 import com.hp.hpl.jena.util.CollectionFactory;
31 import com.hp.hpl.jena.util.iterator.*;
32
33 import java.util.*;
34
35
36 /**
37  * <p>
38  * A graph implementation that presents the union of zero or more subgraphs,
39  * one of which is distinguished as the updateable graph.
40  * </p>
41  *
42  * @author Ian Dickinson, HP Labs
43  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
44  * @version CVS $Id: MultiUnion.java,v 1.21 2005/02/21 11:52:04 andy_seaborne Exp $
45  */

46 public class MultiUnion
47     extends Polyadic
48 {
49     // Constants
50
//////////////////////////////////
51

52
53     // Static variables
54
//////////////////////////////////
55

56
57     // Instance variables
58
//////////////////////////////////
59

60
61     // Constructors
62
//////////////////////////////////
63

64     /**
65      * <p>
66      * Construct a union of exactly no sub graphs.
67      * </p>
68      */

69     public MultiUnion() {
70         super();
71     }
72
73     
74     /**
75      * <p>
76      * Construct a union of all of the given graphs
77      * </p>
78      *
79      * @param graphs An array of the sub-graphs of this union
80      */

81     public MultiUnion( Graph[] graphs) {
82         super( graphs );
83     }
84
85     
86     /**
87      * <p>
88      * Construct a union of all of the given graphs.
89      * </p>
90      *
91      * @param graphs An iterator of the sub-graphs of this union. If graphs is
92      * a closable iterator, it will be automatically closed.
93      */

94     public MultiUnion( Iterator graphs ) {
95         super( graphs );
96     }
97     
98
99     // External signature methods
100
//////////////////////////////////
101

102     /**
103         Unions share the reifiers of their base graphs.
104     */

105     public Reifier getReifier()
106         { Graph base = getBaseGraph();
107         return base == null ? super.getReifier() : base.getReifier(); }
108     
109     /**
110      * <p>
111      * Add the given triple to the union model; the actual component model to
112      * be updated will be the designated (or default) {@linkplain #getBaseGraph updateable} graph.
113      * </p>
114      *
115      * @param t A triple to add to the union graph
116      * @exception JenaException if the union does not contain any sub-graphs yet
117      */

118     public void performAdd( Triple t ) {
119         try {
120             getBaseGraph().add( t );
121         }
122         catch (NullPointerException JavaDoc e) {
123             throw new JenaException( "Tried to add to a union graph that has no component graphs." );
124         }
125     }
126
127     /**
128      * <p>
129      * Delete the given triple from the union model; the actual component model to
130      * be updated will be the designated (or default) {@linkplain #getBaseGraph updateable} graph.
131      * </p>
132      *
133      * @param t A triple to from the union graph
134      * @exception JenaException if the union does not contain any sub-graphs yet
135      */

136     public void performDelete( Triple t ) {
137         try {
138             getBaseGraph().delete( t );
139         }
140         catch (NullPointerException JavaDoc e) {
141             throw new JenaException( "Tried to delete from a union graph that has no component graphs." );
142         }
143     }
144
145
146     /**
147      * <p>
148      * Answer true if at least one of the graphs in this union contain the given triple.
149      * </p>
150      *
151      * @param t A triple
152      * @return True if any of the graphs in the union contain t
153      */

154     public boolean graphBaseContains( Triple t ) {
155         for (Iterator i = m_subGraphs.iterator(); i.hasNext(); ) {
156             if (((Graph) i.next()).contains( t )) {
157                 return true;
158             }
159         }
160         
161         return false;
162     }
163     
164
165     /**
166      * <p>
167      * Answer an iterator over the triples in the union of the graphs in this composition. <b>Note</b>
168      * that the requirement to remove duplicates from the union means that this will be an
169      * expensive operation for large (and especially for persistent) graphs.
170      * </p>
171      *
172      * @param t The matcher to match against
173      * @return An iterator of all triples matching t in the union of the graphs.
174      */

175     public ExtendedIterator graphBaseFind( final TripleMatch t ) {
176         Set seen = CollectionFactory.createHashedSet();
177         ExtendedIterator i = NullIterator.instance;
178         
179         // now add the rest of the chain
180
for (Iterator graphs = m_subGraphs.iterator(); graphs.hasNext(); ) {
181             ExtendedIterator newTriples = recording( rejecting( ((Graph) graphs.next()).find( t ), seen ), seen );
182             i = i.andThen( newTriples );
183         }
184         
185         // ensure that .remove notifies this graph's event manager
186
return SimpleEventManager.notifyingRemove( MultiUnion.this, i );
187     }
188
189
190     /**
191      * <p>
192      * Add the given graph to this union. If it is already a member of the union, don't
193      * add it a second time.
194      * </p>
195      *
196      * @param graph A sub-graph to add to this union
197      */

198     public void addGraph( Graph graph ) {
199         if (!m_subGraphs.contains( graph )) {
200             m_subGraphs.add( graph );
201         }
202     }
203
204 }
205
206
207 /*
208     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
209     All rights reserved.
210
211     Redistribution and use in source and binary forms, with or without
212     modification, are permitted provided that the following conditions
213     are met:
214
215     1. Redistributions of source code must retain the above copyright
216        notice, this list of conditions and the following disclaimer.
217
218     2. Redistributions in binary form must reproduce the above copyright
219        notice, this list of conditions and the following disclaimer in the
220        documentation and/or other materials provided with the distribution.
221
222     3. The name of the author may not be used to endorse or promote products
223        derived from this software without specific prior written permission.
224
225     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
226     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
227     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
228     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
229     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
230     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
233     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
234     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
235 */

236
237
Popular Tags