Source code: jeops/rete/FinalReteNode.java
1 package jeops.rete;
2
3 /*
4 * JEOPS - The Java Embedded Object Production System
5 * Copyright (c) 2000 Carlos Figueira Filho
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Contact: Carlos Figueira Filho (csff@cin.ufpe.br)
22 */
23
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.ArrayList;
27 import jeops.AbstractKnowledgeBase;
28
29 /**
30 * A final node in the Rete network. There will be one final node for
31 * each rule in the rule base; when objects arrive at a final node,
32 * they are stored in a conflict set element and inserted into the
33 * conflict set of the knowledge base.
34 *
35 * @author Carlos Figueira Filho (<a href="mailto:csff@cin.ufpe.br">csff@cin.ufpe.br</a>)
36 * @version 1.0 14 Jul 2000
37 */
38 public class FinalReteNode extends ReteNode {
39
40 /**
41 * The knowledge base that will receive the elements to pass them
42 * to the conflict set. As the conflict set can be changed when
43 * the program is being executed, it is easier to concentrate the
44 * insertion of elements in the conflict set at one single point
45 * (the knowledge base), and have all final nodes to refer that
46 * point.
47 */
48 private AbstractKnowledgeBase knowledgeBase;
49
50 /**
51 * The index of the rule correspondent to this node.
52 */
53 private int ruleIndex;
54
55 /**
56 * Object array, defined as an attribute for efficiency purposes
57 * only.
58 */
59 private final Object[] ARRAY;
60
61 /**
62 * Creates a new discriminant rete node.
63 *
64 * @param numberInputs the number of inputs of this node.
65 * @param knowledgeBase the knowledge base to where the elements
66 * will be sent.
67 * @param ruleIndex the index of the rule that will be stored in
68 * the conflict set by the propagation of objects
69 * arriving at this final node.
70 */
71 public FinalReteNode(int numberInputs, AbstractKnowledgeBase knowledgeBase,
72 int ruleIndex) {
73 super(numberInputs, 0);
74 this.ruleIndex = ruleIndex;
75 this.knowledgeBase = knowledgeBase;
76 ARRAY = new Object[numberInputs];
77 }
78 /**
79 * Remove all objects that may be stored in this node. As no
80 * information is stored in this node, and it has no successors,
81 * this method has been overriden for performance reasons.
82 */
83 public void flush() {}
84 /**
85 * Informs this node that an object has arrived.
86 *
87 * @param obj the object that arrived at this node.
88 * @param input the input number of this node that is to receive
89 * the object. This node has only one input, so this
90 * parameter is not considered in objects of this class.
91 */
92 public void newObject(Object obj, int input) {
93 ARRAY[input] = obj;
94 if (input == getNumberInputs() - 1) {
95 knowledgeBase.newFirableRule(ruleIndex, ARRAY);
96 }
97 }
98 /**
99 * Remove the following objects that may be stored in this node.
100 * As no information is stored in this node, and it has no
101 * successors, this method has been overriden for performance
102 * reasons.
103 */
104 public void remove(Object obj) {}
105 /**
106 * Returns a string representation of this object. Useful for
107 * debugging.
108 *
109 * @return a string representation of this object.
110 */
111 public String toString() {
112 return ("FinalReteNode[ruleIndex="+ruleIndex+"]");
113 }
114 }