diff --git a/pyAmakCore/classes/agent.py b/pyAmakCore/classes/agent.py index a94ae7537228f8cfdb18a9310e353e1598082239..663518650295faed57a764bf60c19d99f0dedad8 100644 --- a/pyAmakCore/classes/agent.py +++ b/pyAmakCore/classes/agent.py @@ -183,7 +183,7 @@ class Agent: self.__phase = next_phase.get(self.__phase) def __eq__(self, other: 'Agent') -> bool: - # check class + # should check class if other is None: return False return self.__id == other.__id diff --git a/pyAmakCore/classes/amas.py b/pyAmakCore/classes/amas.py index 7a460931b4116cf7aa36716b6aa64d51a5ac0c36..ccb253d24e3b68a397f61dd7ed9fc80c02b52379 100644 --- a/pyAmakCore/classes/amas.py +++ b/pyAmakCore/classes/amas.py @@ -50,7 +50,7 @@ class Amas(Schedulable, Loggable): self.__agent_to_add = [] return tmp - def remove_pending_agent(self): + def remove_pending_agent(self) -> List[Agent]: """ add pending agent into agent and return removed agents """ @@ -61,7 +61,7 @@ class Amas(Schedulable, Loggable): return tmp - def add_agent(self, agent: 'Agent') -> None: + def add_agent(self, agent: Agent) -> None: """ add agent in the amas agents list without duplicate """ diff --git a/pyAmakCore/classes/communicating_agent.py b/pyAmakCore/classes/communicating_agent.py index e31b4ccce66e9a84a2856456e0f0e25c8558610e..d70cb0cd6636f4d142ca075a8355c78ccf11d118 100644 --- a/pyAmakCore/classes/communicating_agent.py +++ b/pyAmakCore/classes/communicating_agent.py @@ -17,30 +17,30 @@ class Mail: """ def __init__(self, id_sender: int, id_receiver: int, message: Any, sending_date: int) -> None: - self.__id_sender = id_sender - self.__id_receiver = id_receiver - self.__message = message - self.__sending_date = sending_date + self.__id_sender: int = id_sender + self.__id_receiver: int = id_receiver + self.__message: Any = message + self.__sending_date: int = sending_date - def get_id_sender(self): + def get_id_sender(self) -> int: """ return sender id """ return self.__id_sender - def get_id_receiver(self): + def get_id_receiver(self) -> int: """ return receiver id """ return self.__id_receiver - def get_message(self): + def get_message(self) -> Any: """ return message """ return self.__message - def get_sending_date(self): + def get_sending_date(self) -> int: """ return sending_date """ diff --git a/pyAmakCore/classes/scheduler.py b/pyAmakCore/classes/scheduler.py index b16b13ffedf7dd8bfbaedff0972635e1364138a6..a1bdcc2a94b241639de149b967c458455fb94f0e 100644 --- a/pyAmakCore/classes/scheduler.py +++ b/pyAmakCore/classes/scheduler.py @@ -1,10 +1,13 @@ """ Scheduler class """ +from typing import List + import sys import pathlib import pickle from threading import Semaphore, Thread + from time import sleep sys.path.insert(0, str(pathlib.Path(__file__).parent)) @@ -12,6 +15,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).parent)) from pyAmakCore.classes.amas import Amas from pyAmakCore.classes.scheduler_tool.schedulable_thread import SchedulableThread from pyAmakCore.classes.scheduler_tool.amas_thread import AmasThread +from pyAmakCore.classes.tools.schedulable import Schedulable class Scheduler: @@ -22,22 +26,28 @@ class Scheduler: def __init__(self, amas: Amas) -> None: self.amas: Amas = amas - self.exit_bool = False + self.exit_bool: bool = False - self.semaphore_start_stop = Semaphore(0) + self.semaphore_start_stop: Semaphore = Semaphore(0) - self.schedulables = [] - self.schedulables_threads = [] + self.schedulables: List[SchedulableThread] = [] + self.schedulables_threads: List[Thread] = [] self.add_schedulables(amas, AmasThread) self.add_schedulables(amas.get_environment(), SchedulableThread) - self.sleep_time = 0 + self.sleep_time: float = 0 - def get_amas(self): + def get_amas(self) -> Amas: + """ + return amas pointer + """ return self.amas - def add_schedulables(self, schedulable, cls): + def add_schedulables(self, schedulable: Schedulable, cls) -> None: + """ + add a schedulable in scheduler + """ schedulable_thread = cls(schedulable) self.schedulables.append(schedulable_thread) current_thread = Thread(target=schedulable_thread.run) @@ -99,9 +109,12 @@ class Scheduler: self.last_part() sleep(self.sleep_time) - self.close_childs() + self.close_child() - def close_childs(self): + def close_child(self) -> None: + """ + tell all child to shut down + """ for schedulable in self.schedulables: schedulable.exit_bool = True schedulable.is_waiting.release() @@ -112,29 +125,47 @@ class Scheduler: program interface """ - def exit_program(self): + def exit_program(self) -> None: + """ + Exit the system as soon as possible + """ self.exit_bool = True self.semaphore_start_stop.release() - def start(self): + def start(self) -> None: + """ + Unlock the scheduler + """ self.semaphore_start_stop.release() - def stop(self): + def stop(self) -> None: + """ + Lock the scheduler + """ self.semaphore_start_stop.acquire() - def set_sleep(self, sleep_time: int): + def set_sleep(self, sleep_time: int) -> None: + """ + Set sleep value between 2 cycles + """ self.sleep_time = sleep_time """ load & save program """ - def save(self): + def save(self) -> None: + """ + Save the current state of the system + """ with open('filename.pickle', 'wb') as handle: pickle.dump(self.amas, handle, protocol=pickle.HIGHEST_PROTOCOL) @classmethod - def load(cls): + def load(cls) -> 'Scheduler': + """ + Load the last save of the system + """ with open('filename.pickle', 'rb') as handle: amas_object = pickle.load(handle) diff --git a/pyAmakCore/classes/scheduler_tool/agent_thread.py b/pyAmakCore/classes/scheduler_tool/agent_thread.py index 72098d2bb31d8433ac47ee3525e97e3e5b680b1d..493562d726a8ef2225310a8e4296fa9858025292 100644 --- a/pyAmakCore/classes/scheduler_tool/agent_thread.py +++ b/pyAmakCore/classes/scheduler_tool/agent_thread.py @@ -20,7 +20,7 @@ class AgentThread: action_done = Semaphore(0) execution_policy = ExecutionPolicy.ONE_PHASE - def __init__(self, agent: Agent): + def __init__(self, agent: Agent) -> None: self.agent: Agent = agent self.is_waiting: Semaphore = Semaphore(0) diff --git a/pyAmakCore/classes/scheduler_tool/amas_thread.py b/pyAmakCore/classes/scheduler_tool/amas_thread.py index 1d70f237eca2a161bff77ff0eafde8878cb23d7c..dad5c7461de7e1bb00c4e84d8095153312caf4dd 100644 --- a/pyAmakCore/classes/scheduler_tool/amas_thread.py +++ b/pyAmakCore/classes/scheduler_tool/amas_thread.py @@ -7,10 +7,10 @@ from typing import List import sys import pathlib -from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) +from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy from pyAmakCore.classes.scheduler_tool.agent_thread import AgentThread from pyAmakCore.classes.amas import Amas from pyAmakCore.classes.agent import Agent @@ -105,6 +105,9 @@ class AmasThread(SchedulableThread): self.schedulable.cycle() def run(self) -> None: + """ + override run so when amas_thread try to stop, he closes all agents threads + """ super().run() self.close_child() diff --git a/pyAmakCore/classes/scheduler_tool/schedulable_thread.py b/pyAmakCore/classes/scheduler_tool/schedulable_thread.py index 17f347ab275403fbe548ec6390baeab4b8e4525a..9429e269b2c5ca42b94d28d3ac856236d3f1fee3 100644 --- a/pyAmakCore/classes/scheduler_tool/schedulable_thread.py +++ b/pyAmakCore/classes/scheduler_tool/schedulable_thread.py @@ -16,20 +16,28 @@ class SchedulableThread: thread class used to thread schedulable """ - def __init__(self, schedulable: Schedulable): + def __init__(self, schedulable: Schedulable) -> None: self.schedulable: Schedulable = schedulable self.is_waiting: Semaphore = Semaphore(0) self.exit_bool: bool = False self.action_done: Semaphore = Semaphore(0) - def on_cycle_begin(self): + def on_cycle_begin(self) -> None: + """ + first part of the cycle + """ self.schedulable.on_cycle_begin() - def main_cycle_part(self): - pass + def main_cycle_part(self) -> None: + """ + main part of the cycle + """ - def on_cycle_end(self): + def on_cycle_end(self) -> None: + """ + last part of the cycle + """ self.schedulable.on_cycle_end() self.schedulable.cycle() diff --git a/pyAmakCore/classes/tools/schedulable.py b/pyAmakCore/classes/tools/schedulable.py index c594ba8d90814ad5dbb767db5b7bfb9eb93749a4..dda929cf0f60f62cc7238eaf134f03b92a33e5a2 100644 --- a/pyAmakCore/classes/tools/schedulable.py +++ b/pyAmakCore/classes/tools/schedulable.py @@ -3,6 +3,7 @@ Schedulable interface """ import sys import pathlib + sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) from pyAmakCore.exception.override import ToOverrideWarning @@ -15,12 +16,18 @@ class Schedulable: def __init__(self): - self.__nbr_cycle = 0 + self.__nbr_cycle: int = 0 def get_cycle(self) -> int: + """ + return nbr_cycle + """ return self.__nbr_cycle def cycle(self) -> None: + """ + add 1 to nbr_cycle + """ self.__nbr_cycle += 1 def on_initialization(self) -> None: diff --git a/pyAmakCore/classes/tools/schedulerIHM.py b/pyAmakCore/classes/tools/schedulerIHM.py index 9390671fd74934ee13020da1a0b4c51099fe6a76..a9358b53a8a0c64c1a56ddfd2decdab59c3b806a 100644 --- a/pyAmakCore/classes/tools/schedulerIHM.py +++ b/pyAmakCore/classes/tools/schedulerIHM.py @@ -2,8 +2,6 @@ Scheduler class that need to be used for pyAmakIhm """ import pathlib -from time import sleep - import sys sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))