"""Author: Changbeom Choi (@cbchoi)Copyright (c) 2014-2020 Handong Global UniversityCopyright (c) 2021-2024 Hanbat National UniversityLicense: MIT. The full license text is available at:https://github.com/eventsim/pyjevsim/blob/main/LICENSEThis module contains Buffer Model """frompyjevsim.behavior_modelimportBehaviorModelfrompyjevsim.definitionimport*frompyjevsim.system_messageimportSysMessage
[docs]classBuffer(BehaviorModel):"""Buffer model to store simulation events."""def__init__(self,name):""" Args: name (str): The name of the buffer """BehaviorModel.__init__(self,name)self.init_state("Wait")# Initialize initial stateself.insert_state("Wait",Infinite)# Add "Wait" stateself.insert_state("Delay",0)# Add "Delay" stateself.insert_input_port("recv")# Add input port "recv"self.insert_output_port("output")# Add output port "output"self._msg=None# Initialize message storage variable
[docs]defext_trans(self,port,msg):""" Handles external transitions based on the input port. Args: port (str): The port that received the message msg (SysMessage): The received message """ifport=="recv":print(f"[Buf][IN]: recv")self._msg=msg# Store messageself._cur_state="Delay"# Transition state to "Delay"
[docs]defoutput(self,msg_deliver):""" Generates the output message when in the "Delay" state. Returns: SysMessage: The output message """print(f"[Buf][OUT]: {self._msg.retrieve()[0]}")msg=SysMessage(self.get_name(),"output")msg.insert(f"{self._msg.retrieve()[0]}")# Insert messagereturnmsg
[docs]defint_trans(self):""" Handles internal transitions based on the current state. """ifself._cur_state=="Delay":self._cur_state="Wait"# Transition state to "Wait"