1 10 package examples.gp.tictactoe; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.jgap.gp.impl.*; 15 import org.jgap.util.*; 16 17 24 public class IfColor 25 extends CommandGene implements ICloneable { 26 27 private final static String CVS_REVISION = "$Revision: 1.1 $"; 28 29 private Class m_type; 30 31 private int m_color; 32 33 public IfColor(final GPConfiguration a_conf, Class a_type, 34 int a_color) 35 throws InvalidConfigurationException { 36 this(a_conf, a_type, a_color, 0, null); 37 } 38 39 public IfColor(final GPConfiguration a_conf, Class a_type, 40 int a_color, int a_subReturnType, int[] a_subChildTypes) 41 throws InvalidConfigurationException { 42 super(a_conf, 2, CommandGene.VoidClass, a_subReturnType, a_subChildTypes); 43 m_type = a_type; 44 m_color = a_color; 45 } 46 47 public String toString() { 48 return "if iscolor(&1) then (&2)"; 49 } 50 51 57 public String getName() { 58 return "If is Color"; 59 } 60 61 public void execute_void(ProgramChromosome c, int n, Object [] args) { 62 check(c); 63 boolean condition; 64 if (m_type == CommandGene.IntegerClass) { 65 condition = c.execute_int(n, 0, args) == m_color; 66 } 67 else if (m_type == CommandGene.LongClass) { 68 condition = c.execute_long(n, 0, args) == m_color; 69 } 70 else { 71 throw new IllegalStateException ("IfColor: cannot process type " + m_type); 72 } 73 if (condition) { 74 c.execute_void(n, 1, args); 75 } 76 } 77 78 public int execute_int(ProgramChromosome c, int n, Object [] args) { 79 check(c); 80 int value = c.execute_int(n, 0, args); 81 if (value == m_color) { 82 c.execute_void(n, 1, args); 83 } 84 return value; 85 } 86 87 97 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 98 if (a_chromNum == 0) { 99 return m_type; 100 } 101 return CommandGene.VoidClass; 102 } 103 104 public Object clone() { 105 try { 106 int[] subChildTypes = getSubChildTypes(); 107 if (subChildTypes != null) { 108 subChildTypes = (int[])subChildTypes.clone(); 109 } 110 IfColor result = new IfColor(getGPConfiguration(), m_type, m_color, 111 getSubReturnType(), subChildTypes); 112 return result; 113 } catch (Throwable t) { 114 throw new CloneException(t); 115 } 116 } 117 } 118 | Popular Tags |