KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Increments the value of a memory cell and returns the incremented value.
20  * Allows presetting a value in case* memory cell is not initialized.
21  *
22  * @author Klaus Meffert
23  * @since 3.2
24  */

25 public class IncrementMemory
26     extends MathCommand {
27   /** String containing the CVS revision. Read out via reflection!*/
28   private static final String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
29
30   private int m_increment;
31
32   private String JavaDoc m_memoryName;
33
34   private int m_initialValue;
35
36   /**
37    * Constructor for using an increment of 1.
38    *
39    * @param a_conf the configuration to use
40    * @param a_type the type of the terminal to increment (e.g. IntegerClass)
41    * @throws InvalidConfigurationException
42    *
43    * @author Klaus Meffert
44    * @since 3.2
45    */

46   public IncrementMemory(final GPConfiguration a_conf, Class JavaDoc a_type,
47                          String JavaDoc a_memoryName, int a_initialValue)
48       throws InvalidConfigurationException {
49     this(a_conf, a_type, a_memoryName, a_initialValue, 1);
50   }
51
52   /**
53    * Constructor to freely choose increment.
54    *
55    * @param a_conf the configuration to use
56    * @param a_type the type of the terminal to increment (e.g. IntegerClass)
57    * @param a_increment the increment to use, may also be negative
58    * @throws InvalidConfigurationException
59    *
60    * @author Klaus Meffert
61    * @since 3.2
62    */

63   public IncrementMemory(final GPConfiguration a_conf, Class JavaDoc a_type,
64                          String JavaDoc a_memoryName, int a_initialValue,
65                          int a_increment)
66       throws InvalidConfigurationException {
67     this(a_conf, a_type, a_memoryName, a_initialValue, a_increment, 0, 0);
68   }
69
70   public IncrementMemory(final GPConfiguration a_conf, Class JavaDoc a_type,
71                          String JavaDoc a_memoryName, int a_initialValue,int a_increment,
72                          int a_subReturnType, int a_subChildType)
73       throws InvalidConfigurationException {
74     super(a_conf, 0, a_type, a_subReturnType, a_subChildType);
75     m_increment = a_increment;
76     m_memoryName = a_memoryName;
77     m_initialValue = a_initialValue;
78   }
79
80   public String JavaDoc toString() {
81     if (m_increment == 1) {
82       return "INCMEM(" + m_memoryName + ")";
83     }
84     else {
85       return "INCMEM(" + m_memoryName + ", " + m_increment + ")";
86     }
87   }
88
89   /**
90    * @return textual name of this command
91    *
92    * @author Klaus Meffert
93    * @since 3.2
94    */

95   public String JavaDoc getName() {
96     return "INCMEM(" + m_memoryName + ")";
97   }
98
99   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
100     Integer JavaDoc value = (Integer JavaDoc) getGPConfiguration().readFromMemoryIfExists(
101         m_memoryName);
102     int valueI;
103     if (value == null) {
104       valueI = m_initialValue;
105     }
106     else {
107       valueI = value.intValue() + 1;
108     }
109     getGPConfiguration().storeInMemory(m_memoryName, new Integer JavaDoc(valueI));
110     return valueI;
111   }
112
113   /**
114    * The compareTo-method.
115    *
116    * @param a_other the other object to compare
117    * @return -1, 0, 1
118    *
119    * @author Klaus Meffert
120    * @since 3.2
121    */

122   public int compareTo(Object JavaDoc a_other) {
123     if (a_other == null) {
124       return 1;
125     }
126     else {
127       IncrementMemory other = (IncrementMemory) a_other;
128       return new CompareToBuilder()
129           .append(m_increment, other.m_increment)
130           .append(m_memoryName, other.m_memoryName)
131           .toComparison();
132     }
133   }
134
135   /**
136    * The equals-method.
137    *
138    * @param a_other the other object to compare
139    * @return true if the objects are seen as equal
140    *
141    * @author Klaus Meffert
142    * @since 3.2
143    */

144   public boolean equals(Object JavaDoc a_other) {
145     if (a_other == null) {
146       return false;
147     }
148     else {
149       try {
150         IncrementMemory other = (IncrementMemory) a_other;
151         return new EqualsBuilder()
152             .append(m_increment, other.m_increment)
153             .append(m_memoryName, other.m_memoryName)
154             .isEquals();
155       } catch (ClassCastException JavaDoc cex) {
156         return false;
157       }
158     }
159   }
160 }
161
Popular Tags