KickJava   Java API By Example, From Geeks To Geeks.

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


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

6 package com.hp.hpl.jena.graph.impl;
7
8 import java.util.HashSet JavaDoc;
9 import java.util.*;
10
11 import com.hp.hpl.jena.graph.*;
12 import com.hp.hpl.jena.util.CollectionFactory;
13 import com.hp.hpl.jena.util.iterator.*;
14
15 /**
16     SimpleReifierTripleMap - a map storing complete node -> triple maps.
17     
18     @author kers
19 */

20 public class SimpleReifierTripleMap implements ReifierTripleMap
21     {
22     protected Map inverseMap = CollectionFactory.createHashedMap();
23     
24     protected Map forwardMap = CollectionFactory.createHashedMap();
25     
26     public Triple getTriple( Node tag )
27         { return (Triple) forwardMap.get( tag ); }
28
29     /**
30          Answer true iff we have a reified triple <code>t</code>.
31     */

32     public boolean hasTriple( Triple t )
33         { return inverseMap.containsKey( t ); }
34     
35     public Triple putTriple( Node key, Triple value )
36         {
37         forwardMap.put( key, value );
38         inversePut( value, key );
39         return value;
40         }
41     
42     public void removeTriple( Node key )
43         {
44         Object JavaDoc t = forwardMap.get( key );
45         forwardMap.remove( key );
46         if (t instanceof Triple) inverseRemove( (Triple) t, key );
47         }
48     
49     public void removeTriple( Node key, Triple value )
50         {
51         forwardMap.remove( key );
52         inverseRemove( value, key );
53         }
54     
55     public void removeTriple( Triple t )
56         {
57         ExtendedIterator it = tagIterator( t );
58         Set JavaDoc nodes = CollectionFactory.createHashedSet();
59         while (it.hasNext()) nodes.add( it.next() );
60         Iterator them = nodes.iterator();
61         while (them.hasNext()) removeTriple( (Node) them.next() );
62         }
63     
64     protected void inverseRemove( Triple value, Node key )
65         {
66         Set JavaDoc s = (Set JavaDoc) inverseMap.get( value );
67         if (s != null)
68             {
69             s.remove( key );
70             if (s.isEmpty()) inverseMap.remove( value );
71             }
72         }
73     
74     protected void inversePut( Triple value, Node key )
75         {
76         Set JavaDoc s = (Set JavaDoc) inverseMap.get( value );
77         if (s == null) inverseMap.put( value, s = new HashSet JavaDoc() );
78         s.add( key );
79         }
80
81     public ExtendedIterator tagIterator( Triple t )
82         {
83         Set JavaDoc s = (Set JavaDoc) inverseMap.get( t );
84         return s == null
85             ? (ExtendedIterator) NullIterator.instance
86             : WrappedIterator.create( s.iterator() );
87         }
88
89     protected ExtendedIterator allTriples( TripleMatch tm )
90         {
91         Triple pattern = tm.asTriple();
92         Node tag = pattern.getSubject();
93         if (tag.isConcrete())
94             {
95             Triple x = getTriple( tag );
96             return x == null ? NullIterator.instance : explodeTriple( pattern, tag, x );
97             }
98         else
99             {
100             final Iterator it = forwardMap.entrySet().iterator();
101             return new FragmentTripleIterator( pattern, it )
102                 {
103                 public void fill( GraphAdd ga, Node n, Object JavaDoc fragmentsObject )
104                     {
105                     SimpleReifier.graphAddQuad( ga, n, (Triple) fragmentsObject );
106                     }
107                 };
108             }
109         }
110     
111     /**
112          Answer an interator over all of the quadlets of <code>toExplode</code> with
113          the reifying node <code>tag</code> that match <code>pattern</code>.
114     */

115     public static ExtendedIterator explodeTriple( Triple pattern, Node tag, Triple toExplode )
116         {
117         GraphAddList L = new GraphAddList( pattern );
118         SimpleReifier.graphAddQuad( L, tag, toExplode );
119         return WrappedIterator.create( L.iterator() );
120         }
121     
122     /**
123         Return the fragment map as a read-only Graph of triples. We rely on the
124         default code in GraphBase which allows us to only implement find(TripleMatch)
125         to present a Graph. All the hard work is done by allTriples.
126     */

127     public Graph asGraph()
128         {
129         return new GraphBase()
130             { public ExtendedIterator graphBaseFind( TripleMatch tm ) { return allTriples( tm ); } };
131         }
132     
133     public ExtendedIterator find( TripleMatch m )
134         { return allTriples( m ); }
135     
136     public int size()
137         { return forwardMap.size() * 4; }
138     
139     /**
140          Answer an iterator over all the fragment tags in this map.
141     */

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