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 org.jgap.gp.function;
011:
012: import org.jgap.*;
013: import org.jgap.gp.*;
014: import org.jgap.gp.terminal.*;
015: import org.apache.commons.lang.builder.CompareToBuilder;
016: import org.apache.commons.lang.builder.EqualsBuilder;
017: import org.jgap.gp.impl.*;
018:
019: /**
020: * The for-loop loop from 0 to X-1.
021: *
022: * @author Klaus Meffert
023: * @since 3.0
024: */
025: public class ForXLoop extends CommandGene {
026: /** String containing the CVS revision. Read out via reflection!*/
027: private final static String CVS_REVISION = "$Revision: 1.9 $";
028:
029: private Class m_type;
030:
031: public ForXLoop(final GPConfiguration a_conf, Class a_type)
032: throws InvalidConfigurationException {
033: super (a_conf, 1, CommandGene.VoidClass);
034: m_type = a_type;
035: }
036:
037: public String toString() {
038: return "for(int i=0;i<X;i++) { &1 }";
039: }
040:
041: /**
042: * @return textual name of this command
043: *
044: * @author Klaus Meffert
045: * @since 3.2
046: */
047: public String getName() {
048: return "ForXLoop";
049: }
050:
051: public void execute_void(ProgramChromosome c, int n, Object[] args) {
052: check(c);
053: int index = c.getVariableWithReturnType(0, m_type);
054: if (index < 0) {
055: throw new IllegalStateException("Variable missing for forX");
056: }
057: /**@todo only consider variables appearing before FORX in the program tree*/
058: Variable var = (Variable) c.getNode(index);
059: int x;
060: // Get variable's value as integer ("for" cannot do anything else here).
061: // ---------------------------------------------------------------------
062: if (m_type == CommandGene.IntegerClass) {
063: x = ((Integer) var.getValue()).intValue();
064: } else if (m_type == CommandGene.LongClass) {
065: x = ((Long) var.getValue()).intValue();
066: } else if (m_type == CommandGene.DoubleClass) {
067: x = ((Double) var.getValue()).intValue();
068: } else if (m_type == CommandGene.FloatClass) {
069: x = ((Float) var.getValue()).intValue();
070: } else {
071: throw new RuntimeException("Type " + m_type
072: + " unknown in ForXCommand");
073: }
074: if (x > 15) {
075: x = 15;
076: }
077: for (int i = 0; i < x; i++) {
078: c.execute_void(n, 0, args);
079: }
080: }
081:
082: public boolean isValid(ProgramChromosome a_program) {
083: return a_program.getVariableWithReturnType(0, m_type) >= 0;
084: }
085:
086: public Class getChildType(IGPProgram a_ind, int a_chromNum) {
087: return CommandGene.VoidClass;
088: }
089:
090: public Class getReturnType() {
091: return super .getReturnType();
092: }
093:
094: /**
095: * The compareTo-method.
096: * @param a_other the other object to compare
097: * @return -1, 0, 1
098: *
099: * @author Klaus Meffert
100: * @since 3.0
101: */
102: public int compareTo(Object a_other) {
103: if (a_other == null) {
104: return 1;
105: } else {
106: ForXLoop other = (ForXLoop) a_other;
107: return new CompareToBuilder().append(m_type, other.m_type)
108: .toComparison();
109: }
110: }
111:
112: /**
113: * The equals-method.
114: * @param a_other the other object to compare
115: * @return true if the objects are seen as equal
116: *
117: * @author Klaus Meffert
118: * @since 3.0
119: */
120: public boolean equals(Object a_other) {
121: if (a_other == null) {
122: return false;
123: } else {
124: try {
125: ForXLoop other = (ForXLoop) a_other;
126: return new EqualsBuilder().append(m_type, other.m_type)
127: .isEquals();
128: } catch (ClassCastException cex) {
129: return false;
130: }
131: }
132: }
133: }
|