KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > impl > RuleStore


1 /******************************************************************
2  * File: RuleStore.java
3  * Created by: Dave Reynolds
4  * Created on: 04-May-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: RuleStore.java,v 1.15 2005/02/21 12:18:00 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.impl;
11
12 import java.util.*;
13
14 import com.hp.hpl.jena.graph.*;
15 import com.hp.hpl.jena.reasoner.*;
16 import com.hp.hpl.jena.reasoner.rulesys.*;
17 import com.hp.hpl.jena.util.OneToManyMap;
18
19 /**
20  * Indexes a collection of rule. The currently implementation is
21  * a crude first version aimed at supporting the backchaining
22  * interpreter. It only indexes on predicate.
23  * <p>
24  * The rules are normalized to only contain a single head element
25  * by duplicating any multi headed rules.
26  * </p>
27  *
28  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
29  * @version $Revision: 1.15 $ on $Date: 2005/02/21 12:18:00 $
30  */

31 public class RuleStore {
32
33     /** The set of rules indexed by head predicate */
34     protected OneToManyMap goalMap = new OneToManyMap();
35     
36     /** The list of all rules in the store */
37     protected List allRules;
38     
39     /** Index of the rules, used to block multiple entries */
40     protected Set ruleIndex = new HashSet();
41     
42     /**
43      * Constructor. Create an empty rule store.
44      */

45     public RuleStore() {
46         allRules = new ArrayList();
47     };
48     
49     /**
50      * Constructor. Stores and indexes a list of rules.
51      */

52     public RuleStore(List rules) {
53         for (Iterator i = rules.iterator(); i.hasNext(); ) {
54             addRule( (Rule)i.next() );
55         }
56         allRules = rules;
57     }
58     
59     /**
60      * Add all the rules and from an existing rulestore into this one.
61      */

62     public void addAll(RuleStore store) {
63         for (Iterator i = store.getAllRules().iterator(); i.hasNext(); ) {
64             addRule( (Rule)i.next() );
65         }
66     }
67     
68     /**
69      * Add a single rule to the store.
70      */

71     public void addRule(Rule rule) {
72         addRemoveRule(rule, true);
73     }
74     
75     /**
76      * Remove a single rule from the store
77      */

78     public void deleteRule(Rule rule) {
79         addRemoveRule(rule, false);
80     }
81     
82     /**
83      * Add a single/remove a compound rule from the store.
84      * @param rule the rule, may have multiple heads
85      * @param isAdd true to add, false to remove
86      */

87     private void addRemoveRule(Rule rule, boolean isAdd) {
88         if (rule.headLength() != 1) {
89             for (int j = 0; j < rule.headLength(); j++) {
90                 Rule newRule = new Rule(rule.getName(),
91                                     new ClauseEntry[] {rule.getHeadElement(j)},
92                                     rule.getBody() );
93                 newRule.setNumVars(rule.getNumVars());
94                 doAddRemoveRule(newRule, isAdd);
95             }
96                 
97         } else {
98             doAddRemoveRule(rule, isAdd);
99         }
100     }
101     
102     /**
103      * Add/remove a single rule from the store.
104      * @param rule the rule, single headed only
105      * @param isAdd true to add, false to remove
106      */

107     protected void doAddRemoveRule(Rule rule, boolean isAdd) {
108         if (isAdd && ruleIndex.contains(rule)) return;
109         if (isAdd) {
110             ruleIndex.add(rule);
111             if (allRules != null) allRules.add(rule);
112         } else {
113             ruleIndex.remove(rule);
114             if (allRules != null) allRules.remove(rule);
115         }
116         Object JavaDoc headClause = rule.getHeadElement(0);
117         if (headClause instanceof TriplePattern) {
118             TriplePattern headpattern = (TriplePattern)headClause;
119             Node predicate = headpattern.getPredicate();
120             if (predicate.isVariable()) {
121                 if (isAdd) {
122                     goalMap.put(Node.ANY, rule);
123                 } else {
124                     goalMap.remove(Node.ANY, rule);
125                 }
126             } else {
127                 if (isAdd) {
128                     goalMap.put(predicate, rule);
129                 } else {
130                     goalMap.remove(predicate, rule);
131                 }
132             }
133         }
134     }
135     
136     /**
137      * Return a list of rules that match the given goal pattern
138      * @param goal the goal being matched
139      */

140     public List rulesFor(TriplePattern goal) {
141         List rules = new ArrayList();
142         if (goal.getPredicate().isVariable()) {
143             checkAll(goalMap.values().iterator(), goal, rules);
144         } else {
145             checkAll(goalMap.getAll(goal.getPredicate()), goal, rules);
146             checkAll(goalMap.getAll(Node.ANY), goal, rules);
147         }
148         return rules;
149     }
150     
151     /**
152      * Return an ordered list of all registered rules.
153      */

154     public List getAllRules() {
155         return allRules;
156     }
157     
158     /**
159      * Delete all the rules.
160      */

161     public void deleteAllRules() {
162         allRules.clear();
163         goalMap.clear();
164         ruleIndex.clear();
165     }
166     
167     /**
168      * Helper method to extract all matching clauses from an
169      * iterator over rules
170      */

171     private void checkAll(Iterator candidates, TriplePattern goal, List matchingRules) {
172         while (candidates.hasNext()) {
173             Rule r = (Rule)candidates.next();
174             if ( ((TriplePattern)r.getHeadElement(0)).compatibleWith(goal) ) {
175                 matchingRules.add(r);
176             }
177         }
178     }
179  
180 }
181
182 /*
183     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
184     All rights reserved.
185
186     Redistribution and use in source and binary forms, with or without
187     modification, are permitted provided that the following conditions
188     are met:
189
190     1. Redistributions of source code must retain the above copyright
191        notice, this list of conditions and the following disclaimer.
192
193     2. Redistributions in binary form must reproduce the above copyright
194        notice, this list of conditions and the following disclaimer in the
195        documentation and/or other materials provided with the distribution.
196
197     3. The name of the author may not be used to endorse or promote products
198        derived from this software without specific prior written permission.
199
200     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
201     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
202     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
203     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
204     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
205     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
206     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
207     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
208     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
209     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
210 */
Popular Tags