package ec.gp.syntactic.simplification.numeric;

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

class SumSumSumSameRule 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 Sum))
			return false;

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

		GPNode p1 = subtree.children[0].children[0];
		GPNode p2 = subtree.children[0].children[1];
		GPNode p3 = subtree.children[1].children[0];
		GPNode p4 = subtree.children[1].children[1];

		return p1.rootedTreeEquals(p2) && p1.rootedTreeEquals(p3) && p1.rootedTreeEquals(p4);
	}

	@Override
	public GPNode replace(GPNode subtree) {
		assert match(subtree) : "The given tree is not a (p + p) + (p + p)";

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

		return mul;
	}

}
