1 10 package com.hp.hpl.jena.reasoner.rulesys.impl; 11 12 import com.hp.hpl.jena.reasoner.rulesys.*; 13 import com.hp.hpl.jena.reasoner.*; 14 import com.hp.hpl.jena.graph.*; 15 import com.hp.hpl.jena.vocabulary.*; 16 17 import java.util.*; 18 19 27 public class OWLRuleTranslationHook implements RulePreprocessHook { 28 29 40 public void run(FBRuleInfGraph infGraph, Finder dataFind, Graph inserts) { 41 Iterator it = dataFind.find(new TriplePattern(null, OWL.intersectionOf.asNode(), null)); 42 while (it.hasNext()) { 43 Triple decl = (Triple)it.next(); 44 Node className = decl.getSubject(); 45 List elements = new ArrayList(); 46 translateIntersectionList(decl.getObject(), dataFind, elements); 47 List recognitionBody = new ArrayList(); 49 Node var = new Node_RuleVariable("?x", 0); 50 for (Iterator i = elements.iterator(); i.hasNext(); ) { 51 Node description = (Node)i.next(); 52 Rule ir = new Rule("intersectionImplication", new ClauseEntry[] { 54 new TriplePattern(className, RDFS.subClassOf.asNode(), description) 55 }, new ClauseEntry[0]); 56 ir.setBackward(false); 57 infGraph.addRuleDuringPrepare(ir); 59 recognitionBody.add(new TriplePattern(var, RDF.type.asNode(), description)); 61 } 62 List recognitionHead = new ArrayList(1); 63 recognitionHead.add(new TriplePattern(var, RDF.type.asNode(), className)); 64 Rule rr = new Rule("intersectionRecognition", recognitionHead, recognitionBody); 65 rr.setBackward(true); 66 infGraph.addRuleDuringPrepare(rr); 68 } 69 } 70 71 78 protected static void translateIntersectionList(Node node, Finder dataFind, List elements) { 79 if (node == null) { 80 throw new ReasonerException("Illegal list structure in owl:intersectionOf"); 81 } 82 if (node.equals(RDF.nil.asNode())) { 83 return; } 85 Node description = Util.getPropValue(node, RDF.first.asNode(), dataFind); 86 if (description == null) { 87 throw new ReasonerException("Illegal list structure in owl:intersectionOf"); 88 } 89 116 elements.add(description); 119 Node next = Util.getPropValue(node, RDF.rest.asNode(), dataFind); 121 translateIntersectionList(next, dataFind, elements); 122 } 123 124 } 125 126 127 128 | Popular Tags |