protected void resetChildren() {
   Collection c = getChildren();
   if (c != null) {
     Iterator it = c.iterator();
     while (it.hasNext()) {
       Behaviour b = (Behaviour) it.next();
       b.reset();
     }
   }
 }
  /**
   * Associates this behaviour with the agent it belongs to. Overrides the method in the base class
   * to propagate the setting to all children.
   *
   * @param a The agent this behaviour belongs to.
   * @see Behaviour#setAgent(Agent a)
   */
  public void setAgent(Agent a) {
    Collection c = getChildren();
    if (c != null) {
      Iterator it = c.iterator();
      while (it.hasNext()) {
        Behaviour b = (Behaviour) it.next();
        b.setAgent(a);
      }
    }

    super.setAgent(a);
  }
  /**
   * Puts a <code>CompositeBehaviour</code> back in initial state. The internal state is cleaned up
   * and <code>reset()</code> is recursively called for each child behaviour.
   */
  public void reset() {
    resetChildren();

    starting = true;
    finished = false;
    super.reset();
  }
  /** This method is used internally by the framework. Developer should not call or redefine it. */
  public void handleRestartEvent() {
    // Notify downwards
    myEvent.init(true, NOTIFY_DOWN);
    handle(myEvent);

    // Then notify upwards
    super.handleRestartEvent();
  }
  /** This method is used internally by the framework. Developer should not call or redefine it. */
  protected void handleBlockEvent() {
    // Notify upwards
    super.handleBlockEvent();

    // Then notify downwards
    myEvent.init(false, NOTIFY_DOWN);
    handle(myEvent);
  }
 /** Remove a sub behaviour from this <code>SequentialBehaviour</code> */
 public void removeSubBehaviour(Behaviour b) {
   boolean rc = subBehaviours.remove(b);
   if (rc) {
     b.setParent(null);
   } else {
     // The specified behaviour was not found. Do nothing
   }
 }
  /**
   * Executes this <code>CompositeBehaviour</code>. This method executes children according to the
   * scheduling policy defined by concrete subclasses that implements the <code>scheduleFirst()
   * </code> and <code>scheduleNext()</code> methods.
   */
  public final void run() {
    if (starting) {
      scheduleFirst();
      starting = false;
    } else {
      if (currentExecuted) {
        scheduleNext(currentDone, currentResult);
      }
    }

    // Get the current child
    Behaviour current = getCurrent();
    currentExecuted = false;
    currentDone = false;
    currentResult = 0;

    if (current != null) {
      if (current.isRunnable()) {
        // Execute the current child
        current.actionWrapper();
        currentExecuted = true;

        // If it is done --> call its onEnd() method
        if (current.done()) {
          currentDone = true;
          currentResult = current.onEnd();
        }

        // Check if this CompositeBehaviour is finished
        finished = checkTermination(currentDone, currentResult);
      } else {
        // The currently scheduled child is not runnable --> This
        // Composite behaviour must block too and notify upwards
        myEvent.init(false, NOTIFY_UP);
        super.handle(myEvent);
      }
    } else {
      // There are no children to execute
      finished = true;
    }
  }
 // #APIDOC_EXCLUDE_BEGIN
 protected void registerAsChild(Behaviour b) {
   b.setParent(this);
 }
 /** Add a sub behaviour to this <code>SequentialBehaviour</code> */
 public void addSubBehaviour(Behaviour b) {
   subBehaviours.add(b);
   b.setParent(this);
   b.setAgent(myAgent);
 }