KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.commons.lang.builder.*;
13 import org.jgap.*;
14 import org.jgap.gp.*;
15 import org.jgap.gp.impl.*;
16 import org.jgap.util.*;
17
18 /**
19  * Stores a value in the internal memory but adds the value already stored
20  * in the target memory cell before storing it.
21  *
22  * @author Klaus Meffert
23  * @since 3.2
24  */

25
26 public class AddAndStoreTerminal
27     extends CommandGene implements ICloneable {
28   /** String containing the CVS revision. Read out via reflection!*/
29   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
30
31   /**
32    * Symbolic name of the storage. Must correspond with a chosen name for
33    * ReadTerminalCommand.
34    */

35   private String JavaDoc m_storageName;
36
37   private Class JavaDoc m_type;
38
39   /**
40    * Constructor.
41    *
42    * @param a_conf GPConfiguration
43    * @param a_storageName String
44    * @param a_childType Class
45    * @throws InvalidConfigurationException
46    *
47    * @author Klaus Meffert
48    * @since 3.2
49    */

50   public AddAndStoreTerminal(final GPConfiguration a_conf, String JavaDoc a_storageName,
51                              Class JavaDoc a_childType)
52       throws InvalidConfigurationException {
53     this(a_conf, a_storageName, a_childType, 0, 0);
54   }
55
56   /**
57    * Allows setting a sub type and sub return type.
58    *
59    * @param a_conf GPConfiguration
60    * @param a_storageName String
61    * @param a_childType Class
62    * @param a_subChildType int
63    * @param a_subReturnType int
64    * @throws InvalidConfigurationException
65    *
66    * @author Klaus Meffert
67    * @since 3.2
68    */

69   public AddAndStoreTerminal(final GPConfiguration a_conf, String JavaDoc a_storageName,
70                              Class JavaDoc a_childType, int a_subReturnType,
71                              int a_subChildType
72       )
73       throws InvalidConfigurationException {
74     super(a_conf, 1, CommandGene.VoidClass, a_subReturnType,
75           new int[] {a_subChildType});
76     m_type = a_childType;
77     if (a_storageName == null || a_storageName.length() < 1) {
78       throw new IllegalArgumentException JavaDoc("Memory name must not be empty!");
79     }
80     m_storageName = a_storageName;
81   }
82
83   public String JavaDoc toString() {
84     return "addstore(" + m_storageName + ", &1)";
85   }
86
87   /**
88    * @return textual name of this command
89    *
90    * @author Klaus Meffert
91    * @since 3.2
92    */

93   public String JavaDoc getName() {
94     return "Add+Store Terminal";
95   }
96
97   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
98     check(c);
99     if (m_type == CommandGene.IntegerClass) {
100       int value = c.execute_int(n, 0, args);
101       Integer JavaDoc oldValue = (Integer JavaDoc) getGPConfiguration().readFromMemoryIfExists(
102           m_storageName);
103       if (oldValue != null) {
104         value = value + oldValue.intValue();
105       }
106       // Store in memory.
107
// ----------------
108
getGPConfiguration().storeInMemory(m_storageName, new Integer JavaDoc(value));
109     }
110     else if (m_type == CommandGene.LongClass) {
111       long value = c.execute_long(n, 0, args);
112       Long JavaDoc oldValue = (Long JavaDoc) getGPConfiguration().readFromMemoryIfExists(
113           m_storageName);
114       if (oldValue != null) {
115         value = value + oldValue.longValue();
116       }
117       getGPConfiguration().storeInMemory(m_storageName, new Long JavaDoc(value));
118     }
119     else if (m_type == CommandGene.DoubleClass) {
120       double value = c.execute_double(n, 0, args);
121       Double JavaDoc oldValue = (Double JavaDoc) getGPConfiguration().readFromMemoryIfExists(
122           m_storageName);
123       if (oldValue != null) {
124         value = value + oldValue.doubleValue();
125       }
126       getGPConfiguration().storeInMemory(m_storageName, new Double JavaDoc(value));
127     }
128     else if (m_type == CommandGene.FloatClass) {
129       float value = c.execute_float(n, 0, args);
130       Float JavaDoc oldValue = (Float JavaDoc) getGPConfiguration().readFromMemoryIfExists(
131           m_storageName);
132       if (oldValue != null) {
133         value = value + oldValue.floatValue();
134       }
135       getGPConfiguration().storeInMemory(m_storageName, new Float JavaDoc(value));
136     }
137     else {
138       throw new IllegalStateException JavaDoc("Type " + m_type + " unknown");
139     }
140   }
141
142   public boolean isAffectGlobalState() {
143     return true;
144   }
145
146   public boolean isValid(ProgramChromosome a_program) {
147     return a_program.getIndividual().getCommandOfClass(0, ReadTerminal.class) >
148         0;
149   }
150
151   /**
152    * Determines which type a specific child of this command has.
153    *
154    * @param a_ind ignored here
155    * @param a_chromNum index of child
156    * @return type of the a_chromNum'th child
157    *
158    * @author Klaus Meffert
159    * @since 3.2
160    */

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

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

196   public boolean equals(Object JavaDoc a_other) {
197     if (a_other == null) {
198       return false;
199     }
200     else {
201       try {
202         AddAndStoreTerminal other = (AddAndStoreTerminal) a_other;
203         return new EqualsBuilder()
204             .append(m_storageName, other.m_storageName)
205             .append(m_type, other.m_type)
206             .isEquals();
207       } catch (ClassCastException JavaDoc cex) {
208         return false;
209       }
210     }
211   }
212
213   /**
214    * @return deep clone of this instance
215    *
216    * @author Klaus Meffert
217    * @since 3.2
218    */

219   public Object JavaDoc clone() {
220     try {
221       AddAndStoreTerminal result = new AddAndStoreTerminal(getGPConfiguration(),
222           m_storageName, m_type,
223           getSubReturnType(), getSubChildType(0));
224       return result;
225     } catch (Throwable JavaDoc t) {
226       throw new CloneException(t);
227     }
228   }
229 }
230
Popular Tags