Пример #1
0
        /// <summary>
        /// Fires the trigger.
        ///
        /// The trigger returns True of all senses/sense-acts of the
        /// trigger evaluate to True.
        /// </summary>
        /// <returns>If all the senses/sense-acts evaluate to True.</returns>
        public bool fire()
        {
            bool success = true;

            log.Debug("Firing");
            foreach (POSHSense sense in this.senses)
            {
                if (!sense.fire().continueExecution())
                {
                    log.Debug(string.Format("Sense {0} failed", sense.getName()));
                    success = false;
                    break;
                }
            }

            // logging the event
            FireArgs args = new FireArgs();

            args.FireResult = success;
            args.Time       = DateTime.Now;

            BroadCastFireEvent(args);

            return(success);
        }
Пример #2
0
        /// <summary>
        /// Fires the competence priority element.
        ///
        /// This method goes through its list of competence elements
        /// and fires the first one that is ready. In that case,
        /// the result of the competence element is returned. Otherwise,
        /// it returns FireResult(True, None) (this can never be returned
        /// by a competence element and is therefore uniquely identifyable).
        /// </summary>
        /// <returns>The result of firing the competence priority element.</returns>
        public override FireResult  fire()
        {
            log.Debug("Fired");
            FireArgs args = new FireArgs();

            foreach (CompetenceElement elem in elements)
            {
                // as the method ignores the timestamp, we can give it
                // whatever we want
                if (elem.isReady(0))
                {
                    FireResult result = elem.fire();
                    args.FireResult = result.continueExecution();
                    args.Time       = DateTime.Now;
                    BroadCastFireEvent(args);

                    return(result);
                }
            }
            log.Debug("Priority Element failed");

            args.FireResult = true;
            args.Time       = DateTime.Now;
            BroadCastFireEvent(args);

            return(new FireResult(true, null));
        }
Пример #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");
            FireArgs args = new FireArgs();

            CopiableElement element = elements[elementIdx];

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

                if (!result)
                {
                    log.Debug(string.Format("Action/Sense {0} failed", element.getName()));
                    elementIdx      = 0;
                    args.FireResult = result;
                    args.Time       = DateTime.Now;

                    BroadCastFireEvent(args);
                    return(new FireResult(false, null));
                }

                // check if we've just fired the last action
                elementIdx += 1;
                if (elementIdx >= elements.Count)
                {
                    elementIdx      = 0;
                    args.FireResult = result;
                    args.Time       = DateTime.Now;

                    BroadCastFireEvent(args);
                    return(new FireResult(false, null));
                }
                args.FireResult = result;
                args.Time       = DateTime.Now;

                BroadCastFireEvent(args);
                return(new FireResult(true, null));
            }
            else if (element is Competence)
            {
                // we have a competence
                elementIdx      = 0;
                args.FireResult = true;
                args.Time       = DateTime.Now;

                BroadCastFireEvent(args);
                return(new FireResult(true, element));
            }

            return(null);
        }
Пример #4
0
        /// <summary>
        /// Fires the competence priority element.
        /// 
        /// This method goes through its list of competence elements
        /// and fires the first one that is ready. In that case,
        /// the result of the competence element is returned. Otherwise,
        /// it returns FireResult(True, None) (this can never be returned
        /// by a competence element and is therefore uniquely identifyable).
        /// </summary>
        /// <returns>The result of firing the competence priority element.</returns>
        public override FireResult fire()
        {
            log.Debug("Fired");
            FireArgs args = new FireArgs();

            foreach (CompetenceElement elem in elements)
            {
                // as the method ignores the timestamp, we can give it
                // whatever we want
                if (elem.isReady(0))
                {
                    FireResult result =  elem.fire();
                    args.FireResult = result.continueExecution();
                    args.Time = DateTime.Now;
                    BroadCastFireEvent(args);

                    return result;
                }
            }
            log.Debug("Priority Element failed");

            args.FireResult = true;
            args.Time = DateTime.Now;
            BroadCastFireEvent(args);

            return new FireResult(true, null);
        }
