KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > impl > SimpleReifierFragmentsMap


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: SimpleReifierFragmentsMap.java,v 1.17 2005/02/21 11:52:11 andy_seaborne Exp $
5 */

6 package com.hp.hpl.jena.graph.impl;
7
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11
12 import com.hp.hpl.jena.graph.*;
13 import com.hp.hpl.jena.util.CollectionFactory;
14 import com.hp.hpl.jena.util.iterator.*;
15 import com.hp.hpl.jena.vocabulary.RDF;
16
17 /**
18     SimpleReifierFragmentsMap - a map from nodes to the incompleteb(or
19     overcomplete) reification quadlets.
20     
21     @author kers
22 */

23 public class SimpleReifierFragmentsMap implements ReifierFragmentsMap
24     {
25     
26     protected Map JavaDoc forwardMap = CollectionFactory.createHashedMap();
27     
28     protected Fragments getFragments( Node tag )
29         { return (Fragments) forwardMap.get( tag ); }
30     
31     protected void removeFragments( Node key )
32         { forwardMap.remove( key ); }
33     
34     /**
35     update the map with (node -> fragment); return the fragment.
36     */

37     protected Fragments putFragments( Node key, Fragments value )
38         {
39         forwardMap.put( key, value );
40         return value;
41         }
42     
43     protected ExtendedIterator allTriples( TripleMatch tm )
44         {
45         Triple t = tm.asTriple();
46         Node subject = t.getSubject();
47         if (subject.isConcrete())
48             {
49             Fragments x = (Fragments) forwardMap.get( subject );
50             return x == null
51                 ? NullIterator.instance
52                 : explodeFragments( t, subject, x )
53                 ;
54             }
55         else
56             {
57             final Iterator JavaDoc it = forwardMap.entrySet().iterator();
58             return new FragmentTripleIterator( t, it )
59                 {
60                 public void fill( GraphAdd ga, Node n, Object JavaDoc fragmentsObject )
61                     {
62                     ((Fragments) fragmentsObject).includeInto( ga );
63                     }
64                 };
65             }
66         }
67     
68     /**
69      * @param t
70      * @param subject
71      * @param x
72      * @return
73      */

74     protected ExtendedIterator explodeFragments( Triple t, Node subject, Fragments x )
75         {
76         GraphAddList L = new GraphAddList( t );
77         x.includeInto( L );
78         return WrappedIterator.create( L.iterator() );
79         }
80
81     public ExtendedIterator find( TripleMatch m )
82         { return allTriples( m ); }
83     
84     public int size()
85         {
86         int result = 0;
87         Iterator JavaDoc it = forwardMap.entrySet().iterator();
88         while (it.hasNext())
89             {
90             Map.Entry JavaDoc e = (Map.Entry JavaDoc) it.next();
91             Fragments f = (Fragments) e.getValue();
92             result += f.size();
93             }
94         return result;
95         }
96     
97     /**
98         given a triple t, see if it's a reification triple and if so return the internal selector;
99         otherwise return null.
100     */

101     public ReifierFragmentHandler getFragmentHandler( Triple t )
102         {
103         Node p = t.getPredicate();
104         ReifierFragmentHandler x = (ReifierFragmentHandler) selectors.get( p );
105         if (x == null || (p.equals( RDF.Nodes.type ) && !t.getObject().equals( RDF.Nodes.Statement ) ) ) return null;
106         return x;
107         }
108
109     protected void putAugmentedTriple( SimpleReifierFragmentHandler s, Node tag, Node object, Triple reified )
110         {
111         Fragments partial = new Fragments( tag, reified );
112         partial.add( s, object );
113         putFragments( tag, partial );
114         }
115     
116     protected Triple reifyCompleteQuad( SimpleReifierFragmentHandler s, Triple fragment, Node tag, Node object )
117         {
118         Fragments partial = getFragments( tag );
119         if (partial == null) putFragments( tag, partial = new Fragments( tag ) );
120         partial.add( s, object );
121         if (partial.isComplete())
122             {
123             removeFragments( fragment.getSubject() );
124             return partial.asTriple();
125             }
126         else
127             return null;
128         }
129
130     protected Triple removeFragment( SimpleReifierFragmentHandler s, Node tag, Triple already, Triple fragment )
131         {
132         Fragments partial = getFragments( tag );
133         Fragments fs = (already != null ? explode( tag, already )
134             : partial == null ? putFragments( tag, new Fragments( tag ) )
135             : (Fragments) partial);
136         fs.remove( s, fragment.getObject() );
137         if (fs.isComplete())
138             {
139             Triple result = fs.asTriple();
140             removeFragments( tag );
141             return result;
142             }
143         else
144             {
145             if (fs.isEmpty()) removeFragments( tag );
146             return null;
147             }
148         }
149     
150     protected Fragments explode( Node s, Triple t )
151         { return putFragments( s, new Fragments( s, t ) ); }
152
153     public boolean hasFragments( Node tag )
154         { return getFragments( tag ) != null; }
155
156     protected static class Fragments
157         {
158         
159         /**
160             a Fragments object is represented by four sets, one for each of the reification
161             predicates. The slots are array elements because, sadly, it's easier to dynamically
162             choose a slot by number than any other way I could think of.
163         */

164         private final Set JavaDoc [] slots =
165             {CollectionFactory.createHashedSet(), CollectionFactory.createHashedSet(), CollectionFactory.createHashedSet(), CollectionFactory.createHashedSet()};
166         
167         /**
168             the Node the fragments are about.
169         */

170         private Node anchor;
171         
172         /**
173             a fresh Fragments object remembers the node n and starts
174             off with all sets empty. (In use, at least one of the slots will
175             then immediately be updated - otherwise there was no reason
176             to create the Fragments in the first place ...)
177         */

178         public Fragments( Node n )
179             { this.anchor = n; }
180             
181         public Fragments( Node n, Triple t )
182             {
183             this( n );
184             addTriple( t );
185             }
186             
187         public int size()
188             { return slots[0].size() + slots[1].size() + slots[2].size() + slots[3].size(); }
189         
190         /**
191             true iff this is a complete fragment; every component is present with exactly
192             one value, so n unambiguously reifies (subject, predicate, object).
193         */

194         public boolean isComplete()
195             { return slots[0].size() == 1 && slots[1].size() == 1 && slots[2].size() == 1 && slots[3].size() == 1; }
196             
197         /**
198             true iff this is an empty fragment; no reificational assertions have been made
199             about n. (Hence, in use, the Fragments object can be discarded.)
200         */

201         public boolean isEmpty()
202             { return slots[0].isEmpty() && slots[1].isEmpty() && slots[2].isEmpty() && slots[3].isEmpty(); }
203             
204         /**
205             remove the node n from the set specified by slot which.
206         */

207         public void remove( SimpleReifierFragmentHandler w, Node n )
208             { slots[w.which].remove( n ); }
209             
210         /**
211             add the node n to the slot identified by which).
212        */

213         public void add( SimpleReifierFragmentHandler w, Node n )
214             { slots[w.which].add( n ); }
215             
216         /**
217             include into g all of the reification components that this Fragments
218             represents.
219         */

220         public void includeInto( GraphAdd g )
221             {
222             includeInto( g, RDF.Nodes.subject, SUBJECTS_index );
223             includeInto( g, RDF.Nodes.predicate, PREDICATES_index );
224             includeInto( g, RDF.Nodes.object, OBJECTS_index );
225             includeInto( g, RDF.Nodes.type, TYPES_index );
226             }
227             
228         /**
229             include into g all of the (n, p[which], o) triples for which
230             o is an element of the slot <code>which</code> corresponding to
231             predicate.
232         */

233         private void includeInto( GraphAdd g, Node predicate, int which )
234             {
235             Iterator JavaDoc it = slots[which].iterator();
236             while (it.hasNext())
237                 g.add( Triple.create( anchor, predicate, (Node) it.next() ) );
238             }
239             
240         /**
241             add to this Fragments the entire reification quad needed to
242             reify the triple t.
243             @param t: Triple the (S, P, O) triple to reify
244             @return this with the quad for (S, P, O) added
245         */

246         public Fragments addTriple( Triple t )
247             {
248             slots[SUBJECTS_index].add( t.getSubject() );
249             slots[PREDICATES_index].add( t.getPredicate() );
250             slots[OBJECTS_index].add( t.getObject() );
251             slots[TYPES_index].add( RDF.Nodes.Statement );
252             return this;
253             }
254             
255         /**
256             precondition: isComplete()
257         <p>
258             return the single Triple that this Fragments represents; only legal if
259             isComplete() is true.
260         */

261         Triple asTriple()
262             { return Triple.create( only( slots[SUBJECTS_index] ), only( slots[PREDICATES_index] ), only( slots[OBJECTS_index] ) ); }
263                    
264         /**
265             precondition: s.size() == 1
266         <p>
267             utiltity method to return the only element of a singleton set.
268         */

269         private Node only( Set JavaDoc s )
270             { return (Node) s.iterator().next(); }
271             
272         /**
273             return a readable representation of this Fragment for debugging purposes.
274         */

275         public String JavaDoc toString()
276             { return anchor + " s:" + slots[SUBJECTS_index] + " p:" + slots[PREDICATES_index] + " o:" + slots[OBJECTS_index] + " t:" + slots[TYPES_index]; }
277
278         }
279     
280     /*
281         the magic numbers for the slots. The order doesn't matter, but that they're
282         some permutation of {0, 1, 2, 3} does.
283     */

284     protected static final int TYPES_index = 0;
285     protected static final int SUBJECTS_index = 1;
286     protected static final int PREDICATES_index = 2;
287     protected static final int OBJECTS_index = 3;
288     
289     protected final ReifierFragmentHandler TYPES = new SimpleReifierFragmentHandler( this, TYPES_index) { public boolean clashesWith( ReifierFragmentsMap map, Node n, Triple reified ) { return false; } };
290     protected final ReifierFragmentHandler SUBJECTS = new SimpleReifierFragmentHandler( this, SUBJECTS_index) { public boolean clashesWith( ReifierFragmentsMap map, Node n, Triple reified ) { return !n.equals( reified.getSubject() ); } };
291     protected final ReifierFragmentHandler PREDICATES = new SimpleReifierFragmentHandler( this, PREDICATES_index) { public boolean clashesWith( ReifierFragmentsMap map, Node n, Triple reified ) { return !n.equals( reified.getPredicate() ); } };
292     protected final ReifierFragmentHandler OBJECTS = new SimpleReifierFragmentHandler( this, OBJECTS_index) { public boolean clashesWith( ReifierFragmentsMap map, Node n, Triple reified ) { return !n.equals( reified.getObject() ); } };
293
294     public final Map JavaDoc selectors = makeSelectors();
295           
296     /**
297         make the selector mapping.
298     */

299     protected Map JavaDoc makeSelectors()
300         {
301         Map JavaDoc result = CollectionFactory.createHashedMap();
302         result.put( RDF.Nodes.subject, SUBJECTS );
303         result.put( RDF.Nodes.predicate, PREDICATES );
304         result.put( RDF.Nodes.object, OBJECTS );
305         result.put( RDF.Nodes.type, TYPES );
306         return result;
307         }
308     }
309
310 /*
311     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
312     All rights reserved.
313     
314     Redistribution and use in source and binary forms, with or without
315     modification, are permitted provided that the following conditions
316     are met:
317     
318     1. Redistributions of source code must retain the above copyright
319        notice, this list of conditions and the following disclaimer.
320     
321     2. Redistributions in binary form must reproduce the above copyright
322        notice, this list of conditions and the following disclaimer in the
323        documentation and/or other materials provided with the distribution.
324     
325     3. The name of the author may not be used to endorse or promote products
326        derived from this software without specific prior written permission.
327     
328     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
329     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
330     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
331     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
332     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
333     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
334     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
335     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
336     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
337     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
338 */
Popular Tags