1 10 package com.hp.hpl.jena.reasoner.rdfsReasoner1; 11 12 import com.hp.hpl.jena.reasoner.Finder; 13 import com.hp.hpl.jena.reasoner.InfGraph; 14 import com.hp.hpl.jena.reasoner.ReasonerException; 15 import com.hp.hpl.jena.reasoner.TriplePattern; 16 import com.hp.hpl.jena.reasoner.transitiveReasoner.TransitiveGraphCache; 17 import com.hp.hpl.jena.util.iterator.ExtendedIterator; 18 import com.hp.hpl.jena.graph.*; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import java.util.*; 24 25 41 public class PatternRouter { 42 43 protected static Log logger = LogFactory.getLog(PatternRouter.class); 44 45 46 47 Map patternIndex = new HashMap(); 48 49 52 public PatternRouter() { 53 } 54 55 61 public void register(TransitiveGraphCache cache) { 62 register(new TriplePattern(null, cache.getClosedPredicate(), null), cache); 63 register(new TriplePattern(null, cache.getDirectPredicate(), null), cache); 64 } 65 66 69 public void register(BRWRule rule) { 70 register(rule.getHead(), rule); 71 } 72 73 76 public void register(TriplePattern pattern, Object satisfier) { 77 PatternEntry entry = new PatternEntry(pattern, satisfier); 78 Node predicate = pattern.getPredicate(); 79 if (predicate.isVariable()) { 80 throw new ReasonerException("PatternRouter can't handle non-ground predicates in patterns: " + pattern); 81 } 82 HashSet sats = (HashSet)patternIndex.get(predicate); 83 if (sats == null) { 84 sats = new HashSet(); 85 patternIndex.put(predicate, sats); 86 } 87 sats.add(entry); 88 } 89 90 100 public ExtendedIterator find(TriplePattern pattern, Finder tripleCache, Finder data, InfGraph infGraph) { 101 return find(pattern, tripleCache, data, infGraph, new HashSet()); 102 } 103 104 115 public ExtendedIterator find(TriplePattern pattern, Finder tripleCache, Finder data, InfGraph infGraph, HashSet firedRules) { 116 ExtendedIterator result = tripleCache.findWithContinuation(pattern, data); 117 Node predicate = pattern.getPredicate(); 118 if (predicate.isVariable()) { 119 for (Iterator i = patternIndex.values().iterator(); i.hasNext();) { 121 HashSet sats = (HashSet)i.next(); 122 result = doFind(sats, result, pattern, tripleCache, data, infGraph, firedRules); 123 } 124 return result; 125 } else { 126 HashSet sats = (HashSet)patternIndex.get(predicate); 127 return doFind(sats, result, pattern, tripleCache, data, infGraph, firedRules); 128 } 129 } 130 131 144 private ExtendedIterator doFind(HashSet rules, ExtendedIterator result, 145 TriplePattern pattern, Finder tripleCache, 146 Finder data, InfGraph infGraph, HashSet firedRules) { 147 if (rules != null) { 148 for (Iterator i = rules.iterator(); i.hasNext(); ) { 150 PatternEntry entry = (PatternEntry) i.next(); 151 if (entry.completeFor(pattern)) { 152 return entry.fire(pattern, data, infGraph, firedRules); 153 } 154 } 155 for (Iterator i = rules.iterator(); i.hasNext(); ) { 157 PatternEntry entry = (PatternEntry) i.next(); 158 if (entry.shouldFire(pattern)) { 159 result = result.andThen(entry.fire(pattern, data, infGraph, firedRules)); 160 } 161 } 162 } 163 return result; 164 } 165 166 170 public String toString() { 171 StringBuffer state = new StringBuffer (); 172 for (Iterator i = patternIndex.values().iterator(); i.hasNext(); ) { 173 HashSet ents = (HashSet)i.next(); 174 for (Iterator j = ents.iterator(); j.hasNext(); ) { 175 state.append(j.next().toString()); 176 state.append("\n"); 177 } 178 } 179 return state.toString(); 180 } 181 182 185 static class PatternEntry { 186 187 TriplePattern pattern; 188 189 190 Object action; 191 192 193 PatternEntry(TriplePattern pattern, Object action) { 194 this.pattern = pattern; 195 this.action = action; 196 } 197 198 202 public boolean completeFor(TriplePattern query) { 203 if (action instanceof BRWRule) { 204 return ((BRWRule)action).completeFor(query); 205 } else if (action instanceof TransitiveGraphCache) { 206 TransitiveGraphCache tgc = (TransitiveGraphCache)action; 207 Node prop = query.getPredicate(); 208 return prop.equals(tgc.getDirectPredicate()) || 209 prop.equals(tgc.getClosedPredicate()); 210 } 211 return false; 212 } 213 214 215 boolean shouldFire(TriplePattern query) { 216 return pattern.compatibleWith(query); 217 } 218 219 226 public ExtendedIterator fire(TriplePattern query, Finder data, InfGraph infGraph, HashSet firedRules) { 227 TriplePattern nquery = query; 228 if (nquery.getPredicate().isVariable()) { 229 nquery = new TriplePattern(query.getSubject(), pattern.getPredicate(), query.getObject()); 230 } 231 if (action instanceof TransitiveGraphCache) { 232 return ((TransitiveGraphCache)action).find(nquery); 233 } else if (action instanceof BRWRule) { 234 logger.debug("Fire rule: " + action); 235 return ((BRWRule)action).execute(nquery, infGraph, data, firedRules); 236 } else { 237 throw new ReasonerException("Illegal router action entry"); 238 } 239 } 240 241 242 public String toString() { 243 return pattern.toString() + " <- " + action; 244 } 245 246 247 public boolean equals(Object o) { 248 return o instanceof PatternEntry && 249 pattern.equals(((PatternEntry)o).pattern) && 250 action.equals(((PatternEntry)o).action) ; 251 } 252 253 254 public int hashCode() { 255 return (pattern.hashCode() >> 1) ^ action.hashCode(); 256 } 257 258 } 259 260 } 261 262 291 | Popular Tags |