package ec.gp.syntactic.simplification.numeric;

import ec.EvolutionState;
import ec.app.semanticGP.func.numeric.Constant;
import ec.app.semanticGP.func.numeric.Div;
import ec.gp.GPNode;
import ec.gp.semantic.ISemantics;
import ec.gp.semantic.func.SimpleNodeBase;
import ec.gp.syntactic.simplification.IRule;
import ec.util.Parameter;

class DivSameRule 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 Div))
			return false;

		if (!subtree.children[0].rootedTreeEquals(subtree.children[1]))
			return false;

		/*ISemantics divisor = ((SimpleNodeBase<?>) subtree.children[1]).getSemantics();
		double[] sem = (double[]) divisor.getValue();
		for (int i = 0; i < sem.length; ++i) {
			if (sem[i] == 0.0 || Double.isInfinite(sem[i]) || Double.isNaN(sem[i]))
				return false; // if divisor is 0, our protected divisor returns 0, what is not consistent with this rule
		}*/

		return true;
	}

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

		Constant _const = new Constant(1.0);
		return _const;
	}

}