Пример #5
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");
            FireArgs args = new FireArgs();

            CopiableElement element = elements[elementIdx];
            if (element is POSHAction || element is POSHSense)
            {
                bool result;
                if (element is POSHAction)
                    result = ((POSHAction)element).fire().continueExecution();
                else
                    result = ((POSHSense)element).fire().continueExecution();

                if (!result)
                {
                    log.Debug(string.Format("Action/Sense {0} failed", element.getName()));
                    elementIdx = 0;
                    args.FireResult = result;
                    args.Time = DateTime.Now;

                    BroadCastFireEvent(args);
                    return new FireResult(false, null);
                }

                // check if we've just fired the last action
                elementIdx += 1;
                if (elementIdx >= elements.Count)
                {
                    elementIdx = 0;
                    args.FireResult = result;
                    args.Time = DateTime.Now;

                    BroadCastFireEvent(args);
                    return new FireResult(false, null);
                }
                args.FireResult = result;
                args.Time = DateTime.Now;

                BroadCastFireEvent(args);
                return new FireResult(true, null);
            }
            else if (element is Competence)
            {
                // we have a competence
                elementIdx = 0;
                args.FireResult = true;
                args.Time = DateTime.Now;

                BroadCastFireEvent(args);
                return new FireResult(true, element);
            }

            return null;
        }
Пример #6
0
        /// <summary>
        /// Performs the action and returns if it was successful.
        /// </summary>
        /// <returns>True if the action was successful, and False otherwise.</returns>
        public override FireResult fire()
        {
            bool success = action.Second.ExecuteAction(action.First);
            FireArgs args = new FireArgs();
            args.FireResult = success;
            args.Time = DateTime.Now;

            BroadCastFireEvent(args);

            log.Debug("Firing");
            return new FireResult(success,null);
        }
Пример #7
0
        /// <summary>
        /// Performs the action and returns if it was successful.
        /// </summary>
        /// <returns>True if the action was successful, and False otherwise.</returns>
        public override FireResult fire()
        {
            bool     success = action.Second.ExecuteAction(action.First);
            FireArgs args    = new FireArgs();

            args.FireResult = success;
            args.Time       = DateTime.Now;

            BroadCastFireEvent(args);

            log.Debug("Firing");
            return(new FireResult(success, null));
        }
Пример #8
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;
            FireArgs   args = new FireArgs();


            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;
                args.FireResult = false;
                args.Time       = DateTime.Now;
                BroadCastFireEvent(args);
                return(null);
            }

            // the element is a competence or an action pattern
            result          = ((ElementCollection)element).fire();
            args.FireResult = false;
            args.Time       = DateTime.Now;
            BroadCastFireEvent(args);
            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);
        }
Пример #9
0
        /// <summary>
        /// Fires the competence.
        ///
        /// This method first checks if the competence's goal is satisfied
        /// (if the goal is not None). If that is the case, then it
        /// returns FireResult(False, None). Otherwise it fires the
        /// priority elements one by one. On the first successful firing
        /// of a competence priority element, the method returns the
        /// result of the priority element. If no priority element fired
        /// successfully, then FireResult(False, None) is returned.
        /// </summary>
        /// <returns>The result of firing an element, or
        ///         FireResult(False, None)</returns>
        public override FireResult  fire()
        {
            log.Debug("Fired");
            FireArgs args = new FireArgs();

            // check if the goal is satisfied
            if (goal is Trigger && goal.fire())
            {
                log.Debug("Goal satisfied");
                args.FireResult = false;
                args.Time       = DateTime.Now;
                BroadCastFireEvent(args);

                return(new FireResult(false, null));
            }
            // process the elements
            FireResult result;

            foreach (CompetencePriorityElement elem in elements)
            {
                result = elem.fire();
                // check if the competence priority element failed
                if (result.continueExecution() && !(result.nextElement() is CopiableElement))
                {
                    continue;
                }
                args.FireResult = result.continueExecution();
                args.Time       = DateTime.Now;
                BroadCastFireEvent(args);

                return(result);
            }
            // we failed
            log.Debug("Failed");
            args.FireResult = false;
            args.Time       = DateTime.Now;
            BroadCastFireEvent(args);

            return(new FireResult(false, null));
        }
