package ec.gp.syntactic.simplification.numeric;

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

class ConstRule implements IRule {

	private EvolutionState state;

	@Override
	public void setup(EvolutionState state, Parameter base) {
		this.state = state;
	}

	@Override
	public boolean match(GPNode subtree) {
		for (int ch = 0; ch < subtree.children.length; ++ch) {
			if (!(subtree.children[ch] instanceof IConstant)) {
				return false;
			}
		}

		return subtree.children.length > 0; // do not match terminals
	}

	@Override
	public GPNode replace(GPNode subtree) {
		assert match(subtree) : "Given subtree is not constant!";

		return new Constant((Double) ((SimpleNodeBase<Double>) subtree).getSemantics().getValue(0));
	}

}
