From 40dac72cf3a8c6eff6126cffca31f664c45304e4 Mon Sep 17 00:00:00 2001
From: shinedday <shinedday@gmail.com>
Date: Wed, 19 May 2021 08:19:44 +0200
Subject: [PATCH] Add load and save

---
 note/cycle.txt                                |  20 -------
 note/how to use                               |  13 -----
 pyAmakCore/classes/agent.py                   |   5 +-
 pyAmakCore/classes/amas.py                    |   1 -
 pyAmakCore/classes/main.py                    |  54 ------------------
 pyAmakCore/classes/scheduler.py               |  17 ++++++
 .../classes/scheduler_tool/agent_thread.py    |  11 ++--
 .../scheduler_tool/schedulable_thread.py      |  13 +++--
 pyAmakCore/tests/test_pickle/filename.pickle  | Bin 80 -> 0 bytes
 pyAmakCore/tests/test_pickle/main.py          |  50 ----------------
 10 files changed, 36 insertions(+), 148 deletions(-)
 delete mode 100644 note/cycle.txt
 delete mode 100644 note/how to use
 delete mode 100644 pyAmakCore/classes/main.py
 delete mode 100644 pyAmakCore/tests/test_pickle/filename.pickle
 delete mode 100644 pyAmakCore/tests/test_pickle/main.py

diff --git a/note/cycle.txt b/note/cycle.txt
deleted file mode 100644
index 47ad4b1..0000000
--- a/note/cycle.txt
+++ /dev/null
@@ -1,20 +0,0 @@
-
-
-init :
-    base
-    on_init
-
-    syncro
-
-
-
-cycle :
-
-    on cycle begin
-    syncro
-
-    agent action
-    syncro
-
-    on cycle end
-    syncro
\ No newline at end of file
diff --git a/note/how to use b/note/how to use
deleted file mode 100644
index 94ec796..0000000
--- a/note/how to use	
+++ /dev/null
@@ -1,13 +0,0 @@
-1 - create env
-2 - create amas
-3 - amas.start()
-
-to end :
-amas.exit_program()
-
-
-to start :
-amas.put_token()
-
-to stop :
-amas.take_token()
\ No newline at end of file
diff --git a/pyAmakCore/classes/agent.py b/pyAmakCore/classes/agent.py
index 079348e..a94ae75 100644
--- a/pyAmakCore/classes/agent.py
+++ b/pyAmakCore/classes/agent.py
@@ -169,7 +169,10 @@ class Agent:
         """
         ToOverrideWarning("on_act")
 
-    def next_phase(self):
+    def next_phase(self) -> None:
+        """
+        set agent phase to the next phase
+        """
         next_phase = {
             Phase.INITIALIZING: Phase.PERCEPTION,
             Phase.PERCEPTION: Phase.PERCEPTION_DONE,
diff --git a/pyAmakCore/classes/amas.py b/pyAmakCore/classes/amas.py
index a3e4edc..7a46093 100644
--- a/pyAmakCore/classes/amas.py
+++ b/pyAmakCore/classes/amas.py
@@ -37,7 +37,6 @@ class Amas(Schedulable, Loggable):
         self.__execution_policy: ExecutionPolicy = execution_policy
 
         self.on_initialization()
-
         self.on_initial_agents_creation()
 
     def add_pending_agent(self) -> List[Agent]:
diff --git a/pyAmakCore/classes/main.py b/pyAmakCore/classes/main.py
deleted file mode 100644
index 14a2aed..0000000
--- a/pyAmakCore/classes/main.py
+++ /dev/null
@@ -1,54 +0,0 @@
-from pyAmakCore.classes.communicating_agent import CommunicatingAgent
-
-from pyAmakCore.exception.override import ToOverrideWarning
-from pyAmakCore.classes.agent import Agent
-from pyAmakCore.classes.amas import Amas
-from pyAmakCore.classes.environment import Environment
-from pyAmakCore.classes.scheduler import Scheduler
-
-
-class SimpleAgent(Agent):
-    """
-    test
-    """
-
-
-class SimpleAmas(Amas):
-    """
-    test
-    """
-
-    def on_initial_agents_creation(self) -> None:
-        for i in range(10):
-            self.add_agent(SimpleAgent(self))
-
-
-class SimpleEnv(Environment):
-    """
-    test
-    """
-
-class SimpleAgent2(CommunicatingAgent):
-    """
-    test
-    """
-
-env = SimpleEnv()
-amas = SimpleAmas(env)
-
-agent = SimpleAgent2(amas)
-print(isinstance(agent, CommunicatingAgent))
-
-
-import time
-start_time = time.time()
-ToOverrideWarning.enable_warning(False)
-
-env = SimpleEnv()
-amas = SimpleAmas(env)
-
-scheduler = Scheduler(amas)
-
-scheduler.start()
-scheduler.run()
-print("--- %s seconds ---" % (time.time() - start_time))
diff --git a/pyAmakCore/classes/scheduler.py b/pyAmakCore/classes/scheduler.py
index 887712a..4143e5b 100644
--- a/pyAmakCore/classes/scheduler.py
+++ b/pyAmakCore/classes/scheduler.py
@@ -1,6 +1,7 @@
 """
 Scheduler class
 """
+import pickle
 from threading import Semaphore, Thread
 from typing import List
 
@@ -42,6 +43,10 @@ class Scheduler:
 
         AgentThread.execution_policy = self.amas.get_execution_policy()
 
+        self.amas.add_pending_agent()
+        for agent in amas.get_agents():
+            self.add_agent(agent)
+
     def get_amas(self):
         return self.amas
 
@@ -178,3 +183,15 @@ class Scheduler:
 
     def set_sleep(self, sleep_time: int):
         self.sleep_time = sleep_time
+
+    def save(self):
+        with open('filename.pickle', 'wb') as handle:
+            pickle.dump(self.amas, handle, protocol=pickle.HIGHEST_PROTOCOL)
+
+    @classmethod
+    def load(cls):
+        with open('filename.pickle', 'rb') as handle:
+            amas_object = pickle.load(handle)
+
+        return cls(amas_object)
+
diff --git a/pyAmakCore/classes/scheduler_tool/agent_thread.py b/pyAmakCore/classes/scheduler_tool/agent_thread.py
index 946fc80..ed1e105 100644
--- a/pyAmakCore/classes/scheduler_tool/agent_thread.py
+++ b/pyAmakCore/classes/scheduler_tool/agent_thread.py
@@ -21,9 +21,9 @@ class AgentThread:
 
     def __init__(self, agent: Agent):
 
-        self.agent = agent
-        self.is_waiting = Semaphore(0)
-        self.exit_bool = False
+        self.agent: Agent = agent
+        self.is_waiting: Semaphore = Semaphore(0)
+        self.exit_bool: bool = False
 
     def phase1(self) -> None:
         """
