예제 #1
0
파일: Action.cs 프로젝트: ashr/posh-sharp
 /// <summary>
 /// Picks the given action from the given agent.
 ///
 /// The method uses the agent's behaviour dictionary to get the
 /// action method.
 ///
 /// The log domain is set to "[AgentId].Action.[action_name]".
 ///
 /// The action name is set to "[BehaviourName].[action_name]".
 /// </summary>
 /// <param name="agent">The agent that can perform the action.</param>
 /// <param name="actionName">The name of the action</param>
 public POSHAction(Agent agent, string actionName)
     : base(string.Format("Action.{0}", actionName), agent)
 {
     behaviourDict = agent.getBehaviourDict();
     action        = behaviourDict.getAction(actionName);
     behaviour     = behaviourDict.getActionBehaviour(actionName);
     name          = string.Format("{0}.{1}", behaviour.GetName(), actionName);
     log.Debug("Created");
 }
예제 #2
0
 /// <summary>
 /// Picks the given action from the given agent.
 /// 
 /// The method uses the agent's behaviour dictionary to get the
 /// action method.
 /// 
 /// The log domain is set to "[AgentId].Action.[action_name]".
 /// 
 /// The action name is set to "[BehaviourName].[action_name]".
 /// </summary>
 /// <param name="agent">The agent that can perform the action.</param>
 /// <param name="actionName">The name of the action</param>
 public POSHAction(Agent agent, string actionName)
     : base(string.Format("Action.{0}",actionName),agent)
 {
     behaviourDict = agent.getBehaviourDict();
     action = behaviourDict.getAction(actionName);
     behaviour = behaviourDict.getActionBehaviour(actionName);
     name = string.Format("{0}.{1}",behaviour.GetName(),actionName);
     log.Debug("Created");
 }
예제 #3
0
        /// <summary>
        /// Picks the given sense or sense-act from the given agent.
        ///
        /// The method uses the agent's behaviour dictionary to get the
        /// sense / sense-act method.
        ///
        /// The log domain is set to "[AgentId].Sense.[sense_name]".
        ///
        /// The sense name is set to "[BehaviourName].[sense_name]".
        /// </summary>
        /// <param name="agent">The agent that can use the sense.</param>
        /// <param name="senseName">The name of the sense</param>
        /// <param name="value">The value to compare it to. This is given as a string,
        /// but will be converted to an integer or float or boolean, or
        /// left as a string, whatever is possible. If None is given
        /// then the sense has to evaluate to True.</param>
        /// <param name="predicate">"==", "!=", "<", ">", "<=", ">=". If null is
        ///    given, then "==" is assumed.</param>
        public POSHSense(Agent agent, string senseName, string value = null, string predicate = null)
            :base(string.Format("Sense.{0}",senseName),agent)
        {
            behaviourDict = agent.getBehaviourDict();
            sense = behaviourDict.getSense(senseName);
            behaviour = behaviourDict.getSenseBehaviour(senseName);
            name = string.Format("{0}.{1}", behaviour.GetName(), senseName);
            this.value = (value is string) ? AgentInitParser.strToValue(value) : null;
            this.predicate = (predicate is string) ? predicate : "==";

            log.Debug("Created");
        }
예제 #4
0
        /// <summary>
        /// Picks the given action from the given agent.
        /// 
        /// The method uses the agent's behaviour dictionary to get the
        /// action method.
        /// 
        /// The log domain is set to "[AgentId].Action.[action_name]".
        /// 
        /// The action name is set to "[BehaviourName].[action_name]".
        /// </summary>
        /// <param name="agent">The agent that can perform the action.</param>
        /// <param name="actionName">The name of the action</param>
        public POSHAction(Agent agent, string actionName)
            : base(string.Format("Action.{0}",actionName),agent)
        {
            behaviourDict = agent.getBehaviourDict();
            action = behaviourDict.getAction(actionName);
            behaviour = behaviourDict.getActionBehaviour(actionName);
            name = string.Format("{0}.{1}",behaviour.GetName(),actionName);
            log.Debug("Created");

            //hook up to a listener for fire events
            if (agent.HasListenerForTyp(EventType.Fire))
                agent.SubscribeToListeners(this, EventType.Fire);
        }
예제 #5
0
파일: Action.cs 프로젝트: ashr/posh-sharp
        /// <summary>
        /// Picks the given action from the given agent.
        ///
        /// The method uses the agent's behaviour dictionary to get the
        /// action method.
        ///
        /// The log domain is set to "[AgentId].Action.[action_name]".
        ///
        /// The action name is set to "[BehaviourName].[action_name]".
        /// </summary>
        /// <param name="agent">The agent that can perform the action.</param>
        /// <param name="actionName">The name of the action</param>
        public POSHAction(Agent agent, string actionName)
            : base(string.Format("Action.{0}", actionName), agent)
        {
            behaviourDict = agent.getBehaviourDict();
            action        = behaviourDict.getAction(actionName);
            behaviour     = behaviourDict.getActionBehaviour(actionName);
            name          = string.Format("{0}.{1}", behaviour.GetName(), actionName);
            log.Debug("Created");

            //hook up to a listener for fire events
            if (agent.HasListenerForTyp(EventType.Fire))
            {
                agent.SubscribeToListeners(this, EventType.Fire);
            }
        }
예제 #6
0
        /// <summary>
        /// Picks the given sense or sense-act from the given agent.
        ///
        /// The method uses the agent's behaviour dictionary to get the
        /// sense / sense-act method.
        ///
        /// The log domain is set to "[AgentId].Sense.[sense_name]".
        ///
        /// The sense name is set to "[BehaviourName].[sense_name]".
        /// </summary>
        /// <param name="agent">The agent that can use the sense.</param>
        /// <param name="senseName">The name of the sense</param>
        /// <param name="value">The value to compare it to. This is given as a string,
        /// but will be converted to an integer or float or boolean, or
        /// left as a string, whatever is possible. If None is given
        /// then the sense has to evaluate to True.</param>
        /// <param name="predicate">"==", "!=", "<", ">", "<=", ">=". If null is
        ///    given, then "==" is assumed.</param>
        public POSHSense(Agent agent, string senseName, string value, string predicate)
            : base(string.Format("Sense.{0}", senseName), agent)
        {
            this.senseName = senseName;
            behaviourDict  = agent.getBehaviourDict();
            sense          = behaviourDict.getSense(senseName);
            behaviour      = behaviourDict.getSenseBehaviour(senseName);
            name           = string.Format("{0}.{1}", behaviour.GetName(), senseName);
            this.value     = (value is string) ? AgentInitParser.strToValue(value) : null;
            this.predicate = (predicate is string) ? predicate : "==";

            log.Debug("Created");

            //hook up to a listener for fire events
            if (agent.HasListenerForTyp(EventType.Fire))
            {
                agent.SubscribeToListeners(this, EventType.Fire);
            }
        }
예제 #7
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;
        }
예제 #8
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;
        }
예제 #9
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();
        }