refactor: agents inherit logger from BaseAgent

Created a class `BaseAgent`, from which all agents inherit. They get
assigned a logger with a nice name (something like
`control_backend.agents.AgentName`).

The BDI core takes care of its own logger, as bdi is still a module.

ref: N25B-241
This commit is contained in:
2025-11-04 20:48:55 +01:00
parent d43cb9394a
commit a98018ddda
15 changed files with 174 additions and 159 deletions

View File

@@ -1,5 +1,4 @@
import json
import logging
from spade.agent import Message
from spade.behaviour import CyclicBehaviour
@@ -15,12 +14,11 @@ class BeliefSetterBehaviour(CyclicBehaviour):
"""
agent: BDIAgent
logger = logging.getLogger(__name__)
async def run(self):
"""Polls for messages and processes them."""
msg = await self.receive()
self.logger.debug(
self.agent.logger.debug(
"Received message from %s with thread '%s' and body: %s",
msg.sender,
msg.thread,
@@ -28,23 +26,24 @@ class BeliefSetterBehaviour(CyclicBehaviour):
)
self._process_message(msg)
def _process_message(self, message: Message):
"""Routes the message to the correct processing function based on the sender."""
sender = message.sender.node # removes host from jid and converts to str
self.logger.debug("Processing message from sender: %s", sender)
self.agent.logger.debug("Processing message from sender: %s", sender)
match sender:
case settings.agent_settings.belief_collector_agent_name:
self.logger.debug("Message is from the belief collector agent. Processing as belief message.")
self.agent.logger.debug(
"Message is from the belief collector agent. Processing as belief message."
)
self._process_belief_message(message)
case _:
self.logger.debug("Not the belief agent, discarding message")
self.agent.logger.debug("Not the belief agent, discarding message")
pass
def _process_belief_message(self, message: Message):
if not message.body:
self.logger.debug("Ignoring message with empty body from %s", message.sender.node)
self.agent.logger.debug("Ignoring message with empty body from %s", message.sender.node)
return
match message.thread:
@@ -53,10 +52,10 @@ class BeliefSetterBehaviour(CyclicBehaviour):
beliefs: dict[str, list[str]] = json.loads(message.body)
self._set_beliefs(beliefs)
except json.JSONDecodeError:
self.logger.error(
self.agent.logger.error(
"Could not decode beliefs from JSON. Message body: '%s'",
message.body,
exc_info=True
exc_info=True,
)
case _:
pass
@@ -64,21 +63,23 @@ class BeliefSetterBehaviour(CyclicBehaviour):
def _set_beliefs(self, beliefs: dict[str, list[str]]):
"""Removes previous values for beliefs and updates them with the provided values."""
if self.agent.bdi is None:
self.logger.warning("Cannot set beliefs; agent's BDI is not yet initialized.")
self.agent.logger.warning("Cannot set beliefs; agent's BDI is not yet initialized.")
return
if not beliefs:
self.logger.debug("Received an empty set of beliefs. No beliefs were updated.")
self.agent.logger.debug("Received an empty set of beliefs. No beliefs were updated.")
return
# Set new beliefs (outdated beliefs are automatically removed)
for belief, arguments in beliefs.items():
self.logger.debug("Setting belief %s with arguments %s", belief, arguments)
self.agent.logger.debug("Setting belief %s with arguments %s", belief, arguments)
self.agent.bdi.set_belief(belief, *arguments)
# Special case: if there's a new user message, flag that we haven't responded yet
if belief == "user_said":
self.agent.bdi.set_belief("new_message")
self.logger.debug("Detected 'user_said' belief, also setting 'new_message' belief.")
self.agent.logger.debug(
"Detected 'user_said' belief, also setting 'new_message' belief."
)
self.logger.info("Successfully updated %d beliefs.", len(beliefs))
self.agent.logger.info("Successfully updated %d beliefs.", len(beliefs))