@@ -45,7 +45,10 @@ class AgentThread:
         self.agent.set_criticality(self.agent.compute_criticality())
         self.agent.next_phase()
 
-    def run(self):
+    def run(self) -> None:
+        """
+        main part of an agent thread
+        """
         while not self.exit_bool:
 
             self.is_waiting.acquire()
diff --git a/pyAmakCore/classes/scheduler_tool/schedulable_thread.py b/pyAmakCore/classes/scheduler_tool/schedulable_thread.py
index e24d592..69223bc 100644
--- a/pyAmakCore/classes/scheduler_tool/schedulable_thread.py
+++ b/pyAmakCore/classes/scheduler_tool/schedulable_thread.py
@@ -15,15 +15,18 @@ class SchedulableThread:
     """
     thread class used to thread schedulable
     """
-    action_done = Semaphore(0)
-    exit_bool = False
+    action_done: Semaphore = Semaphore(0)
+    exit_bool: bool = False
 
     def __init__(self, schedulable: Schedulable):
 
-        self.schedulable = schedulable
-        self.is_waiting = Semaphore(0)
+        self.schedulable: Schedulable = schedulable
+        self.is_waiting: Semaphore = Semaphore(0)
 
-    def run(self):
+    def run(self) -> None:
+        """
+        main part of a schedulable thread
+        """
         while not SchedulableThread.exit_bool:
 
             self.is_waiting.acquire()
diff --git a/pyAmakCore/tests/test_pickle/filename.pickle b/pyAmakCore/tests/test_pickle/filename.pickle
deleted file mode 100644
index 631f773d73e9a7d2deac4d9e6351b64571dc2219..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 80
zcmZo*^=8m(V2z!k*T6c}i~$0AlnN>xa}%?j^NUjTQu9i4Q;QNyGV}BF64O)jO5zJL
a5{px(^soki>B&>PnOmm>fei3w&;tO}5*k?m

diff --git a/pyAmakCore/tests/test_pickle/main.py b/pyAmakCore/tests/test_pickle/main.py
deleted file mode 100644
index fef630b..0000000
--- a/pyAmakCore/tests/test_pickle/main.py
+++ /dev/null
@@ -1,50 +0,0 @@
-import pickle
-from time import sleep
-
-from pyAmakCore.classes.amas import Amas
-from pyAmakCore.classes.environment import Environment
-from pyAmakCore.classes.agent import Agent
-
-
-class SimpleAgent(Agent):
-
-    def __init__(self, amas):
-        self.i = 0
-        super().__init__(amas)
-
-    def on_cycle_begin(self) -> None:
-        self.i += 1
-
-class SimpleAmas(Amas):
-    def on_cycle_begin(self) -> None:
-        if self.get_cycle() == 0:
-            self.save()
-
-    def save(self):
-        with open('filename.pickle', 'wb') as handle:
-            pickle.dump(self.get_agents()[0].get_id(), handle, protocol=pickle.HIGHEST_PROTOCOL)
-        sleep(500)
-
-    @classmethod
-    def load(cls, amas_object):
-        with open('filename.pickle', 'wb') as handle:
-            amas_object = pickle.load(handle)
-
-class SimpleEnv(Environment):
-    pass
-
-"""
-
-amas.put_token()
-amas.start()
-"""
-
-env = SimpleEnv()
-amas = SimpleAmas(env)
-agent = SimpleAgent(amas)
-with open('filename.pickle', 'wb') as handle:
-    pickle.dump(agent.get_id(), handle, protocol=pickle.HIGHEST_PROTOCOL)
-    pickle.dump(agent.get_neighbour(), handle, protocol=pickle.HIGHEST_PROTOCOL)
-    pickle.dump(agent.get_phase(), handle, protocol=pickle.HIGHEST_PROTOCOL)
-    pickle.dump(agent.get_criticality(), handle, protocol=pickle.HIGHEST_PROTOCOL)
-    pickle.dump(agent.get_environment(), handle, protocol=pickle.HIGHEST_PROTOCOL)
-- 
GitLab