Пример #10
0
        /// <summary>
        /// Performes one loop through the drive collection.
        ///
        /// This method takes the first triggering drive element and either
        /// descends further down in the competence tree, or performs
        /// the drive's current action.
        ///
        /// It returns either DRIVE_WON if the drive collection's goal was
        /// reached, DRIVE_LOST if no drive triggered, or DRIVE_FOLLOWED if
        /// the goal wasn't reached and a drive triggered.
        /// </summary>
        /// <returns></returns>
        public override int FollowDrive()
        {
            FireResult result;
            FireArgs   args = new FireArgs();

            // FIXME: This test is *very* costly, this function is the most frequently run in a POSH.
            //        In lisp, I used to have a debug version of POSH that had lots of conditionals in it,
            //        and a fast version with none.  Speaking of None, identity is faster to check than equality,
            //        according to python.org
            //        Maybe profile.py should replace the function maned followDrive... note this would require
            //        knowing if your posh was strict or scheduled.
            //        JJB 1 Mar 08
            if (profiler is Profiler)
            {
                profiler.increaseTotalCalls();
            }

            log.Debug("Processing Drive Collection");

            result = dc.fire();

            args.FireResult = (result is FireResult) ? result.continueExecution() : false;
            args.Time       = DateTime.Now;
            BroadCastFireEvent(args);

            timer.LoopEnd();

            if (result.continueExecution())
            {
                return(DRIVEFOLLOWED);
            }
            else if (result.nextElement() is ElementCollection)
            {
                return(DRIVEWON);
            }
            else
            {
                return(DRIVELOST);
            }
        }
Пример #11
0
        /// <summary>
        /// Fires the competence element.
        ///
        /// If the competence element's element is an Action, then this
        /// action is executed and FireResult(False, None) is returned.
        /// Otherwise, FireResult(True, element) is returned,
        /// indicating that at the next execution step that element has
        /// to be fired.
        /// </summary>
        /// <returns>Result of firing the competence element.</returns>
        public override FireResult  fire()
        {
            FireArgs args = new FireArgs();


            log.Debug("Fired");
            if (element is POSHAction)
            {
                ((POSHAction)element).fire();

                args.FireResult = false;
                args.Time       = DateTime.Now;
                BroadCastFireEvent(args);

                return(new FireResult(false, null));
            }

            args.FireResult = true;
            args.Time       = DateTime.Now;
            BroadCastFireEvent(args);
            return(new FireResult(true, element));
        }
Пример #12
0
        /// <summary>
        /// Fires the trigger.
        /// 
        /// The trigger returns True of all senses/sense-acts of the
        /// trigger evaluate to True.
        /// </summary>
        /// <returns>If all the senses/sense-acts evaluate to True.</returns>
        public bool fire()
        {
            bool success = true;

            log.Debug("Firing");
            foreach (POSHSense sense in this.senses)
                if (!sense.fire().continueExecution())
                {
                    log.Debug(string.Format("Sense {0} failed",sense.getName()));
                    success = false;
                    break;
                }

            // logging the event
            FireArgs args = new FireArgs();
            args.FireResult = success;
            args.Time = DateTime.Now;

            BroadCastFireEvent(args);

            return success;
        }
