Пример #1
0
        /// <summary>
        /// Initialises behaviour with given actions and senses.
        /// 
        /// The actions and senses has to correspond to
        ///   - the method names that implement those actions/senses
        ///   - the names used in the plan
        /// 
        /// The log domain of a behaviour is set to
        /// [AgentId].Behaviour
        /// </summary>
        /// <param name="agent">The agent that uses the behaviour</param>
        /// <param name="actions">The action names to register.</param>
        /// <param name="senses">The sense names to register.</param>
        /// <param name="attributes">List of attributes to initialise behaviour state.</param>
        /// <param name="caller"></param>
        public Behaviour(AgentBase agent,string [] actions,string []senses,Dictionary<string,object> a_attributes,Behaviour caller)
            : this(agent)
        {
            Dictionary<string, SortedList<float,POSHPrimitive>> availableActions = new Dictionary<string, SortedList<float,POSHPrimitive>>();
            Dictionary<string, SortedList<float,POSHPrimitive>> availableSenses = new Dictionary<string, SortedList<float,POSHPrimitive>>();

            if (caller != null)
                GetActionsSenses(caller);
            else
            {
                MethodInfo[] methods = this.GetType().GetMethods();

                availableActions = ExtractPrimitives(this, true);
                availableSenses = ExtractPrimitives(this,false);

                // filtering the primitives which sould not be made available for the agent
                FilterPrimitives(actions,availableActions);
                FilterPrimitives(senses,availableSenses);

                this.attributes[ACTIONS] = availableActions;
                this.attributes[SENSES] = availableSenses;
            }

            this.attributes.Add(INSPECTORS,null);

            // assign additional attributes
            if (a_attributes != null)
                AssignAttributes(a_attributes);
        }
Пример #2
0
        /// <summary>
        /// @deprecated I just copied this method from the python code but is seems to overcomplicated stuff and using methods from other behaviours is hard to communicate to the user
        /// </summary>
        /// <param name="sourceObj"></param>
        private void GetActionsSenses(Behaviour sourceObj)
        {
            try
            {
                if (sourceObj.attributes is Dictionary<string, object>)
                {
                    // check if the source object contains more actions and add those to the current attribute dict
                    if (sourceObj.attributes.ContainsKey(ACTIONS) && attributes.ContainsKey(ACTIONS) && attributes[ACTIONS] is Dictionary<string, Behaviour>)
                    {
                        Dictionary<string, Behaviour> a = (Dictionary<string,Behaviour>)sourceObj.attributes[ACTIONS];
                        foreach (KeyValuePair<string, Behaviour> e in a)
                            ((Dictionary<string, Behaviour>)this.attributes[ACTIONS])[e.Key] = e.Value;

                    }
                    // check if the source object contains more senses and add those
                    if (sourceObj.attributes.ContainsKey(SENSES) && attributes.ContainsKey(SENSES) && attributes[SENSES] is Dictionary<string, Behaviour>)
                    {
                        Dictionary<string, Behaviour> s = (Dictionary<string, Behaviour>)sourceObj.attributes[SENSES];
                        foreach (KeyValuePair<string, Behaviour> e in s)
                            ((Dictionary<string, Behaviour>)this.attributes[SENSES])[e.Key] = e.Value;

                    }
                }
            }
            catch(ArgumentNullException ){}
        }
Пример #3
0
        /// <summary>
        /// The method is extracting the primitives based on their string names from a behaviour object.
        /// At this point all possible actions/ senses are extracted and their names are returned as a string list.
        /// </summary>
        /// <param name="source">The behaviour to search for the primitives</param>
        /// <param name="acts">True if search for Actions,\n False if searching for Senses</param>
        /// <returns>Returns a Dict containing the plan name as key and the correct Attribute defintions 
        /// all for Actions/Senses inside the given behaviour linked to the specific plan name</returns>
        private Dictionary<string, SortedList<float, POSHPrimitive>> ExtractPrimitives(Behaviour source, bool acts)
        {
            MethodInfo[] methods = source.GetType().GetMethods();
            Dictionary<string, SortedList<float, POSHPrimitive>> primitives = new Dictionary<string, SortedList<float, POSHPrimitive>>();

            foreach (MethodInfo method in methods)
            {
                POSHPrimitive prim = null;

                // if we want to add actions
                if (acts)
                {
                    // include versioning of primitives at some later point
                    if (method.GetCustomAttributes(typeof(ExecutableAction), true).Length > 0)
                        prim = method.GetCustomAttributes(typeof(ExecutableAction), true).First() as ExecutableAction;
                }
                // if we want to add senses
                else
                    if (method.GetCustomAttributes(typeof(ExecutableSense), true).Length > 0)
                        prim = method.GetCustomAttributes(typeof(ExecutableSense), true).First() as ExecutableSense;

                // if there is a primitive we want we store it in a sorted list using the version number as a criteria for later retrieval
                if (prim != null)
                {
                    // linking the primitive to the method which it augments
                    prim.SetLinkedMethod(method.Name);
                    prim.SetOriginatingBhaviour(this);

                    if (primitives.ContainsKey(prim.command))
                    {
                        primitives[prim.command].Add(prim.version, prim);
                    }
                    else
                    {
                        SortedList<float, POSHPrimitive> list = new SortedList<float, POSHPrimitive>();
                        list[prim.version] = prim;
                        primitives[prim.command] = list;
                    }

                }
            }

            return primitives;
        }
