1 6 7 package com.hp.hpl.jena.graph.impl; 8 9 import com.hp.hpl.jena.graph.*; 10 import com.hp.hpl.jena.graph.query.*; 11 import com.hp.hpl.jena.util.iterator.*; 12 13 import com.hp.hpl.jena.shared.*; 14 import com.hp.hpl.jena.shared.impl.PrefixMappingImpl; 15 16 30 31 public abstract class GraphBase implements GraphWithPerform 32 { 33 37 protected final ReificationStyle style; 38 39 43 protected boolean closed = false; 44 45 48 public GraphBase() 49 { this( ReificationStyle.Minimal ); } 50 51 55 public GraphBase( ReificationStyle style ) 56 { this.style = style; } 57 58 61 protected void checkOpen() 62 { if (closed) throw new ClosedException( "already closed", this ); } 63 64 67 public void close() 68 { closed = true; 69 if (reifier != null) reifier.close(); } 70 71 75 public boolean dependsOn( Graph other ) 76 { return this == other; } 77 78 83 public QueryHandler queryHandler() 84 { 85 if (queryHandler == null) queryHandler = new SimpleQueryHandler(this); 86 return queryHandler; 87 } 88 89 93 protected QueryHandler queryHandler; 94 95 100 public GraphEventManager getEventManager() 101 { 102 if (gem == null) gem = new SimpleEventManager( this ); 103 return gem; 104 } 105 106 110 protected GraphEventManager gem; 111 112 113 116 public void notifyAdd( Triple t ) 117 { getEventManager().notifyAddTriple( this, t ); } 118 119 123 public void notifyDelete( Triple t ) 124 { getEventManager().notifyDeleteTriple( this, t ); } 125 126 130 public TransactionHandler getTransactionHandler() 131 { return new SimpleTransactionHandler(); } 132 133 139 public BulkUpdateHandler getBulkUpdateHandler() 140 { 141 if (bulkHandler == null) bulkHandler = new SimpleBulkUpdateHandler( this ); 142 return bulkHandler; 143 } 144 145 148 protected BulkUpdateHandler bulkHandler; 149 150 155 public Capabilities getCapabilities() 156 { 157 if (capabilities == null) capabilities = new AllCapabilities(); 158 return capabilities; 159 } 160 161 164 protected Capabilities capabilities = null; 165 166 170 public PrefixMapping getPrefixMapping() 171 { return pm; } 172 173 protected PrefixMapping pm = new PrefixMappingImpl(); 174 175 180 public void add( Triple t ) 181 { 182 checkOpen(); 183 performAdd( t ); 184 notifyAdd( t ); 185 } 186 187 192 public void performAdd( Triple t ) 193 { throw new AddDeniedException( "GraphBase::performAdd" ); } 194 195 200 201 public final void delete( Triple t ) 202 { 203 checkOpen(); 204 performDelete( t ); 205 notifyDelete( t ); 206 } 207 208 213 public void performDelete( Triple t ) 214 { throw new DeleteDeniedException( "GraphBase::delete" ); } 215 216 222 public final ExtendedIterator find( TripleMatch m ) 223 { checkOpen(); 224 return reifierTriples( m ) .andThen( graphBaseFind( m ) ); } 225 226 231 protected abstract ExtendedIterator graphBaseFind( TripleMatch m ); 232 233 236 public final ExtendedIterator find( Node s, Node p, Node o ) 237 { checkOpen(); 238 return graphBaseFind( s, p, o ); } 239 240 protected ExtendedIterator graphBaseFind( Node s, Node p, Node o ) 241 { return find( Triple.createMatch( s, p, o ) ); } 242 243 249 public final boolean contains( Triple t ) 250 { checkOpen(); 251 return reifierContains( t ) || graphBaseContains( t ); } 252 253 259 protected boolean reifierContains( Triple t ) 260 { ClosableIterator it = getReifier().findExposed( t ); 261 try { return it.hasNext(); } finally { it.close(); } } 262 263 268 protected boolean graphBaseContains( Triple t ) 269 { return containsByFind( t ); } 270 271 275 public final boolean contains( Node s, Node p, Node o ) { 276 checkOpen(); 277 return contains( Triple.create( s, p, o ) ); 278 } 279 280 287 final protected boolean containsByFind( Triple t ) 288 { 289 ClosableIterator it = find( t ); 290 try { return it.hasNext(); } finally { it.close(); } 291 } 292 293 298 protected ExtendedIterator reifierTriples( TripleMatch m ) 299 { return getReifier().findExposed( m ); } 300 301 306 public Reifier getReifier() 307 { 308 if (reifier == null) reifier = new SimpleReifier( this, style ); 309 return reifier; 310 } 311 312 315 protected Reifier reifier = null; 316 317 323 public final int size() 324 { checkOpen(); 325 return graphBaseSize() + reifierSize(); } 326 327 332 protected int reifierSize() 333 { return getReifier().size(); } 334 335 340 protected int graphBaseSize() 341 { 342 ExtendedIterator it = GraphUtil.findAll( this ); 343 int tripleCount = 0; 344 while (it.hasNext()) { it.next(); tripleCount += 1; } 345 return tripleCount; 346 } 347 348 355 public boolean isEmpty() 356 { return size() == 0; } 357 358 362 public boolean isIsomorphicWith( Graph g ) 363 { checkOpen(); 364 return g != null && GraphMatcher.equals( this, g ); } 365 366 370 371 public String toString() 372 { return toString( (closed ? "closed " : ""), this ); } 373 374 381 public static String toString( String prefix, Graph that ) 382 { 383 PrefixMapping pm = that.getPrefixMapping(); 384 StringBuffer b = new StringBuffer ( prefix + " {" ); 385 String gap = ""; 386 ClosableIterator it = GraphUtil.findAll( that ); 387 while (it.hasNext()) 388 { 389 b.append( gap ); 390 gap = "; "; 391 b.append( ((Triple) it.next()).toString( pm ) ); 392 } 393 b.append( "}" ); 394 return b.toString(); 395 } 396 397 } 398 399 428 | Popular Tags |