1 10 package org.jgap.gp.function; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.jgap.gp.terminal.*; 15 import org.apache.commons.lang.builder.CompareToBuilder; 16 import org.apache.commons.lang.builder.EqualsBuilder; 17 import org.jgap.gp.impl.*; 18 19 25 public class ForXLoop 26 extends CommandGene { 27 28 private final static String CVS_REVISION = "$Revision: 1.8 $"; 29 30 private Class m_type; 31 32 public ForXLoop(final GPConfiguration a_conf, Class a_type) 33 throws InvalidConfigurationException { 34 super(a_conf, 1, CommandGene.VoidClass); 35 m_type = a_type; 36 } 37 38 public String toString() { 39 return "for(int i=0;i<X;i++) { &1 }"; 40 } 41 42 48 public String getName() { 49 return "ForXLoop"; 50 } 51 52 public void execute_void(ProgramChromosome c, int n, Object [] args) { 53 check(c); 54 int index = c.getVariableWithReturnType(0, m_type); 55 if (index < 0) { 56 throw new IllegalStateException ("Variable missing for forX"); 57 } 58 59 Variable var = (Variable) c.getNode(index); 60 int x; 61 if (m_type == CommandGene.IntegerClass) { 64 x = ( (Integer ) var.getValue()).intValue(); 65 } 66 else if (m_type == CommandGene.LongClass) { 67 x = ( (Long ) var.getValue()).intValue(); 68 } 69 else if (m_type == CommandGene.DoubleClass) { 70 x = ( (Double ) var.getValue()).intValue(); 71 } 72 else if (m_type == CommandGene.FloatClass) { 73 x = ( (Float ) var.getValue()).intValue(); 74 } 75 else { 76 throw new RuntimeException ("Type " + m_type + " unknown in ForXCommand"); 77 } 78 if (x > 15) { 79 x = 15; 80 } 81 for (int i = 0; i < x; i++) { 82 c.execute_void(n, 0, args); 83 } 84 } 85 86 public boolean isValid(ProgramChromosome a_program) { 87 return a_program.getVariableWithReturnType(0, m_type) >= 0; 88 } 89 90 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 91 return CommandGene.VoidClass; 92 } 93 94 public Class getReturnType() { 95 return super.getReturnType(); 96 } 97 98 106 public int compareTo(Object a_other) { 107 if (a_other == null) { 108 return 1; 109 } 110 else { 111 ForXLoop other = (ForXLoop) a_other; 112 return new CompareToBuilder() 113 .append(m_type, other.m_type) 114 .toComparison(); 115 } 116 } 117 118 126 public boolean equals(Object a_other) { 127 if (a_other == null) { 128 return false; 129 } 130 else { 131 try { 132 ForXLoop other = (ForXLoop) a_other; 133 return new EqualsBuilder() 134 .append(m_type, other.m_type) 135 .isEquals(); 136 } catch (ClassCastException cex) { 137 return false; 138 } 139 } 140 } 141 } 142 | Popular Tags |