From c7318a4a95e38de61feca88670a0e70ee00fc431 Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Fri, 22 Mar 2019 12:19:38 +0100 Subject: [PATCH] Nouveau mode test (simulateur) --- README.md | 13 +++++-------- pyros.py | 13 ++++++++----- src/agent/Agent.py | 42 ++++++++++++++++++++++++++++++++---------- src/agent/AgentA.py | 16 ++++++++-------- src/agent/AgentB.py | 20 +++++++------------- src/agent/AgentX.py | 16 +++++++--------- 6 files changed, 67 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 6c65924..fc51821 100644 --- a/README.md +++ b/README.md @@ -73,28 +73,25 @@ Author: E. Pallier VERSION: 0.20.25 -Comment: - pyros.py peut lancer plusieurs agents (A et B) en même temps (+ flush_commands + test) +Comment: mode "test" (-t) géré (= mode simulateur) - Scenario de test : - - S'assurer que AgentA.py et AgentB.py sont en mode simulateur (SIMULATOR_MODE=True) - - lancer agents A et B : ./pyros.py start agentA,agentB + - lancer agents A et B en mode simu (option -t): ./pyros.py -t start agentA,agentB - attendre 1 à 2mn jusqu'à obtenir les 2 résultats suivants: (AgentA): Finished testing => result is ok (AgentB): Finished testing => result is ok - - - Mode opératoire pour lancer un agent : - - S'assurer que AgentA.py et AgentB.py ne sont PAS en mode simulateur (SIMULATOR_MODE=False) + - Mode opératoire pour lancer un agent (en mode normal, hors test) : - pour lancer agentA seulement : ./pyros.py start agentA [-c configfile] - pour lancer plusieurs agents : ./pyros.py start agentA,agentB,... [-c configfile] (ou encore: activer l'environnement virtuel, puis lancer "cd src/agent/ ; ./AgentA.py configfile") - pour utiliser thread ou processus : il suffit de mettre la constante RUN_IN_THREAD de AgentA (ou AgentB ou AgentX) à False ou True - Autres remarques: + - pyros.py peut lancer plusieurs agents (A et B) en même temps - send_command() implemented - EVAL is now a generic command - mode DEBUG (2 niveaux pour print) - - Nouvelle commande "flush_commands" pour purger les commmandes en attente + - flush_command : nouvelle commande pour purger les commmandes en attente - routine_process() implemented - Eval command implemented - Timeout géré : si commande pas exécutée en temps raisonnable => la même commande est ré-exéuctée à l'itération suivante diff --git a/pyros.py b/pyros.py index 16f9dd0..91168fb 100755 --- a/pyros.py +++ b/pyros.py @@ -379,11 +379,14 @@ def start(agent:str, configfile:str): ##agentX.run(FOR_REAL=True) os.chdir("src/agent/") #cmd = "-m AgentX" - cmd = f" Agent{agent_name[5:]}.py {configfile}" - - #if not test_mode(): execProcessFromVenv(cmd) - #if not test_mode(): current_processes.append( (execProcessFromVenvAsync(cmd), agent_name, 0) ) - if not test_mode(): current_processes.append( [execProcessFromVenvAsync(cmd), agent_name, -1] ) + #cmd = f" Agent{agent_name[5:]}.py {configfile}" + cmd = f"Agent{agent_name[5:]}.py" + if test_mode(): cmd += " -t" + if configfile: cmd += " {configfile}" + #if not test_mode(): current_processes.append( [execProcessFromVenvAsync(cmd), agent_name, -1] ) + # Append this process ( [process id, agent_name, result=failure] ) + # ("result" will be updated at the end of execution) + current_processes.append( [execProcessFromVenvAsync(cmd), agent_name, -1] ) # self._change_dir("..") os.chdir(current_dir) diff --git a/src/agent/Agent.py b/src/agent/Agent.py index 827d73d..3924ab9 100755 --- a/src/agent/Agent.py +++ b/src/agent/Agent.py @@ -194,8 +194,8 @@ class Agent: # Maximum duration of this agent (only for SIMULATION mode) # If set to None, it will never exit except if asked (or CTRL-C) # If set to 20, it will exit after 20s - MAX_DURATION_SEC = None - #MAX_DURATION_SEC = 30 + SIMULATOR_MAX_DURATION_SEC = None + #SIMULATOR_MAX_DURATION_SEC = 30 # FOR TEST ONLY # Run this agent in simulator mode @@ -338,7 +338,6 @@ class Agent: def __init__(self, name:str="Agent", config_filename:str=None, RUN_IN_THREAD=True): self.name = name self.SIMULATOR_COMMANDS = iter(self.SIMULATOR_COMMANDS_LIST) - if not self.SIMULATOR_MODE: self.MAX_DURATION_SEC=None self.RUN_IN_THREAD = RUN_IN_THREAD self.set_status(self.STATUS_LAUNCH) self.set_idle() @@ -409,6 +408,12 @@ class Agent: FOR_REAL: set to False if you don't want Majordome to send commands to devices """ + if self.SIMULATOR_MODE: + self.print("[IN SIMULATOR MODE]") + else: + self.print("[IN NORMAL MODE]") + self.SIMULATOR_MAX_DURATION_SEC=None + start_time = time.time() self.FOR_REAL = FOR_REAL @@ -498,8 +503,8 @@ class Agent: self._iter_num += 1 # Exit if max duration is reached - if self.MAX_DURATION_SEC and (time.time()-start_time > self.MAX_DURATION_SEC): - self.print("Exit because of max duration set to ", self.MAX_DURATION_SEC, "s") + if self.SIMULATOR_MAX_DURATION_SEC and (time.time()-start_time > self.SIMULATOR_MAX_DURATION_SEC): + self.print("Exit because of max duration set to ", self.SIMULATOR_MAX_DURATION_SEC, "s") self.kill_running_specific_cmd_if_exists() if self.SIMULATOR_MODE and self.SIMULATOR_WITH_TEST: self.simulator_test_results() break @@ -1015,6 +1020,9 @@ class Agent: ================================================================= """ + def setSimulatorMode(self, mode): + self.SIMULATOR_MODE=mode + def simulator_get_next_command_to_send(self)->Command: cmd_name = next(self.SIMULATOR_COMMANDS, None) #return cmd_name @@ -1258,19 +1266,33 @@ class Agent: """ + """ ================================================================= - MAIN FUNCTION + MAIN ================================================================= """ -if __name__ == "__main__": +def extract_parameters(): + """ Usage: Agent.py [-t] [configfile] """ + # arg 1 : -t + # arg 2 : configfile + TEST_MODE = False configfile = None + if len(sys.argv) > 1: + if sys.argv[1] == "-t": + TEST_MODE = True + if len(sys.argv) == 3: + configfile = sys.argv[2] + else: + configfile = sys.argv[1] + return TEST_MODE, configfile - # arg 1 : config file - if len(sys.argv) == 2: - configfile = sys.argv[1] +if __name__ == "__main__": + + TEST_MODE, configfile = extract_parameters() agent = Agent("GenericAgent", configfile, RUN_IN_THREAD=True) + agent.setSimulatorMode(TEST_MODE) print(agent) agent.run() diff --git a/src/agent/AgentA.py b/src/agent/AgentA.py index 7b5370b..4e78d71 100755 --- a/src/agent/AgentA.py +++ b/src/agent/AgentA.py @@ -5,7 +5,7 @@ import sys ##import utils.Logger as L ##from .Agent import Agent -from Agent import Agent +from Agent import Agent, extract_parameters ##log = L.setupLogger("AgentXTaskLogger", "AgentX") @@ -14,14 +14,13 @@ from Agent import Agent class AgentA(Agent): - #MAX_DURATION_SEC = None - MAX_DURATION_SEC = 120 - # FOR TEST ONLY # Run this agent in simulator mode - SIMULATOR_MODE = True + SIMULATOR_MODE = False # Run the assertion tests at the end SIMULATOR_WITH_TEST = True + #SIMULATOR_MAX_DURATION_SEC = None + SIMULATOR_MAX_DURATION_SEC = 120 # Who should I send commands to ? #SIMULATOR_COMMANDS_DEST = "myself" SIMULATOR_COMMANDS_DEST = "AgentB" @@ -220,19 +219,20 @@ class AgentA(Agent): ================================================================= """ if __name__ == "__main__": - # with thread RUN_IN_THREAD=True # with process #RUN_IN_THREAD=False + TEST_MODE, configfile = extract_parameters() + """ configfile = None - # arg 1 : config file if len(sys.argv) == 2: configfile = sys.argv[1] - + """ #agent = AgentX() agent = AgentA("AgentA", configfile, RUN_IN_THREAD) + agent.setSimulatorMode(TEST_MODE) print(agent) agent.run() diff --git a/src/agent/AgentB.py b/src/agent/AgentB.py index ba23580..28bbaca 100755 --- a/src/agent/AgentB.py +++ b/src/agent/AgentB.py @@ -5,7 +5,7 @@ import sys ##import utils.Logger as L ##from .Agent import Agent -from Agent import Agent +from Agent import Agent, extract_parameters ##log = L.setupLogger("AgentXTaskLogger", "AgentX") @@ -14,14 +14,13 @@ from Agent import Agent class AgentB(Agent): - #MAX_DURATION_SEC = None - MAX_DURATION_SEC = 120 - # FOR TEST ONLY # Run this agent in simulator mode SIMULATOR_MODE = False # Run the assertion tests at the end SIMULATOR_WITH_TEST = True + #SIMULATOR_MAX_DURATION_SEC = None + SIMULATOR_MAX_DURATION_SEC = 120 # Who should I send commands to ? #SIMULATOR_COMMANDS_DEST = "myself" SIMULATOR_COMMANDS_DEST = "AgentA" @@ -188,17 +187,12 @@ class AgentB(Agent): if __name__ == "__main__": # with thread - #RUN_IN_THREAD=True + RUN_IN_THREAD=True # with process - RUN_IN_THREAD=False - - configfile = None - - # arg 1 : config file - if len(sys.argv) == 2: - configfile = sys.argv[1] + #RUN_IN_THREAD=False - #agent = AgentX() + TEST_MODE, configfile = extract_parameters() agent = AgentB("AgentB", configfile, RUN_IN_THREAD) + agent.setSimulatorMode(TEST_MODE) print(agent) agent.run() diff --git a/src/agent/AgentX.py b/src/agent/AgentX.py index b0b7147..4d56bd0 100755 --- a/src/agent/AgentX.py +++ b/src/agent/AgentX.py @@ -9,7 +9,7 @@ import sys #from common.models import Command ##from .Agent import Agent -from Agent import Agent +from Agent import Agent, extract_parameters @@ -22,9 +22,11 @@ class AgentX(Agent): # FOR TEST ONLY # Run this agent in simulator mode - SIMULATOR_MODE = True + SIMULATOR_MODE = False # Run the assertion tests at the end SIMULATOR_WITH_TEST = True + #SIMULATOR_MAX_DURATION_SEC = None + SIMULATOR_MAX_DURATION_SEC = 120 ''' # Who should I send commands to ? #SIMULATOR_COMMANDS_DEST = "myself" @@ -164,15 +166,11 @@ if __name__ == "__main__": # with thread RUN_IN_THREAD=True # with process - RUN_IN_THREAD=False - - configfile = None - - # arg 1 : config file - if len(sys.argv) == 2: - configfile = sys.argv[1] + #RUN_IN_THREAD=False + TEST_MODE, configfile = extract_parameters() #agent = AgentX() agent = AgentX("AgentX", configfile, RUN_IN_THREAD) + agent.setSimulatorMode(TEST_MODE) print(agent) agent.run() -- libgit2 0.21.2