package ac.essex.gp.tree;

import ac.essex.gp.params.NodeParams;

/**
 * A terminal node is one that introduces data into the GP
 * system. It is usually connected in some way to the training data
 * (see subclasses of Terminal for how this is done). Terminals are
 * leaves on the tree with no children. A terminal may often have an idea
 * of what <i>kind</i> of data it outputs, so the getDefaultRangeType() method is also
 * provided. Tree builders can use this data to match certain terminals to certain
 * ERC types in the hopes of producing more useful code.
 * 
 * @author Olly Oechsle, University of Essex, Date: 18-Jan-2007
 * @version 1.0
 */
public abstract class Terminal extends Node {

    int rangeID = -1;

    public Terminal() {
        super(0); // no children
        // use the default autorange id
        rangeID = getDefaultRangeType();
    }

    /**
     * Being a terminal this node has no children.
     */
    public int getChildType(int index) {
        return NodeParams.VOID;
    }

    /**
     * Returns this node's output range. This value is set at compile time to give a rough
     * idea of the range this terminal operates in. If automatic range typing is enabled the
     * value returned by this method is overriden with a new one.
     */
    public abstract int getDefaultRangeType();

    /**
     * Returns this node's output range. This information may be
     * used by the tree builder to match this Terminal to an ERC whose
     * values fall in the same range. Note that this is the default range type.
     * If AutorangeERCs are used, this value is not used.
     */
    public int getRangeID() {
        return rangeID;
    }

    /**
     * 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(), rangeID, numChildren);
    }

    public void setRangeID(int autorangeID) {
        this.rangeID = autorangeID;
    }

}
