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 26 public class ResourceBRWRule extends BRWRule { 27 28 29 private static Node TYPE = RDF.type.getNode(); 30 31 32 private static Node RESOURCE = RDFS.Resource.getNode(); 33 34 37 public ResourceBRWRule() { 38 super(new TriplePattern(null, RDF.type.getNode(), RDFS.Resource.getNode()), 39 new TriplePattern(null, null, null)); 40 } 41 42 54 public ExtendedIterator execute(TriplePattern query, InfGraph infGraph, Finder data, HashSet firedRules) { 55 RDFSInfGraph bRr = (RDFSInfGraph)infGraph; 56 if (query.getSubject().isVariable()) { 57 return new ResourceRewriteIterator(bRr.findRawWithContinuation(body, data)); 59 } else { 60 Node subj = query.getSubject(); 62 TriplePattern pattern = new TriplePattern(subj, null, null); 63 String var = "s"; 64 ExtendedIterator it = bRr.findRawWithContinuation(pattern, data); 65 if (!it.hasNext()) { 66 pattern = new TriplePattern(null, null, subj); 67 var = "o"; 68 it = bRr.findRawWithContinuation(pattern, data); 69 if (!it.hasNext()) { 70 pattern = new TriplePattern(null, subj, null); 71 var = "p"; 72 it = bRr.findRawWithContinuation(pattern, data); 73 } 74 } 75 BRWRule rwrule = new BRWRule(new TriplePattern(Node.createVariable(var), TYPE, RESOURCE), body); 76 return new RewriteIterator(it, rwrule); 77 } 78 } 79 80 84 public boolean completeFor(TriplePattern query) { 85 return head.subsumes(query); 86 } 87 88 92 static class ResourceRewriteIterator extends WrappedIterator { 93 94 private Triple[] lookahead = new Triple[3]; 95 96 97 private short nAvailable = 0; 98 99 100 protected HashSet seen = new HashSet(); 101 102 107 public ResourceRewriteIterator(Iterator underlying) { 108 super(underlying); 109 } 110 111 115 private void push(Node resource) { 116 if (seen.add(resource)) { 117 lookahead[nAvailable++] = new Triple(resource, TYPE, RESOURCE); 118 } 119 } 120 121 124 public boolean hasNext() { 125 while (nAvailable == 0 && super.hasNext()) { 126 Triple value = (Triple)super.next(); 127 if (seen.add(value)) { 128 push(value.getSubject()); 129 push(value.getPredicate()); 130 Node object = value.getObject(); 131 if (!object.isLiteral()) { 132 push(object); 133 } 134 135 } 136 } 137 return nAvailable > 0; 138 } 139 140 143 public Object next() { 144 if (nAvailable == 0 && ! hasNext()) { 145 throw new NoSuchElementException("No element available"); 146 } 147 return lookahead[--nAvailable]; 148 } 149 150 } 152 } 153 154 183 | Popular Tags |