KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package com.hp.hpl.jena.rdf.model.impl;
8
9 import com.hp.hpl.jena.enhanced.*;
10 import com.hp.hpl.jena.graph.*;
11 import com.hp.hpl.jena.rdf.model.*;
12
13
14 /**
15     A ReifiedStatementImpl encodes a Statement and behaves as a Resource.
16 */

17
18 public class ReifiedStatementImpl extends ResourceImpl implements ReifiedStatement
19     {
20     /** the Statement that this ReifiedStatement represents */
21     protected Statement statement;
22     
23     /**
24         private constructor, relies (ugh) on super(uri, model) generating
25         bnode if uril == null.
26     */

27
28     private ReifiedStatementImpl( ModelCom m, String JavaDoc uri, Statement s )
29         {
30         super( uri, m );
31         assertStatement( s );
32         }
33         
34     protected ReifiedStatementImpl( EnhGraph m, Node n, Statement s )
35         {
36         super( n, m );
37         assertStatement( s );
38         }
39         
40     private void assertStatement( Statement s )
41         {
42         statement = s;
43         }
44         
45     /**
46         answer [a .equals() version of] the Statement that this ReifiedStatement
47         represents.
48     */

49     public Statement getStatement()
50         { return statement; }
51        
52     static final public Implementation reifiedStatementFactory = new Implementation()
53         {
54         /**
55             convert a _node_ into a ReifiedStatement in the enhanced graph
56             _eg_ by looking into this graph's reifier to find the binding for the
57             node; throw a DoesNotReify exception if there's no mapping.
58         */

59         public EnhNode wrap( Node n, EnhGraph eg )
60             {
61             Triple x = getTriple( eg, n );
62             if (x == null) throw new DoesNotReifyException( n );
63             Statement st = StatementImpl.toStatement( x, (ModelCom) eg );
64             return new ReifiedStatementImpl( eg, n, st );
65             }
66
67         /**
68             Answer true iff the node <code>n</code> can become a reified statement,
69             ie it is associated with a triple by <code>eg</code>'s Reifier.
70             @param eg the (enhanced) graph who's Reifier might hold the triple
71             @param n the node who's triple is required
72             @return true iff there's an associated triple
73         */

74         public boolean canWrap( Node n, EnhGraph eg )
75             { return getTriple( eg, n ) != null; }
76             
77         /**
78             Answer the triple associated with <code>n</code> by eg's graph's Reifier.
79             @param eg the (enhanced) graph who's Reifier might hold the triple
80             @param n the node who's triple is required
81             @return the associated triple if any, otherwise null
82         */

83         private Triple getTriple( EnhGraph eg, Node n )
84             { return eg.asGraph().getReifier().getTriple( n ); }
85         };
86         
87     /**
88         Answer our Reifier (ie our Model's Graph's Reifier).
89     */

90     protected Reifier getReifier()
91         { return getModel().getGraph().getReifier(); }
92         
93     public boolean isValid()
94         { return getModel().getGraph().getReifier().getTriple( this.asNode() ) != null; }
95         
96     /**
97         tell the underlying graph's reifier that this ReifiedStatement's node
98         represents any Statement with this statement's triple. (May throw an
99         exception if the node is already reified to something different.)
100     */

101     private ReifiedStatementImpl cache()
102         {
103         getReifier().reifyAs( this.asNode(), statement.asTriple() );
104         return this;
105         }
106       
107     /**
108         factory method. answer a ReifiedStatement which encodes the
109         Statement _s_. The mapping is remembered.
110     */

111     public static ReifiedStatement create( Statement s )
112         { return create( (ModelCom) s.getModel(), (String JavaDoc) null, s ); }
113
114     /**
115         factory method. answer a ReifiedStatement which encodes the
116         Statement _s_ with uri _uri_. The mapping is remembered.
117     */

118     public static ReifiedStatementImpl create( ModelCom m, String JavaDoc uri, Statement s )
119         { return new ReifiedStatementImpl( m, uri, s ).cache(); }
120         
121     public static ReifiedStatementImpl create( EnhGraph eg, Node n, Statement s )
122         { return new ReifiedStatementImpl( eg, n, s ).cache(); }
123     
124     public String JavaDoc toString()
125         { return super.toString() + "=>" + statement; }
126     }
127     
128 /*
129     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
130     All rights reserved.
131
132     Redistribution and use in source and binary forms, with or without
133     modification, are permitted provided that the following conditions
134     are met:
135
136     1. Redistributions of source code must retain the above copyright
137        notice, this list of conditions and the following disclaimer.
138
139     2. Redistributions in binary form must reproduce the above copyright
140        notice, this list of conditions and the following disclaimer in the
141        documentation and/or other materials provided with the distribution.
142
143     3. The name of the author may not be used to endorse or promote products
144        derived from this software without specific prior written permission.
145
146     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
148     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
149     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
150     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
151     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
152     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
153     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
154     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
155     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
156 */

157
Popular Tags