1 10 package com.hp.hpl.jena.reasoner.rulesys.impl; 11 12 import com.hp.hpl.jena.reasoner.TriplePattern; 13 import com.hp.hpl.jena.reasoner.rulesys.*; 14 import com.hp.hpl.jena.graph.*; 15 16 import java.util.*; 17 18 25 public class LPRuleStore extends RuleStore { 26 27 28 protected boolean isCompiled = false; 29 30 33 protected Map predicateToCodeMap; 34 35 36 protected ArrayList allRuleClauseCodes; 37 38 39 protected Map indexPredicateToCodeMap; 40 41 42 protected HashSet tabledPredicates = new HashSet(); 43 44 45 private static final int INDEX_THRESHOLD = 20; 46 47 48 protected boolean allTabled = false; 49 50 54 public LPRuleStore(List rules) { 55 super(rules); 56 } 57 58 61 public LPRuleStore() { 62 super(); 63 } 64 65 68 public void addAll(LPRuleStore store) { 69 super.addAll(store); 70 tabledPredicates.addAll(store.tabledPredicates); 71 allTabled = tabledPredicates.contains(Node.ANY); 72 } 73 74 78 public synchronized void tablePredicate(Node predicate) { 79 tabledPredicates.add(predicate); 80 if (predicate == Node.ANY) allTabled = true; 81 } 82 83 88 public List codeFor(Node predicate) { 89 if (!isCompiled) { 90 compileAll(); 91 } 92 if (predicate.isVariable()) { 93 return allRuleClauseCodes; 94 } else { 95 List codeList = (List) predicateToCodeMap.get(predicate); 96 if (codeList == null) { 97 codeList = (List) predicateToCodeMap.get(Node_RuleVariable.WILD); 99 } 100 return codeList; 101 } 102 } 103 104 109 public List codeFor(TriplePattern goal) { 110 List allRules = codeFor(goal.getPredicate()); 111 if (allRules == null) { 112 return allRules; 113 } 114 Map indexedCodeTable = (Map) indexPredicateToCodeMap.get(goal.getPredicate()); 115 if (indexedCodeTable != null) { 116 List indexedCode = (List) indexedCodeTable.get(goal.getObject()); 117 if (indexedCode != null) { 118 return indexedCode; 119 } 120 } 121 return allRules; 122 } 123 124 127 public boolean isIndexedPredicate(Node predicate) { 128 return (indexPredicateToCodeMap.get(predicate) != null); 129 } 130 131 136 public boolean isTabled(TriplePattern goal) { 137 return isTabled(goal.getPredicate()); 138 } 139 140 145 public boolean isTabled(Node predicate) { 146 if (allTabled) return true; 147 if (predicate.isVariable() && !tabledPredicates.isEmpty()) { 148 return true; 149 } else { 150 return tabledPredicates.contains(predicate); 151 } 152 } 153 154 158 protected void compileAll() { 159 isCompiled = true; 160 161 predicateToCodeMap = new HashMap(); 162 allRuleClauseCodes = new ArrayList(); 163 indexPredicateToCodeMap = new HashMap(); 164 for (Iterator ri = getAllRules().iterator(); ri.hasNext(); ) { 165 Rule r = (Rule)ri.next(); 166 ClauseEntry term = r.getHeadElement(0); 167 if (term instanceof TriplePattern) { 168 RuleClauseCode code = new RuleClauseCode(r); 169 allRuleClauseCodes.add(code); 170 Node predicate = ((TriplePattern)term).getPredicate(); 171 if (predicate.isVariable()) { 172 predicate = Node_RuleVariable.WILD; 173 } 174 List predicateCode = (List)predicateToCodeMap.get(predicate); 175 if (predicateCode == null) { 176 predicateCode = new ArrayList(); 177 predicateToCodeMap.put(predicate, predicateCode); 178 } 179 predicateCode.add(code); 180 if (predicateCode.size() > INDEX_THRESHOLD) { 181 indexPredicateToCodeMap.put(predicate, new HashMap()); 182 } 183 } 184 } 185 186 List wildRules = (List) predicateToCodeMap.get(Node_RuleVariable.WILD); 188 if (wildRules != null) { 189 for (Iterator i = predicateToCodeMap.entrySet().iterator(); i.hasNext(); ) { 190 Map.Entry entry = (Map.Entry)i.next(); 191 Node predicate = (Node)entry.getKey(); 192 List predicateCode = (List)entry.getValue(); 193 if (predicate != Node_RuleVariable.WILD) { 194 predicateCode.addAll(wildRules); 195 } 196 } 197 } 198 indexPredicateToCodeMap.put(Node_RuleVariable.WILD, new HashMap()); 199 200 for (Iterator i = indexPredicateToCodeMap.entrySet().iterator(); i.hasNext(); ) { 202 Map.Entry entry = (Map.Entry)i.next(); 203 Node predicate = (Node) entry.getKey(); 204 HashMap predicateMap = (HashMap) entry.getValue(); 205 List wildRulesForPredicate = new ArrayList(); 206 List allRulesForPredicate = predicate.isVariable() ? allRuleClauseCodes : (List) predicateToCodeMap.get(predicate); 207 for (Iterator j = allRulesForPredicate.iterator(); j.hasNext(); ) { 208 RuleClauseCode code = (RuleClauseCode)j.next(); 209 ClauseEntry head = code.getRule().getHeadElement(0); 210 boolean indexed = false; 211 if (head instanceof TriplePattern) { 212 Node objectPattern = ((TriplePattern)head).getObject(); 213 if (!objectPattern.isVariable() && !Functor.isFunctor(objectPattern)) { 214 List indexedCode = (List) predicateMap.get(objectPattern); 216 if (indexedCode == null) { 217 indexedCode = new ArrayList(); 218 predicateMap.put(objectPattern, indexedCode); 219 } 220 indexedCode.add(code); 221 indexed = true; 222 } 223 } 224 if (!indexed) { 225 wildRulesForPredicate.add(code); 226 } 227 } 228 for (Iterator k = predicateMap.entrySet().iterator(); k.hasNext(); ) { 230 Map.Entry ent = (Map.Entry)k.next(); 231 Node pred = (Node)ent.getKey(); 232 List predicateCode = (List)ent.getValue(); 233 predicateCode.addAll(wildRulesForPredicate); 234 } 235 } 236 237 for (Iterator i = allRuleClauseCodes.iterator(); i.hasNext(); ) { 239 RuleClauseCode code = (RuleClauseCode)i.next(); 240 code.compile(this); 241 } 242 } 243 244 251 protected void doAddRemoveRule(Rule rule, boolean isAdd) { 252 isCompiled = false; 253 super.doAddRemoveRule(rule, isAdd); 254 } 255 256 } 257 258 259 | Popular Tags |