package at.oefai.aaa.agent.jam;

import java.io.Serializable;

import at.oefai.aaa.agent.jam.types.Binding;
import at.oefai.aaa.agent.jam.types.Value;
import at.oefai.aaa.agent.jam.types.Variable;

/**
 * A built-in JAM primitive action for binding plan variables with world
 * model entries and providing a means for access to ALL of the matching relations rather than just one as RETRIEVE does.
 * @author Marc Huber
 */
class RetrieveAllAction extends WorldModelAction implements Serializable {
    private final Variable variable;

    /** Constructor w/ relation to retrieve from the World Model as an argument in addition to the interpreter. */
    RetrieveAllAction(final Variable v, final Relation r, final WorldModelTable pWorldModel) {
        super(r, pWorldModel);
        this.variable = v;
    }

    // Member functions

    public boolean isExecutableAction() { return true; }

    /** Retrieve the relation from the World Model. */
    public Result execute(final Binding b, final Goal currentGoal) {
        WorldModelRelationIterator wmtbe = new WorldModelRelationIterator(this.worldModel, this.relation);
        // Bind the local variable to the wmtbe
        b.setValue(this.variable, Value.newValue(wmtbe));
        return Result.SUCCEEDED;
    }


    /** Output information to the stream in an in-line manner. */
    public String formattedString(final Binding b) {
        return "RETRIEVEALL " + this.relation.formattedString(b) + ";";
    }

}

