001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package examples.gp.tictactoe;
011:
012: import org.jgap.*;
013: import org.jgap.gp.*;
014: import org.jgap.gp.impl.*;
015: import org.jgap.util.*;
016:
017: /**
018: * The if-then construct. If-Condition is: if specific color, then
019: * execute X else do nothing.
020: *
021: * @author Klaus Meffert
022: * @since 3.2
023: */
024: public class IfColor extends CommandGene implements ICloneable {
025: /** String containing the CVS revision. Read out via reflection!*/
026: private final static String CVS_REVISION = "$Revision: 1.2 $";
027:
028: private Class m_type;
029:
030: private int m_color;
031:
032: public IfColor(final GPConfiguration a_conf, Class a_type,
033: int a_color) throws InvalidConfigurationException {
034: this (a_conf, a_type, a_color, 0, null);
035: }
036:
037: public IfColor(final GPConfiguration a_conf, Class a_type,
038: int a_color, int a_subReturnType, int[] a_subChildTypes)
039: throws InvalidConfigurationException {
040: super (a_conf, 2, CommandGene.VoidClass, a_subReturnType,
041: a_subChildTypes);
042: m_type = a_type;
043: m_color = a_color;
044: }
045:
046: public String toString() {
047: return "if iscolor(&1) then (&2)";
048: }
049:
050: /**
051: * @return textual name of this command
052: *
053: * @author Klaus Meffert
054: * @since 3.2
055: */
056: public String getName() {
057: return "If is Color";
058: }
059:
060: public void execute_void(ProgramChromosome c, int n, Object[] args) {
061: check(c);
062: boolean condition;
063: if (m_type == CommandGene.IntegerClass) {
064: condition = c.execute_int(n, 0, args) == m_color;
065: } else if (m_type == CommandGene.LongClass) {
066: condition = c.execute_long(n, 0, args) == m_color;
067: } else {
068: throw new IllegalStateException(
069: "IfColor: cannot process type " + m_type);
070: }
071: if (condition) {
072: c.execute_void(n, 1, args);
073: }
074: }
075:
076: public int execute_int(ProgramChromosome c, int n, Object[] args) {
077: check(c);
078: int value = c.execute_int(n, 0, args);
079: if (value == m_color) {
080: c.execute_void(n, 1, args);
081: }
082: return value;
083: }
084:
085: /**
086: * Determines which type a specific child of this command has.
087: *
088: * @param a_ind ignored here
089: * @param a_chromNum index of child
090: * @return type of the a_chromNum'th child
091: *
092: * @author Klaus Meffert
093: * @since 3.2
094: */
095: public Class getChildType(IGPProgram a_ind, int a_chromNum) {
096: if (a_chromNum == 0) {
097: return m_type;
098: }
099: return CommandGene.VoidClass;
100: }
101:
102: public Object clone() {
103: try {
104: int[] subChildTypes = getSubChildTypes();
105: if (subChildTypes != null) {
106: subChildTypes = (int[]) subChildTypes.clone();
107: }
108: IfColor result = new IfColor(getGPConfiguration(), m_type,
109: m_color, getSubReturnType(), subChildTypes);
110: return result;
111: } catch (Throwable t) {
112: throw new CloneException(t);
113: }
114: }
115: }
|