KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: BaseRule.java
3  * Created by: Dave Reynolds
4  * Created on: 26-Jan-03
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: BaseFRule.java,v 1.7 2005/02/21 12:16:35 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.reasoner.transitiveReasoner.TransitiveReasoner;
14 import com.hp.hpl.jena.graph.*;
15
16 import java.util.*;
17
18 /**
19  * Base class for forward rules. Holds a head pattern (which will
20  * be matched against the triple store) and an array of body patterns.
21  * The body patterns can be instantiated following a successful head
22  * match and then processed be descendant class.
23  *
24  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
25  * @version $Revision: 1.7 $ on $Date: 2005/02/21 12:16:35 $
26  */

27 public class BaseFRule {
28
29     /** The head triple pattern */
30     protected TriplePattern head;
31     
32     /** The body triple patterns */
33     protected TriplePattern[] body;
34     
35     /**
36      * Constructor
37      */

38     public BaseFRule(TriplePattern head, TriplePattern[] body) {
39         this.head = head;
40         this.body = body;
41     }
42     
43     /**
44      * Constructor
45      */

46     public BaseFRule(String JavaDoc spec) {
47         List patterns = parseTripleSequence(spec);
48         head = (TriplePattern) patterns.get(0);
49         body = new TriplePattern[patterns.size() - 1];
50         for (int i = 1; i < patterns.size(); i++) {
51             body[i-1] = (TriplePattern) patterns.get(i);
52         }
53     }
54     
55     /**
56      * Match the rule against a single triple.
57      * Instantiating the variables then firing the consequent action.
58      */

59     public void bindAndFire(Triple value, RDFSInfGraph reasoner) {
60         // special case filter of reflexive subClass/subProp cases
61
// somewhat hacky doing it here ...
62
if ((value.getPredicate().equals(TransitiveReasoner.subPropertyOf) ||
63              value.getPredicate().equals(TransitiveReasoner.subClassOf))
64              && value.getSubject().equals(value.getObject())) {
65                 // skip this case
66
return;
67         }
68         Map bindings = new HashMap();
69         matchNode(value.getSubject(), head.getSubject(), bindings);
70         matchNode(value.getPredicate(), head.getPredicate(), bindings);
71         matchNode(value.getObject(), head.getObject(), bindings);
72         // Instantiate the body
73
TriplePattern[] newBody = new TriplePattern[body.length];
74         for (int i = 0; i < body.length; i++) {
75             newBody[i] = new TriplePattern(
76                                 instantiate(body[i].getSubject(), bindings),
77                                 instantiate(body[i].getPredicate(), bindings),
78                                 instantiate(body[i].getObject(), bindings));
79         }
80         fire(newBody, reasoner);
81     }
82     
83     /**
84      * Called when the rule fires.
85      * Subclasses should override.
86      */

87     void fire(TriplePattern[] body, RDFSInfGraph reasoner) {
88     }
89                     
90     /**
91      * Match a single node pair and add any new variable binding
92      */

93     static void matchNode(Node valueNode, Node patternNode, Map bindings) {
94         if (patternNode.isVariable()) {
95             bindings.put(patternNode.getName(), valueNode);
96         }
97     }
98     
99     /**
100      * Instantiate a node using the bindings
101      */

102     static Node instantiate(Node elt, Map bindings) {
103         if (elt.isVariable()) {
104             Node result = (Node) bindings.get(elt.getName());
105             if (result != null) return result;
106         }
107         return elt;
108     }
109     
110     /**
111      * Assistant method to parse a string into a triple
112      */

113     public static Triple parseTriple(String JavaDoc spec) {
114         StringTokenizer tokenizer = new StringTokenizer(spec);
115         try {
116             Node s = BRWRule.parseNode(tokenizer.nextToken());
117             Node p = BRWRule.parseNode(tokenizer.nextToken());
118             Node o = BRWRule.parseNode(tokenizer.nextToken());
119             return new Triple(s, p, o);
120         } catch (NoSuchElementException e) {
121             throw new ReasonerException("Illegal triple: " + spec);
122         }
123     }
124           
125     /**
126      * Assistant method to parse a token stream into a triple pattern
127      */

128     private static TriplePattern parseTriplePattern(StringTokenizer tokenizer) {
129         try {
130             Node s = BRWRule.parseNode(tokenizer.nextToken());
131             Node p = BRWRule.parseNode(tokenizer.nextToken());
132             Node o = BRWRule.parseNode(tokenizer.nextToken());
133             return new TriplePattern(s, p, o);
134         } catch (NoSuchElementException e) {
135             throw new ReasonerException("Illegal triple in rule");
136         }
137     }
138     
139     /**
140      * Assistant method to parse a string into a sequence oftriple patterns.
141      * The patterns may be separated by "<-", "->" or "|" strings.
142      * @return a list of TriplePatterns
143      */

144     public static List parseTripleSequence(String JavaDoc spec) {
145         StringTokenizer tokenizer = new StringTokenizer(spec);
146         List triples = new ArrayList();
147         while (tokenizer.hasMoreElements()) {
148             triples.add(parseTriplePattern(tokenizer));
149             if (tokenizer.hasMoreElements()) {
150                 String JavaDoc sep = tokenizer.nextToken();
151                 if (!sep.equals("|") && !sep.equals("->") && !sep.equals("<-")) {
152                     throw new ReasonerException("Illegal FRUle spec: " + spec);
153                 }
154             }
155         }
156         return triples;
157     }
158     /**
159      * Returns the head.
160      * @return TriplePattern
161      */

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

197
Popular Tags