package ac.essex.ooechs.facedetection.learners.gp;

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

/**
 * Based on Viola and Jones' idea of a classifier. This contains a value,
 * a threshold and a polarity.
 *
 * @author Olly Oechsle, University of Essex, Date: 18-May-2007
 * @version 1.0
 */
public class ClassifierNode extends Node {

    public ClassifierNode() {
        // I have three children
        super(3);
    }

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

    public int getChildType(int i) {
        switch (i) {
            case 0:
                // first child is always a haar feature
                return NodeParams.HAAR_FEATURE;
            case 1:
                // threshold
                return NodeParams.NUMBER;
            case 2:
                // polarity
                return NodeParams.BOOLEAN;
        }
        return NodeParams.NUMBER;
    }

    public double execute(DataStack data) {

        double value = child[0].execute(data);
        double threshold = child[1].execute(data);
        boolean polarity = child[2].execute(data) == 1;

        if (polarity) {
            data.value = value > threshold? 1 : 0;
        } else {
            data.value = value < threshold? 1 : 0;
        }

        return debugger.record(data.value);

    }

    public String toJava() {
        return child[2].getName() + "? " + child[0].getName() + " > " + child[1].getName() + " : "  + child[0].getName() + " < " + child[1].getName();
    }

}
