Esempio n. 1
0
        /// <summary>
        /// Initialises the competence element.
        ///
        /// The log domain is set to [AgentName].CE.[element_name].
        /// </summary>
        /// <param name="agent">The competence element's agent.</param>
        /// <param name="elementName">The name of the competence element.</param>
        /// <param name="trigger">The element's trigger</param>
        /// <param name="element">The element to fire (Action,Competence or ActionPattern).</param>
        /// <param name="maxRetries">The maximum number of retires. If this is set
        ///         to a negative number, it is ignored.</param>
        public CompetenceElement(Agent agent, string elementName, Trigger trigger, CopiableElement element, int maxRetries)
            : base(string.Format("CE.{0}", elementName), agent)
        {
            this.name       = elementName;
            this.trigger    = trigger;
            this.element    = element;
            this.maxRetries = maxRetries;
            this.retries    = 0;

            log.Debug("Created");
        }
Esempio n. 2
0
 /// <summary>
 /// Initialises the result of firing an element.
 ///
 /// For a more detailed description of the arguments, read the
 /// class documentation.
 /// </summary>
 /// <param name="continueExecution">If we want to continue executing the current
 /// part of the plan.</param>
 /// <param name="nextElement">The next plan element to fire.</param>
 public FireResult(bool continueExecution, CopiableElement nextElement)
 {
     continueExecuting = continueExecution;
     if (continueExecution && nextElement is CopiableElement)
     {
         // copy the next element, if there is one
         next = (ElementCollection)nextElement.copy();
     }
     else
     {
         next = null;
     }
     // FIX: @swen: here must be an error in the original implementation I just uncommented the next line because it seemed wrong
     // next = nextElement;
 }
Esempio n. 3
0
        /// <summary>
        /// Fires the action pattern.
        ///
        /// This method fires the current action / sense / sense-act or
        /// competence of the pattern. In case of firing an action / sense
        /// / sense-act, the method points to the next element in the
        /// pattern and returns FireResult(True, None) if the current
        /// action / sense / sense-act was successful (i.e. evaluated to
        /// True) and not the last action in the sequence, in which case
        /// it returns FireResult(False, None) and resets the action
        /// pattern.
        ///
        /// If the current element is a competence, then competence is
        /// returned as the next element by returning
        /// FireResult(True, competence), and the action pattern is
        /// reset.
        /// </summary>
        /// <returns>The result of firing the action pattern.</returns>
        public override FireResult  fire()
        {
            log.Debug("Fired");
            CopiableElement element = elements[elementIdx];

            if (element is POSHAction || element is POSHSense)
            {
                bool result;
                if (element is POSHAction)
                {
                    result = ((POSHAction)element).fire();
                }
                else
                {
                    result = ((POSHSense)element).fire();
                }

                if (!result)
                {
                    log.Debug(string.Format("Action/Sense {0} failed", element.getName()));
                    elementIdx = 0;
                    return(new FireResult(false, null));
                }

                // check if we've just fired the last action
                elementIdx += 1;
                if (elementIdx >= elements.Count)
                {
                    elementIdx = 0;
                    return(new FireResult(false, null));
                }
                return(new FireResult(true, null));
            }
            else if (elements[elementIdx] is Competence)
            {
                // we have a competence
                elementIdx = 0;
                return(new FireResult(true, element));
            }

            return(null);
        }
Esempio n. 4
0
        /// <summary>
        /// Fires the drive element.
        ///
        /// This method fires the current drive element and always
        /// returns None. It uses the slip-stack architecture to determine
        /// the element to fire in the next step.
        /// </summary>
        /// <returns>The result returned is null.</returns>
        public override FireResult fire()
        {
            FireResult result;

            log.Debug("Fired");
            // if our element is an action, we just fire it and do
            // nothing afterwards. That's because we can only have an action
            // as an element, if it is the drive element's root element.
            // Hence, we didn't descend in the plan tree and can keep
            // the same element.

            if (element is POSHAction || element.GetType().IsSubclassOf(typeof(POSHAction)))
            {
                ((POSHAction)element).fire();
                element = root;
                return(null);
            }

            // the element is a competence or an action pattern
            result = ((ElementCollection)element).fire();

            if (result.continueExecution())
            {
                // if we have a new next element, store it as the next
                // element to execute
                CopiableElement next = result.nextElement();
                if (next is CopiableElement)
                {
                    element = next;
                }
            }
            else
            {
                // we were told not to continue the execution -> back to root
                // We must not call reset() here, as that would also reset
                // the firing frequency of the element.
                element = root;
            }

            return(null);
        }