1 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 19 public class StatementImpl extends StatementBase implements Statement { 20 21 protected Resource subject; 22 protected Property predicate; 23 protected RDFNode object; 24 25 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 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 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 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 getObject(ObjectF f) { 103 try { 104 return f.createObject( getLiteral().toString()); 105 } catch (Exception e) { 106 throw new JenaException(e); 107 } 108 } 109 110 111 private EnhNode get( Class 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 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 137 public boolean equals(Object o) 138 { return o instanceof Statement && sameAs( (Statement) o ); } 139 140 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 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 193 public ReifiedStatement createReifiedStatement() 194 { return ReifiedStatementImpl.create( this ); } 195 196 200 public ReifiedStatement createReifiedStatement( String uri ) 201 { return ReifiedStatementImpl.create( (ModelCom) this.getModel(), uri, this ); } 202 203 public RSIterator listReifiedStatements() 204 { return model.listReifiedStatements( this ); } 205 206 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 242 | Popular Tags |