Skip to content
Snippets Groups Projects
Commit 064f30fd authored by Wissam Benadjaoud's avatar Wissam Benadjaoud
Browse files

Suite du nettoyage

parent a0182a58
Branches
Tags
No related merge requests found
......@@ -14,7 +14,6 @@ sys.path.insert(0, str(pathlib.Path(__file__).parent))
from pyAmakCore.classes.environment import Environment
from pyAmakCore.classes.scheduler import Scheduler
from pyAmakCore.classes.schedulable import Schedulable
from pyAmakCore.enumeration.scheduling import *
from pyAmakCore.exception.override import ToOverrideWarning
from pyAmakCore.enumeration.executionPolicy import *
......@@ -24,8 +23,6 @@ class Amas(Schedulable):
Class Amas
"""
#TODO : corriger l'initialisation du scheduler
#TODO : scheduling_amas is not str.
def __init__(
self,
environment: 'Environment',
......@@ -101,7 +98,6 @@ class Amas(Schedulable):
for agent in agents:
self.add_agent(agent)
#TODO : tout les semaphores sont faux. Ils attendent un bool pas un int.
def cycle(self) -> None:
"""
Runs each agent of the list
......
......@@ -5,6 +5,7 @@ import sys
import pathlib
import threading
import time
from typing import List
sys.path.insert(0, str(pathlib.Path(__file__).parent))
......@@ -16,55 +17,41 @@ class Scheduler:
Class Scheduler
"""
#TODO : est ce que un scheduler peut vraiment etre initialisé sans schedulables?
def __init__(self, schedulables=[]):
def __init__(self, schedulables: List['Schedulable']):
self._state = state.IDLE
self._sleep = 10
self._locked = 0
self._onChange = []
self._defaultScheduler = None
self._pendingAdditionSchedulables = []
self._pendingRemovalSchedulables = []
self._schedulables = schedulables
self._stateLock = threading.Lock()
def get_pending_removal_schedulables(self):
def get_pending_removal_schedulables(self) -> List['Schedulable']:
"""
return _pendingRemovalSchedulables
"""
return self._pendingRemovalSchedulables
def get_pending_addition_schedulables(self):
def get_pending_addition_schedulables(self) -> List['Schedulable']:
"""
return _pendingAdditionSchedulables
"""
return self._pendingAdditionSchedulables
def get_on_change(self):
"""
return on change value
"""
return self._onChange
def get_state(self):
def get_state(self) -> 'state':
"""
return current state of scheduler
"""
return self._state
def get_locked(self):
def get_locked(self) -> int:
"""
return locked
"""
return self._locked
def get_on_stop(self):
"""
return current on_stop value
"""
return self._onStop
def get_default_scheduler(self):
def get_default_scheduler(self) -> 'Scheduler':
"""
Create or return the default scheduler
"""
......@@ -73,72 +60,51 @@ class Scheduler:
return self._defaultScheduler
def get_sleep(self):
def get_sleep(self) -> int:
"""
Getter for the sleep time
"""
return self._sleep
#TODO : type
#TODO : sleep shadow method
def set_sleep(self, sleep):
def set_sleep(self, sleep: int) -> None:
"""
Setter for the sleep time
"""
self._sleep = 10/sleep
#TODO : type
#TODO : Argument name should be lowercase
def set_on_stop(self, onStop):
"""
Set the method that must be executed when the system is stopped
"""
self._onStop = onStop
#TODO : type
#TODO : Argument name should be lowercase
def add_on_change(self, onChange):
"""
Add a method that must be executed when the scheduler speed is changed
"""
self._onChange = onChange
def lock(self):
def lock(self) -> None:
"""
Soft lock the scheduler to avoid a too early running
"""
self._locked += 1
def unlock(self):
def unlock(self) -> None:
"""
Soft unlock the scheduler to avoid a too early running
"""
self._locked -= 1
#TODO : type
def add(self, schedulable):
def add(self, schedulable: 'Schedulable') -> None:
"""
Plan to add a schedulable
"""
self._pendingAdditionSchedulables.append(schedulable)
#TODO : type
def remove(self, schedulable):
def remove(self, schedulable: 'Schedulable') -> None:
"""
Plan to remove a schedulable
"""
self._pendingRemovalSchedulables.append(schedulable)
#TODO : type
def is_running(self):
def is_running(self) -> bool:
"""
Is the scheduler running ?
"""
return self._state == state.RUNNING
#TODO : type
#TODO : supprimer les partie inutile / finir la methode
def start_with_sleep(self, time_sleep):
def start_with_sleep(self, time_sleep: int) -> None:
"""
Set the delay between two cycles and launch the scheduler if it is not
running
......@@ -146,7 +112,6 @@ class Scheduler:
if self._locked > 0:
return
self.set_sleep(time_sleep)
# A vérifier si c'est juste ou "stateLock.lock();"
self._stateLock.acquire()
if self._state == state.IDLE:
self._state = state.RUNNING
......@@ -168,7 +133,6 @@ class Scheduler:
if self._locked > 0:
return
self.set_sleep(0)
# A vérifier si c'est juste ou "stateLock.lock();"
self._stateLock.acquire()
if self._state == state.IDLE:
self._state = state.PENDING_STOP
......@@ -181,23 +145,20 @@ class Scheduler:
"""
Stop the scheduler if it is running
"""
# A vérifier si c'est juste ou "stateLock.lock();"
self._stateLock.acquire()
if self._state == state.RUNNING:
self._state = state.PENDING_STOP
self._stateLock.release()
#TODO : add() n'existe pas en python.
def treat_pending_schedulables(self):
#TODO : C'est quoi ce lien ??? il suffit pas de recopier java mot a mot ^^"
"""
Effectively Add or Remove the schedulables that were added or removed during
a cycle to avoid {@link ConcurrentModificationException
a cycle
"""
while self._pendingAdditionSchedulables != []:
while self._pendingAdditionSchedulables:
self._schedulables.append(self._pendingAdditionSchedulables[0])
del self._pendingAdditionSchedulables[0]
while self._pendingRemovalSchedulables != []:
while self._pendingRemovalSchedulables:
self._schedulables.append(self._pendingRemovalSchedulables[0])
del self._pendingRemovalSchedulables[0]
......@@ -209,7 +170,7 @@ class Scheduler:
for schedulable in self._schedulables:
schedulable.on_scheduling_starts()
must_stop = False
while self._state == state.RUNNING and must_stop == False:
while self._state == state.RUNNING and not must_stop:
for schedulable in self._schedulables:
schedulable.cycle()
if self.get_sleep() != 0:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment