Esempio n. 1
0
        public override BehaviourDict GetBehaviours(string lib, ILog log, AgentBase agent)
#endif
        {
            BehaviourDict dict = new BehaviourDict();

            Behaviour[] behaviours = null;
            if (log is ILog)
            {
                log.Debug("Scanning library " + lib + " for behaviour classes");
            }

            if (this.connector is IBehaviourConnector)
            {
                behaviours = this.connector.GetBehaviours(agent);
            }

            if (behaviours != null)
            {
                foreach (Behaviour behave in behaviours)
                {
                    if (behave != null && behave.GetType().IsSubclassOf(typeof(Behaviour)))
                    {
                        dict.RegisterBehaviour(behave);
                    }
                }
            }



            return(dict);
        }
Esempio n. 2
0
        /// <summary>
        /// Checks for naming clashes in actions / senses / action pattern /
        /// competences.
        /// </summary>
        /// <param name="agent">The agent to check clashes for (as the agent provides
        ///     the behaviour dictionary).</param>
        /// <exception cref="NameException">If a clash is detected.
        /// </exception>
        internal void checkNamesClashes(Agent agent)
        {
            BehaviourDict dict = agent.getBehaviourDict();

            string [] actions = dict.getActionNames();
            string [] senses  = dict.getSenseNames();

            foreach (string competence in this.competences.Keys)
            {
                if (actions.Contains(competence))
                {
                    throw new NameException(string.Format("Competence name '{0}' clashes with " +
                                                          "action of same name", competence));
                }
                if (senses.Contains(competence))
                {
                    throw new NameException(string.Format("Competence name '{0}' clashes with " +
                                                          "sense of same name", competence));
                }
            }

            foreach (string actionpattern in this.actionPatterns.Keys)
            {
                if (actions.Contains(actionpattern))
                {
                    throw new NameException(string.Format("Action pattern name '{0}' clashes with " +
                                                          "action of same name", actionpattern));
                }
                if (senses.Contains(actionpattern))
                {
                    throw new NameException(string.Format("Action pattern name '{0}' clashes with " +
                                                          "sense of same name", actionpattern));
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Returns all behaviours of the agent's library as a behaviour
        /// dictionary.
        /// </summary>
        /// <returns>Behaviour dictionary with all behaviours in the library</returns>
        internal BehaviourDict LoadBehaviours()
        {
            BehaviourDict dict = AssemblyControl.GetControl().GetBehaviours(library, log, this);

            // TODO: Profiler needs to be included later on.
            //    self.log.info("Creating instance of behaviour '%s'" % \
            //                  behaviour_class.__name__)
            //    if (self.profiler is not None):
            //        try:
            //            behaviour = behaviour_class(self,self.profiler)
            //        except TypeError:
            //            behaviour = behaviour_class(self)
            //    else:
            //        behaviour = behaviour_class(self)
            return(dict);
        }
Esempio n. 4
0
        /// <summary>
        /// Initialises the agent to use the given library and plan.
        ///
        /// The plan has to be given as the plan name without the '.lap' extension.
        /// The attributes are the ones that are assigned to the behaviours
        /// when they are initialised. The world is the one that can be accessed
        /// by the behaviours by the L{AgentBase.getWorld} method.
        ///
        /// Note that when the behaviours are loaded from the given library, then
        /// they are reflected onto the agent object. That means, given that
        /// there is a behaviour called 'bot', then it can be accessed from another
        /// behaviour either by self.agent.getBehaviour("bot"), or by
        /// self.agent.bot. Consequently, behaviour names that clash with already
        /// existing agent attributes cause an AttributeError to be raise upon
        /// initialising the behaviours.
        ///
        /// The attributes are to be given in the same format as for the
        /// method L{AgentBase.assignAttributes}.
        /// </summary>
        /// <param name="library">The behaviour library to use.</param>
        /// <param name="plan">The plan to use (without the '.lap' ending).</param>
        /// <param name="attributes">The attributes to be assigned to the behaviours</param>
        /// <param name="world"></param>
        public AgentBase(string library, string plan, Dictionary <Tuple <string, string>, object> attributes, World world)
            : base("")
        {
            // get unique id for agent first, as constructor of LogBase accesses it
            id = AssemblyControl.GetControl().UniqueAgendId();
            this.Init(id);
            // store library for use when spawning new agents
            this.library = library;
            this.world   = world;

            linkedPlanName = plan;
            _listeners     = new List <IListener>();

            // we need to set the random number generator before we
            // load the behaviours, as they might access it upon
            // construction
            this.random = new Random();

            // if you are profiling, you need to fix this in your init_world.  see library/latchTest for an example & documentation
            // do this before loading Behaviours
            // TODO: disabled profiler
            // profiler= Profiler.initProfile(this);

            // load and register the behaviours, and reflect back onto agent
            this._bdict = LoadBehaviours();
            ReflectBehaviours();

            //# more for the profiler
            // FIXME: PR -- is there another place to do this?  will it succeed without MASON? JJB 1 March 2008
            //try:
            //    other.profiler.set_second_name(other._bdict._behaviours['MASON'].name)
            //except:
            //    # may want this for debugging:  print "profiler is off and/or MASON is not being used"
            //    pass # normally don't expect profiling, nor necessarily MASON

            // assign the initial attributes to the behaviours
            this.AssignAttributes(attributes);

            // load the plan
            LoadPlan(plan);
            // loop thread control
            _execLoop  = false;
            _loopPause = false;
        }
Esempio n. 5
0
        public virtual BehaviourDict GetBehaviours(string lib, ILog log, AgentBase agent)
#endif
        {
            BehaviourDict dict = new BehaviourDict();

            Type[] types = new Type[1] {
                typeof(AgentBase)
            };

            if (log is ILog)
            {
                log.Debug("Scanning library " + lib + " for behaviour classes");
            }


            Assembly a = GetAssembly(lib);

            foreach (Type t in a.GetTypes())
            {
                if (t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(POSH.sys.Behaviour)) && (this.worldScript == null || t.Name != this.worldScript.Second))
                {
                    log.Info(String.Format("Creating instance of behaviour {0}.", t));
                    ConstructorInfo behaviourConstruct = t.GetConstructor(types);
                    object[]        para = new object[1] {
                        agent
                    };
                    log.Debug("Registering behaviour in behaviour dictionary");
                    if (behaviourConstruct != null)
                    {
                        Behaviour behave = (Behaviour)behaviourConstruct.Invoke(para);
                        if (behave.IsSuitedForAgent(agent))
                        {
                            dict.RegisterBehaviour(behave);
                        }
                    }
                }
            }

            return((dict.getBehaviours().Count() > 0) ? dict : new BehaviourDict());
        }