package at.oefai.aaa.agent.jam;

import java.util.Vector;

/**
 * Represents a parallel-execution construct within plans
 * @author Marc Huber
 * @author Jaeho Lee
 */
class PlanParallelConstruct implements PlanConstruct {

    private Vector<PlanSequenceConstruct> threads; // Vector of PlanSequenceConstructs

    /**  */
    PlanParallelConstruct() {
        this.threads = new Vector<PlanSequenceConstruct>(1, 1);
    }

    /** Create a number of sequences of constructs and actions that will be managed as threads by this construct. */
    PlanParallelConstruct(PlanConstruct be) {
        this.threads = new Vector<PlanSequenceConstruct>(1, 1);
        if (be != null) {
            if (be instanceof PlanSequenceConstruct) {
                this.threads.addElement((PlanSequenceConstruct) be);
            } else {
                PlanSequenceConstruct s;
                s = new PlanSequenceConstruct(be);
                this.threads.addElement(s);
            }
        }
    }

    // Member functions
    Vector getConstructs() { return this.threads; }

    int getNumConstructs() { return this.threads.size(); }

    /**  */
    public PlanRuntimeState newRuntimeState() {
        return new PlanRuntimeParallelState(this);
    }

    /** Add an action/construct to this sequence of actions/constructs */
    void insertConstruct(PlanConstruct be) {
        if (be != null) {
            if (be instanceof PlanSequenceConstruct) {
                this.threads.addElement((PlanSequenceConstruct) be);
            } else {
                PlanSequenceConstruct s;
                s = new PlanSequenceConstruct(be);
                this.threads.addElement(s);
            }
        }
    }

    /** Return the indicated construct in the construct/action sequence */
    PlanConstruct getConstruct(int n) {
        try {
            return this.threads.elementAt(n);
        } catch (ArrayIndexOutOfBoundsException e) {
            return null;
        }
    }

}

