KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > function > StoreTerminalIndexed


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.gp.function;
11
12 import org.jgap.*;
13 import org.jgap.gp.*;
14 import org.apache.commons.lang.builder.*;
15 import org.jgap.gp.impl.*;
16
17 /**
18  * Stores a value in the internal indexed memory.
19  *
20  * @author Klaus Meffert
21  * @since 3.2
22  */

23 public class StoreTerminalIndexed
24     extends CommandGene {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
27
28   private int m_index;
29
30   private Class JavaDoc m_type;
31
32   public StoreTerminalIndexed(final GPConfiguration a_conf,
33                               int a_index,
34                               Class JavaDoc a_childType)
35       throws InvalidConfigurationException {
36     this(a_conf, a_index, a_childType, 0, 0);
37   }
38
39   /**
40    * Allows setting a sub type and sub return type.
41    *
42    * @param a_conf GPConfiguration
43    * @param a_index
44    * @param a_childType Class
45    * @param a_subChildType int
46    * @param a_subReturnType int
47    * @throws InvalidConfigurationException
48    *
49    * @author Klaus Meffert
50    * @since 3.2
51    */

52   public StoreTerminalIndexed(final GPConfiguration a_conf,
53                               int a_index,
54                               Class JavaDoc a_childType, int a_subReturnType,
55                               int a_subChildType
56       )
57       throws InvalidConfigurationException {
58     super(a_conf, 1, CommandGene.VoidClass, a_subReturnType,
59           new int[] {a_subChildType});
60     m_type = a_childType;
61     m_index = a_index;
62   }
63
64   public String JavaDoc toString() {
65     return "store(" + m_index + ", &1)";
66   }
67
68   /**
69    * @return textual name of this command
70    *
71    * @author Klaus Meffert
72    * @since 3.2
73    */

74   public String JavaDoc getName() {
75     return "Store Terminal(" + m_index + ")";
76   }
77
78   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
79     check(c);
80     Object JavaDoc value = null;
81     if (m_type == CommandGene.IntegerClass) {
82       value = new Integer JavaDoc(c.execute_int(n, 0, args));
83     }
84     else if (m_type == CommandGene.LongClass) {
85       value = new Long JavaDoc(c.execute_long(n, 0, args));
86     }
87     else if (m_type == CommandGene.DoubleClass) {
88       value = new Double JavaDoc(c.execute_double(n, 0, args));
89     }
90     else if (m_type == CommandGene.FloatClass) {
91       value = new Float JavaDoc(c.execute_float(n, 0, args));
92     }
93     else {
94       value = c.execute(n, 0, args);
95     }
96     // Store in memory.
97
// ----------------
98
getGPConfiguration().storeIndexedMemory(m_index, value);
99   }
100
101   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
102     check(c);
103     int value = c.execute_int(n, 0, args);
104     // Store in memory.
105
// ----------------
106
getGPConfiguration().storeIndexedMemory(m_index, new Integer JavaDoc(value));
107     return value;
108   }
109
110   public long execute_long(ProgramChromosome c, int n, Object JavaDoc[] args) {
111     check(c);
112     long value = c.execute_long(n, 0, args);
113     getGPConfiguration().storeIndexedMemory(m_index, new Long JavaDoc(value));
114     return value;
115   }
116
117   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
118     check(c);
119     double value = c.execute_double(n, 0, args);
120     getGPConfiguration().storeIndexedMemory(m_index, new Double JavaDoc(value));
121     return value;
122   }
123
124   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
125     check(c);
126     float value = c.execute_float(n, 0, args);
127     getGPConfiguration().storeIndexedMemory(m_index, new Float JavaDoc(value));
128     return value;
129   }
130
131   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
132     check(c);
133     Object JavaDoc value = c.execute_object(n, 0, args);
134     getGPConfiguration().storeIndexedMemory(m_index, value);
135     return value;
136   }
137
138   public boolean isAffectGlobalState() {
139     return true;
140   }
141
142   public boolean isValid(ProgramChromosome a_program) {
143     return a_program.getIndividual().getCommandOfClass(0, ReadTerminal.class) >
144         0;
145   }
146
147   /**
148    * Determines which type a specific child of this command has.
149    *
150    * @param a_ind ignored here
151    * @param a_chromNum index of child
152    * @return type of the a_chromNum'th child
153    *
154    * @author Klaus Meffert
155    * @since 3.0
156    */

157   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
158     return m_type;
159   }
160
161   /**
162    * The compareTo-method.
163    *
164    * @param a_other the other object to compare
165    * @return -1, 0, 1
166    *
167    * @author Klaus Meffert
168    * @since 3.0
169    */

170   public int compareTo(Object JavaDoc a_other) {
171     if (a_other == null) {
172       return 1;
173     }
174     else {
175       StoreTerminalIndexed other = (StoreTerminalIndexed) a_other;
176       return new CompareToBuilder()
177           .append(m_index, other.m_index)
178           .append(m_type, other.m_type)
179           .toComparison();
180     }
181   }
182
183   /**
184    * The equals-method.
185    *
186    * @param a_other the other object to compare
187    * @return true if the objects are seen as equal
188    *
189    * @author Klaus Meffert
190    * @since 3.0
191    */

192   public boolean equals(Object JavaDoc a_other) {
193     if (a_other == null) {
194       return false;
195     }
196     else {
197       try {
198         StoreTerminalIndexed other = (StoreTerminalIndexed) a_other;
199         return new EqualsBuilder()
200             .append(m_index, other.m_index)
201             .append(m_type, other.m_type)
202             .isEquals();
203       } catch (ClassCastException JavaDoc cex) {
204         return false;
205       }
206     }
207   }
208 }
209
Popular Tags