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 licencing 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.impl.*;
015:
016: /**
017: * Pops a value from the stack after it has been pushed onto it (PushCommand)
018: *
019: * @author Klaus Meffert
020: * @since 3.0
021: */
022: public class Pop extends MathCommand {
023: /** String containing the CVS revision. Read out via reflection!*/
024: private final static String CVS_REVISION = "$Revision: 1.6 $";
025:
026: public Pop(final GPConfiguration a_conf, Class a_type)
027: throws InvalidConfigurationException {
028: this (a_conf, a_type, 0);
029: }
030:
031: public Pop(final GPConfiguration a_conf, Class a_type,
032: int a_subReturnType) throws InvalidConfigurationException {
033: super (a_conf, 0, a_type, a_subReturnType, null);
034: }
035:
036: public String toString() {
037: return "pop &1";
038: }
039:
040: /**
041: * @return textual name of this command
042: *
043: * @author Klaus Meffert
044: * @since 3.2
045: */
046: public String getName() {
047: return "Pop";
048: }
049:
050: public int execute_int(ProgramChromosome c, int n, Object[] args) {
051: check(c);
052: // Pop from stack.
053: // ---------------
054: if (getGPConfiguration().stackSize() < 1) {
055: throw new IllegalStateException("pop without push");
056: }
057: return ((Integer) getGPConfiguration().popFromStack())
058: .intValue();
059: }
060:
061: public long execute_long(ProgramChromosome c, int n, Object[] args) {
062: check(c);
063: if (getGPConfiguration().stackSize() < 1) {
064: throw new IllegalStateException("pop without push");
065: }
066: return ((Long) getGPConfiguration().popFromStack()).longValue();
067: }
068:
069: public double execute_double(ProgramChromosome c, int n,
070: Object[] args) {
071: check(c);
072: if (getGPConfiguration().stackSize() < 1) {
073: throw new IllegalStateException("pop without push");
074: }
075: return ((Double) getGPConfiguration().popFromStack())
076: .doubleValue();
077: }
078:
079: public float execute_float(ProgramChromosome c, int n, Object[] args) {
080: check(c);
081: if (getGPConfiguration().stackSize() < 1) {
082: throw new IllegalStateException("pop without push");
083: }
084: return ((Float) getGPConfiguration().popFromStack())
085: .floatValue();
086: }
087:
088: public Object execute_object(ProgramChromosome c, int n,
089: Object[] args) {
090: check(c);
091: if (getGPConfiguration().stackSize() < 1) {
092: throw new IllegalStateException("pop without push");
093: }
094: return getGPConfiguration().popFromStack();
095: }
096:
097: public boolean isValid(ProgramChromosome a_program) {
098: return a_program.getCommandOfClass(0, Push.class) >= 0;
099: }
100: }
|