@@ -30,33 +30,25 @@ class MS4MeKafkaWorker(InMemoryKafkaWorker):
3030
3131 OUT_TOPIC = "ms4meOut"
3232
33- def __init__ (self , aDEVS , index , bootstrap_servers ):
34- super ().__init__ (aDEVS , index , bootstrap_servers , in_topic = f"ms4me{ aDEVS .getBlockModel ().label } In" , out_topic = MS4MeKafkaWorker .OUT_TOPIC )
33+ def __init__ (self , model_name , aDEVS , bootstrap_server ):
34+ """ Constructor
35+ """
36+ super ().__init__ (model_name , aDEVS , bootstrap_server , in_topic = f"ms4me{ model_name } In" , out_topic = MS4MeKafkaWorker .OUT_TOPIC )
3537
3638 self .wire = StandardWireAdapter
3739
3840 def get_topic_to_write (self ) -> str :
41+ """ Return the topic used to contact the model
3942 """
40- Retourne le nom de topic à utiliser pour contacter ce modèle (obj)
41- """
42-
4343 return self .in_topic
44-
45- @staticmethod
46- def get_topic_to_read () -> str :
47- """
48- Retourne le nom de topic à utiliser pour lire les résultats de tous les modèles
44+
45+ def get_topic_to_read (self ) -> str :
46+ """ Return the name of the topic to used to read the results od the model
4947 """
50- return MS4MeKafkaWorker . OUT_TOPIC
48+ return self . out_topic
5149
5250 def output_msg_mapping (self ) -> ModelOutputMessage :
53- """
54-
55- Args:
56- aDEVS (AtomicDEVS): DEVS atomic model from DomainBehaviorInterface
57-
58- Returns:
59- ModelOutputMessage: ModelOutputMessage containing the output port values from Ms4me
51+ """ Returns aModelOutputMessage message that contain the output port values from Ms4me
6052 """
6153
6254 result_portvalue_list = []
@@ -85,16 +77,15 @@ def output_msg_mapping(self) -> ModelOutputMessage:
8577 return result_portvalue_list
8678
8779 def _handle_devs_message (self , msg : BaseMessage ) -> BaseMessage :
88- """
89- Reçoit un BaseMessage (InitSim, ExecuteTransition, SendOutput, ...)
90- et renvoie un BaseMessage de réponse (NextTime, ModelOutputMessage, ...).
80+ """ Receive a BaseMessage (InitSim, ExecuteTransition, SendOutput, ...)
81+ and send a BasMessage in repsonce (NextTime, ModelOutputMessage, ...).
9182 """
9283 t = msg .time .t
9384
9485 # --- InitSim : initialisation + timeAdvance initial ---
9586 if isinstance (msg , InitSim ):
9687 self .do_initialize (t )
97- return NextTime (SimTime (t = float (self .aDEVS .timeNext )), sender = self .aBlock . label )
88+ return NextTime (SimTime (t = float (self .aDEVS .timeNext )), sender = self .model_name )
9889
9990 # --- ExecuteTransition
10091 if isinstance (msg , ExecuteTransition ):
@@ -146,7 +137,7 @@ def temp_peek(port, *args):
146137
147138 ta = float (ta ) if ta is not None else float ("inf" )
148139
149- return TransitionDone (time = SimTime (t = t ), nextTime = SimTime (t = ta ), sender = self .aBlock . label )
140+ return TransitionDone (time = SimTime (t = t ), nextTime = SimTime (t = ta ), sender = self .model_name )
150141
151142 # --- SendOutput : outputFnc + calcul des sorties ---
152143 if isinstance (msg , SendOutput ):
@@ -159,42 +150,39 @@ def temp_peek(port, *args):
159150 return ModelOutputMessage (
160151 modelOutput = port_values ,
161152 nextTime = next_time ,
162- sender = self .aBlock . label ,
153+ sender = self .model_name ,
163154 )
164155
165156 if isinstance (msg , SimulationDone ):
166157 self .running = False
167158 return ModelDone (
168159 time = msg .time ,
169- sender = self .aDEVS . getBlockModel (). label ,
160+ sender = self .model_name ,
170161 )
171-
172- # --- NextTime, ModelOutputMessage, etc. ---
173- # Ces types sont normalement utilisés comme réponses, pas comme requêtes
174- # côté worker, donc on ne les traite pas ici.
162+
175163 raise ValueError (f"Unsupported message type in worker: { type (msg ).__name__ } " )
176164
177165 def _process_standard (self , data ):
178166 """Process standard DEVS message format."""
179167
180168 devs_msg = self .wire .from_wire (data )
181169
182- # Traiter le message DEVS
170+ # handel the DEVS msessage
183171 try :
184172 reply_msg = self ._handle_devs_message (devs_msg )
185173 except Exception as e :
186- logger .exception (" [Thread-%s] Error handling message: %s" , self .index , e )
174+ logger .exception (" [Thread-%s] Error handling message: %s" , self .aDEVS . myID , e )
187175
188- # Préparer le message de réponse
176+ # define a msg to send
189177 reply_wire = self .wire .to_wire (reply_msg )
190178 reply_json = json .dumps (reply_wire ).encode ("utf-8" )
191179
192- # Log et envoi de la réponse
180+ # Log
193181 worker_kafka_logger .debug (
194- f"[Thread-{ self .index } ] OUT: topic={ self .out_topic } value={ reply_json .decode ('utf-8' )} "
182+ f"[Thread-{ self .aDEVS . myID } ] OUT: topic={ self .out_topic } value={ reply_json .decode ('utf-8' )} "
195183 )
196184
197- # Envoi de la réponse
185+ # send the msg to the out_topic
198186 self .producer .produce (
199187 self .out_topic ,
200188 value = reply_json ,
0 commit comments