/// <summary> /// Reflect the agent objects onto the agent. /// /// This action is performed for all behaviour objects in the agent's /// behaviour dictionary. If an attribute with the same name already /// exists, an error is raised. /// </summary> /// internal void ReflectBehaviours() { foreach (Behaviour behave in _bdict.getBehaviours()) { log.Debug(String.Format("Reflecting behaviour {0} back onto agent", behave.GetName())); if (this.attributes.ContainsKey(behave.GetName())) { throw new AttributeException(string.Format( "Assigning behaviour {0} failed due to name clash with existing attribute", behave.GetName() )); } this.attributes.Add(behave.GetName(), behave); } }
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()); }