KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > gp > tictactoe > IfColor


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

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 /**
18  * The if-then construct. If-Condition is: if specific color, then
19  * execute X else do nothing.
20  *
21  * @author Klaus Meffert
22  * @since 3.2
23  */

24 public class IfColor
25     extends CommandGene implements ICloneable {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
28
29   private Class JavaDoc m_type;
30
31   private int m_color;
32
33   public IfColor(final GPConfiguration a_conf, Class JavaDoc 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 JavaDoc 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 JavaDoc toString() {
48     return "if iscolor(&1) then (&2)";
49   }
50
51   /**
52    * @return textual name of this command
53    *
54    * @author Klaus Meffert
55    * @since 3.2
56    */

57   public String JavaDoc getName() {
58     return "If is Color";
59   }
60
61   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] 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 JavaDoc("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 JavaDoc[] 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   /**
88    * Determines which type a specific child of this command has.
89    *
90    * @param a_ind ignored here
91    * @param a_chromNum index of child
92    * @return type of the a_chromNum'th child
93    *
94    * @author Klaus Meffert
95    * @since 3.2
96    */

97   public Class JavaDoc 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 JavaDoc 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 JavaDoc t) {
114       throw new CloneException(t);
115     }
116   }
117 }
118
Popular Tags