001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: StatementImpl.java,v 1.32 2008/01/02 12:04:57 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.rdf.model.impl;
007:
008: import com.hp.hpl.jena.rdf.model.*;
009: import com.hp.hpl.jena.enhanced.*;
010: import com.hp.hpl.jena.shared.*;
011:
012: import com.hp.hpl.jena.graph.*;
013:
014: /** An implementation of Statement.
015: *
016: * @author bwm
017: * @version $Name: $ $Revision: 1.32 $ $Date: 2008/01/02 12:04:57 $
018: */
019: public class StatementImpl extends StatementBase implements Statement {
020:
021: protected Resource subject;
022: protected Property predicate;
023: protected RDFNode object;
024:
025: /** Creates new StatementImpl */
026: public StatementImpl(Resource subject, Property predicate,
027: RDFNode object, ModelCom model) {
028: super (model);
029: this .subject = (Resource) subject.inModel(model);
030: this .predicate = (Property) predicate.inModel(model);
031: this .object = object.inModel(model);
032: }
033:
034: // TODO fix this hack
035: protected static ModelCom empty = (ModelCom) ModelFactory
036: .createDefaultModel();
037:
038: public StatementImpl(Resource subject, Property predicate,
039: RDFNode object) {
040: super (empty);
041: this .subject = (Resource) subject.inModel(model);
042: this .predicate = (Property) predicate.inModel(model);
043: this .object = object.inModel(model);
044: }
045:
046: /**
047: * create a Statement from the triple _t_ in the enhanced graph _eg_. The
048: * Statement has subject, predicate, and object corresponding to those of
049: * _t_.
050: */
051: public static Statement toStatement(Triple t, ModelCom eg) {
052: Resource s = new ResourceImpl(t.getSubject(), eg);
053: Property p = new PropertyImpl(t.getPredicate(), eg);
054: RDFNode o = createObject(t.getObject(), eg);
055: return new StatementImpl(s, p, o, eg);
056: }
057:
058: public Resource getSubject() {
059: return subject;
060: }
061:
062: public Property getPredicate() {
063: return predicate;
064: }
065:
066: public RDFNode getObject() {
067: return object;
068: }
069:
070: public Statement getStatementProperty(Property p) {
071: return asResource().getRequiredProperty(p);
072: }
073:
074: public Resource getResource() {
075: return mustBeResource(object);
076: }
077:
078: public Resource getResource(ResourceF f) {
079: return f.createResource(getResource());
080: }
081:
082: public Statement getProperty(Property p) {
083: return getResource().getRequiredProperty(p);
084: }
085:
086: /**
087: get the object field of this statement, insisting that it be a Literal.
088: If it isn't, throw LiteralRequiredException.
089: */
090: public Literal getLiteral() {
091: if (object instanceof Literal) {
092: return (Literal) object;
093: } else {
094: throw new LiteralRequiredException(object);
095: }
096: }
097:
098: public Object getObject(ObjectF f) {
099: try {
100: return f.createObject(getLiteral().toString());
101: } catch (Exception e) {
102: throw new JenaException(e);
103: }
104: }
105:
106: /** I suspect that this is now not pulling its weight. */
107: private EnhNode get(Class interf) {
108: return (EnhNode) object.as(interf);
109: }
110:
111: public Bag getBag() {
112: return (Bag) get(Bag.class);
113: }
114:
115: public Alt getAlt() {
116: return (Alt) get(Alt.class);
117: }
118:
119: public Seq getSeq() {
120: return (Seq) get(Seq.class);
121: }
122:
123: /** it turns out to be handy to return the new StatementImpl as the result */
124: protected StatementImpl replace(RDFNode n) {
125: StatementImpl s = new StatementImpl(subject, predicate, n,
126: model);
127: model.remove(this ).add(s);
128: return s;
129: }
130:
131: /**
132: .equals() defers to .sameAs so we only get the complexity of one cast.
133: */
134: public boolean equals(Object o) {
135: return o instanceof Statement && sameAs((Statement) o);
136: }
137:
138: /**
139: sameAs - is this statement equal to the statement o? We can't assume
140: o is a StatementImpl
141: */
142: private final boolean sameAs(Statement o) {
143: return subject.equals(o.getSubject())
144: && predicate.equals(o.getPredicate())
145: && object.equals(o.getObject());
146: }
147:
148: public int hashCode() {
149: return asTriple().hashCode();
150: }
151:
152: public Resource asResource() {
153: return model.getAnyReifiedStatement(this );
154: }
155:
156: public Statement remove() {
157: model.remove(this );
158: return this ;
159: }
160:
161: public void removeReification() {
162: model.removeAllReifications(this );
163: }
164:
165: public Triple asTriple() {
166: return Triple.create(subject.asNode(), predicate.asNode(),
167: object.asNode());
168: }
169:
170: /**
171: returns an array of triples corresponding to the array of statements; ie
172: the i'th element of the result is the i'th element of the input as a triple.
173: @param statements the array of statements to convert
174: @return the corresponding array of triples
175: */
176: public static Triple[] asTriples(Statement[] statements) {
177: Triple[] triples = new Triple[statements.length];
178: for (int i = 0; i < statements.length; i += 1)
179: triples[i] = statements[i].asTriple();
180: return triples;
181: }
182:
183: public boolean isReified() {
184: return model.isReified(this );
185: }
186:
187: /**
188: create a ReifiedStatement corresponding to this Statement
189: */
190: public ReifiedStatement createReifiedStatement() {
191: return ReifiedStatementImpl.create(this );
192: }
193:
194: /**
195: create a ReifiedStatement corresponding to this Statement
196: and with the given _uri_.
197: */
198: public ReifiedStatement createReifiedStatement(String uri) {
199: return ReifiedStatementImpl.create((ModelCom) this .getModel(),
200: uri, this );
201: }
202:
203: public RSIterator listReifiedStatements() {
204: return model.listReifiedStatements(this );
205: }
206:
207: /**
208: create an RDF node which might be a literal, or not.
209: */
210: public static RDFNode createObject(Node n, EnhGraph g) {
211: return n.isLiteral() ? (RDFNode) new LiteralImpl(n, g)
212: : new ResourceImpl(n, g);
213: }
214:
215: }
216:
217: /*
218: (c) Copyright 2000, 2001, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
219: All rights reserved.
220:
221: Redistribution and use in source and binary forms, with or without
222: modification, are permitted provided that the following conditions
223: are met:
224: 1. Redistributions of source code must retain the above copyright
225: notice, this list of conditions and the following disclaimer.
226: 2. Redistributions in binary form must reproduce the above copyright
227: notice, this list of conditions and the following disclaimer in the
228: documentation and/or other materials provided with the distribution.
229: 3. The name of the author may not be used to endorse or promote products
230: derived from this software without specific prior written permission.
231:
232: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
233: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
234: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
235: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
236: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
237: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
239: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
240: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
241: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
242: */
|