KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: SimpleReifier.java,v 1.46 2005/02/21 11:52:10 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.impl;
8
9 /**
10     @author kers
11 <p>
12     A base-level implementation of Reifier, intended to be straightforward
13     and obvious. It fails this test nowadays ...
14 */

15
16 import com.hp.hpl.jena.graph.*;
17 import com.hp.hpl.jena.shared.*;
18 import com.hp.hpl.jena.util.iterator.*;
19
20 import com.hp.hpl.jena.vocabulary.RDF;
21
22 public class SimpleReifier implements Reifier
23     {
24     protected final GraphBase parent;
25     protected final boolean intercepting;
26     protected final boolean concealing;
27     protected final ReificationStyle style;
28     
29     protected ReifierFragmentsMap fragmentsMap;
30     protected ReifierTripleMap tripleMap;
31     
32     protected boolean closed = false;
33         
34     /**
35         construct a simple reifier that is bound to the parent graph .
36         
37         @param parent the Graph which we're reifiying for
38         @param style the reification style to use
39     */

40     public SimpleReifier( GraphBase parent, ReificationStyle style )
41         { this( parent, new SimpleReifierTripleMap(), new SimpleReifierFragmentsMap(), style ); }
42     
43     public SimpleReifier
44         ( GraphBase parent, ReifierTripleMap tm, ReifierFragmentsMap fm, ReificationStyle style )
45         {
46         this.parent = parent;
47         this.fragmentsMap = fm;
48         this.tripleMap = tm;
49         this.intercepting = style.intercepts();
50         this.concealing = style.conceals();
51         this.style = style;
52         }
53         
54     public ReificationStyle getStyle()
55         { return style; }
56             
57     /** return the parent graph we are bound to */
58     public Graph getParentGraph()
59         { return parent; }
60         
61     /** return the triple bound to _n_ */
62     public Triple getTriple( Node n )
63         { return tripleMap.getTriple( n ); }
64         
65     /** true iff there is a triple bound to _n_ */
66     public boolean hasTriple( Node n )
67         { return getTriple( n ) != null; }
68         
69     /** */
70     public ExtendedIterator allNodes()
71         { return tripleMap.tagIterator(); }
72         
73     public ExtendedIterator allNodes( Triple t )
74         { return tripleMap.tagIterator( t ); }
75
76     /**
77         reifiy <code>toReify</code> with tag <code>tag</code>. If a different triple is
78         already reified under <code>tag</code>, throw an AlreadyReifiedException.
79     */

80     public Node reifyAs( Node tag, Triple toReify )
81         {
82         Triple existing = (Triple) tripleMap.getTriple( tag );
83         if (existing != null)
84             { if (!toReify.equals( existing )) throw new AlreadyReifiedException( tag ); }
85         else
86             reifyNewTriple( tag, toReify );
87         if (concealing == false) graphAddQuad( parent, tag, toReify );
88         return tag;
89         }
90         
91     /**
92          Reify <code>toReify</code> under <code>tag</code>; there is no existing
93          complete reification. This code goes around the houses by adding the
94          fragments one-by-one and then seeing if that made a complete reification.
95          Perhaps there's a better way, but I couldn't see it.
96     */

97     protected void reifyNewTriple( Node tag, Triple toReify )
98         {
99         if (fragmentsMap.hasFragments( tag ))
100             {
101             graphAddQuad( parent, tag, toReify );
102             if (tripleMap.getTriple( tag ) == null) throw new CannotReifyException( tag );
103             }
104         else
105             tripleMap.putTriple( tag, toReify );
106         }
107
108     /**
109         If n is bound to the triple t, remove that triple. If we're not concealing reification
110         quadlets, we need to remove them from the parent graph too.
111     */

112     public void remove( Node n, Triple t )
113         {
114         Triple x = (Triple) tripleMap.getTriple( n );
115         if (t.equals( x ))
116             { tripleMap.removeTriple( n, t );
117             if (!concealing) parentRemoveQuad( n, t ); }
118         }
119
120     public void remove( Triple t )
121         { tripleMap.removeTriple( t ); }
122             
123     public boolean hasTriple( Triple t )
124         { return tripleMap.hasTriple( t ); }
125           
126     public boolean handledAdd( Triple fragment )
127         {
128         if (intercepting)
129             {
130             ReifierFragmentHandler s = fragmentsMap.getFragmentHandler( fragment );
131             if (s == null)
132                 return false;
133             else
134                 {
135                 addFragment( s, fragment );
136                 return true;
137                 }
138             }
139         else
140             return false;
141         }
142
143     /**
144          Add <code>fragment</code> to the fragments already present. This may
145          create a complete triple, or over-specify.
146      * @param s
147      * @param fragment
148      */

149     protected void addFragment( ReifierFragmentHandler s, Triple fragment )
150         {
151         Node tag = fragment.getSubject(), object = fragment.getObject();
152         Triple reified = tripleMap.getTriple( tag );
153         if (reified == null)
154             updateFragments( s, fragment, tag, object );
155         else if (s.clashedWith( object, reified ))
156             tripleMap.removeTriple( tag, reified );
157         }
158
159     /**
160      * @param s
161      * @param fragment
162      * @param tag
163      * @param object
164      */

165     private void updateFragments( ReifierFragmentHandler s, Triple fragment, Node tag, Node object )
166         {
167         Triple t = s.reifyIfCompleteQuad( fragment, tag, object );
168         if (t instanceof Triple) tripleMap.putTriple( tag, t );
169         }
170
171     public boolean handledRemove( Triple fragment )
172         {
173         if (intercepting)
174             {
175             ReifierFragmentHandler s = fragmentsMap.getFragmentHandler( fragment );
176             if (s == null)
177                 return false;
178             else
179                 {
180                 removeFragment( s, fragment );
181                 return true;
182                 }
183             }
184         else
185             return false;
186         }
187                   
188     /**
189      * @param s
190      * @param fragment
191      */

192     private void removeFragment( ReifierFragmentHandler s, Triple fragment )
193         {
194         Node tag = fragment.getSubject();
195         Triple already = tripleMap.getTriple( tag );
196         Triple complete = s.removeFragment( tag, already, fragment );
197         if (complete == null)
198             tripleMap.removeTriple( tag );
199         else
200             tripleMap.putTriple( tag, complete );
201         }
202     
203     public ExtendedIterator find( TripleMatch m )
204         { return tripleMap.find( m ).andThen( fragmentsMap.find( m ) ); }
205     
206     public ExtendedIterator findExposed( TripleMatch m )
207         { return findEither( m, false ); }
208     
209     public ExtendedIterator findEither( TripleMatch m, boolean showHidden )
210         { return showHidden == concealing ? find( m ) : NullIterator.instance; }
211         
212     public int size()
213         { return concealing ? 0 : tripleMap.size() + fragmentsMap.size(); }
214     
215     /**
216         remove from the parent all of the triples that correspond to a reification
217         of t on tag.
218     */

219     private void parentRemoveQuad( Node n, Triple t )
220         {
221         parent.delete( Triple.create( n, RDF.Nodes.type, RDF.Nodes.Statement ) );
222         parent.delete( Triple.create( n, RDF.Nodes.subject, t.getSubject() ) );
223         parent.delete( Triple.create( n, RDF.Nodes.predicate, t.getPredicate() ) );
224         parent.delete( Triple.create( n, RDF.Nodes.object, t.getObject() ) );
225         }
226     
227     public static void graphAddQuad( GraphAdd g, Node node, Triple t )
228         {
229         g.add( Triple.create( node, RDF.Nodes.subject, t.getSubject() ) );
230         g.add( Triple.create( node, RDF.Nodes.predicate, t.getPredicate() ) );
231         g.add( Triple.create( node, RDF.Nodes.object, t.getObject() ) );
232         g.add( Triple.create( node, RDF.Nodes.type, RDF.Nodes.Statement ) );
233         }
234     
235     /**
236         our string representation is <R ...> wrapped round the string representation
237         of our node map.
238     */

239     public String JavaDoc toString()
240         { return "<R " + fragmentsMap + "|" + tripleMap + ">"; }
241
242     /**
243          Close this reifier - discard (big) resources.
244     */

245     public void close()
246         {
247         fragmentsMap = null;
248         tripleMap = null;
249         closed = true;
250         }
251
252     /**
253         Answer true iff this SImpleReifier has been closed.
254     */

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

288
Popular Tags