1 10 package com.hp.hpl.jena.reasoner.transitiveReasoner; 11 12 import com.hp.hpl.jena.graph.*; 13 import com.hp.hpl.jena.reasoner.*; 14 import com.hp.hpl.jena.util.iterator.ExtendedIterator; 15 import com.hp.hpl.jena.vocabulary.RDFS; 16 17 import java.util.*; 18 19 26 public class TransitiveEngine { 27 28 29 protected TransitiveGraphCache subClassCache; 30 31 32 protected TransitiveGraphCache subPropertyCache; 33 34 35 protected Finder data; 36 37 38 protected boolean isPrepared = false; 39 40 41 protected static HashSet subClassAliases; 42 43 44 protected static HashSet subPropertyAliases; 45 46 47 private static final int NOT_RELEVANT = 1; 48 49 50 private static final int SUBCLASS = 2; 51 52 53 private static final int SUBPROPERTY = 4; 54 55 56 private static final int UPDATE_MASK = SUBCLASS | SUBPROPERTY | NOT_RELEVANT; 58 59 60 private static final int REBUILD_SUBCLASS = 8; 61 62 63 private static final int REBUILD_SUBPROPERTY = 16; 64 65 66 public static Node directSubPropertyOf; 67 68 69 public static Node directSubClassOf; 70 71 72 public static Node subPropertyOf; 73 74 75 public static Node subClassOf; 76 77 static { 79 directSubPropertyOf = TransitiveReasoner.directSubPropertyOf; 80 directSubClassOf = TransitiveReasoner.directSubClassOf; 81 subPropertyOf = RDFS.subPropertyOf.getNode(); 82 subClassOf = RDFS.subClassOf.getNode(); 83 } 84 85 90 public TransitiveEngine(TransitiveGraphCache subClassCache, 91 TransitiveGraphCache subPropertyCache) { 92 this.subClassCache = subClassCache; 93 this.subPropertyCache = subPropertyCache; 94 } 95 96 100 public TransitiveEngine(TransitiveEngine tengine) { 101 this.subClassCache = tengine.getSubClassCache().deepCopy(); 102 this.subPropertyCache = tengine.getSubPropertyCache().deepCopy(); 103 } 104 105 113 public Finder insert(Finder baseData, FGraph newData) { 114 Graph newDataG = newData.getGraph(); 115 if (baseData != null) { 116 data = FinderUtil.cascade(baseData, newData); 117 } else { 118 data = newData; 119 } 120 if ((TransitiveEngine.checkOccuranceUtility(subPropertyOf, newDataG, subPropertyCache) || 121 TransitiveEngine.checkOccuranceUtility(subClassOf, newDataG, subPropertyCache))) { 122 subClassCache = new TransitiveGraphCache(directSubClassOf, subClassOf); 123 subPropertyCache = new TransitiveGraphCache(directSubPropertyOf, subPropertyOf); 124 TransitiveEngine.cacheSubPropUtility(data, subPropertyCache); 125 TransitiveEngine.cacheSubClassUtility(data, subPropertyCache, subClassCache); 126 } 127 return data; 128 } 129 130 133 public TransitiveGraphCache getSubClassCache() { 134 return subClassCache; 135 } 136 137 140 public TransitiveGraphCache getSubPropertyCache() { 141 return subPropertyCache; 142 } 143 144 149 public void setCaching(boolean cacheSP, boolean cacheSC) { 150 subPropertyCache.setCaching(cacheSP); 151 subClassCache.setCaching(cacheSC); 152 } 153 154 157 private void prepare() { 158 if (isPrepared) return; 159 subClassAliases = new HashSet(); 160 subClassAliases.add(subClassOf); 161 subClassAliases.add(directSubClassOf); 162 163 subPropertyAliases = new HashSet(); 164 subPropertyAliases.add(subPropertyOf); 165 subPropertyAliases.add(directSubPropertyOf); 166 167 Iterator subProps = subPropertyCache.find(new TriplePattern(null, subPropertyOf, subPropertyOf)); 168 while (subProps.hasNext()) { 169 Triple spT = (Triple) subProps.next(); 170 Node spAlias = spT.getSubject(); 171 subPropertyAliases.add(spAlias); 172 Iterator subClasses = subPropertyCache.find(new TriplePattern(null, spAlias, subClassOf)); 173 while (subClasses.hasNext()) { 174 subClassAliases.add(((Triple)subClasses.next()).getObject()); 175 } 176 } 177 isPrepared = true; 178 } 179 180 185 private int triage(Triple t) { 186 if (!isPrepared) prepare(); 187 Node predicate = t.getPredicate(); 188 if (subClassAliases.contains(predicate)) { 189 return SUBCLASS; 190 } else if (subPropertyAliases.contains(predicate)) { 191 Node target = t.getObject(); 192 if (subClassAliases.contains(target)) { 193 return REBUILD_SUBCLASS | SUBPROPERTY; 194 } else if (subPropertyAliases.contains(target)) { 195 return REBUILD_SUBCLASS | REBUILD_SUBPROPERTY; 196 } else { 197 return SUBPROPERTY; 198 } 199 } else { 200 return NOT_RELEVANT; 201 } 202 203 } 204 205 208 public Finder getFinder(TriplePattern pattern, Finder continuation) { 209 if (!isPrepared) prepare(); 210 Node predicate = pattern.getPredicate(); 211 if (predicate.isVariable()) { 212 return FinderUtil.cascade(subPropertyCache, subClassCache, continuation); 214 } else if (subPropertyAliases.contains(predicate)) { 215 return subPropertyCache; 216 } else if (subClassAliases.contains(predicate)) { 217 return subClassCache; 218 } else { 219 return continuation; 220 } 221 } 222 223 227 public synchronized boolean add(Triple t) { 228 int triageClass = triage(t); 229 switch (triageClass & UPDATE_MASK) { 230 case SUBCLASS: 231 subClassCache.addRelation(t); 232 break; 233 case SUBPROPERTY: 234 subPropertyCache.addRelation(t); 235 break; 236 case NOT_RELEVANT: 237 return false; 238 } 239 if ((triageClass & REBUILD_SUBPROPERTY) != 0) { 241 TransitiveEngine.cacheSubPropUtility(data, subPropertyCache); 242 isPrepared = false; 243 } 244 if ((triageClass & REBUILD_SUBCLASS) != 0) { 245 TransitiveEngine.cacheSubClassUtility(data, subPropertyCache, subClassCache); 246 isPrepared = false; 247 } 248 return true; 249 } 250 251 255 public synchronized boolean delete(Triple t) { 256 int triageClass = triage(t); 257 switch (triageClass & UPDATE_MASK) { 258 case SUBCLASS: 259 subClassCache.removeRelation(t); 260 break; 261 case SUBPROPERTY: 262 subPropertyCache.removeRelation(t); 263 break; 264 case NOT_RELEVANT: 265 return false; 266 } 267 if ((triageClass & REBUILD_SUBPROPERTY) != 0) { 269 subPropertyCache.clear(); 270 TransitiveEngine.cacheSubPropUtility(data, subPropertyCache); 271 isPrepared = false; 272 } 273 if ((triageClass & REBUILD_SUBCLASS) != 0) { 274 subClassCache.clear(); 275 TransitiveEngine.cacheSubClassUtility(data, subPropertyCache, subClassCache); 276 isPrepared = false; 277 } 278 return true; 279 } 280 281 282 290 public boolean checkOccurance(Node prop, Graph graph) { 291 return checkOccuranceUtility(prop, graph, subPropertyCache); 292 } 293 294 298 308 public static boolean cacheSubClassUtility(Finder graph, TransitiveGraphCache spCache, TransitiveGraphCache scCache) { 309 if (graph == null) return false; 310 311 scCache.cacheAll(graph, TransitiveReasoner.subClassOf); 312 313 boolean foundAny = false; 315 ExtendedIterator subClasses 316 = spCache.find(new TriplePattern(null, TransitiveReasoner.subPropertyOf, TransitiveReasoner.subClassOf)); 317 while (subClasses.hasNext()) { 318 foundAny = true; 319 Triple t = (Triple)subClasses.next(); 320 Node subClass = t.getSubject(); 321 if (!subClass.equals(TransitiveReasoner.subClassOf)) { 322 scCache.cacheAll(graph, subClass); 323 } 324 } 325 326 return foundAny; 327 } 328 329 340 public static boolean checkOccuranceUtility(Node prop, Graph graph, TransitiveGraphCache spCache) { 341 boolean foundOne = false; 342 ExtendedIterator uses = graph.find( null, prop, null ); 343 foundOne = uses.hasNext(); 344 uses.close(); 345 if (foundOne) return foundOne; 346 347 ExtendedIterator propVariants 348 = spCache.find(new TriplePattern(null, TransitiveReasoner.subPropertyOf, prop)); 349 while (propVariants.hasNext() && !foundOne) { 350 Triple t = (Triple)propVariants.next(); 351 Node propVariant = t.getSubject(); 352 uses = graph.find( null, propVariant, null ); 353 foundOne = uses.hasNext(); 354 uses.close(); 355 } 356 propVariants.close(); 357 return foundOne; 358 } 359 360 369 public static boolean cacheSubPropUtility(Finder graph, TransitiveGraphCache spCache) { 370 if (graph == null) return false; 371 372 spCache.cacheAll(graph, TransitiveReasoner.subPropertyOf); 373 374 boolean foundAny = false; 379 boolean foundMore = false; 380 HashSet cached = new HashSet(); 381 do { 382 ExtendedIterator subProps 383 = spCache.find(new TriplePattern(null, TransitiveReasoner.subPropertyOf, TransitiveReasoner.subPropertyOf)); 384 while (subProps.hasNext()) { 385 foundMore = false; 386 Triple t = (Triple)subProps.next(); 387 Node subProp = t.getSubject(); 388 if (!subProp.equals(TransitiveReasoner.subPropertyOf) && !cached.contains(subProp)) { 389 foundAny = true; 390 cached.add(subProp); 391 spCache.cacheAll(graph, subProp); 392 foundMore = true; 393 } 394 } 395 } while (foundMore); 396 397 return foundAny; 398 } 399 400 } 401 402 403 404 | Popular Tags |