Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jeops/rete/ReteNode.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  
28  /**
29   * A node in the Rete network. This class is the base for all kinds of
30   * nodes that exist in the implementation of the Rete network for JEOPS.
31   *
32   * @author Carlos Figueira Filho (<a href="mailto:csff@cin.ufpe.br">csff@cin.ufpe.br</a>)
33   * @version 1.0   13 Jul 2000
34   */
35  public abstract class ReteNode {
36  
37    /**
38     * The nodes that succede this one. It's actually a list of lists,
39     * where each row represents the input index of this node, and the
40     * columns represent the connections whose source is the
41     * corresponding input.
42     */
43    private List successors;
44  
45    /**
46     * The number of inputs of this node.
47     */
48    private int numberInputs;
49  
50    /**
51     * The number of outputs of this node.
52     */
53    private int numberOutputs;
54  
55    /**
56     * Creates a new rete node.
57     *
58     * @param numberInputs the number of inputs of this node.
59     * @param numberOutputs the number of inputs of this node.
60     */
61    public ReteNode(int numberInputs, int numberOutputs) {
62      this.numberInputs = numberInputs;
63      this.numberOutputs = numberOutputs;
64      successors = new ArrayList();
65    }
66  
67    /**
68     * Adds a connection from some input of this node to a given
69     * input of the given node.
70     *
71     * @param input the input of this node that is being connected to
72     *          the given input of the successor node.
73     * @param succNode the successor to be added to this node.
74     * @param succInput the input in the successor node to where the
75     *          propagated objects must be sent.
76     */
77    public void addSuccessor(int input, ReteNode succNode, int succInput) {
78      while (successors.size() <= input) {
79        successors.add(new ArrayList());
80      }
81      List l = (List) successors.get(input);
82      l.add(new PairIntReteNode(succInput, succNode));
83    }
84  
85    /**
86     * Adds a connection from the only input of this node to the
87     * only input of the given node.
88     *
89     * @param succNode the successor to be added to this node.
90     */
91    public void addSuccessor(ReteNode succNode) {
92      addSuccessor(0, succNode, 0);
93    }
94  
95    /**
96     * Adds a connection from the only input of this node to a
97     * given input of the given node.
98     *
99     * @param succNode the successor to be added to this node.
100    * @param succInput the input in the successor node to where the
101    *          propagated objects must be sent.
102    */
103   public void addSuccessor(ReteNode succNode, int succIndex) {
104     addSuccessor(0, succNode, succIndex);
105   }
106 
107   /**
108    * Returns the number of inputs of this node.
109    *
110    * @return the number of inputs of this node.
111    */
112   public int getNumberInputs() {
113     return numberInputs;
114   }
115 
116   /**
117    * Returns the number of outputs of this node.
118    *
119    * @return the number of outputs of this node.
120    */
121   public int getNumberOutputs() {
122     return numberOutputs;
123   }
124 
125   /**
126    * Returns the successors of this node. Available only for subclasses.
127    *
128    * @return the successors of this node.
129    */
130   protected List getSuccessors() {
131     return successors;
132   }
133 
134   /**
135    * Informs this node that an object has arrived.
136    *
137    * @param obj the object that arrived at this node.
138    * @param input the input number of this node that is to receive
139    *          the object.
140    */
141   public abstract void newObject(Object obj, int input);
142 
143   /**
144    * Propagates the objects from this node to the successors. It's
145    * an auxiliar method that must be invoked by the subclasses in
146    * order to propagate the objects to the registered successors.
147    *
148    * @param obj the object to be propagated.
149    * @param input the input from which the object entered.
150    */
151   protected void propagate(Object obj, int input) {
152     List l = (List) successors.get(input);
153     for (Iterator i = l.iterator(); i.hasNext(); ) {
154       PairIntReteNode pair = (PairIntReteNode) i.next();
155       pair.getNode().newObject(obj, pair.getIntValue());
156     }
157   }
158 
159 }