KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > impl > ModelReifier


1 /*
2     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3     [see end of file]
4     $Id: ModelReifier.java,v 1.19 2005/02/21 12:14:35 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.impl;
8
9 import com.hp.hpl.jena.rdf.model.*;
10 import com.hp.hpl.jena.shared.*;
11 import com.hp.hpl.jena.graph.*;
12 import com.hp.hpl.jena.graph.compose.*;
13 import com.hp.hpl.jena.graph.impl.GraphBase;
14 import com.hp.hpl.jena.util.iterator.*;
15
16 /**
17     This class impedance-matches the reification requests of Model[Com] to the operations
18     supplied by it's Graph's Reifier.
19     
20     @author kers
21 */

22 public class ModelReifier
23     {
24     private ModelCom model;
25     public Reifier reifier;
26     
27     /**
28         DEVEL. setting this _true_ means that nodes that reify statements
29         will drag their reification quads into other nodes when they are
30         added to them inside statements.
31     */

32     private static boolean copyingReifications = false;
33     
34     /**
35         establish the internal state of this ModelReifier: the associated
36         Model[Com] and its graph's Reifier.
37     */

38     public ModelReifier( ModelCom model )
39         {
40         this.model = model;
41         this.reifier = model.asGraph().getReifier();
42         }
43         
44     public ReificationStyle getReificationStyle()
45         { return reifier.getStyle(); }
46         
47     /**
48         Answer a version of the model, but with all its reifiying statements
49         added.
50         @param m a model that may have reified statements
51         @return a new model, the union of m and the reification statements of m
52     */

53     public static Model withHiddenStatements( Model m )
54         {
55         Graph mGraph = m.getGraph();
56         Graph hiddenTriples = getHiddenTriples( m );
57         return new ModelCom( new DisjointUnion( mGraph, hiddenTriples ) );
58         }
59     
60     /**
61         @param mGraph
62         @return
63     */

64     protected static Graph getHiddenTriples( Model m )
65         {
66         Graph mGraph = m.getGraph();
67         final Reifier r = mGraph.getReifier();
68         return new GraphBase()
69             {
70             public ExtendedIterator graphBaseFind( TripleMatch m )
71                 { return r.findEither( m, true ); }
72             };
73         }
74
75     /**
76         Answer a model that consists of the hidden reification statements of this model.
77         @return a new model containing the hidden statements of this model
78     */

79     public Model getHiddenStatements()
80         { return new ModelCom( getHiddenTriples( model ) ); }
81         
82     /**
83         Answer a fresh reification of a statement associated with a fresh bnode.
84         @param s a Statement to reifiy
85         @return a reified statement object who's name is a new bnode
86     */

87     public ReifiedStatement createReifiedStatement( Statement s )
88         { return createReifiedStatement( null, s ); }
89
90     /**
91         Answer a reification of a statement with a given uri. If that uri
92         already reifies a distinct Statement, throw an AlreadyReifiedException.
93         @param uri the URI of the resource which will reify <code>s</code>
94         @param s the Statement to reify
95         @return a reified statement object associating <code>uri</code> with <code>s</code>.
96         @throws AlreadyReifiedException if uri already reifies something else.
97     */

98     public ReifiedStatement createReifiedStatement( String JavaDoc uri, Statement s )
99         { return ReifiedStatementImpl.create( model, uri, s ); }
100    
101     /**
102         Find any existing reified statement that reifies a givem statement. If there isn't one,
103         create one.
104         @param s a Statement for which to find [or create] a reification
105         @return a reification for s, re-using an existing one if possible
106     */

107     public Resource getAnyReifiedStatement( Statement s )
108         {
109         RSIterator it = listReifiedStatements( s );
110         if (it.hasNext())
111             try { return it.nextRS(); } finally { it.close(); }
112         else
113             return createReifiedStatement( s );
114         }
115          
116     /**
117         Answer true iff a given statement is reified in this model
118         @param s the statement for which a reification is sought
119         @return true iff s has a reification in this model
120     */

121     public boolean isReified( FrontsTriple s )
122         { return reifier.hasTriple( s.asTriple() ); }
123
124     /**
125         Remove all the reifications of a given statement in this model, whatever
126         their associated resources.
127         @param s the statement whose reifications are to be removed
128     */

129     public void removeAllReifications( FrontsTriple s )
130         { reifier.remove( s.asTriple() ); }
131       
132     /**
133         Remove a given reification from this model. Other reifications of the same statement
134         are untouched.
135         @param rs the reified statement to be removed
136     */

137     public void removeReification( ReifiedStatement rs )
138         { reifier.remove( rs.asNode(), rs.getStatement().asTriple() ); }
139         
140     /**
141         Answer an iterator that iterates over all the reified statements
142         in this model.
143         @return an iterator over all the reifications of the model.
144     */

145     public RSIterator listReifiedStatements()
146         { return new RSIteratorImpl( findReifiedStatements() ); }
147    
148     /**
149         Answer an iterator that iterates over all the reified statements in
150         this model that reify a given statement.
151         @param s the statement whose reifications are sought.
152         @return an iterator over the reifications of s.
153     */

154     public RSIterator listReifiedStatements( FrontsTriple s )
155         { return new RSIteratorImpl( findReifiedStatements( s.asTriple() ) ); }
156       
157     /**
158         the triple (s, p, o) has been asserted into the model. Any reified statements
159         among them need to be added to this model.
160     */

161     public void noteIfReified( RDFNode s, RDFNode p, RDFNode o )
162         {
163         if (copyingReifications)
164             {
165             noteIfReified( s );
166             noteIfReified( p );
167             noteIfReified( o );
168             }
169         }
170         
171     /**
172         If _n_ is a ReifiedStatement, create a local copy of it, which
173         will force the underlying reifier to take note of the mapping.
174     */

175     private void noteIfReified( RDFNode n )
176         {
177         if (n.canAs( ReifiedStatement.class ))
178             {
179             ReifiedStatement rs = (ReifiedStatement) n.as( ReifiedStatement.class );
180             createReifiedStatement( rs.getURI(), rs.getStatement() );
181             }
182         }
183         
184     /**
185         A mapper that maps modes to their corresponding ReifiedStatement objects. This
186         cannot be static: getRS cannot be static, because the mapping is model-specific.
187     */

188     protected final Map1 mapToRS = new Map1()
189         {
190         public Object JavaDoc map1( Object JavaDoc node ) { return getRS( (Node) node ); }
191         };
192
193     private ExtendedIterator findReifiedStatements()
194         { return reifier .allNodes() .mapWith( mapToRS ); }
195
196     private ExtendedIterator findReifiedStatements( Triple t )
197         { return reifier .allNodes( t ) .mapWith( mapToRS ); }
198         
199     /**
200         Answer a ReifiedStatement that is based on the given node.
201         @param n the node which represents the reification (and is bound to some triple t)
202         @return a ReifiedStatement associating the resource of n with the statement of t.
203     */

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

241
242
Popular Tags