KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > function > IfElse


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 org.jgap.gp.function;
11
12 import org.jgap.*;
13 import org.jgap.gp.*;
14 import org.jgap.gp.impl.*;
15
16 /**
17  * The if-then-else construct.
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

22 public class IfElse
23     extends CommandGene {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.8 $";
26
27   private Class JavaDoc m_type;
28
29   public IfElse(final GPConfiguration a_conf, Class JavaDoc a_type)
30       throws InvalidConfigurationException {
31     this(a_conf, a_type, 0, null);
32   }
33
34   public IfElse(final GPConfiguration a_conf, Class JavaDoc a_type, int a_subReturnType,
35                 int[] a_subChildTypes)
36       throws InvalidConfigurationException {
37     super(a_conf, 3, CommandGene.VoidClass, a_subReturnType, a_subChildTypes);
38     m_type = a_type;
39   }
40
41   public String JavaDoc toString() {
42     return "if(&1) then (&2) else(&3)";
43   }
44
45   /**
46    * @return textual name of this command
47    *
48    * @author Klaus Meffert
49    * @since 3.2
50    */

51   public String JavaDoc getName() {
52     return "IfElse";
53   }
54
55   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
56     int x = c.execute_int(n, 0, args);
57     int value = 0;
58     if (x >= 0) {
59       value = c.execute_int(n, 1, args);
60     }
61     else {
62       value = c.execute_int(n, 2, args);
63     }
64     return value;
65   }
66
67   public long execute_long(ProgramChromosome c, int n, Object JavaDoc[] args) {
68     long x = c.execute_long(n, 0, args);
69     long value = 0;
70     if (x >= 0) {
71       value = c.execute_long(n, 1, args);
72     }
73     else {
74       value = c.execute_long(n, 2, args);
75     }
76     return value;
77   }
78
79   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
80     float x = c.execute_float(n, 0, args);
81     float value = 0;
82     if (x >= 0) {
83       value = c.execute_float(n, 1, args);
84     }
85     else {
86       value = c.execute_float(n, 2, args);
87     }
88     return value;
89   }
90
91   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
92     double x = c.execute_double(n, 0, args);
93     double value = 0;
94     if (x >= 0) {
95       value = c.execute_double(n, 1, args);
96     }
97     else {
98       value = c.execute_double(n, 2, args);
99     }
100     return value;
101   }
102
103   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
104     check(c);
105     boolean condition;
106     if (m_type == CommandGene.IntegerClass) {
107       condition = c.execute_int(n, 0, args) > 0;
108     }
109     else if (m_type == CommandGene.BooleanClass) {
110       condition = c.execute_boolean(n, 0, args);
111     }
112     else if (m_type == CommandGene.LongClass) {
113       condition = c.execute_long(n, 0, args) > 0;
114     }
115     else if (m_type == CommandGene.DoubleClass) {
116       condition = c.execute_double(n, 0, args) > 0;
117     }
118     else if (m_type == CommandGene.FloatClass) {
119       condition = c.execute_float(n, 0, args) > 0;
120     }
121     else {
122       throw new IllegalStateException JavaDoc("IfElse: cannot process type "+m_type);
123     }
124     if (condition) {
125       c.execute_void(n, 1, args);
126     }
127     else {
128       c.execute_void(n, 2, args);
129     }
130   }
131
132   /**
133    * Determines which type a specific child of this command has.
134    *
135    * @param a_ind ignored here
136    * @param a_chromNum index of child
137    * @return type of the a_chromNum'th child
138    *
139    * @author Klaus Meffert
140    * @since 3.2
141    */

142   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
143     if (a_chromNum == 0) {
144       return m_type;
145     }
146     return CommandGene.VoidClass;
147   }
148 }
149
Popular Tags