Ejemplo n.º 1
0
        /// <summary>
        /// Used to kick off execution of a node with a default execution context for all subjects using Async WhenAll semantics internally .
        /// </summary>
        /// <param name="subjects">Subject to be moved through the node.</param>
        /// <param name="options">Execution options to apply to running this enumerable of subjects.</param>
        /// <returns>An aggregated NodeResult.</returns>
        public async Task <NodeResult> ExecuteManyAsync(IEnumerable <T> subjects, ExecutionOptions options = null)
        {
            Guard.AgainstNullArgument("subjects", subjects);

            var nodeTimer = new NodeTimer();

            try
            {
                nodeTimer.LogStart(LogWriter, this, "ExecuteManyAsync");
                _processManyMode = true;
                Result           = new NodeResult(default(T), Id, FlowId);

                var subjectList = subjects.ToList();

                if (subjectList.Count == 0)
                {
                    return(Result);
                }

                if (options == null)
                {
                    options = new ExecutionOptions();
                }

                LogWriter.Debug("Running all subjects asynchronously.");

                Task aggregateTask = null;
                try
                {
                    var resultsQueue = new ConcurrentQueue <NodeResult>();
                    aggregateTask = subjectList.ForEachAsync(options.DegreeOfParallelism,
                                                             async x => resultsQueue.Enqueue(await ExecuteAsync(new ExecutionContext <T>(x, options))));

                    await aggregateTask;

                    Result.AddChildResults(resultsQueue);
                }
                catch
                {
                    if (options.ThrowOnError)
                    {
                        if (aggregateTask != null && aggregateTask.Exception != null)
                        {
                            throw aggregateTask.Exception;
                        }

                        throw;
                    }
                }

                ProcessExecuteManyResults(options);
                return(Result);
            }
            finally
            {
                _processManyMode = false;
                nodeTimer.LogStop(LogWriter, this, "ExecuteAsync");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Used to kick off execution of a node with a default execution context for all subjects in a serial manner.
        /// </summary>
        /// <param name="subjects">Subject to be moved through the node.</param>
        /// <param name="options">Execution options to apply to running this enumerable of subjects.</param>
        /// <returns>An aggregated NodeResult.</returns>
        public async Task <NodeResult> ExecuteManySeriallyAsync(IEnumerable <T> subjects, ExecutionOptions options = null)
        {
            Guard.AgainstNullArgument("subjects", subjects);

            var nodeTimer = new NodeTimer();

            try
            {
                nodeTimer.LogStart(LogWriter, this, "ExecuteManySeriallyAsync");

                _processManyMode = true;

                var subjectList = subjects.ToList();

                Result = new NodeResult(default(T), Id, FlowId);

                if (subjectList.Count == 0)
                {
                    return(Result);
                }

                if (options == null)
                {
                    options = new ExecutionOptions();
                }

                foreach (var subject in subjectList)
                {
                    try
                    {
                        LogWriter.Debug("Running all subjects asynchronously in a serial manner.");

                        NodeResult result = await ExecuteAsync(new ExecutionContext <T>(subject, options)).ConfigureAwait(false);

                        Result.AddChildResult(result);
                    }
                    catch (Exception)
                    {
                        if (options.ThrowOnError)
                        {
                            throw;
                        }
                        if (!options.ContinueOnFailure)
                        {
                            break;
                        }
                    }
                }

                ProcessExecuteManyResults(options);

                return(Result);
            }
            finally
            {
                _processManyMode = false;
                nodeTimer.LogStop(LogWriter, this, "ExecuteAsync");
            }
        }
 private void OnChildNodeStoppedSilent_Listener(BehaviourNode child, bool success)
 {
     if (success &&
         !(State == NodeState.STOPPING || (TotalLoops >= 0 && ++LoopCount >= TotalLoops)))
     {
         _restartChildTimer = AddTimer(0, 0, 0, RestartChild);
     }
 }
Ejemplo n.º 4
0
 private void OnChildNodeStoppedSilent_Listener(BehaviourNode child, bool success)
 {
     if (success &&
         !(State == NodeState.STOPPING || !m_isConditionMetFunc()))
     {
         m_restartChildTimer = StartFirstChildNodeOnNextTick();
     }
 }
        protected NodeTimer AddTimer(float time, float randomVariance, int repeat, System.Action timeoutAction, bool autoRemove = false, UpdateType updateType = UpdateType.DEFAULT)
        {
            var new_timer = new NodeTimer(time, randomVariance, repeat, timeoutAction, autoRemove);

            //m_timers.Add(new_timer);
            _timers[updateType].Add(new_timer);
            return(new_timer);
        }
Ejemplo n.º 6
0
            public override Node Create(Vector2 pos)
            {
                NodeTimer node = CreateInstance <NodeTimer> ();

                node.rect = new Rect(pos.x, pos.y, 200, 100);
                node.name = "Delay";

                node.CreateInput("Previous Node", "ActionForward", NodeSide.Left, 30);
                node.CreateOutput("Next Node", "ActionForward", NodeSide.Right, 30);

                return(node);
            }
Ejemplo n.º 7
0
 private void OnChildNodeStopped_Listener(BehaviourNode child, bool success)
 {
     if (State != NodeState.STOPPING)
     {
         // wait one tick, to prevent endless recursions
         m_rootTimer = StartFirstChildNodeOnNextTick();
     }
     else
     {
         StopNodeOnNextTick(success);
     }
 }
 private void OnStarted_Listener()
 {
     if (!m_isConditionMetFunc.Invoke())
     {
         m_conditionTimer = AddTimer(m_checkInterval, m_checkVariance, -1, checkCondition);
     }
     else
     {
         // Children[0].StartNode();
         StartFirstChildNodeOnNextTick();
     }
 }
 private void OnStarted_Common()
 {
     if (m_interval <= 0f)
     {
         // AddUpdateObserver
         m_serviceTimer = AddTimer(0f, 0f, -1, m_serviceAction);
     }
     else if (m_randomVariation <= 0f)
     {
         AddRandomTimer();
     }
     else
     {
         InvokeServiceActionAtRandomInterval();
     }
 }
Ejemplo n.º 10
0
        private void OnStarted_Common()
        {
            float seconds = WaitSeconds;

            if (seconds < 0)
            {
                seconds = 0;
            }
            if (randomVariance >= 0f)
            {
                m_waitTimout = AddTimer(seconds, randomVariance, 0, OnTimeOut);
            }
            else
            {
                m_waitTimout = AddTimer(seconds, 0, OnTimeOut);
            }
        }
Ejemplo n.º 11
0
 private void OnChildNodeStopped_Listener(BehaviourNode child, bool success)
 {
     if (success)
     {
         if (State == NodeState.STOPPING || m_isConditionMetFunc())
         {
             StopNodeOnNextTick(true);
         }
         else
         {
             m_restartChildTimer = StartFirstChildNodeOnNextTick();
         }
     }
     else
     {
         // OnStopped.Invoke(false);
         // StopNode(false);
         StopNodeOnNextTick(false);
     }
 }
Ejemplo n.º 12
0
 private void OnStarted_Listener()
 {
     if (m_isReady)
     {
         m_isReady = false;
         if (!m_startAfterChild)
         {
             m_timeoutTimer = AddTimer(m_cooldownTime, m_randomVariation, 0, OnTimeout);
         }
         // Children[0].StartNode();
         StartFirstChildNodeOnNextTick();
     }
     else
     {
         if (m_failOnCooldown)
         {
             // OnStopped.Invoke(false);
             // StopNode(false);
             StopNodeOnNextTick(false);
         }
     }
 }
 /*
  * private void OnChildNodeStopped_Listener(BehaviourNode child, bool success)
  * {
  *   if (success)
  *   {
  *       if(State == NodeState.STOPPING || (TotalLoops >= 0 && ++LoopCount >= TotalLoops))
  *       {
  *           StopNodeOnNextTick(true);
  *       }
  *       else
  *       {
  *           m_restartChildTimer = AddTimer(0, 0, 0, RestartChild);
  *       }
  *   }
  *   else
  *   {
  *       StopNodeOnNextTick(false);
  *   }
  * }
  */
 // To Refactor
 private void OnChildNodeStopped_Listener(BehaviourNode child, bool success)
 {
     if (success)
     {
         if (State == NodeState.STOPPING || (TotalLoops >= 0 && ++LoopCount >= TotalLoops))
         {
             StopNodeOnNextTick(true);
         }
         else
         {
             _restartChildTimer = AddTimer(0, 0, 0, RestartChild);
         }
     }
     else if (State == NodeState.STOPPING || StopOnChildFail)
     {
         StopNodeOnNextTick(false);
     }
     else
     {
         _restartChildTimer = AddTimer(0, 0, 0, RestartChild);
     }
 }
Ejemplo n.º 14
0
 private void OnStarted_Common()
 {
     RemoveTimer(m_timeoutTimer);
     m_timeoutTimer = AddTimer(m_limit, m_randomVariation, 0, OnTimeout);
 }
 private void InvokeServiceActionAtRandomInterval()
 {
     m_serviceAction();
     m_serviceTimer = AddTimer(m_interval, m_randomVariation, 0, InvokeServiceActionAtRandomInterval);
 }
Ejemplo n.º 16
0
 protected override void StartObserving()
 {
     _conditionTimer = AddTimer(_checkInterval, _checkVariance, -1, Evaluate);
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Used to kick off execution of a node with a specified execution context.
        /// </summary>
        /// <param name="sourceContext">ExecutionContext that includes a subject to be moved through the node.</param>
        /// <returns>A NodeResult</returns>
        public async Task <NodeResult> ExecuteAsync(IExecutionContext <T> sourceContext)
        {
            Guard.AgainstNullArgument("context", sourceContext);
            Guard.AgainstNullArgumentProperty("context", "Subject", sourceContext.Subject);

            var nodeTimer = new NodeTimer();

            try
            {
                nodeTimer.LogStart(LogWriter, this, "ExecuteAsync");

                if (Status != NodeRunStatus.NotRun)
                {
                    LogWriter.Debug("Status does not equal 'NotRun', resetting the node before execution");
                    Reset();
                }

                var subject = sourceContext.Subject;
                var result  = new NodeResult(subject, Id, FlowId);
                if (!_processManyMode)
                {
                    Result = result;
                }

                IExecutionContext <T> context = PrepareExecutionContext(sourceContext, result);

                OnBeforeExecute(context);

                if (!context.CancelProcessing)
                {
                    var effectiveOptions = GetEffectiveOptions(context.GlobalOptions);

                    if (!await ShouldExecuteInternalAsync(context).ConfigureAwait(false))
                    {
                        LogWriter.Info("ShouldExecute returned false, skipping execution");
                        return(result);
                    }

                    Status = NodeRunStatus.Running;
                    LogWriter.Debug("Executing the node");

                    try
                    {
                        result.Status = await PerformExecuteAsync(context).ConfigureAwait(false);

                        //Reset the subject in case it was changed.
                        result.Subject = context.Subject;
                        Status         = NodeRunStatus.Completed;
                        LogWriter.Info("Node completed execution, status is {0}", result.Status);
                    }
                    catch (Exception ex)
                    {
                        LogWriter.Error("Node erred during execution, status is Failed", ex);
                        Status           = NodeRunStatus.Faulted;
                        result.Subject   = context.Subject;
                        result.Status    = NodeResultStatus.Failed;
                        result.Exception = ex;

                        if (effectiveOptions.ThrowOnError)
                        {
                            throw;
                        }
                    }

                    OnAfterExecute(context);
                }

                sourceContext.CancelProcessing = context.CancelProcessing;

                return(result);
            }
            finally
            {
                nodeTimer.LogStop(LogWriter, this, "ExecuteAsync");
            }
        }
Ejemplo n.º 18
0
 protected void RemoveTimer(NodeTimer to_remove, UpdateType updateType = UpdateType.DEFAULT)
 {
     _timers[updateType].Remove(to_remove); //m_timers.Remove(to_remove);
     to_remove = null;
 }
Ejemplo n.º 19
0
 protected bool HasTimer(NodeTimer timer, UpdateType updateType = UpdateType.DEFAULT)
 {
     return(_timers[updateType].Contains(timer)); //m_timers.Contains(timer);
 }
Ejemplo n.º 20
0
 private void OnStarted_Listener()
 {
     OnStarted_Common();
     m_restartChildTimer = StartFirstChildNodeOnNextTick();
 }
Ejemplo n.º 21
0
 private void OnStarted_Common()
 {
     m_isLimitReached = false;
     m_timeoutTimer   = AddTimer(m_limit, m_randomVariation, 0, OnTimeout);
 }
 private void AddRandomTimer()
 {
     m_serviceAction();
     m_serviceTimer = AddTimer(m_interval, 0f, -1, m_serviceAction);
 }