KickJava   Java API By Example, From Geeks To Geeks.

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


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.CompareToBuilder;
15 import org.apache.commons.lang.builder.EqualsBuilder;
16 import org.jgap.gp.impl.*;
17
18 /**
19  * Stores a value in the internal memory.
20  *
21  * @author Klaus Meffert
22  * @since 3.0
23  */

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

33   private String JavaDoc m_storageName;
34
35   private Class JavaDoc m_type;
36
37   public StoreTerminal(final GPConfiguration a_conf, String JavaDoc a_storageName,
38                        Class JavaDoc a_childType)
39       throws InvalidConfigurationException {
40     this(a_conf, a_storageName, a_childType, 0, 0);
41   }
42
43   /**
44    * Allows setting a sub type and sub return type.
45    *
46    * @param a_conf GPConfiguration
47    * @param a_storageName String
48    * @param a_childType Class
49    * @param a_subChildType int
50    * @param a_subReturnType int
51    * @throws InvalidConfigurationException
52    *
53    * @author Klaus Meffert
54    * @since 3.2
55    */

56   public StoreTerminal(final GPConfiguration a_conf, String JavaDoc a_storageName,
57                        Class JavaDoc a_childType, int a_subReturnType, int a_subChildType
58                        )
59       throws InvalidConfigurationException {
60     super(a_conf, 1, CommandGene.VoidClass, a_subReturnType,
61           new int[] {a_subChildType});
62     m_type = a_childType;
63     if (a_storageName == null || a_storageName.length() < 1) {
64       throw new IllegalArgumentException JavaDoc("Memory name must not be empty!");
65     }
66     m_storageName = a_storageName;
67   }
68
69   public String JavaDoc toString() {
70     return "store(" + m_storageName + ", &1)";
71   }
72
73   /**
74    * @return textual name of this command
75    *
76    * @author Klaus Meffert
77    * @since 3.2
78    */

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

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

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

197   public boolean equals(Object JavaDoc a_other) {
198     if (a_other == null) {
199       return false;
200     }
201     else {
202       try {
203         StoreTerminal other = (StoreTerminal) a_other;
204         return new EqualsBuilder()
205             .append(m_storageName, other.m_storageName)
206             .append(m_type, other.m_type)
207             .isEquals();
208       } catch (ClassCastException JavaDoc cex) {
209         return false;
210       }
211     }
212   }
213 }
214
Popular Tags