1 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 25 26 public class AddAndStoreTerminal 27 extends CommandGene implements ICloneable { 28 29 private final static String CVS_REVISION = "$Revision: 1.1 $"; 30 31 35 private String m_storageName; 36 37 private Class m_type; 38 39 50 public AddAndStoreTerminal(final GPConfiguration a_conf, String a_storageName, 51 Class a_childType) 52 throws InvalidConfigurationException { 53 this(a_conf, a_storageName, a_childType, 0, 0); 54 } 55 56 69 public AddAndStoreTerminal(final GPConfiguration a_conf, String a_storageName, 70 Class 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 ("Memory name must not be empty!"); 79 } 80 m_storageName = a_storageName; 81 } 82 83 public String toString() { 84 return "addstore(" + m_storageName + ", &1)"; 85 } 86 87 93 public String getName() { 94 return "Add+Store Terminal"; 95 } 96 97 public void execute_void(ProgramChromosome c, int n, Object [] args) { 98 check(c); 99 if (m_type == CommandGene.IntegerClass) { 100 int value = c.execute_int(n, 0, args); 101 Integer oldValue = (Integer ) getGPConfiguration().readFromMemoryIfExists( 102 m_storageName); 103 if (oldValue != null) { 104 value = value + oldValue.intValue(); 105 } 106 getGPConfiguration().storeInMemory(m_storageName, new Integer (value)); 109 } 110 else if (m_type == CommandGene.LongClass) { 111 long value = c.execute_long(n, 0, args); 112 Long oldValue = (Long ) getGPConfiguration().readFromMemoryIfExists( 113 m_storageName); 114 if (oldValue != null) { 115 value = value + oldValue.longValue(); 116 } 117 getGPConfiguration().storeInMemory(m_storageName, new Long (value)); 118 } 119 else if (m_type == CommandGene.DoubleClass) { 120 double value = c.execute_double(n, 0, args); 121 Double oldValue = (Double ) getGPConfiguration().readFromMemoryIfExists( 122 m_storageName); 123 if (oldValue != null) { 124 value = value + oldValue.doubleValue(); 125 } 126 getGPConfiguration().storeInMemory(m_storageName, new Double (value)); 127 } 128 else if (m_type == CommandGene.FloatClass) { 129 float value = c.execute_float(n, 0, args); 130 Float oldValue = (Float ) getGPConfiguration().readFromMemoryIfExists( 131 m_storageName); 132 if (oldValue != null) { 133 value = value + oldValue.floatValue(); 134 } 135 getGPConfiguration().storeInMemory(m_storageName, new Float (value)); 136 } 137 else { 138 throw new IllegalStateException ("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 161 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 162 return m_type; 163 } 164 165 174 public int compareTo(Object 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 196 public boolean equals(Object 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 cex) { 208 return false; 209 } 210 } 211 } 212 213 219 public Object clone() { 220 try { 221 AddAndStoreTerminal result = new AddAndStoreTerminal(getGPConfiguration(), 222 m_storageName, m_type, 223 getSubReturnType(), getSubChildType(0)); 224 return result; 225 } catch (Throwable t) { 226 throw new CloneException(t); 227 } 228 } 229 } 230 | Popular Tags |