package ac.essex.gp.nodes.ercs;

import ac.essex.gp.tree.Node;
import ac.essex.gp.params.NodeParams;
import ac.essex.gp.util.DataStack;

/**
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version,
 * provided that any use properly credits the author.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details at http://www.gnu.org
 * </p>
 *
 * @author Olly Oechsle, University of Essex, Date: 15-Jan-2007
 * @version 1.0
 */
public abstract class BasicERC extends Node {

    public static final double NO_VALUE = Double.MIN_VALUE;
    protected double value = NO_VALUE;
    protected final int max = 20;

    public BasicERC() {
        super(0);       
    }

    public void setValue(double value) {
        this.value = value;
    }

    // START ERC METHODS
    public void mutate() {
       value = setValue();
    }

    public void jitter() {
        // jitter within 30% of the value
        double amount = 0.3;
        double change = 1.0 + (Math.random() * amount * 2);
        value = (int) (value * change);
    }
    // END ERC METHODS

    public double setValue() {
        return (int) (Math.random() * max) + 1;
    }

    public double getValue() {
        if (value == NO_VALUE) value = setValue();
        return value;
    }

    public int getReturnType() {
        return NodeParams.NUMBER;
    }

    public double execute(DataStack data) {
        data.value = getValue();
        return debugger.record(data.value);
    }

    public String toJava() {
        return String.valueOf(value);
    }

    public int getChildType(int index) {
        return NodeParams.VOID;
    }

    /**
     * Returns the range type of this ERC. If Range Typing is enabled, nodes can be matched
     * to ERCs which return values in the same range as the output.
     */
    public abstract int getRangeID();
    
    /**
     * Standard nodes don't have range types, but Terminals do, so we need to override the NodeParams object.
     * @return
     */
    public NodeParams createNodeParamsObject() {
        return new NodeParams(getClass().getCanonicalName(), getReturnType(), getRangeID(), numChildren);
    }

}
