KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode;
11
12 import com.hp.hpl.jena.reasoner.TriplePattern;
13 import com.hp.hpl.jena.reasoner.rulesys.impl.StateFlag;
14
15 import java.util.Iterator JavaDoc;
16
17 /**
18  * Wraps up the backward chaining engine as an iterator-like object that can be
19  * used in InfGraphs to implement a "find" operation. It creates
20  * a top level GoalState and pumps that for results until the
21  * agenda is empty.
22  *
23  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
24  * @version $Revision: 1.4 $ on $Date: 2005/02/21 12:18:06 $
25  */

26 public class TopGoalIterator implements Iterator JavaDoc {
27         
28     /** The GoalState which is traversing the top level derivation tree */
29     GoalState goalState;
30     
31     /** The next result to be returned, or null if we have finished */
32     Object JavaDoc lookAhead;
33     
34     /** The parent backward chaining engine */
35     BRuleEngine engine;
36     
37     /**
38      * Constructor. Wraps a top level goal state as an iterator
39      */

40     public TopGoalIterator(BRuleEngine engine, TriplePattern goal) {
41         this.engine = engine;
42         this.goalState = engine.findGoal(goal);
43         moveForward();
44     }
45     
46     /**
47      * Find the next result in the goal state and put it in the
48      * lookahead buffer.
49      */

50     private void moveForward() {
51         lookAhead = goalState.next();
52         if (lookAhead == StateFlag.SUSPEND) {
53             if (engine.next(goalState) != null) {
54                 lookAhead = goalState.next();
55             } else {
56                 lookAhead = null;
57             }
58         } else if (lookAhead == StateFlag.FAIL) {
59             lookAhead = null;
60         }
61         if (lookAhead == null) close();
62     }
63     
64     /**
65      * @see com.hp.hpl.jena.util.iterator.ClosableIterator#close()
66      */

67     public void close() {
68         goalState.close();
69         engine.halt();
70     }
71
72     /**
73      * @see java.util.Iterator#hasNext()
74      */

75     public boolean hasNext() {
76         return (lookAhead != null);
77     }
78
79     /**
80      * @see java.util.Iterator#next()
81      */

82     public Object JavaDoc next() {
83         Object JavaDoc result = lookAhead;
84         moveForward();
85         return result;
86     }
87
88     /**
89      * @see java.util.Iterator#remove()
90      */

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