Skip to content
Snippets Groups Projects
Commit fd04a07d authored by shinedday's avatar shinedday
Browse files

Add all missing docstring & type

parent e8e3c4ea
No related branches found
No related tags found
No related merge requests found
...@@ -183,7 +183,7 @@ class Agent: ...@@ -183,7 +183,7 @@ class Agent:
self.__phase = next_phase.get(self.__phase) self.__phase = next_phase.get(self.__phase)
def __eq__(self, other: 'Agent') -> bool: def __eq__(self, other: 'Agent') -> bool:
# check class # should check class
if other is None: if other is None:
return False return False
return self.__id == other.__id return self.__id == other.__id
......
...@@ -50,7 +50,7 @@ class Amas(Schedulable, Loggable): ...@@ -50,7 +50,7 @@ class Amas(Schedulable, Loggable):
self.__agent_to_add = [] self.__agent_to_add = []
return tmp return tmp
def remove_pending_agent(self): def remove_pending_agent(self) -> List[Agent]:
""" """
add pending agent into agent and return removed agents add pending agent into agent and return removed agents
""" """
...@@ -61,7 +61,7 @@ class Amas(Schedulable, Loggable): ...@@ -61,7 +61,7 @@ class Amas(Schedulable, Loggable):
return tmp 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 add agent in the amas agents list without duplicate
""" """
......
...@@ -17,30 +17,30 @@ class Mail: ...@@ -17,30 +17,30 @@ class Mail:
""" """
def __init__(self, id_sender: int, id_receiver: int, message: Any, sending_date: int) -> None: def __init__(self, id_sender: int, id_receiver: int, message: Any, sending_date: int) -> None:
self.__id_sender = id_sender self.__id_sender: int = id_sender
self.__id_receiver = id_receiver self.__id_receiver: int = id_receiver
self.__message = message self.__message: Any = message
self.__sending_date = sending_date self.__sending_date: int = sending_date
def get_id_sender(self): def get_id_sender(self) -> int:
""" """
return sender id return sender id
""" """
return self.__id_sender return self.__id_sender
def get_id_receiver(self): def get_id_receiver(self) -> int:
""" """
return receiver id return receiver id
""" """
return self.__id_receiver return self.__id_receiver
def get_message(self): def get_message(self) -> Any:
""" """
return message return message
""" """
return self.__message return self.__message
def get_sending_date(self): def get_sending_date(self) -> int:
""" """
return sending_date return sending_date
""" """
......
""" """
Scheduler class Scheduler class
""" """
from typing import List
import sys import sys
import pathlib import pathlib
import pickle import pickle
from threading import Semaphore, Thread from threading import Semaphore, Thread
from time import sleep from time import sleep
sys.path.insert(0, str(pathlib.Path(__file__).parent)) sys.path.insert(0, str(pathlib.Path(__file__).parent))
...@@ -12,6 +15,7 @@ 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.amas import Amas
from pyAmakCore.classes.scheduler_tool.schedulable_thread import SchedulableThread from pyAmakCore.classes.scheduler_tool.schedulable_thread import SchedulableThread
from pyAmakCore.classes.scheduler_tool.amas_thread import AmasThread from pyAmakCore.classes.scheduler_tool.amas_thread import AmasThread
from pyAmakCore.classes.tools.schedulable import Schedulable
class Scheduler: class Scheduler:
...@@ -22,22 +26,28 @@ class Scheduler: ...@@ -22,22 +26,28 @@ class Scheduler:
def __init__(self, amas: Amas) -> None: def __init__(self, amas: Amas) -> None:
self.amas: Amas = amas 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: List[SchedulableThread] = []
self.schedulables_threads = [] self.schedulables_threads: List[Thread] = []
self.add_schedulables(amas, AmasThread) self.add_schedulables(amas, AmasThread)
self.add_schedulables(amas.get_environment(), SchedulableThread) 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 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) schedulable_thread = cls(schedulable)
self.schedulables.append(schedulable_thread) self.schedulables.append(schedulable_thread)
current_thread = Thread(target=schedulable_thread.run) current_thread = Thread(target=schedulable_thread.run)
...@@ -99,9 +109,12 @@ class Scheduler: ...@@ -99,9 +109,12 @@ class Scheduler:
self.last_part() self.last_part()
sleep(self.sleep_time) 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: for schedulable in self.schedulables:
schedulable.exit_bool = True schedulable.exit_bool = True
schedulable.is_waiting.release() schedulable.is_waiting.release()
...@@ -112,29 +125,47 @@ class Scheduler: ...@@ -112,29 +125,47 @@ class Scheduler:
program interface program interface
""" """
def exit_program(self): def exit_program(self) -> None:
"""
Exit the system as soon as possible
"""
self.exit_bool = True self.exit_bool = True
self.semaphore_start_stop.release() self.semaphore_start_stop.release()
def start(self): def start(self) -> None:
"""
Unlock the scheduler
"""
self.semaphore_start_stop.release() self.semaphore_start_stop.release()
def stop(self): def stop(self) -> None:
"""
Lock the scheduler
"""
self.semaphore_start_stop.acquire() 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 self.sleep_time = sleep_time
""" """
load & save program load & save program
""" """
def save(self): def save(self) -> None:
"""
Save the current state of the system
"""
with open('filename.pickle', 'wb') as handle: with open('filename.pickle', 'wb') as handle:
pickle.dump(self.amas, handle, protocol=pickle.HIGHEST_PROTOCOL) pickle.dump(self.amas, handle, protocol=pickle.HIGHEST_PROTOCOL)
@classmethod @classmethod
def load(cls): def load(cls) -> 'Scheduler':
"""
Load the last save of the system
"""
with open('filename.pickle', 'rb') as handle: with open('filename.pickle', 'rb') as handle:
amas_object = pickle.load(handle) amas_object = pickle.load(handle)
......
...@@ -20,7 +20,7 @@ class AgentThread: ...@@ -20,7 +20,7 @@ class AgentThread:
action_done = Semaphore(0) action_done = Semaphore(0)
execution_policy = ExecutionPolicy.ONE_PHASE execution_policy = ExecutionPolicy.ONE_PHASE
def __init__(self, agent: Agent): def __init__(self, agent: Agent) -> None:
self.agent: Agent = agent self.agent: Agent = agent
self.is_waiting: Semaphore = Semaphore(0) self.is_waiting: Semaphore = Semaphore(0)
......
...@@ -7,10 +7,10 @@ from typing import List ...@@ -7,10 +7,10 @@ from typing import List
import sys import sys
import pathlib import pathlib
from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) 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.scheduler_tool.agent_thread import AgentThread
from pyAmakCore.classes.amas import Amas from pyAmakCore.classes.amas import Amas
from pyAmakCore.classes.agent import Agent from pyAmakCore.classes.agent import Agent
...@@ -105,6 +105,9 @@ class AmasThread(SchedulableThread): ...@@ -105,6 +105,9 @@ class AmasThread(SchedulableThread):
self.schedulable.cycle() self.schedulable.cycle()
def run(self) -> None: def run(self) -> None:
"""
override run so when amas_thread try to stop, he closes all agents threads
"""
super().run() super().run()
self.close_child() self.close_child()
......
...@@ -16,20 +16,28 @@ class SchedulableThread: ...@@ -16,20 +16,28 @@ class SchedulableThread:
thread class used to thread schedulable thread class used to thread schedulable
""" """
def __init__(self, schedulable: Schedulable): def __init__(self, schedulable: Schedulable) -> None:
self.schedulable: Schedulable = schedulable self.schedulable: Schedulable = schedulable
self.is_waiting: Semaphore = Semaphore(0) self.is_waiting: Semaphore = Semaphore(0)
self.exit_bool: bool = False self.exit_bool: bool = False
self.action_done: Semaphore = Semaphore(0) 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() self.schedulable.on_cycle_begin()
def main_cycle_part(self): def main_cycle_part(self) -> None:
pass """
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.on_cycle_end()
self.schedulable.cycle() self.schedulable.cycle()
......
...@@ -3,6 +3,7 @@ Schedulable interface ...@@ -3,6 +3,7 @@ Schedulable interface
""" """
import sys import sys
import pathlib import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from pyAmakCore.exception.override import ToOverrideWarning from pyAmakCore.exception.override import ToOverrideWarning
...@@ -15,12 +16,18 @@ class Schedulable: ...@@ -15,12 +16,18 @@ class Schedulable:
def __init__(self): def __init__(self):
self.__nbr_cycle = 0 self.__nbr_cycle: int = 0
def get_cycle(self) -> int: def get_cycle(self) -> int:
"""
return nbr_cycle
"""
return self.__nbr_cycle return self.__nbr_cycle
def cycle(self) -> None: def cycle(self) -> None:
"""
add 1 to nbr_cycle
"""
self.__nbr_cycle += 1 self.__nbr_cycle += 1
def on_initialization(self) -> None: def on_initialization(self) -> None:
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
Scheduler class that need to be used for pyAmakIhm Scheduler class that need to be used for pyAmakIhm
""" """
import pathlib import pathlib
from time import sleep
import sys import sys
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment