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.Pow;
import ec.gp.GPNode;
import ec.gp.syntactic.simplification.IRule;
import ec.util.Parameter;

class PowPowConstConstRule 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 Pow))
			return false;

		if (!(subtree.children[0] instanceof Pow))
			return false;

		return subtree.children[1] instanceof IConstant && subtree.children[0].children[1] instanceof IConstant;
	}

	@Override
	public GPNode replace(GPNode subtree) {
		assert match(subtree) : "The given tree is not a form of (a^const1)^const2";

		double v1 = ((IConstant<Double>) subtree.children[1]).getConstant();
		double v2 = ((IConstant<Double>) subtree.children[0].children[1]).getConstant();

		Constant constant = new Constant(v1 * v2);

		Pow pow = new Pow();
		pow.children = new GPNode[] { subtree.children[0].children[0], constant };

		return pow;
	}

}
