package ec.gp.syntactic.simplification.numeric;

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

class MulSqrtSqrtRule 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;

		Mul m = (Mul) subtree;
		return m.children[0] instanceof Sqrt && m.children[1] instanceof Sqrt;
	}

	@Override
	public GPNode replace(GPNode subtree) {
		assert match(subtree) : "The given tree is not sqrt(p1) * sqrt(p2).";

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

		Sqrt sqrt = new Sqrt();
		sqrt.children = new GPNode[] { mul };

		return sqrt;
	}

}
