package ec.gp.semantic.geometry;

import ec.EvolutionState;
import ec.gp.*;
import ec.gp.semantic.ISemanticProblem;
import ec.gp.semantic.func.SimpleNodeBase;
import ec.util.Parameter;
import library.semantics.TestCase;

import java.util.*;

/**
 * Creates Boolean programs, which semantics are guaranteed to constitute a basis.
 *
 * @author Tomasz Pawlak
 */
public class BooleanBasisConstructor extends GPNodeBuilder {
    private final static Parameter DEFAULT_BASE = new Parameter("LC");

    private final BooleanInterpolator interpolator = new BooleanInterpolator();
    private Queue<SimpleNodeBase<?>> programs = new ArrayDeque<>();

    @Override
    public Parameter defaultBase() {
        return DEFAULT_BASE;
    }

    @Override
    public GPNode newRootedTree(EvolutionState state, GPType type, int thread, GPNodeParent parent, GPFunctionSet set, int argposition, int requestedSize) {
        if (this.programs.size() == 0) {
            // build base here
            ISemanticProblem<Boolean> problem = (ISemanticProblem<Boolean>) state.evaluator.p_problem;
            List<TestCase<Boolean>> fitnessCases = problem.getFitnessCases();

            this.programs.addAll(this.interpolator.getBasePrograms(state, fitnessCases, true));
        }

        return this.programs.poll();
    }
}
