refactor: improve logging and module structure
Changed some folders to not be modules and organized some `__init__.py` files. ref: N25B-223
This commit is contained in:
@@ -3,7 +3,7 @@ import logging
|
||||
|
||||
from spade.agent import Message
|
||||
from spade.behaviour import CyclicBehaviour
|
||||
from spade_bdi.bdi import BDIAgent, BeliefNotInitiated
|
||||
from spade_bdi.bdi import BDIAgent
|
||||
|
||||
from control_backend.core.config import settings
|
||||
|
||||
@@ -11,26 +11,32 @@ from control_backend.core.config import settings
|
||||
class BeliefSetterBehaviour(CyclicBehaviour):
|
||||
"""
|
||||
This is the behaviour that the BDI agent runs. This behaviour waits for incoming
|
||||
message and processes it based on sender.
|
||||
message and updates the agent's beliefs accordingly.
|
||||
"""
|
||||
|
||||
agent: BDIAgent
|
||||
logger = logging.getLogger("BDI/Belief Setter")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
async def run(self):
|
||||
msg = await self.receive(timeout=0.1)
|
||||
if msg:
|
||||
self.logger.info(f"Received message {msg.body}")
|
||||
self._process_message(msg)
|
||||
"""Polls for messages and processes them."""
|
||||
msg = await self.receive()
|
||||
self.logger.debug(
|
||||
"Received message from %s with thread '%s' and body: %s",
|
||||
msg.sender,
|
||||
msg.thread,
|
||||
msg.body,
|
||||
)
|
||||
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("Sender: %s", sender)
|
||||
self.logger.debug("Processing message from sender: %s", sender)
|
||||
|
||||
match sender:
|
||||
case settings.agent_settings.belief_collector_agent_name:
|
||||
self.logger.debug("Processing message from belief collector.")
|
||||
self.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")
|
||||
@@ -38,6 +44,7 @@ class BeliefSetterBehaviour(CyclicBehaviour):
|
||||
|
||||
def _process_belief_message(self, message: Message):
|
||||
if not message.body:
|
||||
self.logger.debug("Ignoring message with empty body from %s", message.sender.node)
|
||||
return
|
||||
|
||||
match message.thread:
|
||||
@@ -45,22 +52,33 @@ class BeliefSetterBehaviour(CyclicBehaviour):
|
||||
try:
|
||||
beliefs: dict[str, list[str]] = json.loads(message.body)
|
||||
self._set_beliefs(beliefs)
|
||||
except json.JSONDecodeError as e:
|
||||
self.logger.error("Could not decode beliefs into JSON format: %s", e)
|
||||
except json.JSONDecodeError:
|
||||
self.logger.error(
|
||||
"Could not decode beliefs from JSON. Message body: '%s'",
|
||||
message.body,
|
||||
exc_info=True
|
||||
)
|
||||
case _:
|
||||
pass
|
||||
|
||||
def _set_beliefs(self, beliefs: dict[str, list[str]]):
|
||||
"""Remove previous values for beliefs and update them with the provided values."""
|
||||
"""Removes previous values for beliefs and updates them with the provided values."""
|
||||
if self.agent.bdi is None:
|
||||
self.logger.warning("Cannot set beliefs, since agent's BDI is not yet initialized.")
|
||||
self.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.")
|
||||
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.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")
|
||||
if belief == "user_said":
|
||||
self.agent.bdi.set_belief("new_message")
|
||||
self.logger.debug("Detected 'user_said' belief, also setting 'new_message' belief.")
|
||||
|
||||
self.logger.info("Set belief %s with arguments %s", belief, arguments)
|
||||
self.logger.info("Successfully updated %d beliefs.", len(beliefs))
|
||||
Reference in New Issue
Block a user