capture
[rrq/gorite.git] / com / intendico / data / Rule.java
1 /*********************************************************************
2 Copyright 2012, Ralph Ronnquist.
3
4 This file is part of GORITE.
5
6 GORITE is free software: you can redistribute it and/or modify it
7 under the terms of the Lesser GNU General Public License as published
8 by the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GORITE is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14 License for more details.
15
16 You should have received a copy of the Lesser GNU General Public
17 License along with GORITE.  If not, see <http://www.gnu.org/licenses/>.
18 **********************************************************************/
19
20 package com.intendico.data;
21
22 import java.util.ConcurrentModificationException;
23
24 /**
25  * The Rule class defines a belief propagation relationship
26  * between a pair of {@link Query} objects that share {@link Ref}
27  * objects. The Rule provides a forward directed implication such
28  * that when the antecedent comes true for a binding, then the
29  * consequent is made true for that binding.
30  *
31  * <p> A Rule object is operated by first calling {@link #reset},
32  * which results in a {link Snapshot#reset} that collects all new
33  * bindings for the antecedent {@link Query}. The second step is a
34  * subsequent call to {@link #propagate}, which results in calls to
35  * {@link Query#add} on the consequent {@link Query} with bindings as
36  * given by the antecedent snapshot.
37  */
38 public class Rule {
39
40     /**
41      * The rule antecedent.
42      */
43     public Snapshot antecedent;
44
45     /**
46      * The rule consequent.
47      */
48     public Query consequent;
49
50     /**
51      * Constructor.
52      */
53     public Rule(Query a,Query c) {
54         if ( a != null )
55             antecedent = new Snapshot( a );
56         consequent = c;
57     }
58
59     /**
60      * Invoked to restart the Rule from the current belief state.
61      */
62     public void reset() throws Exception {
63         try {
64             antecedent.reset();
65         } catch (ConcurrentModificationException e) {
66             System.err.println( "Rule: " + this );
67             throw e;
68         }
69     }
70
71     /**
72      * Invoked to propagate antecedent bindings into consequent
73      * additions.
74      */
75     public void propagate() throws Exception {
76         while ( antecedent.next() ) {
77             consequent.reset();
78             if ( ! consequent.next() )
79                 consequent.add();
80         }
81     }
82
83     /**
84      * Returns a textual representation of a Rule.
85      */
86     public String toString() {
87         return antecedent.query + " => " + consequent;
88     }
89 }