1 10 package com.hp.hpl.jena.reasoner.rdfsReasoner1; 11 12 import com.hp.hpl.jena.reasoner.*; 13 import com.hp.hpl.jena.graph.*; 14 import com.hp.hpl.jena.vocabulary.*; 15 import com.hp.hpl.jena.util.iterator.*; 16 17 import java.util.*; 18 19 31 public class BRWRule { 32 33 34 protected TriplePattern head; 35 36 37 protected TriplePattern body; 38 39 40 43 public BRWRule(TriplePattern head, TriplePattern body) { 44 this.head = head; 45 this.body = body; 46 } 47 48 58 public static BRWRule makeRule(String rulespec) { 59 StringTokenizer tokenizer = new StringTokenizer(rulespec); 60 try { 61 Node headS = parseNode(tokenizer.nextToken()); 62 Node headP = parseNode(tokenizer.nextToken()); 63 Node headO = parseNode(tokenizer.nextToken()); 64 TriplePattern head = new TriplePattern(headS, headP, headO); 65 if (!tokenizer.nextToken().equals("<-")) 66 throw new NoSuchElementException(); 67 Node bodyS = parseNode(tokenizer.nextToken()); 68 Node bodyP = parseNode(tokenizer.nextToken()); 69 Node bodyO = parseNode(tokenizer.nextToken()); 70 TriplePattern body = new TriplePattern(bodyS, bodyP, bodyO); 71 return new BRWRule(head, body); 72 } catch (NoSuchElementException e) { 73 throw new ReasonerException("Illegal BRWRule: " + rulespec); 74 } 75 } 76 77 89 public ExtendedIterator execute(TriplePattern query, InfGraph infGraph, Finder data, HashSet firedRules) { 90 TriplePattern iBody = instantiate(body, query); 91 BRWRule iRule = new BRWRule(head, iBody); 92 if (firedRules.contains(iRule)) { 93 return NullIterator.instance; 95 } 96 firedRules.add(iRule); 97 Iterator it = ((RDFSInfGraph) infGraph).findNested(iBody, data, firedRules); 98 firedRules.remove(iRule); 99 return new RewriteIterator(it, iRule); 100 } 101 102 106 public boolean completeFor(TriplePattern query) { 107 return false; 108 } 109 110 113 protected static TriplePattern instantiate(TriplePattern pattern, TriplePattern query) { 114 return new TriplePattern( instantiate(pattern.getSubject(), query), 115 instantiate(pattern.getPredicate(), query), 116 instantiate(pattern.getObject(), query) ); 117 } 118 119 122 protected static Node instantiate(Node elt, TriplePattern query) { 123 if (elt.isVariable()) { 124 String var = elt.getName(); if (var.equals("s")) return query.getSubject(); 126 if (var.equals("p")) return query.getPredicate(); 127 if (var.equals("o")) return query.getObject(); 128 } 129 return elt; 130 } 131 132 135 protected static Node instantiate(Node elt, Triple value) { 136 if (elt.isVariable()) { 137 String var = elt.getName(); if (var.equals("s")) return value.getSubject(); 139 if (var.equals("p")) return value.getPredicate(); 140 if (var.equals("o")) return value.getObject(); 141 } 142 return elt; 143 } 144 145 148 public static Node parseNode(String token) { 149 if (token.startsWith("?")) { 150 return Node.createVariable(token.substring(1)); 151 } else if (token.equals("_")) { 152 return Node.createVariable("*"); 153 } else if (token.indexOf(':') != -1) { 154 int split = token.indexOf(':'); 155 String nsPrefix = token.substring(0, split); 156 String localname = token.substring(split+1); 157 if (nsPrefix.equalsIgnoreCase("rdf")) { 158 return Node.createURI(RDF.getURI() + localname); 159 } else if (nsPrefix.equalsIgnoreCase("rdfs")) { 160 return Node.createURI(RDFS.getURI() + localname); 161 } else { 162 return Node.createURI(token); 163 } 164 } else { 165 return Node.createURI(token); 166 } 167 } 168 169 172 public String toString() { 173 return head.toString() + " <- " + body.toString(); 174 } 175 176 177 181 public TriplePattern getBody() { 182 return body; 183 } 184 185 189 public TriplePattern getHead() { 190 return head; 191 } 192 193 194 public boolean equals(Object o) { 195 return o instanceof BRWRule && 196 head.equals(((BRWRule)o).head) && 197 body.equals(((BRWRule)o).body) ; 198 } 199 200 201 public int hashCode() { 202 return (head.hashCode() >> 1) ^ body.hashCode(); 203 } 204 205 209 static class RewriteIterator extends WrappedIterator { 210 211 TriplePattern head; 212 213 218 public RewriteIterator(Iterator underlying, BRWRule rule) { 219 super(underlying); 220 this.head = rule.head; 221 } 222 223 226 public Object next() { 227 Triple value = (Triple)super.next(); 228 return new Triple( instantiate(head.getSubject(), value), 229 instantiate(head.getPredicate(), value), 230 instantiate(head.getObject(), value) ); 231 } 232 } 233 234 } 235 236 265 | Popular Tags |