001: /******************************************************************
002: * File: ResourceBRWRule.java
003: * Created by: Dave Reynolds
004: * Created on: 28-Jan-03
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: ResourceBRWRule.java,v 1.13 2008/01/02 12:06:44 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rdfsReasoner1;
010:
011: import com.hp.hpl.jena.reasoner.*;
012: import com.hp.hpl.jena.graph.*;
013: import com.hp.hpl.jena.vocabulary.*;
014: import com.hp.hpl.jena.util.iterator.*;
015:
016: import java.util.*;
017:
018: /**
019: * A special case of a backchaing rule to handle the nasty case
020: * of "anything mentioned in any triple is a Resource".
021: *
022: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
023: * @version $Revision: 1.13 $ on $Date: 2008/01/02 12:06:44 $
024: */
025: public class ResourceBRWRule extends BRWRule {
026:
027: /** node form of rdf:type */
028: private static Node TYPE = RDF.Nodes.type;
029:
030: /** node form of rdfs:Resource */
031: private static Node RESOURCE = RDFS.Nodes.Resource;
032:
033: /**
034: * Constructor
035: */
036: public ResourceBRWRule() {
037: super (new TriplePattern(null, TYPE, RESOURCE),
038: new TriplePattern(null, null, null));
039: }
040:
041: /**
042: * Use the rule to implement the given query. This will
043: * instantiate the rule against the query, run the new query
044: * against the whole reasoner+rawdata again and then rewrite the
045: * results from that query according the rule.
046: * @param query the query being processed
047: * @param infGraph link to originating inference graph, may be re-invoked after a pattern rewrite
048: * @param data the raw data graph which gets passed back to the reasoner as part of the recursive invocation
049: * @param firedRules set of rules which have already been fired and should now be blocked
050: * @return a ExtendedIterator which aggregates the matches and rewrites them
051: * according to the rule
052: */
053: public ExtendedIterator execute(TriplePattern query,
054: InfGraph infGraph, Finder data, HashSet firedRules) {
055: RDFSInfGraph bRr = (RDFSInfGraph) infGraph;
056: if (query.getSubject().isVariable()) {
057: // Find all things of type resource
058: return new ResourceRewriteIterator(bRr
059: .findRawWithContinuation(body, data));
060: } else {
061: // Just check for a specific resource
062: Node subj = query.getSubject();
063: TriplePattern pattern = new TriplePattern(subj, null, null);
064: String var = "s";
065: ExtendedIterator it = bRr.findRawWithContinuation(pattern,
066: data);
067: if (!it.hasNext()) {
068: pattern = new TriplePattern(null, null, subj);
069: var = "o";
070: it = bRr.findRawWithContinuation(pattern, data);
071: if (!it.hasNext()) {
072: pattern = new TriplePattern(null, subj, null);
073: var = "p";
074: it = bRr.findRawWithContinuation(pattern, data);
075: }
076: }
077: BRWRule rwrule = new BRWRule(new TriplePattern(Node
078: .createVariable(var), TYPE, RESOURCE), body);
079: return new RewriteIterator(it, rwrule);
080: }
081: }
082:
083: /**
084: * Return true if this rule is a a complete solution to the given
085: * query and the router need look no further
086: */
087: public boolean completeFor(TriplePattern query) {
088: return head.subsumes(query);
089: }
090:
091: /**
092: * Inner class. This implements an iterator that uses the rule to rewrite any
093: * results from the supplied iterator according to the rule.
094: */
095: static class ResourceRewriteIterator extends WrappedIterator {
096: /** short stack of triples generated but not yet delivered */
097: private Triple[] lookahead = new Triple[3];
098:
099: /** the number of values available in lookahead */
100: private short nAvailable = 0;
101:
102: /** The set of objects already seen */
103: protected HashSet seen = new HashSet();
104:
105: /**
106: * Constructor
107: * @param underlying the iterator whose results are to be rewritten
108: * @param rule the BRWRule which defines the rewrite
109: */
110: public ResourceRewriteIterator(Iterator underlying) {
111: super (underlying);
112: }
113:
114: /**
115: * Record a new instance of Resource so long as it has not
116: * been seen before
117: */
118: private void push(Node resource) {
119: if (seen.add(resource)) {
120: lookahead[nAvailable++] = new Triple(resource, TYPE,
121: RESOURCE);
122: }
123: }
124:
125: /**
126: * @see Iterator#hasNext()
127: */
128: public boolean hasNext() {
129: while (nAvailable == 0 && super .hasNext()) {
130: Triple value = (Triple) super .next();
131: if (seen.add(value)) {
132: push(value.getSubject());
133: push(value.getPredicate());
134: Node object = value.getObject();
135: if (!object.isLiteral()) {
136: push(object);
137: }
138:
139: }
140: }
141: return nAvailable > 0;
142: }
143:
144: /**
145: * @see Iterator#next()
146: */
147: public Object next() {
148: if (nAvailable == 0 && !hasNext()) {
149: throw new NoSuchElementException("No element available");
150: }
151: return lookahead[--nAvailable];
152: }
153:
154: } // End of inner class - ResourceRewriteIterator
155:
156: }
157:
158: /*
159: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
160: All rights reserved.
161:
162: Redistribution and use in source and binary forms, with or without
163: modification, are permitted provided that the following conditions
164: are met:
165:
166: 1. Redistributions of source code must retain the above copyright
167: notice, this list of conditions and the following disclaimer.
168:
169: 2. Redistributions in binary form must reproduce the above copyright
170: notice, this list of conditions and the following disclaimer in the
171: documentation and/or other materials provided with the distribution.
172:
173: 3. The name of the author may not be used to endorse or promote products
174: derived from this software without specific prior written permission.
175:
176: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
178: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
179: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
180: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
181: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
182: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
183: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
184: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
185: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
186: */
|