/**
  * Spawns the child task.
  *
  * @see jbt.execution.core.ExecutionTask#internalSpawn()
  */
 protected override void InternalSpawn()
 {
     child = ((ModelDecorator) ModelTask).getChild().CreateExecutor(
         Executor, this);
     child.AddTaskListener(this);
     child.Spawn(Context);
 }
 /**
  * Spawns the only child.
  *
  * @see jbt.execution.core.ExecutionTask#internalSpawn()
  */
 protected override void InternalSpawn()
 {
     /* Just spawn the only child. */
     child = ((ModelInverter) ModelTask).getChild().CreateExecutor(
         Executor, this);
     child.AddTaskListener(this);
     child.Spawn(Context);
 }
 /**
  * Spawns the first child of the sequence.
  *
  * @see jbt.execution.core.ExecutionTask#internalSpawn()
  */
 protected override void InternalSpawn()
 {
     _activeChildIndex = 0;
     _children = ModelTask.Children;
     _activeChild = _children[0].CreateExecutor(Executor, this);
     _activeChild.AddTaskListener(this);
     _activeChild.Spawn(Context);
 }
 /**
  * Spawns the child task. This method creates a new HierarchicalContext,
  * sets its parent to the context of the ExecutionHierarchicalContextManager, and spawns
  * the child task using this HierarchicalContext.
  *
  * @see jbt.execution.core.ExecutionTask#internalSpawn()
  */
 protected override void InternalSpawn()
 {
     var newContext = new HierarchicalContext();
     newContext.SetParent(Context);
     _child = ((ModelDecorator) ModelTask).getChild().CreateExecutor(
         Executor, this);
     _child.AddTaskListener(this);
     _child.Spawn(newContext);
 }
 /**
  * Spawns the child task. This method creates a new SafeOutputContext, and
  * spawns the child task using this SafeContext. The input context of the
  * SafeOutputContext is that of this ExecutionSafeOutputContextManager task.
  * The list of output variables of the SafeOutputContext is retrieved from
  * the ModelSafeOutputContextManager associated to this task.
  *
  * @see jbt.execution.core.ExecutionTask#internalSpawn()
  */
 protected override void InternalSpawn()
 {
     var newContext = new SafeOutputContext(Context,
         ((ModelSafeOutputContextManager) ModelTask).getOutputVariables());
     child = ((ModelDecorator) ModelTask).getChild().CreateExecutor(
         Executor, this);
     child.AddTaskListener(this);
     child.Spawn(newContext);
 }
        /**
         * Spawns the first task (randomly selected).
         *
         * @see jbt.execution.core.ExecutionTask#internalSpawn()
         */
        protected override void InternalSpawn()
        {
            _children = ModelTask.Children;
            /*
             * First we initialize the list with the order in which the list of
             * children will be evaluated.
             */
            _order = new List<int>();
            for (int i = 0; i < _children.Count; i++)
            {
                _order.Add(i);
            }

            _random = _random ?? new Random();
            _order = _order.OrderBy(i => _random.Next()).ToList();

            /*
             * Then we spawn the first child.
             */
            _activeChildIndex = 0;
            _activeChild = _children[_order[_activeChildIndex]].CreateExecutor(Executor, this);
            _activeChild.AddTaskListener(this);
            _activeChild.Spawn(Context);
        }
        /**
         * Checks the status of the currently active child. If it is running,
         * {@link Status#RUNNING} is returned. If it has finished in failure,
         * {@link Status#FAILURE} is returned. If it has finished successfully, it
         * tries to spawn the next child (and returns {@link Status#RUNNING}). If it
         * was the last child of the sequence, returns {@link Status#SUCCESS}.
         *
         * @see jbt.execution.core.ExecutionTask#internalTick()
         */
        protected override Status InternalTick()
        {
            Status childStatus = _activeChild.Status;

            if (childStatus == Status.Running)
            {
                return Status.Running;
            }
            if (childStatus == Status.Success)
            {
                /* If it was the last child of the sequence, returns success. */
                if (_activeChildIndex == _children.Count - 1)
                {
                    return Status.Success;
                }

                _activeChildIndex++;
                _activeChild = _children[_order[_activeChildIndex]].CreateExecutor(Executor, this);
                _activeChild.AddTaskListener(this);
                _activeChild.Spawn(Context);
                return Status.Running;
            }
            return Status.Failure;
        }
        /**
         * Checks if the currently active child has finished. It it has not, it
         * returns {@link Status#RUNNING}. If it has finished successfully, it
         * returns {@link Status#SUCCESS}. If it has finished in failure, then:
         * <ul>
         * <li>If it was the last child of the selector, returns
         * {@link Status#FAILURE}.
         * <li>Otherwise, it spawns the next child of the selector and returns
         * {@link Status#RUNNING}.
         * </ul>
         *
         * @see jbt.execution.core.ExecutionTask#internalTick()
         */
        protected override Status InternalTick()
        {
            Status childStatus = _activeChild.Status;

            if (childStatus == Status.Running)
            {
                return Status.Running;
            }
            if (childStatus == Status.Success)
            {
                return Status.Success;
            }
            /*
             * If the current child has failed, and it was the last one, return
             * failure.
             */
            if (_activeChildIndex == _children.Count - 1)
            {
                return Status.Failure;
            }
            /*
                 * Otherwise, if it was not the last child, spawn the next
                 * child.
                 */
            _activeChildIndex++;
            _activeChild = _children[_activeChildIndex].CreateExecutor(Executor, this);
            _activeChild.AddTaskListener(this);
            _activeChild.Spawn(Context);
            return Status.Running;
        }
        /**
         * Spawns the first child with active guard. If there is no active guard,
         * the spawning process is considered to have failed, so
         * {@link #internalTick()} will return {@link Status#FAILURE}. If some
         * guards are still running the spawning process is not considered to have
         * started yet.
         *
         * @see jbt.execution.core.ExecutionTask#internalSpawn()
         */
        protected override void InternalSpawn()
        {
            _children = ModelTask.Children;

            /* Initialize guard executors. */
            _guardsExecutors = new List<BTExecutor>();
            guardsResults = new List<Status>();
            foreach (ModelTask child in _children)
            {
                if (child.Guard != null)
                {
                    _guardsExecutors.Add(new BTExecutor(child.Guard, Context));
                    guardsResults.Add(Status.Running);
                }
                else
                {
                    _guardsExecutors.Add(null);
                    guardsResults.Add(Status.Success);
                }
            }

            /* Evaluate guards. */
            resetGuardsEvaluation();
            Tuple<Status, int> activeGuard = evaluateGuards();

            /*
             * Flag that tells if the static priority list must be inserted into the
             * list of tickable nodes.
             */
            bool insertIntoTickableNodesList = false;

            /*
             * If all the guards have failed, the spawning process has also failed.
             * In such a case, the task must be inserted into the list of tickable
             * nodes.
             */
            if (activeGuard.Item1 == Status.Failure)
            {
                spawnFailed = true;
                insertIntoTickableNodesList = true;
            }
            else if (activeGuard.Item1 == Status.Running)
            {
                /*
             * If not all the guards have been evaluated yet, the spawning
             * process is not considered to have started. In such a case, the
             * task must be inserted into the list of tickable nodes.
             */
                stillNotSpawned = true;
                insertIntoTickableNodesList = true;
            }
            else
            {
                /*
             * If all the guards have been evaluated and one succeeded, spawn
             * the corresponding child.
             */
                spawnFailed = false;
                stillNotSpawned = false;
                _activeChildIndex = activeGuard.Item2;
                _activeChild = _children[_activeChildIndex].CreateExecutor(
                    Executor, this);
                _activeChild.AddTaskListener(this);
                _activeChild.Spawn(Context);
            }

            /* Insert into the list of tickable nodes if required. */
            if (insertIntoTickableNodesList)
            {
                Executor.RequestInsertionIntoList(BTExecutor.BTExecutorList.Tickable, this);
            }
        }
        /**
         * If the spawning process has not finished yet (because there are some
         * guards running), then this method keeps evaluating the guards, and
         * returns {@link Status#RUNNING}. Whenever there is an active child
         * (because the spawning process has finished), its status is returned.
         * <p>
         * If the spawning process failed, this method just returns
         * {@link Status#FAILURE}.
         *
         * @see jbt.execution.core.ExecutionTask#internalTick()
         */
        protected override Status InternalTick()
        {
            /* If the spawning process failed, return failure. */
            if (spawnFailed)
            {
                return Status.Failure;
            }

            /*
             * If no child has been spawned yet (not all the guards had completed in
             * the internalSpawn() method)...
             */
            if (stillNotSpawned)
            {
                /* Evaluate guards. */
                Tuple<Status, int> activeGuard = evaluateGuards();

                /* If all the guards have failed, return failure. */
                if (activeGuard.Item1 == Status.Failure)
                {
                    return Status.Failure;
                }
                if (activeGuard.Item1 == Status.Running)
                {
                    /*
                 * If not all the guards have finished, do no nothing (return
                 * RUNNING).
                 */
                }
                else
                {
                    /*
                 * If all the guards have been evaluated and one succeeded,
                 * spawn the child. In this case, the static priority list
                 * must be removed from the list of tickable nodes.
                 */
                    spawnFailed = false;
                    stillNotSpawned = false;
                    _activeChildIndex = activeGuard.Item2;
                    _activeChild = _children[_activeChildIndex].CreateExecutor(
                        Executor, this);
                    _activeChild.AddTaskListener(this);
                    _activeChild.Spawn(Context);

                    Executor.RequestRemovalFromList(BTExecutor.BTExecutorList.Tickable, this);
                }

                return Status.Running;
            }

            /* If this point has been reached, there must be an active child. */
            return _activeChild.Status;
        }
        /**
         * Checks if there is an active guard with a priority higher than that of the active child. If
         * there is such a task, it terminates the active child and spawns the child of the guard with
         * higher priority, and {@link Status#RUNNING} is returned. If there is no such task, then the
         * status of the active child is returned.
         * <p>
         * If the spawning process failed, this method just returns {@link Status#FAILURE}. If the
         * spawning process has not finished yet, this method keeps evaluating the guards, and returns
         * {@link Status#RUNNING}.
         *
         * @see jbt.execution.core.ExecutionTask#internalTick()
         */
        protected override Status InternalTick()
        {
            /* If the spawning process failed, return failure. */
            if (_spawnFailed)
            {
                return Status.Failure;
            }

            /* Evaluate guards. */
            Tuple<Status, int> activeGuard = EvaluateGuards();

            /*
             * If no child has been spawned yet (not all the guards had completed yet in the
             * internalSpawn() method)...
             */
            if (_stillNotSpawned)
            {
                /* If all the guards have failed, return failure. */
                if (activeGuard.Item1 == Status.Failure)
                {
                    return Status.Failure;
                }
                if (activeGuard.Item1 == Status.Running)
                {
                    /*
                 * If not all the guards have finished, do no nothing (return RUNNING).
                 */
                }
                else
                {
                    /*
                 * If all the guards have been evaluated and one succeeded, spawn the child.
                 */
                    _spawnFailed = false;
                    _stillNotSpawned = false;
                    _activeChildIndex = activeGuard.Item2;
                    _activeChild = _children[_activeChildIndex].CreateExecutor(Executor, this);
                    _activeChild.AddTaskListener(this);
                    _activeChild.Spawn(Context);

                    /* Reset the guards evaluators. */
                    ResetGuardsEvaluation();
                }

                return Status.Running;
            }

            /* If this point has been reached, there must be an active child. */
            if (activeGuard.Item1 == Status.Failure)
            {
                /* If all the guards have failed, return failure. */
                return Status.Failure;
            }
            if (activeGuard.Item1 == Status.Running)
            {
                /*
             * If the guards are being evaluated, return the status of the active child.
             */
                return _activeChild.Status;
            }
            if (activeGuard.Item2 != _activeChildIndex)
            {
                /*
                 * If the child with the highest priority guard has changed, terminate the currently
                 * active child.
                 */
                if (_activeChild.ModelTask.Interrupter != null)
                {
                    var executor = new BTExecutor(_activeChild.ModelTask.Interrupter, Context);
                    ExecutionInterrupter.RunInterrupterBranch(executor, 10);
                }

                _activeChild.Terminate();
                _activeChildIndex = activeGuard.Item2;

                /*
                 * Spawn the new child.
                 */
                _activeChild = _children[_activeChildIndex].CreateExecutor(Executor, this);
                _activeChild.AddTaskListener(this);
                _activeChild.Spawn(Context);

                ResetGuardsEvaluation();
                return Status.Running;
            }
            /*
                 * If the child with the highest priority guard has not changed, return the status
                 * of the active child.
                 */
            ResetGuardsEvaluation();
            return _activeChild.Status;
        }
        ///Spawns its child and registers itself into the list of interrupters of
        ///the BTExecutor.
        ///
        protected override void InternalSpawn()
        {
            _executionChild = ((ModelInterrupter)ModelTask).getChild().CreateExecutor(Executor, this);
            _executionChild.AddTaskListener(this);

            //Register the ExecutionInterrupter so that ExecutionPerformInterruption can find it.
            Executor.RegisterInterrupter(this);
            _executionChild.Spawn(Context);
        }
        /**
         * This method first retrieve from the context the tree (ModelTask) that is
         * going to be emulated by this task. Then, it creates its corresponding
         * executor and finally spawns it. If the tree cannot be found in the
         * context, does nothing.
         *
         * @see jbt.execution.core.ExecutionTask#internalSpawn()
         */
        protected override void InternalSpawn()
        {
            /* Retrieve the tree to run from the context. */
            treeToRun = Context.GetBT(
                ((ModelSubtreeLookup)ModelTask).TreeName);

            if (treeToRun == null)
            {
                treeRetrieved = false;
                /*
             * Must request to be inserted into the list of tickable nodes,
             * since no tree has been retrieved and as a result it must be the
             * task the one continuin the work.
             */
                Executor.RequestInsertionIntoList(BTExecutor.BTExecutorList.Tickable, this);
                //			System.err.println("Could not retrieve tree "
                //					+ ((ModelSubtreeLookup) this.getModelTask()).getTreeName()
                //					+ " from the context. Check if the context has been properly initialized.");
            }
            else
            {
                treeRetrieved = true;
                /* Compute positions for the retrieved tree. */
                treeToRun.ComputePositions();

                executionTree = treeToRun.CreateExecutor(Executor, this);
                executionTree.AddTaskListener(this);
                executionTree.Spawn(Context);
            }
        }
        /**
         * If the child task has finished, it spawns it again. Always returns
         * {@link Status#RUNNING}.
         *
         * @see jbt.execution.core.ExecutionTask#internalTick()
         */
        protected override Status InternalTick()
        {
            Status childStatus = _child.Status;

            /*
             * If the child has finished, spawn it again
             */
            if (childStatus != Status.Running)
            {
                _child = ((ModelDecorator) ModelTask).getChild().CreateExecutor(Executor, this);
                _child.AddTaskListener(this);
                _child.Spawn(Context);
            }

            return Status.Running;
        }
        /**
         * If the child has finished in failure or been terminated, return
         * {@link Status#SUCCESS}. Otherwise, {@link Status#RUNNING} is returned. If
         * the child has finished successfully, it is spawned again.
         *
         * @see jbt.execution.core.ExecutionTask#internalTick()
         */
        protected override Status InternalTick()
        {
            Status childStatus = child.Status;

            /*
             * If the child has finished in failure or been terminated, return
             * success.
             */
            if (childStatus == Status.Failure || childStatus == Status.Terminated)
            {
                return Status.Success;
            }
            /* If the child has finished successfully, spawn it again. */
            if (childStatus == Status.Success)
            {
                child = ((ModelDecorator) ModelTask).getChild().CreateExecutor(
                    Executor, this);
                child.AddTaskListener(this);
                child.Spawn(Context);
            }

            /*
             * In case the child has not finished in failure, return
             * Status.RUNNING.
             */
            return Status.Running;
        }
 /**
  * Spawns the child task if it has not been run more than the maximum
  * allowed number of times. Otherwise, it requests to be inserted into the
  * list of tickable nodes, since the child is not spawned.
  *
  * @see jbt.execution.core.ExecutionTask#internalSpawn()
  */
 protected override void InternalSpawn()
 {
     if (_numRunsSoFar < _maxNumTimes)
     {
         _numRunsSoFar++;
         _child = ((ModelLimit) ModelTask).getChild().CreateExecutor(Executor, this);
         _child.AddTaskListener(this);
         _child.Spawn(Context);
     }
     else
     {
         Executor.RequestInsertionIntoList(BTExecutor.BTExecutorList.Tickable, this);
     }
 }
 /**
  * Checks if the currently active child has finished. If it has not
  * finished, returns {@link Status#SUCCESS}. If it has finished in failure,
  * returns {@link Status#FAILURE}. If it has finished successfully, it
  * checks if there is any remaining child. If so, it spawns it. Otherwise,
  * returns {@link Status#SUCCESS}.
  */
 protected override Status InternalTick()
 {
     Status childStatus = _activeChild.Status;
     if (childStatus == Status.Running)
     {
         return Status.Running;
     }
     if (childStatus == Status.Failure || childStatus == Status.Terminated)
     {
         return Status.Failure;
     }
     if (_activeChildIndex == _children.Count - 1)
     {
         /*
          * If this was the last child, return success.
          */
         return Status.Success;
     }
     /*
          * If the current child has finished successfully, but it is not
          * the last one, spawn the next child.
          */
     _activeChildIndex++;
     _activeChild = _children[_activeChildIndex].CreateExecutor(Executor, this);
     _activeChild.AddTaskListener(this);
     _activeChild.Spawn(Context);
     return Status.Running;
 }
        /**
         * Spawns the first child with active guard. It also requests to be inserted into the list of
         * tickable nodes of the BTExecutor, since this task has to check its children's guards all the
         * time. If there is no active guard, the spawning process is considered to have failed, so
         * {@link #internalTick()} will return {@link Status#FAILURE}. If some guards are still running
         * the spawning process is not considered to have started yet.
         *
         * @see jbt.execution.core.ExecutionTask#internalSpawn()
         */
        protected override void InternalSpawn()
        {
            /*
             * The dynamic priority list has to be inserted into the list of tickable nodes because it
             * has to check its children's guards all the time.
             */
            Executor.RequestInsertionIntoList(BTExecutor.BTExecutorList.Tickable, this);

            _children = ModelTask.Children;

            /* Initialize guard executors. */
            _guardsExecutors = new List<BTExecutor>();
            _guardsResults = new List<Status>();

            foreach (var child in _children)
            {
                if (child.Guard != null)
                {
                    _guardsExecutors.Add(new BTExecutor(child.Guard, Context));
                    _guardsResults.Add(Status.Running);
                }
                else
                {
                    _guardsExecutors.Add(null);
                    _guardsResults.Add(Status.Success);
                }
            }

            /* Evaluate guards. */
            ResetGuardsEvaluation();
            var activeGuard = EvaluateGuards();

            /* If all guards have failed, the spawning process has also failed. */
            if (activeGuard.Item1 == Status.Failure)
            {
                _spawnFailed = true;
            }
            else if (activeGuard.Item1 == Status.Running)
            {
                /*
                 * If not all the guards have been evaluated yet, the spawning process is not considered
                 * to have started.
                 */
                _stillNotSpawned = true;
            }
            else
            {

                // If all the guards have been evaluated and one succeeded, spawn the corresponding
                //  child.

                _spawnFailed = false;
                _stillNotSpawned = false;
                _activeChildIndex = activeGuard.Item2;
                _activeChild = _children[_activeChildIndex].CreateExecutor(Executor, this);
                _activeChild.AddTaskListener(this);
                _activeChild.Spawn(Context);

                /* Reset the guards evaluators. */
                ResetGuardsEvaluation();
            }
        }