1 10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode; 11 12 import java.util.*; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 16 import com.hp.hpl.jena.reasoner.*; 17 import com.hp.hpl.jena.reasoner.rulesys.BasicForwardRuleInfGraph; 18 import com.hp.hpl.jena.reasoner.rulesys.BasicForwardRuleReasoner; 19 import com.hp.hpl.jena.reasoner.rulesys.ClauseEntry; 20 import com.hp.hpl.jena.reasoner.rulesys.Functor; 21 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable; 22 import com.hp.hpl.jena.reasoner.rulesys.Rule; 23 import com.hp.hpl.jena.reasoner.rulesys.Util; 24 import com.hp.hpl.jena.shared.WrappedIOException; 25 import com.hp.hpl.jena.vocabulary.*; 26 import com.hp.hpl.jena.graph.*; 27 28 42 public class OWLRuleReasoner extends BasicForwardRuleReasoner { 43 44 45 protected static final String RULE_FILE = "etc/owl.rules"; 46 47 48 protected static List ruleSet; 49 50 protected static Log logger = LogFactory.getLog(OWLRuleReasoner.class); 51 52 53 protected static long nRulesFired = 0; 54 55 56 protected static long timeCost = 0; 57 58 61 public OWLRuleReasoner() { 62 super(loadRules(), OWLRuleReasonerFactory.theInstance()); 63 64 } 65 66 70 private OWLRuleReasoner(List rules, InfGraph schemaGraph) { 71 super(rules, OWLRuleReasonerFactory.theInstance()); 72 this.rules = rules; 73 this.schemaGraph = schemaGraph; 74 } 75 76 79 public static List loadRules() { 80 if (ruleSet == null) { 81 try { 82 ruleSet = Rule.parseRules(Util.loadRuleParserFromResourceFile(RULE_FILE)); 83 } catch (WrappedIOException e) { 84 throw new ReasonerException("Can't load rules file: " + RULE_FILE, e); 85 } 86 } 87 return ruleSet; 88 } 89 90 94 public Reasoner bindSchema(Graph tbox) throws ReasonerException { 95 InfGraph graph = new BasicForwardRuleInfGraph(this, rules, tbox); 96 Iterator i = tbox.find(null, OWL.intersectionOf.asNode(), null); 99 ArrayList rules = (ArrayList) ruleSet; 100 if (i.hasNext()) { 101 rules = (ArrayList) rules.clone(); 102 while(i.hasNext()) { 103 translateIntersectionOf((Triple)i.next(), rules, tbox); 104 } 105 } 106 return new OWLRuleReasoner(rules, graph); 107 } 108 109 120 public InfGraph bind(Graph data) throws ReasonerException { 121 long startTime = System.currentTimeMillis(); 124 Iterator i = data.find(null, OWL.intersectionOf.asNode(), null); 125 ArrayList rules = (ArrayList) ruleSet; 126 if (i.hasNext()) { 127 rules = (ArrayList) rules.clone(); 128 while(i.hasNext()) { 129 translateIntersectionOf((Triple)i.next(), rules, data); 130 } 131 } 132 133 BasicForwardRuleInfGraph graph = new BasicForwardRuleInfGraph(this, rules, schemaGraph); 135 graph.setDerivationLogging(recordDerivations); 136 graph.setTraceOn(traceOn); 137 graph.rebind(data); 138 long endTime = System.currentTimeMillis(); 139 timeCost += (double)(endTime - startTime); 140 nRulesFired += graph.getNRulesFired(); 141 142 return graph; 143 } 144 145 150 public static void printStats() { 151 logger.info("Fired " + nRulesFired + " over " + (timeCost/1000.0) + " s = " 152 + (nRulesFired*1000/timeCost) + " r/s"); 153 } 154 155 163 private void translateIntersectionOf(Triple decl, List rules, Graph data) { 164 Node className = decl.getSubject(); 165 List elements = new ArrayList(); 166 translateIntersectionList(decl.getObject(), data, elements); 167 List recognitionBody = new ArrayList(); 169 Node var = new Node_RuleVariable("?x", 0); 170 for (Iterator i = elements.iterator(); i.hasNext(); ) { 171 Node description = (Node)i.next(); 172 Rule ir = new Rule("intersectionImplication", new ClauseEntry[] { 174 new TriplePattern(className, RDFS.subClassOf.asNode(), description) 175 }, new ClauseEntry[0]); 176 rules.add(ir); 177 recognitionBody.add(new TriplePattern(var, RDF.type.asNode(), description)); 180 } 181 List recognitionHead = new ArrayList(1); 182 recognitionHead.add(new TriplePattern(var, RDF.type.asNode(), className)); 183 Rule rr = new Rule("intersectionRecognition", recognitionHead, recognitionBody); 184 rules.add(rr); 186 } 187 188 195 private void translateIntersectionList(Node node, Graph data, List elements) { 196 if (node.equals(RDF.nil.asNode())) { 197 return; } 199 Node description = Util.getPropValue(node, RDF.first.asNode(), data); 200 if (data.contains(description, RDF.type.asNode(), OWL.Restriction.asNode())) { 202 Node onprop = Util.getPropValue(description, OWL.onProperty.asNode(), data); 204 Node value; 205 if ((value = Util.getPropValue(description, OWL.allValuesFrom.asNode(), data)) != null) { 206 elements.add(Functor.makeFunctorNode("all", new Node[] {onprop, value})); 207 } else if ((value = Util.getPropValue(description, OWL.someValuesFrom.asNode(), data)) != null) { 208 elements.add(Functor.makeFunctorNode("some", new Node[] {onprop, value})); 209 } else if ((value = Util.getPropValue(description, OWL.minCardinality.asNode(), data)) != null) { 210 elements.add(Functor.makeFunctorNode("min", new Node[] {onprop, value})); 211 } else if ((value = Util.getPropValue(description, OWL.maxCardinality.asNode(), data)) != null) { 212 elements.add(Functor.makeFunctorNode("max", new Node[] {onprop, value})); 213 } else if ((value = Util.getPropValue(description, OWL.cardinality.asNode(), data)) != null) { 214 elements.add(Functor.makeFunctorNode("max", new Node[] {onprop, value})); 215 elements.add(Functor.makeFunctorNode("min", new Node[] {onprop, value})); 216 } 217 } else { 218 elements.add(description); 220 } 221 Node next = Util.getPropValue(node, RDF.rest.asNode(), data); 223 translateIntersectionList(next, data, elements); 224 } 225 226 } 227 228 257 | Popular Tags |