Skip to content
Snippets Groups Projects
Commit e5347167 authored by shined day's avatar shined day
Browse files

Merge branch '25-ajout-d-une-boite-au-lettre' into 'master'

Resolve "Ajout d'une boite au lettre"

Closes #25

See merge request be-pyamak/pyamak-noyau!16
parents 1f2ccab6 f04b75f6
Branches
Tags
No related merge requests found
......@@ -7,10 +7,10 @@ from typing import List
import sys
import pathlib
from pyAmakCore.exception.override import ToOverrideWarning
sys.path.insert(0, str(pathlib.Path(__file__).parent))
from pyAmakCore.exception.override import ToOverrideWarning
from pyAmakCore.enumeration.agent_phase import Phase
from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy
......@@ -163,24 +163,24 @@ class Agent:
"""
ToOverrideWarning("on_act")
def __phase1(self) -> None:
def _phase1(self) -> None:
"""
this is the first phase of a cycle
"""
self.on_perceive()
self.compute_criticality()
self.__next_phase()
self._next_phase()
def __phase2(self) -> None:
def _phase2(self) -> None:
"""
this is the second phase of a cycle
"""
self.on_decide()
self.on_act()
self.compute_criticality()
self.__next_phase()
self._next_phase()
def __next_phase(self):
def _next_phase(self):
next_phase = {
Phase.INITIALIZING: Phase.PERCEPTION,
Phase.PERCEPTION: Phase.PERCEPTION_DONE,
......@@ -194,26 +194,26 @@ class Agent:
"""
Full cycle of an agent
"""
self.__next_phase()
self._next_phase()
execution_policy = self.__amas.get_execution_policy()
if execution_policy == ExecutionPolicy.TWO_PHASES:
if self.__phase == Phase.PERCEPTION:
self.on_cycle_begin()
self.__phase1()
self._phase1()
return
if self.__phase == Phase.DECISION_AND_ACTION:
self.__phase2()
self._phase2()
self.on_cycle_end()
return
if execution_policy == ExecutionPolicy.ONE_PHASE:
self.on_cycle_begin()
self.__phase1()
self.__next_phase()
self.__phase2()
self._phase1()
self._next_phase()
self._phase2()
self.on_cycle_end()
def __eq__(self, other: 'Agent') -> bool:
......
"""
Class Mail Mailbox and CommunicatingAgent
"""
from typing import Any, List
import pathlib
import sys
from pyAmakCore.exception.mailbox import ReceiverIsNotSelf, ReceiverDontExist
sys.path.insert(0, str(pathlib.Path(__file__).parent))
from pyAmakCore.classes.agent import Agent
class Mail:
"""
Class message
"""
def __init__(self, id_sender: int, id_receiver: int, message: Any) -> None:
self.__id_sender = id_sender
self.__id_receiver = id_receiver
self.__message = message
def get_id_sender(self):
"""
return sender id
"""
return self.__id_sender
def get_id_receiver(self):
"""
return receiver id
"""
return self.__id_receiver
def get_message(self):
"""
return message
"""
return self.__message
class Mailbox:
"""
Each agent have 1 unique mailbox that they can call to send or receive mail
"""
def __init__(self, owner_id: int, amas: 'Amas') -> None:
self.__mail_list: List['Mail'] = []
self.__owner_id: int = owner_id
self.__amas: 'Amas' = amas
def get_mail(self) -> 'Mail':
"""
return the next mail in the box, None if the mailBox is empty
"""
if len(self.__mail_list) == 0:
return None
mail = self.__mail_list.pop()
if mail.get_id_receiver() != self.__owner_id:
raise ReceiverIsNotSelf(self.__owner_id, mail.get_id_receiver())
return mail
def receive_mail(self, mail: 'Mail'):
"""
this method is called to put a mail in this mailbox
"""
self.__mail_list.append(mail)
def send_message(self, message: Any, id_receiver: int) -> None:
"""
this method is called to send a message
"""
mail = Mail(self.__owner_id, id_receiver, message)
for agent in self.__amas.get_agents():
if agent.get_id() == id_receiver:
return agent.receive_mail(mail)
raise ReceiverDontExist(id_receiver)
class CommunicatingAgent(Agent):
"""
Agent class that can communicate
"""
def __init__(self, amas: 'Amas') -> None:
super().__init__(amas)
self.__mailbox: 'Mailbox' = Mailbox(self.get_id(), amas)
def send_message(self, message: Any, id_receiver: int) -> None:
"""
send a message to another agent
"""
self.__mailbox.send_message(message, id_receiver)
def receive_mail(self, mail: 'Mail') -> None:
"""
this method is called by another mailbox to add a mail in the mailbox
"""
self.__mailbox.receive_mail(mail)
def _phase1(self):
"""
Override of phase 1 agent so he read mail before he perceive
this is the first phase of a cycle
"""
self._read_mails()
self.on_perceive()
self.compute_criticality()
self._next_phase()
def _read_mails(self) -> None:
"""
method that open all mail in the mailbox
"""
mail = self.__mailbox.get_mail()
while mail is not None:
self.read_mail(mail)
mail = self.__mailbox.get_mail()
def read_mail(self, mail: 'Mail') -> None:
"""
This method should be override to make an action whenever you read a mail
"""
"""
exception for mailbox
"""
class ReceiverIsNotSelf(Exception):
"""
this exception is called whenever you received a mail that is not sent to you
"""
def __init__(self, self_id, message_id):
self.self_id = self_id
self.message_id = message_id
super().__init__("Mail id is not self id")
def __str__(self):
return f' [Mailbox] : Mail id is not self id -> {self.message_id} != {self.self_id}'
class ReceiverDontExist(Exception):
"""
this exception is called whenever you try to send a mail to someone that doesn't exist
"""
def __init__(self, receiver_id):
self.receiver_id = receiver_id
super().__init__("receiver_id doesn't exist")
def __str__(self):
return f' [Mailbox] : Receiver_id could not be found -> {self.receiver_id}'
"""
test that Mail work as intended
"""
from unittest import TestCase, main
from pyAmakCore.classes.communicating_agent import Mail
class TestMail(TestCase):
"""
Test class Mail
"""
def test_mail(self) -> None:
"""
Test mail init
"""
mail = Mail(1, 5, None)
self.assertEqual(mail.get_id_sender(), 1)
self.assertEqual(mail.get_id_receiver(), 5)
self.assertEqual(mail.get_message(), None)
mail = Mail(255, 0, "test")
self.assertEqual(mail.get_id_sender(), 255)
self.assertEqual(mail.get_id_receiver(), 0)
self.assertEqual(mail.get_message(), "test")
if __name__ == '__main__':
main()
"""
test that Mailbox work as intended
"""
from unittest import TestCase, main
from pyAmakCore.classes.amas import Amas
from pyAmakCore.classes.communicating_agent import Mailbox
from pyAmakCore.classes.environment import Environment
class SimpleMailbox(Mailbox):
def get_amas(self):
return self._Mailbox__amas
def get_owner_id(self):
return self._Mailbox__owner_id
def get_mail_list(self):
return self._Mailbox__mail_list
class TestMailbox(TestCase):
"""
Test class Mailbox
"""
def test_init_mailbox(self) -> None:
"""
Test mailbox init
"""
environment = Environment()
amas = Amas(environment)
mailbox = SimpleMailbox(0, amas)
self.assertEqual(mailbox.get_amas(), amas)
self.assertEqual(mailbox.get_owner_id(), 0)
self.assertEqual(mailbox.get_mail_list(), [])
self.assertEqual(mailbox.get_mail(), None)
if __name__ == '__main__':
main()
......@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
setup(
name='pyAmakCore',
packages=find_packages(),
version='0.0.2',
version='0.0.3',
description='AmakFramework in python',
author='BE',
install_requires=[],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment