1 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 24 public class StoreTerminal 25 extends CommandGene { 26 27 private final static String CVS_REVISION = "$Revision: 1.11 $"; 28 29 33 private String m_storageName; 34 35 private Class m_type; 36 37 public StoreTerminal(final GPConfiguration a_conf, String a_storageName, 38 Class a_childType) 39 throws InvalidConfigurationException { 40 this(a_conf, a_storageName, a_childType, 0, 0); 41 } 42 43 56 public StoreTerminal(final GPConfiguration a_conf, String a_storageName, 57 Class 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 ("Memory name must not be empty!"); 65 } 66 m_storageName = a_storageName; 67 } 68 69 public String toString() { 70 return "store(" + m_storageName + ", &1)"; 71 } 72 73 79 public String getName() { 80 return "Store Terminal("+m_storageName+")"; 81 } 82 83 public void execute_void(ProgramChromosome c, int n, Object [] args) { 84 check(c); 85 Object value = null; 86 if (m_type == CommandGene.IntegerClass) { 87 value = new Integer (c.execute_int(n, 0, args)); 88 } 89 else if (m_type == CommandGene.LongClass) { 90 value = new Long (c.execute_long(n, 0, args)); 91 } 92 else if (m_type == CommandGene.DoubleClass) { 93 value = new Double (c.execute_double(n, 0, args)); 94 } 95 else if (m_type == CommandGene.FloatClass) { 96 value = new Float (c.execute_float(n, 0, args)); 97 } 98 else { 99 value = c.execute(n, 0, args); 100 } 101 getGPConfiguration().storeInMemory(m_storageName, value); 104 } 105 106 public int execute_int(ProgramChromosome c, int n, Object [] args) { 107 check(c); 108 int value = c.execute_int(n, 0, args); 109 getGPConfiguration().storeInMemory(m_storageName, new Integer (value)); 112 return value; 113 } 114 115 public long execute_long(ProgramChromosome c, int n, Object [] args) { 116 check(c); 117 long value = c.execute_long(n, 0, args); 118 getGPConfiguration().storeInMemory(m_storageName, new Long (value)); 119 return value; 120 } 121 122 public double execute_double(ProgramChromosome c, int n, Object [] args) { 123 check(c); 124 double value = c.execute_double(n, 0, args); 125 getGPConfiguration().storeInMemory(m_storageName, new Double (value)); 126 return value; 127 } 128 129 public float execute_float(ProgramChromosome c, int n, Object [] args) { 130 check(c); 131 float value = c.execute_float(n, 0, args); 132 getGPConfiguration().storeInMemory(m_storageName, new Float (value)); 133 return value; 134 } 135 136 public Object execute_object(ProgramChromosome c, int n, Object [] args) { 137 check(c); 138 Object 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 162 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 163 return m_type; 164 } 165 166 175 public int compareTo(Object 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 197 public boolean equals(Object 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 cex) { 209 return false; 210 } 211 } 212 } 213 } 214 | Popular Tags |