package ec.gp.syntactic.simplification.numeric;

import ec.EvolutionState;
import ec.app.semanticGP.func.IConstant;
import ec.app.semanticGP.func.numeric.Constant;
import ec.app.semanticGP.func.numeric.Mul;
import ec.gp.GPNode;
import ec.gp.syntactic.simplification.IRule;
import ec.util.Parameter;

class MulZeroRule implements IRule {

	private EvolutionState state;

	@Override
	public void setup(EvolutionState state, Parameter base) {
		this.state = state;
	}

	@Override
	public boolean match(GPNode subtree) {
		if (!(subtree instanceof Mul))
			return false;

		Mul m = (Mul) subtree;
		return (m.children[0] instanceof IConstant && ((IConstant<Double>) m.children[0]).getConstant() == 0.0)
				||
				(m.children[1] instanceof IConstant && ((IConstant<Double>) m.children[1]).getConstant() == 0.0);
	}

	@Override
	public GPNode replace(GPNode subtree) {
		assert this.match(subtree) : "The rule does not match the given subtree.";

		return Constant.ZERO;
	}

}
