KickJava   Java API By Example, From Geeks To Geeks.

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


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

6 package com.hp.hpl.jena.rdf.model.impl;
7
8 import com.hp.hpl.jena.rdf.model.*;
9 import com.hp.hpl.jena.enhanced.*;
10 import com.hp.hpl.jena.shared.*;
11
12 import com.hp.hpl.jena.graph.*;
13
14 /** An implementation of Statement.
15  *
16  * @author bwm
17  * @version $Name: $ $Revision: 1.29 $ $Date: 2005/02/21 12:14:55 $
18  */

19 public class StatementImpl extends StatementBase implements Statement {
20     
21     protected Resource subject;
22     protected Property predicate;
23     protected RDFNode object;
24     
25     /** Creates new StatementImpl */
26     public StatementImpl(Resource subject, Property predicate, RDFNode object,
27             ModelCom model)
28         {
29         super( model );
30         this.subject = (Resource) subject.inModel( model );
31         this.predicate = (Property) predicate.inModel( model );
32         this.object = object.inModel( model );
33         }
34     
35     // TODO fix this hack
36
protected static ModelCom empty = (ModelCom) ModelFactory.createDefaultModel();
37     
38     public StatementImpl(Resource subject, Property predicate, RDFNode object)
39         {
40         super( empty );
41         this.subject = (Resource) subject.inModel( model );
42         this.predicate = (Property) predicate.inModel( model );
43         this.object = object.inModel( model );
44         }
45     
46     /**
47      * create a Statement from the triple _t_ in the enhanced graph _eg_. The
48      * Statement has subject, predicate, and object corresponding to those of
49      * _t_.
50      */

51     public static Statement toStatement( Triple t, ModelCom eg )
52         {
53         Resource s = new ResourceImpl( t.getSubject(), eg );
54         Property p = new PropertyImpl( t.getPredicate(), eg );
55         RDFNode o = createObject( t.getObject(), eg );
56         return new StatementImpl( s, p, o, eg );
57         }
58     
59     public Resource getSubject()
60         {
61         return subject;
62         }
63
64     public Property getPredicate()
65         {
66         return predicate;
67         }
68
69     public RDFNode getObject()
70         {
71         return object;
72         }
73
74     public Statement getStatementProperty( Property p )
75         {
76         return asResource().getRequiredProperty( p );
77         }
78     
79     public Resource getResource()
80         { return mustBeResource( object ); }
81     
82     public Resource getResource( ResourceF f )
83         { return f.createResource( getResource() ); }
84     
85     public Statement getProperty(Property p) {
86         return getResource().getRequiredProperty( p );
87     }
88         
89     /**
90         get the object field of this statement, insisting that it be a Literal.
91         If it isn't, throw LiteralRequiredException.
92     */

93     public Literal getLiteral() {
94         if (object instanceof Literal) {
95             return (Literal) object;
96         } else {
97             throw new LiteralRequiredException( object );
98         }
99     }
100     
101
102     public Object JavaDoc getObject(ObjectF f) {
103         try {
104             return f.createObject( getLiteral().toString());
105         } catch (Exception JavaDoc e) {
106             throw new JenaException(e);
107         }
108     }
109     
110    /** I suspect that this is now not pulling its weight. */
111    private EnhNode get( Class JavaDoc interf ) {
112         return (EnhNode) object.as( interf );
113     }
114         
115     public Bag getBag() {
116         return (Bag) get(Bag.class);
117     }
118     
119     public Alt getAlt() {
120         return (Alt)get(Alt.class);
121     }
122     
123     public Seq getSeq() {
124         return (Seq)get(Seq.class);
125     }
126     
127     /** it turns out to be handy to return the new StatementImpl as the result */
128     protected StatementImpl replace(RDFNode n) {
129         StatementImpl s = new StatementImpl( subject, predicate, n, model );
130         model.remove( this ).add( s );
131         return s;
132     }
133         
134     /**
135         .equals() defers to .sameAs so we only get the complexity of one cast.
136     */

137     public boolean equals(Object JavaDoc o)
138         { return o instanceof Statement && sameAs( (Statement) o ); }
139         
140     /**
141         sameAs - is this statement equal to the statement o? We can't assume
142         o is a StatementImpl
143     */

144     private final boolean sameAs( Statement o )
145         {
146         return subject.equals( o.getSubject() )
147             && predicate.equals( o.getPredicate() )
148             && object.equals( o.getObject() );
149         }
150     
151     public int hashCode() {
152         return asTriple().hashCode();
153     }
154     
155     public Resource asResource() {
156         return model.getAnyReifiedStatement(this);
157     }
158
159     public Statement remove()
160         {
161         model.remove( this );
162         return this;
163         }
164     
165     public void removeReification() {
166         model.removeAllReifications(this);
167     }
168     
169     public Triple asTriple() {
170         return Triple.create( subject.asNode(), predicate.asNode(), object.asNode() );
171     }
172     
173     /**
174         returns an array of triples corresponding to the array of statements; ie
175         the i'th element of the result is the i'th element of the input as a triple.
176         @param statements the array of statements to convert
177         @return the corresponding array of triples
178     */

179     public static Triple [] asTriples( Statement [] statements )
180         {
181         Triple [] triples = new Triple[statements.length];
182         for (int i = 0; i < statements.length; i += 1) triples[i] = statements[i].asTriple();
183         return triples;
184         }
185     
186     public boolean isReified() {
187         return model.isReified( this );
188     }
189         
190     /**
191         create a ReifiedStatement corresponding to this Statement
192     */

193     public ReifiedStatement createReifiedStatement()
194         { return ReifiedStatementImpl.create( this ); }
195         
196     /**
197         create a ReifiedStatement corresponding to this Statement
198         and with the given _uri_.
199     */

200     public ReifiedStatement createReifiedStatement( String JavaDoc uri )
201         { return ReifiedStatementImpl.create( (ModelCom) this.getModel(), uri, this ); }
202         
203     public RSIterator listReifiedStatements()
204         { return model.listReifiedStatements( this ); }
205
206     /**
207         create an RDF node which might be a literal, or not.
208     */

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

242
Popular Tags