KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: ConsumerChoicePoint.java
3  * Created by: Dave Reynolds
4  * Created on: 07-Aug-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: ConsumerChoicePointFrame.java,v 1.9 2005/04/12 17:01:41 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.impl;
11
12 import com.hp.hpl.jena.graph.*;
13 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable;
14
15 import java.util.*;
16
17 /**
18  * Frame in the LPInterpreter's control stack used to represent matching
19  * to the results of a tabled predicate. Conventionally the system state which
20  * finds and tables the goal results is called the generator and states which
21  * require those results are called consumers.
22  * <p>
23  * This is used in the inner loop of the interpreter and so is a pure data structure
24  * not an abstract data type and assumes privileged access to the interpreter state.
25  * </p>
26  *
27  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
28  * @version $Revision: 1.9 $ on $Date: 2005/04/12 17:01:41 $
29  */

30 public class ConsumerChoicePointFrame extends GenericTripleMatchFrame
31                 implements LPAgendaEntry, LPInterpreterState {
32         
33     /** The generator whose tabled results we are selecting over */
34     protected Generator generator;
35     
36     /** The index in the generator's result set that we have reached so far. */
37     protected int resultIndex;
38     
39     /** The preserved permanent registers for the pickled interpreter */
40     protected Node[] pVars;
41
42     /** The preserved trail variables for the picked interpreter */
43     protected Node_RuleVariable[] trailVars;
44
45     /** The preserved trail bound values for the picked interpreter */
46     protected Node[] trailValues;
47     
48     /** The length of the preserved trail */
49     protected int trailLength;
50     
51     /** The generator or top iterator we are producting results for */
52     protected LPInterpreterContext context;
53         
54     /**
55      * Constructor.
56      * @param interpreter the parent interpreter whose state is to be preserved here, its arg stack
57      * defines the parameters for the target goal
58      */

59     public ConsumerChoicePointFrame(LPInterpreter interpreter) {
60         init(interpreter);
61     }
62     
63     /**
64      * Initialize the choice point state.
65      * @param interpreter the parent interpreter whose state is to be preserved here, its arg stack
66      * defines the parameters for the target goal
67      */

68     public void init(LPInterpreter interpreter) {
69         super.init(interpreter);
70         context = interpreter.getContext();
71         generator = interpreter.getEngine().generatorFor(goal);
72         generator.addConsumer(this);
73         resultIndex = 0;
74     }
75     
76     /**
77      * Preserve the state of an interpreter into this frame.
78      */

79     public void preserveState(List trail) {
80         // Save the trail state
81
int trailLen = trail.size();
82         if (trailLen > trailLength) {
83             trailValues = new Node[trailLen];
84             trailVars = new Node_RuleVariable[trailLen];
85         }
86         trailLength = trailLen;
87         for (int i = 0; i < trailLen; i++) {
88             Node_RuleVariable var = (Node_RuleVariable) trail.get(i);
89             trailVars[i] = var;
90             trailValues[i] = var.getRawBoundValue();
91         }
92         // Save the permanent variables
93
Node[] currentPVars = envFrame.pVars;
94         if (currentPVars != null) {
95             if (pVars == null || pVars.length < currentPVars.length) {
96                 pVars = new Node[currentPVars.length];
97             }
98             System.arraycopy(currentPVars, 0, pVars, 0, currentPVars.length);
99         }
100     }
101     
102     /**
103      * Restore the state of an interpreter from this frame
104      */

105     public void restoreState(LPInterpreter interp) {
106         interp.unwindTrail(0);
107         for (int i = 0; i < trailLength; i++) {
108             interp.bind(trailVars[i], trailValues[i]);
109         }
110         if (pVars != null) {
111             System.arraycopy(pVars, 0, envFrame.pVars, 0, pVars.length);
112         }
113     }
114     
115     /**
116      * Find the next result triple and bind the result vars appropriately.
117      * @param interpreter the calling interpreter whose trail should be used
118      * @return FAIL if there are no more matches and the generator is closed, SUSPEND if
119      * there are no more matches but the generator could generate more, SATISFIED if
120      * a match has been found.
121      */

122     public synchronized StateFlag nextMatch(LPInterpreter interpreter) {
123         while (resultIndex < generator.results.size()) {
124             Triple result = (Triple) generator.results.get(resultIndex++);
125             // Check if we have finished with this generator
126
if (resultIndex >= generator.results.size() && generator.isComplete()) {
127                 generator.removeConsumer(this);
128             }
129             if (bindResult(result, interpreter)) {
130                 return StateFlag.SATISFIED;
131             }
132         }
133         if (generator.isComplete()) {
134             setFinished();
135             generator.removeConsumer(this);
136             return StateFlag.FAIL;
137         } else {
138             return StateFlag.SUSPEND;
139         }
140     }
141     
142     /**
143      * Return true if this choice point could usefully be restarted.
144      */

145     public boolean isReady() {
146         return generator.numResults() > resultIndex;
147     }
148     
149     /**
150      * Called by generator when there are more results available.
151      */

152     public void setReady() {
153         context.setReady(this);
154     }
155     
156     /**
157      * Notify that this consumer choice point has finished consuming all
158      * the results of a closed generator.
159      */

160     public void setFinished() {
161         context.notifyFinished(this);
162     }
163     
164     /**
165      * Reactivate this choice point to return new results.
166      */

167     public void pump() {
168         if (context instanceof Generator) {
169             ((Generator)context).pump(this);
170         } else {
171             // The top level iterator is in charge and will restore and run this choice point itself
172
}
173     }
174     
175     /**
176      * Return the generator associated with this entry (might be the entry itself)
177      */

178     public Generator getGenerator() {
179         return generator;
180     }
181     
182     /**
183      * Return the interpeter context which is reading the results of this consumer.
184      */

185     public LPInterpreterContext getConsumingContext() {
186         return context;
187     }
188
189 }
190
191
192 /*
193     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
194     All rights reserved.
195
196     Redistribution and use in source and binary forms, with or without
197     modification, are permitted provided that the following conditions
198     are met:
199
200     1. Redistributions of source code must retain the above copyright
201        notice, this list of conditions and the following disclaimer.
202
203     2. Redistributions in binary form must reproduce the above copyright
204        notice, this list of conditions and the following disclaimer in the
205        documentation and/or other materials provided with the distribution.
206
207     3. The name of the author may not be used to endorse or promote products
208        derived from this software without specific prior written permission.
209
210     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
211     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
212     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
213     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
214     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
215     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
216     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
217     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
218     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
219     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
220 */
Popular Tags