Пример #4
0
 public LatchedBehaviour(AgentBase agent, string[] actions, string[] senses, 
     Dictionary<string, object> attributes, Behaviour caller)
     : base(agent,actions,senses,attributes,caller)
 {
 }
Пример #5
0
        /// <summary>
        /// Initialises behaviour with given actions and senses.
        ///
        /// The actions and senses has to correspond to
        ///   - the method names that implement those actions/senses
        ///   - the names used in the plan
        ///
        /// The log domain of a behaviour is set to
        /// [AgentId].Behaviour
        /// </summary>
        /// <param name="agent">The agent that uses the behaviour</param>
        /// <param name="actions">The action names to register.</param>
        /// <param name="senses">The sense names to register.</param>
        /// <param name="attributes">List of attributes to initialise behaviour state.</param>
        /// <param name="caller"></param>
        public Behaviour(AgentBase agent, string [] actions, string [] senses, Dictionary <string, object> a_attributes, Behaviour caller)
            : this(agent)
        {
            Dictionary <string, SortedList <float, POSHPrimitive> > availableActions = new Dictionary <string, SortedList <float, POSHPrimitive> >();
            Dictionary <string, SortedList <float, POSHPrimitive> > availableSenses  = new Dictionary <string, SortedList <float, POSHPrimitive> >();

            if (caller != null)
            {
                GetActionsSenses(caller);
            }
            else
            {
                MethodInfo[] methods = this.GetType().GetMethods();

                availableActions = ExtractPrimitives(this, true);
                availableSenses  = ExtractPrimitives(this, false);


                // filtering the primitives which sould not be made available for the agent
                FilterPrimitives(actions, availableActions);
                FilterPrimitives(senses, availableSenses);



                this.attributes[ACTIONS] = availableActions;
                this.attributes[SENSES]  = availableSenses;
            }

            this.attributes.Add(INSPECTORS, null);

            // assign additional attributes
            if (a_attributes != null)
            {
                AssignAttributes(a_attributes);
            }
        }
Пример #6
0
        /// <summary>
        /// The method is extracting the primitives based on their string names from a behaviour object.
        /// At this point all possible actions/ senses are extracted and their names are returned as a string list.
        /// </summary>
        /// <param name="source">The behaviour to search for the primitives</param>
        /// <param name="acts">True if search for Actions,\n False if searching for Senses</param>
        /// <returns>Returns a Dict containing the plan name as key and the correct Attribute defintions
        /// all for Actions/Senses inside the given behaviour linked to the specific plan name</returns>
        private Dictionary <string, SortedList <float, POSHPrimitive> > ExtractPrimitives(Behaviour source, bool acts)
        {
            MethodInfo[] methods = source.GetType().GetMethods();
            Dictionary <string, SortedList <float, POSHPrimitive> > primitives = new Dictionary <string, SortedList <float, POSHPrimitive> >();


            foreach (MethodInfo method in methods)
            {
                POSHPrimitive prim = null;

                // if we want to add actions
                if (acts)
                {
                    // include versioning of primitives at some later point
                    if (method.GetCustomAttributes(typeof(ExecutableAction), true).Length > 0)
                    {
                        prim = method.GetCustomAttributes(typeof(ExecutableAction), true).First() as ExecutableAction;
                    }
                }
                // if we want to add senses
                else
                if (method.GetCustomAttributes(typeof(ExecutableSense), true).Length > 0)
                {
                    prim = method.GetCustomAttributes(typeof(ExecutableSense), true).First() as ExecutableSense;
                }

                // if there is a primitive we want we store it in a sorted list using the version number as a criteria for later retrieval
                if (prim != null)
                {
                    // linking the primitive to the method which it augments
                    prim.SetLinkedMethod(method.Name);
                    prim.SetOriginatingBhaviour(this);

                    if (primitives.ContainsKey(prim.command))
                    {
                        primitives[prim.command].Add(prim.version, prim);
                    }
                    else
                    {
                        SortedList <float, POSHPrimitive> list = new SortedList <float, POSHPrimitive>();
                        list[prim.version]       = prim;
                        primitives[prim.command] = list;
                    }
                }
            }

            return(primitives);
        }