[docs]classAgentServices:""" This class contains all the methods related to the agent services. As well as the asset has services that can be exposed, the agent also has services that can be exposed and used during the execution of the software. """def__init__(self,agent_object):""" The constructor method adds the object of the agent to have access to its information. Args: agent_object (spade.Agent): the SPADE agent object of the SMIA agent. """# The SPADE agent object is stored as a variable of the behaviour classself.myagent=agent_object# The services dictionary contains all available services of the agent, with its associated executable methodsself.services={}# The Lock object is used to manage the access to global service dictionaryself.lock=asyncio.Lock()asyncio.run(self.save_agent_service('RAM_memory_function',self.get_software_ram_memory))# TODO no hacerlo asi, rellenarlo de otra forma (p.e. por metodos de extension)# ------------------------# Services general methods# ------------------------
[docs]asyncdefget_agent_service_by_id(self,service_id):""" This method gets the agent service by its identifier. It returns None if the service is not found. Args: service_id (str): unique identifier of the agent service. Returns: method: executable method of the agent service. """asyncwithself.lock:ifservice_idnotinself.services:returnNoneelse:returnself.services[service_id]
[docs]asyncdefsave_agent_service(self,service_id,service_method):""" This method adds a new agent service with a given identifier and the associated execution method. Args: service_id (str): unique identifier of the agent service. service_method: execution method associated to the agent service. """# If it is an external function, it is bounded as a method of AgentServices class. This ensures that self is# automatically passed when the method is calledservice_method=types.MethodType(service_method,self)asyncwithself.lock:self.services[service_id]=service_method.__func__# The executable function is saved
[docs]asyncdefexecute_agent_service_by_id(self,service_id,**kwargs):""" This method executes the agent service by its identifier. The parameters of the method with their values are available in kwargs. Args: service_id (str): identifier of the agent service. **kwargs: received parameters with the values. Returns: object: the result of the executed agent service ('OK' if the service does not return anything). """# First, the method of the service via its identifier is gotservice_method=awaitself.get_agent_service_by_id(service_id)ifservice_methodisNone:raiseKeyError(f"Agent service with identifier {service_id} does not exist in this DT.")else:# Parameters adapted to the types required in the method are obtainedadapted_params=awaitAgentServiceUtils.get_adapted_service_parameters(service_method,**kwargs)# Eventually, the method with the transformed arguments is called, and it is waited for the resultresult=awaitAgentServiceUtils.safe_execute_agent_service(service_method,**adapted_params)ifresultisnotNone:returnresultelse:return"OK"
[docs]asyncdefget_software_ram_memory(self):""" This agent service gets the current used RAM memory of the software. Returns: float: the current used RAM memory of the software. """returnpsutil.virtual_memory().percent