KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: PropertyBRWRule.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: PropertyBRWRule.java,v 1.10 2005/02/21 12:16:36 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 predicated position is a Property".
22  *
23  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
24  * @version $Revision: 1.10 $ on $Date: 2005/02/21 12:16:36 $
25  */

26 public class PropertyBRWRule extends BRWRule {
27
28     /**
29      * Constructor
30      */

31     public PropertyBRWRule() {
32         super(new TriplePattern(Node.createVariable("p"), RDF.type.getNode(), RDF.Property.getNode()),
33                new TriplePattern(null, Node.createVariable("s"), null));
34     }
35     
36     /**
37      * Use the rule to implement the given query. This will
38      * instantiate the rule against the query, run the new query
39      * against the whole reasoner+rawdata again and then rewrite the
40      * results from that query according the rule.
41      * @param query the query being processed
42      * @param infGraph the parent InfGraph that invoked us, will be called recursively
43      * @param data the raw data graph which gets passed back to the reasoner as part of the recursive invocation
44      * @param firedRules set of rules which have already been fired and should now be blocked
45      * @return a ExtendedIterator which aggregates the matches and rewrites them
46      * according to the rule
47      */

48     public ExtendedIterator execute(TriplePattern query, InfGraph infGraph, Finder data, HashSet firedRules) {
49         RDFSInfGraph bRr = (RDFSInfGraph) infGraph;
50         Node prop = query.getSubject();
51         if (bRr.getScanProperties()) {
52             // All properties are cached in the subProperty graph
53
// That in turn will be accessed via the rules generated by the subPropertyOf domain/range
54
// rules do don't need to do anything here
55
return NullIterator.instance;
56         } else {
57             // Have to scan all the raw data for property usage
58
TriplePattern pattern = instantiate(body, query);
59             return new RewriteIterator(bRr.findRawWithContinuation(body, data), this);
60         }
61     }
62
63     /**
64      * Inner class. Rewrites property node lists in to (p, type, Property)
65      * assertions.
66      */

67     static class PropertyNodeIterator extends UniqueExtendedIterator {
68         
69         /**
70          * Constructor
71          * @param underlying the iterator whose results are to be rewritten
72          * @param rule the BRWRule which defines the rewrite
73          */

74         public PropertyNodeIterator(Iterator underlying) {
75             super(underlying);
76         }
77     
78         /**
79          * Fetch the next object to be returned, only if not already seen.
80          * Filters on the property rather than the generated triple.
81          *
82          * @return the object to be returned or null if the object has been filtered.
83          */

84         protected Object JavaDoc nextIfNew() {
85             Node prop = (Node)super.next();
86             if (seen.add(prop)) {
87                 return new Triple(prop, RDF.type.getNode(), RDF.Property.getNode());
88             } else {
89                 return null;
90             }
91         }
92         
93     } // End of inner class - ResourceRewriteIterator
94

95 }
96
97 /*
98     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
99     All rights reserved.
100
101     Redistribution and use in source and binary forms, with or without
102     modification, are permitted provided that the following conditions
103     are met:
104
105     1. Redistributions of source code must retain the above copyright
106        notice, this list of conditions and the following disclaimer.
107
108     2. Redistributions in binary form must reproduce the above copyright
109        notice, this list of conditions and the following disclaimer in the
110        documentation and/or other materials provided with the distribution.
111
112     3. The name of the author may not be used to endorse or promote products
113        derived from this software without specific prior written permission.
114
115     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
116     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
117     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
118     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
119     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
120     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
121     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
122     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
123     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
124     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
125 */

126
Popular Tags