package at.oefai.aaa.agent.jam;

import java.util.ListIterator;

import at.oefai.aaa.agent.jam.types.Binding;
import at.oefai.aaa.agent.jam.types.DList;
import at.oefai.aaa.agent.jam.types.Expression;

/**
 * A boolean-evaluable expression condition.
 * @author Marc Huber
 * @author Jaeho Lee
 */
class ExpressionCondition implements Condition {

    private final Expression expression;

    /**  */
    ExpressionCondition(Expression e) {
        super();
        this.expression = e;
    }

    // Member functions

    /** Remove from the given binding list the ones not satisfying the expression. */
    public boolean check(final DList<Binding> bl) {
        ListIterator<Binding> bli = bl.listIterator(); // needed for .remove()
        while (bli.hasNext()) {
            Binding b = bli.next();
            if (!this.expression.eval(b).isTrue()) {
                bli.remove();
            }
        }
        return (bl.size() > 0) ? true : false;
    }

    /** Confirm whether the binding is still valid against the current World Model. */
    public boolean confirm(final Binding b) {
        return this.expression.eval(b).isTrue();
    }

}

