1 10 package com.hp.hpl.jena.reasoner; 11 12 import java.util.Map ; 13 14 import com.hp.hpl.jena.graph.*; 15 import com.hp.hpl.jena.reasoner.rulesys.ClauseEntry; 16 import com.hp.hpl.jena.reasoner.rulesys.Functor; 17 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable; 18 import com.hp.hpl.jena.util.CollectionFactory; 19 import com.hp.hpl.jena.vocabulary.RDF; 20 import com.hp.hpl.jena.vocabulary.RDFS; 21 22 38 public class TriplePattern implements ClauseEntry { 39 40 41 protected Node subject; 42 43 44 protected Node predicate; 45 46 47 protected Node object; 48 49 55 public TriplePattern(Node subject, Node predicate, Node object) { 56 this.subject = normalize(subject); 57 this.predicate = normalize(predicate); 58 this.object = normalize(object); 59 } 60 61 67 public TriplePattern(TripleMatch match) { 68 this.subject = normalize(match.getMatchSubject()); 69 this.predicate = normalize(match.getMatchPredicate()); 70 this.object = normalize(match.getMatchObject()); 71 } 72 73 77 public TriplePattern(Triple match) { 78 this.subject = normalize(match.getSubject()); 79 this.predicate = normalize(match.getPredicate()); 80 this.object = normalize(match.getObject()); 81 } 82 83 87 public Node getObject() { 88 return object; 89 } 90 91 95 public Node getPredicate() { 96 return predicate; 97 } 98 99 103 public Node getSubject() { 104 return subject; 105 } 106 107 110 public TripleMatch asTripleMatch() { 111 return Triple.createMatch(toMatch(subject), 112 toMatch(predicate), 113 toMatch(object)); 114 } 115 116 119 public Triple asTriple() { 120 return new Triple(subject,predicate, object); 121 } 122 123 132 public boolean compatibleWith(TriplePattern pattern) { 133 boolean ok = subject.isVariable() || pattern.subject.isVariable() || subject.equals(pattern.subject); 134 if (!ok) return false; 135 ok = predicate.isVariable() || pattern.predicate.isVariable() || predicate.equals(pattern.predicate); 136 if (!ok) return false; 137 if (object.isVariable() || pattern.object.isVariable()) return true; 138 if (Functor.isFunctor(object) && Functor.isFunctor(pattern.object)) { 140 Functor functor = (Functor)object.getLiteral().getValue(); 141 Functor pFunctor = (Functor)pattern.object.getLiteral().getValue(); 142 return (functor.getName().equals(pFunctor.getName()) 143 && functor.getArgs().length == pFunctor.getArgs().length); 144 } else { 145 return object.sameValueAs(pattern.object); 146 } 147 } 148 149 154 public boolean variantOf(TriplePattern pattern) { 155 Map vmap = CollectionFactory.createHashedMap(); 156 if ( ! variantOf(subject, pattern.subject, vmap) ) return false; 157 if ( ! variantOf(predicate, pattern.predicate, vmap) ) return false; 158 if (Functor.isFunctor(object) && Functor.isFunctor(pattern.object)) { 159 Functor functor = (Functor)object.getLiteral().getValue(); 160 Functor pFunctor = (Functor)pattern.object.getLiteral().getValue(); 161 if ( ! functor.getName().equals(pFunctor.getName()) ) return false; 162 Node[] args = functor.getArgs(); 163 Node[] pargs = pFunctor.getArgs(); 164 if ( args.length != pargs.length ) return false; 165 for (int i = 0; i < args.length; i++) { 166 if ( ! variantOf(args[i], pargs[i], vmap) ) return false; 167 } 168 return true; 169 } else { 170 return variantOf(object, pattern.object, vmap); 171 } 172 } 173 174 177 private boolean variantOf(Node n, Node p, Map vmap) { 178 if (n instanceof Node_RuleVariable) { 179 if (p instanceof Node_RuleVariable) { 180 Object nMatch = vmap.get(n); 181 if (nMatch == null) { 182 vmap.put(n, p); 184 return true; 185 } else { 186 return nMatch == p; 187 } 188 } else { 189 return false; 190 } 191 } else { 192 return n.sameValueAs(p); 193 } 194 } 195 196 201 public boolean isLegal() { 202 if (subject.isLiteral() || predicate.isLiteral()) return false; 203 if (Functor.isFunctor(subject)) return false; 204 if (Functor.isFunctor(object)) { 205 Node[] args = ((Functor)object.getLiteral().getValue()).getArgs(); 206 for (int i = 0; i < args.length; i++) { 207 if (Functor.isFunctor(args[i])) return false; 208 } 209 } 210 return true; 211 } 212 213 218 public boolean subsumes(TriplePattern arg) { 219 return (subject.isVariable() || subject.equals(arg.subject)) 220 && (predicate.isVariable() || predicate.equals(arg.predicate)) 221 && (object.isVariable() || object.equals(arg.object)); 222 } 223 224 227 public boolean isGround() { 228 if (subject.isVariable() || predicate.isVariable() || object.isVariable()) return false; 229 if (Functor.isFunctor(object)) { 230 return ((Functor)object.getLiteral().getValue()).isGround(); 231 } 232 return true; 233 } 234 235 238 public String toString() { 239 return simplePrintString(subject) + 240 " @" + simplePrintString(predicate) + 241 " " + simplePrintString(object); 242 } 243 244 247 public static String simplePrintString(Triple t) { 248 return simplePrintString(t.getSubject()) + 249 " @" + simplePrintString(t.getPredicate()) + 250 " " + simplePrintString(t.getObject()); 251 } 252 253 256 public static String simplePrintString(Node n) { 257 if (n instanceof Node_URI) { 258 String uri = n.getURI(); 259 int split = uri.lastIndexOf('#'); 260 if (split == -1) { 261 split = uri.lastIndexOf('/'); 262 if (split == -1) split = -1; 263 } 264 String ns = uri.substring(0, split+1); 265 String prefix = ""; 266 if (ns.equals(RDF.getURI())) { 267 prefix = "rdf:"; 268 } else if (ns.equals(RDFS.getURI())) { 269 prefix = "rdfs:"; 270 } 271 return prefix + uri.substring(split+1); 272 } else { 273 return n.toString(); 274 } 275 } 276 277 280 private static Node normalize(Node node) { 281 if (node == null || node == Node.ANY) return Node_RuleVariable.WILD; 282 return node; 284 } 285 286 291 private static Node toMatch(Node node) { 292 return node.isVariable() ? null : node; 293 } 294 295 298 public boolean equals(Object o) { 299 return o instanceof TriplePattern && 304 nodeEqual(subject, ((TriplePattern)o).subject) && 305 nodeEqual(predicate, ((TriplePattern)o).predicate) && 306 nodeEqual(object, ((TriplePattern)o).object); 307 } 308 309 310 private boolean nodeEqual(Node n1, Node n2) { 311 if ((n1 instanceof Node_RuleVariable) && (n2 instanceof Node_RuleVariable)) { 312 return true; 313 } else { 314 return n1.equals(n2); 315 } 316 } 317 318 319 public int hashCode() { 320 int hash = 0; 321 if (!(subject instanceof Node_RuleVariable)) hash ^= (subject.hashCode() >> 1); 322 if (!(predicate instanceof Node_RuleVariable)) hash ^= predicate.hashCode(); 323 if (!(object instanceof Node_RuleVariable)) hash ^= (object.hashCode() << 1); 324 return hash; 325 } 327 328 332 public boolean sameAs(Object o) { 333 if (! (o instanceof TriplePattern) ) return false; 334 TriplePattern other = (TriplePattern) o; 335 return Node_RuleVariable.sameNodeAs(subject, other.subject) && Node_RuleVariable.sameNodeAs(predicate, other.predicate) && Node_RuleVariable.sameNodeAs(object, other.object); 336 } 337 338 } 339 340 369 370 | Popular Tags |