1 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 23 public class StoreTerminalIndexed 24 extends CommandGene { 25 26 private final static String CVS_REVISION = "$Revision: 1.1 $"; 27 28 private int m_index; 29 30 private Class m_type; 31 32 public StoreTerminalIndexed(final GPConfiguration a_conf, 33 int a_index, 34 Class a_childType) 35 throws InvalidConfigurationException { 36 this(a_conf, a_index, a_childType, 0, 0); 37 } 38 39 52 public StoreTerminalIndexed(final GPConfiguration a_conf, 53 int a_index, 54 Class 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 toString() { 65 return "store(" + m_index + ", &1)"; 66 } 67 68 74 public String getName() { 75 return "Store Terminal(" + m_index + ")"; 76 } 77 78 public void execute_void(ProgramChromosome c, int n, Object [] args) { 79 check(c); 80 Object value = null; 81 if (m_type == CommandGene.IntegerClass) { 82 value = new Integer (c.execute_int(n, 0, args)); 83 } 84 else if (m_type == CommandGene.LongClass) { 85 value = new Long (c.execute_long(n, 0, args)); 86 } 87 else if (m_type == CommandGene.DoubleClass) { 88 value = new Double (c.execute_double(n, 0, args)); 89 } 90 else if (m_type == CommandGene.FloatClass) { 91 value = new Float (c.execute_float(n, 0, args)); 92 } 93 else { 94 value = c.execute(n, 0, args); 95 } 96 getGPConfiguration().storeIndexedMemory(m_index, value); 99 } 100 101 public int execute_int(ProgramChromosome c, int n, Object [] args) { 102 check(c); 103 int value = c.execute_int(n, 0, args); 104 getGPConfiguration().storeIndexedMemory(m_index, new Integer (value)); 107 return value; 108 } 109 110 public long execute_long(ProgramChromosome c, int n, Object [] args) { 111 check(c); 112 long value = c.execute_long(n, 0, args); 113 getGPConfiguration().storeIndexedMemory(m_index, new Long (value)); 114 return value; 115 } 116 117 public double execute_double(ProgramChromosome c, int n, Object [] args) { 118 check(c); 119 double value = c.execute_double(n, 0, args); 120 getGPConfiguration().storeIndexedMemory(m_index, new Double (value)); 121 return value; 122 } 123 124 public float execute_float(ProgramChromosome c, int n, Object [] args) { 125 check(c); 126 float value = c.execute_float(n, 0, args); 127 getGPConfiguration().storeIndexedMemory(m_index, new Float (value)); 128 return value; 129 } 130 131 public Object execute_object(ProgramChromosome c, int n, Object [] args) { 132 check(c); 133 Object 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 157 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 158 return m_type; 159 } 160 161 170 public int compareTo(Object 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 192 public boolean equals(Object 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 cex) { 204 return false; 205 } 206 } 207 } 208 } 209 | Popular Tags |