1 /*********************************************************************
2 Copyright 2012, Ralph Ronnquist.
4 This file is part of GORITE.
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.
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.
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 **********************************************************************/
20 package com.intendico.data;
22 import java.util.ConcurrentModificationException;
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.
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.
41 * The rule antecedent.
43 public Snapshot antecedent;
46 * The rule consequent.
48 public Query consequent;
53 public Rule(Query a,Query c) {
55 antecedent = new Snapshot( a );
60 * Invoked to restart the Rule from the current belief state.
62 public void reset() throws Exception {
65 } catch (ConcurrentModificationException e) {
66 System.err.println( "Rule: " + this );
72 * Invoked to propagate antecedent bindings into consequent
75 public void propagate() throws Exception {
76 while ( antecedent.next() ) {
78 if ( ! consequent.next() )
84 * Returns a textual representation of a Rule.
86 public String toString() {
87 return antecedent.query + " => " + consequent;