package ec.gp.syntactic.simplification.logic;

import library.semantics.BitSet;
import ec.EvolutionState;
import ec.app.semanticGP.func.IConstant;
import ec.app.semanticGP.func.logic.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 true;
	}

	@Override
	public GPNode replace(GPNode subtree) {
		assert match(subtree);

		BitSet semantics = (BitSet) ((SimpleNodeBase<Boolean>) subtree).getSemantics().getValue();
		return Constant.get(semantics.get(0));
	}

}
