package ec.gp.syntactic.simplification.numeric;

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

class MulPowPowRule implements IRule {

	@Override
	public void setup(EvolutionState state, Parameter base) {
		// TODO Auto-generated method stub

	}

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

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

		return subtree.children[0].children[0].rootedTreeEquals(subtree.children[1].children[0]);
	}

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

		Sum sum = new Sum();
		sum.children = new GPNode[] { subtree.children[0].children[1], subtree.children[1].children[1] };

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

		return pow;
	}

}
