Не удаётся передать сообщение другому агенту в PADE
Не удаётся передать сообщение от GroupAgent к TeacherAgent. В дебаге выдаёт, что TeacherAgent is not Active.
from pade.acl.aid import AID
from pade.acl.messages import ACLMessage
from pade.misc.utility import display_message
from pade.core.agent import Agent
from pade.misc.utility import start_loop
import sys
# Агент группы
class GroupAgent(Agent):
def __init__(self, aid, teacher_aid, available_slots):
super(GroupAgent, self).__init__(aid=aid, debug=True)
display_message(self.aid.localname, 'Группа готова!')
self.teacher_aid = teacher_aid
self.available_slots = available_slots
def on_start(self):
super().on_start()
self.send_request()
def send_request(self):
msg = ACLMessage(ACLMessage.REQUEST)
msg.add_receiver(AID(self.teacher_aid))
msg.set_content(str(self.available_slots))
print(AID.getPort(AID(self.teacher_aid)))
self.send(msg)
display_message(self.aid.localname, "Отправка запроса учителю")
def send_cancel_request(self, slot):
display_message(self.aid.localname, f"Отправка запроса на отмену брони {slot}")
msg = ACLMessage(ACLMessage.CANCEL)
msg.add_receiver(AID(self.teacher_aid))
msg.set_content(slot)
self.send(msg)
def react(self, message):
super().react(message)
display_message(self.aid.localname, f"Получен ответ от учителя: {message.content}")
# Агент преподавателя
class TeacherAgent(Agent):
def __init__(self, aid, available_slots):
super(TeacherAgent, self).__init__(aid=aid, debug=True)
display_message(self.aid.localname, 'Учитель готов!')
self.available_slots = available_slots
self.booked_slots = {} # Словарь забронированных слотов (ключ: время, значение: группа)
def react(self, message):
super().react(message)
if message.performative == ACLMessage.REQUEST:
display_message(self.aid.localname, f"Получен запрос от группы: {message.content}")
group_slots = eval(message.content)
matched_slots = [slot for slot in group_slots if
slot in self.available_slots and slot not in self.booked_slots]
if matched_slots:
booked_slot = matched_slots[0] # Выбираем первый доступный слот
self.booked_slots[booked_slot] = message.sender.localname
response_content = f"Бронь подтверждена: {booked_slot}"
else:
response_content = "Нет доступных слотов"
response = ACLMessage(ACLMessage.INFORM)
response.add_receiver(message.sender)
response.set_content(response_content)
self.send(response)
display_message(self.aid.localname, f"Отправлен ответ: {response.content}")
elif message.performative == ACLMessage.CANCEL:
slot_to_cancel = message.content
if slot_to_cancel in self.booked_slots and self.booked_slots[slot_to_cancel] == message.sender.localname:
del self.booked_slots[slot_to_cancel]
response_content = f"Бронь отменена: {slot_to_cancel}"
else:
response_content = "Ошибка: Слот не найден или не принадлежит этой группе"
response = ACLMessage(ACLMessage.INFORM)
response.add_receiver(message.sender)
response.set_content(response_content)
self.send(response)
display_message(self.aid.localname, f"Отправлен ответ: {response.content}")
if __name__ == "__main__":
agents = []
port = 20000
teacher = TeacherAgent(AID('teacher@localhost:20005'), ['09:00', '14:00', '15:00'])
group1 = GroupAgent(AID('group1@localhost:20000'), 'teacher@localhost:20005', ['10:00', '14:00', '16:00'])
agents.append(teacher)
agents.append(group1)
start_loop(agents)
[teacher] 14/03/2025 20:33:40.106 --> Учитель готов!
[group1] 14/03/2025 20:33:40.106 --> Группа готова!
('[MESSAGE DELIVERY]', 'subscribe', 'FROM', 'teacher@localhost:20005', 'TO', 'ams@localhost:8000')
('[MESSAGE DELIVERY]', 'subscribe', 'FROM', 'group1@localhost:20000', 'TO', 'ams@localhost:8000')
20005
[group1] 14/03/2025 20:33:40.107 --> Agent teacher@localhost:20005 is not active
[group1] 14/03/2025 20:33:40.107 --> Отправка запроса учителю