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 Push 25 extends CommandGene { 26 27 private final static String CVS_REVISION = "$Revision: 1.9 $"; 28 29 private Class m_type; 30 31 public Push(final GPConfiguration a_conf, Class a_type) 32 throws InvalidConfigurationException { 33 this(a_conf, a_type, 0, 0); 34 } 35 36 public Push(final GPConfiguration a_conf, Class a_type, int a_subReturnType, 37 int a_subChildType) 38 throws InvalidConfigurationException { 39 super(a_conf, 1, CommandGene.VoidClass, a_subReturnType, a_subChildType); 40 m_type = a_type; 41 } 42 43 public String toString() { 44 return "push &1"; 45 } 46 47 53 public String getName() { 54 return "Push"; 55 } 56 57 public void execute_void(ProgramChromosome c, int n, Object [] args) { 58 check(c); 59 int value = c.execute_int(n, 0, args); 60 pushIt(new Integer (value)); 63 } 64 65 public int execute_int(ProgramChromosome c, int n, Object [] args) { 66 check(c); 67 int value = c.execute_int(n, 0, args); 68 pushIt(new Integer (value)); 71 return value; 72 } 73 74 public long execute_long(ProgramChromosome c, int n, Object [] args) { 75 check(c); 76 long value = c.execute_long(n, 0, args); 77 pushIt(new Long (value)); 80 return value; 81 } 82 83 public double execute_double(ProgramChromosome c, int n, Object [] args) { 84 check(c); 85 double value = c.execute_double(n, 0, args); 86 pushIt(new Double (value)); 87 return value; 88 } 89 90 public float execute_float(ProgramChromosome c, int n, Object [] args) { 91 check(c); 92 float value = c.execute_float(n, 0, args); 93 pushIt(new Float (value)); 94 return value; 95 } 96 97 public Object execute_object(ProgramChromosome c, int n, Object [] args) { 98 check(c); 99 Object value = c.execute_object(n, 0, args); 100 pushIt(value); 101 return value; 102 } 103 104 public boolean isAffectGlobalState() { 105 return true; 106 } 107 108 public boolean isValid(ProgramChromosome a_program) { 109 110 return a_program.getCommandOfClass(0, Pop.class) >= 0; 111 } 112 113 117 protected void pushIt(Object a_value) { 118 getGPConfiguration().pushToStack(a_value); 119 } 120 121 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 122 return m_type; 123 } 124 125 134 public int compareTo(Object a_other) { 135 if (a_other == null) { 136 return 1; 137 } 138 else { 139 Push other = (Push) a_other; 140 return new CompareToBuilder() 141 .append(m_type, other.m_type) 142 .toComparison(); 143 } 144 } 145 146 155 public boolean equals(Object a_other) { 156 if (a_other == null) { 157 return false; 158 } 159 else { 160 try { 161 Push other = (Push) a_other; 162 return new EqualsBuilder() 163 .append(m_type, other.m_type) 164 .isEquals(); 165 } catch (ClassCastException cex) { 166 return false; 167 } 168 } 169 } 170 } 171 | Popular Tags |