1 10 package com.hp.hpl.jena.reasoner.rulesys; 11 12 import java.util.*; 13 14 import com.hp.hpl.jena.graph.*; 15 import com.hp.hpl.jena.rdf.model.*; 16 import com.hp.hpl.jena.reasoner.*; 17 import com.hp.hpl.jena.reasoner.rulesys.impl.RDFSCMPPreprocessHook; 18 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary; 19 20 29 public class RDFSRuleReasoner extends GenericRuleReasoner { 30 31 32 public static final String DEFAULT_RULES = "default"; 33 34 35 public static final String FULL_RULES = "full"; 36 37 38 public static final String SIMPLE_RULES = "simple"; 39 40 41 protected static final String RULE_FILE = "etc/rdfs-fb-tgc-noresource.rules"; 42 43 44 protected static final String FULL_RULE_FILE = "etc/rdfs-fb-tgc.rules"; 45 46 47 protected static final String SIMPLE_RULE_FILE = "etc/rdfs-fb-tgc-simple.rules"; 48 49 50 protected static HashMap ruleSets = new HashMap(); 51 52 53 protected static HashMap ruleFiles; 54 55 56 protected static RulePreprocessHook cmpProcessor = new RDFSCMPPreprocessHook(); 57 58 static { 59 ruleFiles = new HashMap(); 60 ruleFiles.put(DEFAULT_RULES, RULE_FILE); 61 ruleFiles.put(FULL_RULES, FULL_RULE_FILE); 62 ruleFiles.put(SIMPLE_RULES, SIMPLE_RULE_FILE); 63 } 64 65 68 public RDFSRuleReasoner(ReasonerFactory parent) { 69 super(loadRulesLevel(DEFAULT_RULES), parent); 70 setMode(HYBRID); 71 setTransitiveClosureCaching(true); 72 } 74 75 80 public RDFSRuleReasoner(ReasonerFactory factory, Resource configuration) { 81 this(factory); 82 if (configuration != null) { 83 StmtIterator i = configuration.listProperties(); 84 while (i.hasNext()) { 85 Statement st = i.nextStatement(); 86 doSetParameter(st.getPredicate(), st.getObject().toString()); 87 } 88 } 89 } 90 91 95 protected RDFSRuleReasoner(FBRuleInfGraph schemaGraph, ReasonerFactory factory) { 96 super(schemaGraph.getRules(), factory); 97 this.schemaGraph = schemaGraph; 98 } 99 100 105 protected boolean doSetParameter(Property parameter, Object value) { 106 if (parameter.equals(ReasonerVocabulary.PROPenableCMPScan)) { 107 boolean scanProperties = Util.convertBooleanPredicateArg(parameter, value); 108 if (scanProperties) { 109 addPreprocessingHook(cmpProcessor); 110 } else { 111 removePreprocessingHook(cmpProcessor); 112 } 113 return true; 114 } else if (parameter.equals(ReasonerVocabulary.PROPsetRDFSLevel)) { 115 String level = ((String )value).toLowerCase(); 116 setRules(loadRulesLevel(level)); 117 if (level.equals(FULL_RULES)) { 118 addPreprocessingHook(cmpProcessor); 119 } else { 120 removePreprocessingHook(cmpProcessor); 121 } 122 return true; 123 } else { 124 return super.doSetParameter(parameter, value); 125 } 126 } 127 128 139 public InfGraph bind(Graph data) throws ReasonerException { 140 Graph schemaArg = schemaGraph == null ? getPreload() : schemaGraph; 141 InfGraph graph = null; 142 List ruleSet = ((FBRuleInfGraph)schemaArg).getRules(); 143 FBRuleInfGraph fbgraph = new RDFSRuleInfGraph(this, ruleSet, schemaArg); 144 graph = fbgraph; 145 if (enableTGCCaching) fbgraph.setUseTGCCache(); 146 fbgraph.setTraceOn(traceOn); 147 if (preprocessorHooks!= null) { 148 for (Iterator i = preprocessorHooks.iterator(); i.hasNext(); ) { 149 fbgraph.addPreprocessingHook((RulePreprocessHook)i.next()); 150 } 151 } 152 graph.setDerivationLogging(recordDerivations); 153 graph.rebind(data); 154 return graph; 155 } 156 157 161 public Reasoner bindSchema(Graph tbox) throws ReasonerException { 162 if (schemaGraph != null) { 163 throw new ReasonerException("Can only bind one schema at a time to an RDFSRuleReasoner"); 164 } 165 FBRuleInfGraph graph = new FBRuleInfGraph(this, rules, getPreload(), tbox); 166 if (enableTGCCaching) ((FBRuleInfGraph)graph).setUseTGCCache(); 167 graph.prepare(); 168 RDFSRuleReasoner grr = new RDFSRuleReasoner(graph, factory); 169 grr.setDerivationLogging(recordDerivations); 170 grr.setTraceOn(traceOn); 171 grr.setTransitiveClosureCaching(enableTGCCaching); 172 grr.setFunctorFiltering(filterFunctors); 173 if (preprocessorHooks != null) { 174 for (Iterator i = preprocessorHooks.iterator(); i.hasNext(); ) { 175 grr.addPreprocessingHook((RulePreprocessHook)i.next()); 176 } 177 } 178 return grr; 179 } 180 181 185 public static List loadRulesLevel(String level) { 186 List ruleSet = (List)ruleSets.get(level); 187 if (ruleSet == null) { 188 String file = (String )ruleFiles.get(level); 189 if (file == null) { 190 throw new ReasonerException("Illegal RDFS conformance level: " + level); 191 } 192 ruleSet = loadRules( file ); 193 ruleSets.put(level, ruleSet); 194 } 195 return ruleSet; 196 } 197 198 202 public Capabilities getGraphCapabilities() { 203 if (capabilities == null) { 204 capabilities = new BaseInfGraph.InfFindSafeCapabilities(); 205 } 206 return capabilities; 207 } 208 209 } 210 211 | Popular Tags |