1 10 package com.hp.hpl.jena.reasoner; 11 12 import com.hp.hpl.jena.graph.*; 13 import com.hp.hpl.jena.graph.compose.Union; 14 import com.hp.hpl.jena.graph.impl.*; 15 import com.hp.hpl.jena.shared.*; 16 import com.hp.hpl.jena.util.iterator.*; 17 import java.util.Iterator ; 18 19 25 public abstract class BaseInfGraph extends GraphBase implements InfGraph { 26 27 28 protected Reasoner reasoner; 29 30 31 protected FGraph fdata; 32 33 34 protected boolean recordDerivations; 35 36 37 protected boolean isPrepared = false; 38 39 43 public PrefixMapping getPrefixMapping() 44 { return getRawGraph().getPrefixMapping(); } 45 46 52 public Reifier getReifier() 53 { return getRawGraph().getReifier(); } 54 55 61 public BaseInfGraph(Graph data, Reasoner reasoner) { 62 this.fdata = new FGraph(data); 63 this.reasoner = reasoner; 64 } 65 66 69 public Capabilities getCapabilities() { 70 if (capabilities == null) { 71 return getReasoner().getGraphCapabilities(); 72 } else { 73 return capabilities; 74 } 75 } 76 77 85 public static class InfCapabilities extends AllCapabilities 86 { 87 public boolean sizeAccurate() { return false; } 88 public boolean deleteAllowed( boolean every ) { return !every; } 89 public boolean iteratorRemoveAllowed() { return false; } 90 public boolean findContractSafe() { return false; } 91 } 92 93 101 public static class InfFindSafeCapabilities extends InfCapabilities 102 { 103 public boolean findContractSafe() { return true; } 104 } 105 106 public BulkUpdateHandler getBulkUpdateHandler() 107 { 108 if (bulkHandler == null) bulkHandler = new InfBulkUpdateHandler( this ); 109 return bulkHandler; 110 } 111 112 118 static class InfBulkUpdateHandler extends SimpleBulkUpdateHandler 119 { 120 public InfBulkUpdateHandler( BaseInfGraph graph ) 121 { super(graph); } 122 123 public void remove( Node s, Node p, Node o ) 124 { 125 BaseInfGraph g = (BaseInfGraph) graph; 126 g.getRawGraph().getBulkUpdateHandler().remove( s, p, o ); 127 g.discardState(); 128 g.rebind(); 129 manager.notifyEvent( graph, GraphEvents.remove( s, p, o ) ); 130 } 131 132 public void removeAll() 133 { 134 BaseInfGraph g = (BaseInfGraph) graph; 135 g.getRawGraph().getBulkUpdateHandler().removeAll(); 136 g.discardState(); 137 g.rebind(); 138 g.getEventManager().notifyEvent( g, GraphEvents.removeAll ); 139 } 140 } 141 142 public TransactionHandler getTransactionHandler() 143 { return new InfTransactionHandler( this ); } 144 145 public static class InfTransactionHandler 146 extends TransactionHandlerBase implements TransactionHandler 147 { 148 protected final BaseInfGraph base; 149 150 public InfTransactionHandler( BaseInfGraph base ) 151 { this.base = base; } 152 153 public boolean transactionsSupported() 154 { return getBaseHandler().transactionsSupported(); } 155 156 protected TransactionHandler getBaseHandler() 157 { return base.getRawGraph().getTransactionHandler(); } 158 159 public void begin() 160 { getBaseHandler().begin(); } 161 162 public void abort() 163 { getBaseHandler().abort(); 164 base.rebind(); } 165 166 public void commit() 167 { getBaseHandler().commit(); } 168 } 169 170 174 protected void discardState() 175 {} 176 177 181 public Graph getRawGraph() { 182 return fdata.getGraph(); 183 } 184 185 188 public Reasoner getReasoner() { 189 return reasoner; 190 } 191 192 199 public void rebind(Graph data) { 200 fdata = new FGraph(data); 201 isPrepared = false; 202 } 203 204 211 public void rebind() { 212 isPrepared = false; 213 } 214 215 221 public void reset() { 222 } 223 224 232 public void prepare() { 233 isPrepared = true; 235 } 236 237 247 public Graph getDeductionsGraph() { 248 return null; 249 } 250 251 260 public Node getGlobalProperty(Node property) { 261 throw new ReasonerException("Global property not implemented: " + property); 262 } 263 264 268 public boolean testGlobalProperty(Node property) { 269 Node resultNode = getGlobalProperty(property); 270 if (resultNode.isLiteral()) { 271 Object result = resultNode.getLiteral().getValue(); 272 if (result instanceof Boolean ) { 273 return ((Boolean )result).booleanValue(); 274 } 275 } 276 throw new ReasonerException("Global property test returned non-boolean value" + 277 "\nTest was: " + property + 278 "\nResult was: " + resultNode); 279 } 280 281 287 public ValidityReport validate() { 288 checkOpen(); 289 return new StandardValidityReport(); 290 } 291 292 306 public ExtendedIterator find(Node subject, Node property, Node object, Graph param) { 307 return cloneWithPremises(param).find(subject, property, object); 308 } 309 310 321 public ExtendedIterator graphBaseFind(TripleMatch m) { 322 return graphBaseFind(m.getMatchSubject(), m.getMatchPredicate(), m.getMatchObject()) 323 ; 325 } 326 327 332 public ExtendedIterator graphBaseFind(Node subject, Node property, Node object) { 333 return findWithContinuation(new TriplePattern(subject, property, object), fdata); 334 } 335 336 347 abstract public ExtendedIterator findWithContinuation(TriplePattern pattern, Finder continuation); 348 349 350 358 public ExtendedIterator find(TriplePattern pattern) { 359 checkOpen(); 360 return findWithContinuation(pattern, fdata); 361 } 362 363 366 public void setDerivationLogging(boolean logOn) { 367 recordDerivations = logOn; 368 } 369 370 377 public Iterator getDerivation(Triple triple) { 378 return null; 379 } 380 381 384 public int graphBaseSize() { 385 checkOpen(); 386 return fdata.getGraph().size(); 387 } 388 389 393 public boolean isEmpty() { 394 return fdata.getGraph().isEmpty(); 395 } 396 397 400 public void close() { 401 if (!closed) { 402 fdata.getGraph().close(); 403 fdata = null; 404 super.close(); 405 } 406 } 407 408 412 public synchronized void performAdd(Triple t) { 413 if (!isPrepared) prepare(); 414 fdata.getGraph().add(t); 415 } 416 417 420 public void performDelete(Triple t) { 421 if (!isPrepared) prepare(); 422 fdata.getGraph().delete(t); 423 } 424 425 428 public abstract Graph getSchemaGraph(); 429 430 436 public InfGraph cloneWithPremises(Graph premises) { 437 return getReasoner().bindSchema(getSchemaGraph()).bind(new Union(getRawGraph(), premises)); 438 } 439 440 } 441 442 471 | Popular Tags |