KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rdfsReasoner1 > ResourceBRWRule


1 /******************************************************************
2  * File: ResourceBRWRule.java
3  * Created by: Dave Reynolds
4  * Created on: 28-Jan-03
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: ResourceBRWRule.java,v 1.9 2005/02/21 12:16:51 andy_seaborne Exp $
9  *****************************************************************/

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 /**
20  * A special case of a backchaing rule to handle the nasty case
21  * of "anything mentioned in any triple is a Resource".
22  *
23  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
24  * @version $Revision: 1.9 $ on $Date: 2005/02/21 12:16:51 $
25  */

26 public class ResourceBRWRule extends BRWRule {
27     
28     /** node form of rdf:type */
29     private static Node TYPE = RDF.type.getNode();
30     
31     /** node form of rdfs:Resource */
32     private static Node RESOURCE = RDFS.Resource.getNode();
33
34     /**
35      * Constructor
36      */

37     public ResourceBRWRule() {
38         super(new TriplePattern(null, RDF.type.getNode(), RDFS.Resource.getNode()),
39                new TriplePattern(null, null, null));
40     }
41     
42     /**
43      * Use the rule to implement the given query. This will
44      * instantiate the rule against the query, run the new query
45      * against the whole reasoner+rawdata again and then rewrite the
46      * results from that query according the rule.
47      * @param query the query being processed
48      * @param infGraph link to originating inference graph, may be re-invoked after a pattern rewrite
49      * @param data the raw data graph which gets passed back to the reasoner as part of the recursive invocation
50      * @param firedRules set of rules which have already been fired and should now be blocked
51      * @return a ExtendedIterator which aggregates the matches and rewrites them
52      * according to the rule
53      */

54     public ExtendedIterator execute(TriplePattern query, InfGraph infGraph, Finder data, HashSet firedRules) {
55         RDFSInfGraph bRr = (RDFSInfGraph)infGraph;
56         if (query.getSubject().isVariable()) {
57             // Find all things of type resource
58
return new ResourceRewriteIterator(bRr.findRawWithContinuation(body, data));
59         } else {
60             // Just check for a specific resource
61
Node subj = query.getSubject();
62             TriplePattern pattern = new TriplePattern(subj, null, null);
63             String JavaDoc 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     /**
81      * Return true if this rule is a a complete solution to the given
82      * query and the router need look no further
83      */

84     public boolean completeFor(TriplePattern query) {
85         return head.subsumes(query);
86     }
87
88     /**
89      * Inner class. This implements an iterator that uses the rule to rewrite any
90      * results from the supplied iterator according to the rule.
91      */

92     static class ResourceRewriteIterator extends WrappedIterator {
93         /** short stack of triples generated but not yet delivered */
94         private Triple[] lookahead = new Triple[3];
95         
96         /** the number of values available in lookahead */
97         private short nAvailable = 0;
98         
99         /** The set of objects already seen */
100         protected HashSet seen = new HashSet();
101     
102         /**
103          * Constructor
104          * @param underlying the iterator whose results are to be rewritten
105          * @param rule the BRWRule which defines the rewrite
106          */

107         public ResourceRewriteIterator(Iterator underlying) {
108             super(underlying);
109         }
110
111         /**
112          * Record a new instance of Resource so long as it has not
113          * been seen before
114          */

115         private void push(Node resource) {
116             if (seen.add(resource)) {
117                 lookahead[nAvailable++] = new Triple(resource, TYPE, RESOURCE);
118             }
119         }
120                 
121         /**
122          * @see Iterator#hasNext()
123          */

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         /**
141          * @see Iterator#next()
142          */

143         public Object JavaDoc next() {
144             if (nAvailable == 0 && ! hasNext()) {
145                 throw new NoSuchElementException("No element available");
146             }
147             return lookahead[--nAvailable];
148         }
149
150     } // End of inner class - ResourceRewriteIterator
151

152 }
153
154 /*
155     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
156     All rights reserved.
157
158     Redistribution and use in source and binary forms, with or without
159     modification, are permitted provided that the following conditions
160     are met:
161
162     1. Redistributions of source code must retain the above copyright
163        notice, this list of conditions and the following disclaimer.
164
165     2. Redistributions in binary form must reproduce the above copyright
166        notice, this list of conditions and the following disclaimer in the
167        documentation and/or other materials provided with the distribution.
168
169     3. The name of the author may not be used to endorse or promote products
170        derived from this software without specific prior written permission.
171
172     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
174     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
175     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
176     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
177     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
178     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
179     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
180     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
181     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
182 */

183
Popular Tags