KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode;
11
12 import com.hp.hpl.jena.graph.Triple;
13 import com.hp.hpl.jena.reasoner.*;
14 import com.hp.hpl.jena.reasoner.rulesys.*;
15
16 import java.util.*;
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 /**
21  * Part of the backward chaining rule interpreter. The goal table
22  * is a table of partially evaluated goals. Each entry is an instance
23  * of GoalResults which contains the goal (a generalized triple pattern
24  * which supports structured literals), a set of triple values, a completion
25  * flag and a generator (which represents a continuation point for
26  * finding further goal values). This is essentially an encapsulation of
27  * the OR graph of the evaluation trace.
28  * <p>
29  * This implementation is not very space efficient. Once a GoalResult is
30  * complete we could flush all the results out to a single shared deductions
31  * graph in the inference engine wrapper. Care would be needed to do this
32  * in a thread-safe fashion since there can be multiple GoalStates scanning each
33  * GoalResult at any given time.
34  * </p>
35  *
36  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
37  * @version $Revision: 1.5 $ on $Date: 2005/02/21 12:18:04 $
38  */

39 public class GoalResults {
40
41 // =======================================================================
42
// variables
43

44     /** The goal who values are being memoised by this entry */
45     protected TriplePattern goal;
46     
47     /** The sequence of answers available so far */
48     protected ArrayList resultSet;
49     
50     /** A searchable version of the resultSet */
51     protected HashSet resultSetIndex;
52      
53     /** True if all the values for this goal are known */
54     protected boolean isComplete;
55     
56     /** True if this goal generator has started running */
57     protected boolean started = false;
58     
59     /** The set of RuleStates which are currently blocked
60      * waiting for this table entry to have more results */

61     protected Set dependents = new HashSet();
62     
63     /** The rule engine which this table entry is part of */
64     protected BRuleEngine engine;
65     
66     /** Reference count of the number of rulestates working on values for this entry */
67     protected int refCount = 0;
68     
69     /** Flag to indicate that the goal is a singleton and so should close once one result is in */
70     protected boolean isSingleton = false;
71         
72     static Log logger = LogFactory.getLog(GoalResults.class);
73     
74 // =======================================================================
75
// methods
76

77     /**
78      * Contructor.
79      *
80      * @param goal the goal whose matches are to be memoised.
81      * @param ruleEngine the parent rule engine for the goal table containing this entry
82      */

83     public GoalResults(TriplePattern goal, BRuleEngine ruleEngine) {
84         this.goal = goal;
85         resultSet = new ArrayList();
86         resultSetIndex = new HashSet();
87         isComplete = false;
88         engine = ruleEngine;
89         isSingleton = !(goal.getSubject().isVariable() || goal.getPredicate().isVariable() || goal.getObject().isVariable());
90     }
91     
92     /**
93      * Return true of this goal is known to have been completely
94      * evaluated.
95      */

96     public boolean isComplete() {
97         return isComplete;
98     }
99         
100     /**
101      * Return the number of available memoized results for this goal.
102      */

103     public int numResults() {
104         if (!started) start();
105         return resultSet.size();
106     }
107     
108     /**
109      * Return the n'th memoized result for this goal.
110      */

111     public Triple getResult(int n) {
112         return (Triple)resultSet.get(n);
113     }
114     
115     /**
116      * Record that a rule node has suspened waiting for more
117      * results from this subgoal
118      */

119     public void addDependent(RuleState dependent) {
120         if (!isComplete) dependents.add(dependent);
121     }
122     
123     /**
124      * Move all the blocked dependents to the agenda for further processing.
125      */

126     public void flushDependents() {
127         for (Iterator i = dependents.iterator(); i.hasNext(); ) {
128             RuleState dep = (RuleState)i.next();
129             engine.prependToAgenda(dep);
130         }
131 // dependents.clear();
132
}
133     
134     /**
135      * Return the rule engine processing this goal
136      */

137     public BRuleEngine getEngine() {
138         return engine;
139     }
140     
141     /**
142      * Indicate that the goal has completed.
143      */

144     public void setComplete() {
145         if (!isComplete) {
146             if (engine.isTraceOn()) {
147                 logger.debug("Completed " + this);
148             }
149             isComplete = true;
150             resultSetIndex = null;
151             flushDependents();
152             dependents.clear();
153         }
154     }
155     
156     /**
157      * Indicate that all goals have been completed, sets this to complete
158      * but does not bother to add the dependents to the agenda.
159      */

160     public void setAllComplete() {
161         isComplete = true;
162         dependents.clear();
163     }
164     
165     /**
166      * Start up a GoalResults stream. This finds all the relevant rules
167      * and adds initial states for them to the agenda.
168      */

169     public void start() {
170         List rules = engine.rulesFor(goal);
171         for (Iterator i = rules.iterator(); i.hasNext(); ) {
172             Rule rule = (Rule)i.next();
173             RuleState rs = RuleState.createInitialState(rule, this);
174             if (rs != null) {
175                 engine.appendToAgenda(rs);
176             }
177         }
178         if (refCount <= 0) setComplete();
179         started = true;
180     }
181     
182     /**
183      * Add a new result to the result set for this goal.
184      * @return ture if this is a new result for this goal
185      */

186     public boolean addResult(Triple result) {
187         if (!isComplete && !resultSetIndex.contains(result)) {
188             // Temp ... replace when we flush results to the deductions graph
189
// TODO remove
190
// if (engine.infGraph.dataContains(result)) return false;
191
// ... end temp
192
resultSet.add(result);
193             resultSetIndex.add(result);
194             if (isSingleton) {
195                 setComplete();
196             } else {
197                 flushDependents();
198             }
199             return true;
200         }
201         return false;
202     }
203     
204     /**
205      * Increment the reference count, called when a new RuleState refering to this result set
206      * is created.
207      */

208     public void incRefCount() {
209         refCount++;
210     }
211     
212     /**
213      * Decrement the reference count, called when a RuleState for this result set either
214      * fails or completes.
215      */

216     public void decRefCount() {
217         refCount--;
218         if (refCount <= 0) {
219             setComplete();
220         }
221     }
222     
223     /**
224      * Printable form
225      */

226     public String JavaDoc toString() {
227         return "GoalResult for: " + goal;
228     }
229 }
230
231 /*
232     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
233     All rights reserved.
234
235     Redistribution and use in source and binary forms, with or without
236     modification, are permitted provided that the following conditions
237     are met:
238
239     1. Redistributions of source code must retain the above copyright
240        notice, this list of conditions and the following disclaimer.
241
242     2. Redistributions in binary form must reproduce the above copyright
243        notice, this list of conditions and the following disclaimer in the
244        documentation and/or other materials provided with the distribution.
245
246     3. The name of the author may not be used to endorse or promote products
247        derived from this software without specific prior written permission.
248
249     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
250     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
251     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
252     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
253     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
254     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
258     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
259 */
Popular Tags