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 AddAndStore 25 extends MathCommand { 26 27 private final static String CVS_REVISION = "$Revision: 1.9 $"; 28 29 33 private String m_storageName; 34 35 private Class m_type; 36 37 public AddAndStore(final GPConfiguration a_conf, Class a_type, 38 String a_storageName) 39 throws InvalidConfigurationException { 40 super(a_conf, 2, CommandGene.VoidClass); 41 m_type = a_type; 42 m_storageName = a_storageName; 43 } 44 45 public String toString() { 46 return "Store(" + m_storageName + ", &1 + &2)"; 47 } 48 49 55 public String getName() { 56 return "AddAndStore"; 57 } 58 59 public void execute_void(ProgramChromosome c, int n, Object [] args) { 60 Object value = null; 61 if (m_type == CommandGene.IntegerClass) { 62 value = new Integer (c.execute_int(n, 0, args) + c.execute_int(n, 1, args)); 63 } 64 else if (m_type == CommandGene.LongClass) { 65 value = new Long (c.execute_long(n, 0, args) + c.execute_long(n, 1, args)); 66 } 67 else if (m_type == CommandGene.DoubleClass) { 68 value = new Double (c.execute_double(n, 0, args) + 69 c.execute_double(n, 1, args)); 70 } 71 else if (m_type == CommandGene.FloatClass) { 72 value = new Float (c.execute_float(n, 0, args) + 73 c.execute_float(n, 1, args)); 74 } 75 else { 76 throw new RuntimeException ("Type " + m_type + 77 " not supported by AddAndStore"); 78 } 79 getGPConfiguration().storeInMemory(m_storageName, value); 80 } 81 82 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 83 return m_type; 84 } 85 86 95 public int compareTo(Object a_other) { 96 if (a_other == null) { 97 return 1; 98 } 99 else { 100 AddAndStore other = (AddAndStore) a_other; 101 return new CompareToBuilder() 102 .append(m_type, other.m_type) 103 .append(m_storageName, other.m_storageName) 104 .toComparison(); 105 } 106 } 107 108 116 public boolean equals(Object a_other) { 117 if (a_other == null) { 118 return false; 119 } 120 else { 121 try { 122 AddAndStore other = (AddAndStore) a_other; 123 return new EqualsBuilder() 124 .append(m_type, other.m_type) 125 .append(m_storageName, other.m_storageName) 126 .isEquals(); 127 } catch (ClassCastException cex) { 128 return false; 129 } 130 } 131 } 132 } 133 | Popular Tags |