Commit c84ca326ec3d20f0cdb4b93ba74a0bff650d25e3

Authored by Etienne Pallier
1 parent ff0550d2
Exists in dev

Multi-agents (3 agents) : AgentA, AgentB, et AgentX s'envoient des

commandes (en //)

- AgentA, AgentB, et AgentX ont chacun leur propre scenario
- GROSSE OPTIMISATION : plus besoin du script intermédiaire
"start_agent.py" !!!
- pyros.py lance directement "cd src/agent/ ; python AgentX.py"
- Mode opératoire pour lancer un agent:
- pour démarrer agentX : ./pyros.py start agentX [-c configfile]
		(ou encore: activer l'environnement virtuel, puis lancer "./AgentX.py
configfile")
- pour démarrer agentA : ouvrir un autre terminal et taper "./pyros.py
start agentA"
- pour utiliser thread ou processus : il suffit de mettre la constante
RUN_IN_THREAD de AgentX (ou AgentA) à False ou True
README.md
... ... @@ -74,9 +74,9 @@ Author: E. Pallier
74 74 VERSION: 0.20.19
75 75  
76 76 Comment:
77   - Multi-agents : AgentA et AgentX s'envoient mutuellement des commandes (en //)
  77 + Multi-agents (3 agents) : AgentA, AgentB, et AgentX s'envoient des commandes (en //)
78 78  
79   - - AgentA et AgentX ont chacun leur propre scenario
  79 + - AgentA, AgentB, et AgentX ont chacun leur propre scenario
80 80 - GROSSE OPTIMISATION : plus besoin du script intermédiaire "start_agent.py" !!!
81 81 - pyros.py lance directement "cd src/agent/ ; python AgentX.py"
82 82 - Mode opératoire pour lancer un agent:
... ...
pyros.py
... ... @@ -27,6 +27,7 @@ AGENTS = {
27 27 "agent" : "Agent",
28 28 "agentX" : "AgentX",
29 29 "agentA" : "AgentA",
  30 + "agentB" : "AgentB",
30 31 "webserver" : "webserver",
31 32 "monitoring" : "monitoring",
32 33 "majordome" : "majordome",
... ...
src/agent/AgentA.py
... ... @@ -33,32 +33,6 @@ class AgentA(Agent):
33 33 "exit",
34 34 ]
35 35  
36   - """
37   - How to run this agent thread_exec_specific_cmd() method ?
38   - - True = inside a Thread (cannot be killed, must be asked to stop, and inadequate for computation)
39   - - False = inside a Process
40   - If thread, displays :
41   - >>>>> Thread: starting execution of command specific1
42   - >>>>> Thread: PID: 2695, Process Name: MainProcess, Thread Name: Thread-1
43   - ...
44   - >>>>> Thread: starting execution of command specific2
45   - >>>>> Thread: PID: 2695, Process Name: MainProcess, Thread Name: Thread-2
46   - ...
47   - >>>>> Thread: starting execution of command specific3
48   - >>>>> Thread: PID: 2695, Process Name: MainProcess, Thread Name: Thread-3
49   - If process, displays :
50   - >>>>> Thread: starting execution of command specific1
51   - >>>>> Thread: PID: 2687, Process Name: Process-1, Thread Name: MainThread
52   - ...
53   - >>>>> Thread: starting execution of command specific2
54   - >>>>> Thread: PID: 2689, Process Name: Process-2, Thread Name: MainThread
55   - ...
56   - >>>>> Thread: starting execution of command specific3
57   - >>>>> Thread: PID: 2690, Process Name: Process-3, Thread Name: MainThread
58   - """
59   - RUN_IN_THREAD = True
60   - #RUN_IN_THREAD = False
61   -
62 36  
63 37 """
64 38 =================================================================
... ...
src/agent/AgentB.py 0 → 100644
... ... @@ -0,0 +1,173 @@
  1 +#!/usr/bin/env python3
  2 +
  3 +
  4 +import sys
  5 +##import utils.Logger as L
  6 +
  7 +##from .Agent import Agent
  8 +from Agent import Agent
  9 +
  10 +
  11 +##log = L.setupLogger("AgentXTaskLogger", "AgentX")
  12 +
  13 +
  14 +
  15 +class AgentB(Agent):
  16 +
  17 + # FOR TEST ONLY
  18 + # Run this agent in simulator mode
  19 + SIMULATOR_MODE = True
  20 + # Run the assertion tests at the end
  21 + SIMULATOR_WITH_TEST = False
  22 + # Who should I send commands to ?
  23 + #SIMULATOR_COMMANDS_DEST = "myself"
  24 + SIMULATOR_COMMANDS_DEST = "AgentX"
  25 + # Scenario to be executed
  26 + SIMULATOR_COMMANDS = [
  27 + "go_active",
  28 + "go_idle",
  29 + "go_active",
  30 + "abort",
  31 + "abort",
  32 + "go_active",
  33 + "go_idle",
  34 + "exit",
  35 + ]
  36 +
  37 +
  38 + """
  39 + =================================================================
  40 + FUNCTIONS RUN INSIDE MAIN THREAD
  41 + =================================================================
  42 + """
  43 +
  44 + # @override
  45 + def __init__(self, name:str=None, config_filename=None, RUN_IN_THREAD=True):
  46 + if name is None: name = self.__class__.__name__
  47 + super().__init__(name, config_filename, RUN_IN_THREAD)
  48 +
  49 + # @override
  50 + def init(self):
  51 + super().init()
  52 + # --- Set the mode according the startmode value
  53 + ##agent_alias = self.__class__.__name__
  54 + ##self.set_mode_from_config(agent_alias)
  55 +
  56 + '''
  57 + # @override
  58 + def load_config(self):
  59 + super().load_config()
  60 + '''
  61 +
  62 + '''
  63 + # @override
  64 + def update_survey(self):
  65 + super().update_survey()
  66 + '''
  67 +
  68 + '''
  69 + # @override
  70 + def get_next_command(self):
  71 + return super().get_next_command()
  72 + '''
  73 +
  74 + # @override
  75 + def do_log(self):
  76 + super().do_log()
  77 +
  78 +
  79 +
  80 + """
  81 + =================================================================
  82 + FUNCTIONS RUN INSIDE A SUB-THREAD (OR A PROCESS) (thread_*())
  83 + =================================================================
  84 + """
  85 +
  86 + # Define your own command step(s) here
  87 + def cmd_step1(self, step:int):
  88 + cmd = self._current_specific_cmd
  89 + cmd.result = f"in step #{step}/{self._thread_total_steps_number}"
  90 + cmd.save()
  91 + """
  92 + if self.RUN_IN_THREAD:
  93 + print("(save from thread)")
  94 + cmd.save()
  95 + else:
  96 + #@transaction.atomic
  97 + print("(save from process)")
  98 + with transaction.atomic():
  99 + cmd.save()
  100 + #Command.objects.select_for_update()
  101 + """
  102 +
  103 + def cmd_step2(self, step:int):
  104 + self.cmd_step1(step)
  105 + def cmd_step3(self, step:int):
  106 + self.cmd_step1(step)
  107 + def cmd_step4(self, step:int):
  108 + self.cmd_step1(step)
  109 +
  110 + """
  111 + # @override
  112 + def thread_exec_specific_cmd_step(self, step:int, sleep_time:float=1.0):
  113 + self.thread_stop_if_asked()
  114 + cmd = self._current_specific_cmd
  115 + print(f">>>>> Thread (cmd {cmd.name}): step #{step}/5")
  116 + self.sleep(sleep_time)
  117 + """
  118 +
  119 + '''
  120 + # @override
  121 + def exec_specific_cmd_start(self, cmd:Command, from_thread=True):
  122 + super().exec_specific_cmd_start(cmd, from_thread)
  123 + '''
  124 +
  125 +
  126 + # @override
  127 + def thread_exec_specific_cmd_main(self):
  128 + # This is optional
  129 + self.thread_set_total_steps_number(5)
  130 +
  131 + # HERE, write your own scenario
  132 +
  133 + # scenario OK
  134 + self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1)
  135 + self.thread_exec_specific_cmd_step(2, self.cmd_step2, 3)
  136 + self.thread_exec_specific_cmd_step(3, self.cmd_step1, 5)
  137 + self.thread_exec_specific_cmd_step(4, self.cmd_step3, 10)
  138 + self.thread_exec_specific_cmd_step(5, self.cmd_step1, 4)
  139 + # ... as many as you need
  140 +
  141 + """ autre scenario
  142 + self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1)
  143 + self.thread_exec_specific_cmd_step(2, self.cmd_step2, 2)
  144 + self.thread_exec_specific_cmd_step(3, self.cmd_step1, 2)
  145 + self.thread_exec_specific_cmd_step(4, self.cmd_step3, 2)
  146 + self.thread_exec_specific_cmd_step(5, self.cmd_step1, 3)
  147 + """
  148 +
  149 + '''
  150 + # @override
  151 + def exec_specific_cmd_end(self, cmd:Command, from_thread=True):
  152 + super().exec_specific_cmd_end(cmd, from_thread)
  153 + '''
  154 +
  155 +
  156 +
  157 +if __name__ == "__main__":
  158 +
  159 + # with thread
  160 + #RUN_IN_THREAD=True
  161 + # with process
  162 + RUN_IN_THREAD=False
  163 +
  164 + configfile = None
  165 +
  166 + # arg 1 : config file
  167 + if len(sys.argv) == 2:
  168 + configfile = sys.argv[1]
  169 +
  170 + #agent = AgentX()
  171 + agent = AgentB("AgentB", configfile, RUN_IN_THREAD)
  172 + print(agent)
  173 + agent.run()
... ...