Пример #13
0
        /// <summary>
        /// Fires the drive collection.
        ///
        /// This method first checks if the goal (if not null) is met. If
        /// that is the case, then FireResult(False, self) is
        /// returned. Otherwise it goes through the list of priority
        /// elements until the first one was fired successfully (returning
        /// something else than None). In that case, FireResult(True,
        /// None) is returned. If none of the priority elements were
        /// successful, FireResult(False, None) is returned, indicating a
        /// failing of the drive collection.
        ///
        /// To summarise:
        ///     - FireResult(True, None): drive element fired
        ///     - FireResult(False, self): goal reached
        ///     - FireResult(False, None): drive failed
        /// </summary>
        /// <returns>The result of firing the drive.</returns>
        public override FireResult  fire()
        {
            log.Debug("Fired");
            FireArgs args = new FireArgs();

            // check if goal reached
            if (goal is Trigger && goal.fire())
            {
                log.Debug("Goal Satisfied");
                args.FireResult = false;
                args.Time       = DateTime.Now;
                BroadCastFireEvent(args);
                return(new FireResult(false, this));
            }

            // fire elements
            foreach (DrivePriorityElement elem in elements)
            {
                // a priority element returns None if it wasn't
                // successfully fired
                if (elem.fire() != null)
                {
                    args.FireResult = true;
                    args.Time       = DateTime.Now;
                    BroadCastFireEvent(args);
                    return(new FireResult(true, null));
                }
            }

            // drive failed (no element fired)
            log.Debug("Failed");

            args.FireResult = false;
            args.Time       = DateTime.Now;
            BroadCastFireEvent(args);
            return(new FireResult(false, null));
        }
Пример #14
0
        /// <summary>
        /// Fires the competence.
        /// 
        /// This method first checks if the competence's goal is satisfied
        /// (if the goal is not None). If that is the case, then it
        /// returns FireResult(False, None). Otherwise it fires the
        /// priority elements one by one. On the first successful firing
        /// of a competence priority element, the method returns the
        /// result of the priority element. If no priority element fired
        /// successfully, then FireResult(False, None) is returned.
        /// </summary>
        /// <returns>The result of firing an element, or
        ///         FireResult(False, None)</returns>
        public override FireResult fire()
        {
            log.Debug("Fired");
            FireArgs args = new FireArgs();

            // check if the goal is satisfied
            if (goal is Trigger && goal.fire())
            {
                log.Debug("Goal satisfied");
                args.FireResult = false;
                args.Time = DateTime.Now;
                BroadCastFireEvent(args);

                return new FireResult(false, null);
            }
            // process the elements
            FireResult result;
            foreach (CompetencePriorityElement elem in elements)
            {
                result = elem.fire();
                // check if the competence priority element failed
                if (result.continueExecution() && !(result.nextElement() is CopiableElement) )
                    continue;
                args.FireResult = result.continueExecution();
                args.Time = DateTime.Now;
                BroadCastFireEvent(args);

                return result;
            }
            // we failed
            log.Debug("Failed");
            args.FireResult = false;
            args.Time = DateTime.Now;
            BroadCastFireEvent(args);

            return new FireResult(false, null);
        }
Пример #15
0
        /// <summary>
        /// Fires the drive collection.
        /// 
        /// This method first checks if the goal (if not null) is met. If
        /// that is the case, then FireResult(False, self) is
        /// returned. Otherwise it goes through the list of priority
        /// elements until the first one was fired successfully (returning
        /// something else than None). In that case, FireResult(True,
        /// None) is returned. If none of the priority elements were
        /// successful, FireResult(False, None) is returned, indicating a
        /// failing of the drive collection.
        /// 
        /// To summarise:
        ///     - FireResult(True, None): drive element fired
        ///     - FireResult(False, self): goal reached
        ///     - FireResult(False, None): drive failed
        /// </summary>
        /// <returns>The result of firing the drive.</returns>
        public override FireResult fire()
        {
            log.Debug("Fired");
            FireArgs args = new FireArgs();
            // check if goal reached
            if (goal is Trigger && goal.fire())
            {
                log.Debug("Goal Satisfied");
                args.FireResult = false;
                args.Time = DateTime.Now;
                BroadCastFireEvent(args);
                return new FireResult(false, this);
            }

            // fire elements
            foreach (DrivePriorityElement elem in elements)
                // a priority element returns None if it wasn't
                // successfully fired
                if (elem.fire() != null)
                {
                    args.FireResult = true;
                    args.Time = DateTime.Now;
                    BroadCastFireEvent(args);
                    return new FireResult(true, null);
                }

            // drive failed (no element fired)
            log.Debug("Failed");

            args.FireResult = false;
            args.Time = DateTime.Now;
            BroadCastFireEvent(args);
            return new FireResult(false, null);
        }
