KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: GoalTable.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: GoalTable.java,v 1.5 2005/02/21 12:18:05 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode;
11
12 import com.hp.hpl.jena.reasoner.*;
13
14 import java.util.*;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 /**
19  * Part of the backwared chaining rule interpreter. The goal table
20  * is a table of partially evaluated goals. This could be done by
21  * variant-based or sumsumption-based tabling. We currently use variant-based.
22  * TODO Investigate performance impact of switching to subsumption-based.
23  *
24  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
25  * @version $Revision: 1.5 $ on $Date: 2005/02/21 12:18:05 $
26  */

27 public class GoalTable {
28
29     /** The set of goal entries indexed by goal */
30     protected Map table = new HashMap();
31     
32     /** The parent inference engine for the goal table */
33     protected BRuleEngine ruleEngine;
34     
35     static Log logger = LogFactory.getLog(GoalTable.class);
36         
37     /**
38      * Constructor. Creates a new, empty GoalTable. Any goal search on
39      * this table will include the results from searching the given set of
40      * raw data graphs.
41      * @param ruleEngine the parent inference engine instance for this table
42      */

43     public GoalTable(BRuleEngine ruleEngine) {
44         this.ruleEngine = ruleEngine;
45     }
46
47     /**
48      * Find the set of memoized solutions for the given goal
49      * and return a GoalState that can traverse all the solutions.
50      *
51      * @param goal the goal to be solved
52      * @return a GoalState which can iterate over all of the goal solutions
53      */

54     public GoalState findGoal(TriplePattern goal) {
55 // if (ruleEngine.getInfGraph().isTraceOn()) {
56
// logger.debug("findGoal on " + goal.toString());
57
// }
58
GoalResults results = (GoalResults) table.get(goal);
59         if (results == null || !goal.variantOf(results.goal)) {
60             results = new GoalResults(goal, ruleEngine);
61             table.put(goal, results);
62         }
63         return new GoalState(ruleEngine.getInfGraph().findDataMatches(goal), results);
64     }
65         
66     /**
67      * Clear all tabled results.
68      */

69     public void reset() {
70         table = new HashMap();
71     }
72     
73     /**
74      * Clear all partial results, closing any pending RuleStates but leaving
75      * completed goals intact.
76      */

77     public void removePartialGoals() {
78         for (Iterator i= table.entrySet().iterator(); i.hasNext(); ) {
79             Map.Entry entry = (Map.Entry)i.next();
80             TriplePattern goal = (TriplePattern)entry.getKey();
81             GoalResults result = (GoalResults)entry.getValue();
82             if ( ! result.isComplete()) {
83                 // Close any iterators in the dependent goal states.
84
for (Iterator d = result.dependents.iterator(); d.hasNext(); ) {
85                     RuleState rs = (RuleState)d.next();
86                     if (rs.goalState != null) rs.goalState.close();
87                 }
88                 i.remove();
89             }
90         }
91     }
92     
93     /**
94      * Set all the goals in the table to "complete".
95      */

96     public void setAllComplete() {
97         for (Iterator i = table.values().iterator(); i.hasNext(); ) {
98             ((GoalResults)i.next()).setAllComplete();
99         }
100     }
101     
102     /**
103      * Dump an a summary of the goal table state to stdout.
104      * Just debugging, do not use for real.
105      */

106     public void dump() {
107         System.out.println("Final goal table");
108         for (Iterator i = table.values().iterator(); i.hasNext(); ) {
109             GoalResults gr = (GoalResults)i.next();
110             System.out.println(gr.toString() );
111             for (int j = 0; j < gr.numResults(); j++) {
112                 System.out.println(" - " + gr.getResult(j));
113             }
114         }
115     }
116     
117 }
118
119
120
121 /*
122     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
123     All rights reserved.
124
125     Redistribution and use in source and binary forms, with or without
126     modification, are permitted provided that the following conditions
127     are met:
128
129     1. Redistributions of source code must retain the above copyright
130        notice, this list of conditions and the following disclaimer.
131
132     2. Redistributions in binary form must reproduce the above copyright
133        notice, this list of conditions and the following disclaimer in the
134        documentation and/or other materials provided with the distribution.
135
136     3. The name of the author may not be used to endorse or promote products
137        derived from this software without specific prior written permission.
138
139     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
141     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
142     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
143     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
144     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
145     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
146     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
147     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
148     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
149 */
Popular Tags