Пример #16
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;
            FireArgs args = new FireArgs();

            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;
                args.FireResult = false;
                args.Time = DateTime.Now;
                BroadCastFireEvent(args);
                return null;
            }

            // the element is a competence or an action pattern
            result = ((ElementCollection)element).fire();
            args.FireResult = false;
            args.Time = DateTime.Now;
            BroadCastFireEvent(args);
            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;
        }
Пример #17
0
        /// <summary>
        /// Fires the drive prority element.
        ///
        /// This method fires the first ready drive element in its
        /// list and returns FireResult(False, None). If no
        /// drive element was ready, then None is returned.
        /// </summary>
        /// <returns>The result of firing the element.</returns>
        public override FireResult fire()
        {
            log.Debug("Fired");
            long timeStamp = timer.Time();

            elements = LAPParser.ShuffleList(elements);
            FireArgs args = new FireArgs();

            if (elements.Contains(agent.dc.lastTriggeredElement))
            {
                if (agent.dc.lastTriggeredElement.isReady(timeStamp))
                {
                    //if not self.agent._dc.last_triggered_element._behaviours[0].wants_to_interrupt():
                    //    for element in new_elements:
                    //        if element.isReady(timestamp) and element._behaviours[0].wants_to_interrupt():#and element!=self.agent._dc.last_triggered_element
                    //            self.agent._dc.last_triggered_element=element
                    //            element.fire()
                    //            return FireResult(False, None)
                    agent.dc.lastTriggeredElement.fire();
                    args.FireResult = false;
                    args.Time       = DateTime.Now;
                    BroadCastFireEvent(args);
                    return(new FireResult(false, null));
                }
            }
            // for element in new_elements:
            foreach (DriveElement element in elements)
            {
                if (element.isReady(timeStamp))
                {
                    if (element != agent.dc.lastTriggeredElement)
                    {
                        if (agent.dc.lastTriggeredElement == null)
                        {
                            if (element.isLatched)
                            {
                                agent.dc.lastTriggeredElement = element;
                            }
                        }
                        else if (!agent.dc.lastTriggeredElement.isReady(timeStamp))
                        {
                            // event finished natually
                            agent.dc.lastTriggeredElement = null;
                        }
                        else
                        {
                            behaviours = agent.dc.lastTriggeredElement.behaviours;

                            // TODO: check this latching behaviour thing
                            foreach (Behaviour b in behaviours)
                            {
                                if (b.GetType().IsSubclassOf(typeof(LatchedBehaviour)))
                                {
                                    ((LatchedBehaviour)b).signalInterrupt();
                                }
                            }

                            if (element.isLatched)
                            {
                                agent.dc.lastTriggeredElement = element;
                            }
                            else
                            {
                                agent.dc.lastTriggeredElement = null;
                            }
                        }
                    }
                    element.fire();
                    args.FireResult = false;
                    args.Time       = DateTime.Now;
                    BroadCastFireEvent(args);
                    return(new FireResult(false, null));
                }
            }
            args.FireResult = false;
            args.Time       = DateTime.Now;
            BroadCastFireEvent(args);
            return(null);
        }