Source code for examples.banksim.model.model_accountant
"""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 Banksim Accountant Model """frompyjevsim.behavior_modelimportBehaviorModelfrompyjevsim.definitionimport*frompyjevsim.system_messageimportSysMessage
[docs]classBankAccountant(BehaviorModel):"""A Model representing a bank accountant processing bank users."""def__init__(self,name,proc_num):"""Initializes the BankAccountant with states and ports. Args: name (str): Name of the accountant proc_num (int): Processor number for identification """BehaviorModel.__init__(self,name)self.init_state("WAIT")# Initialize initial stateself.insert_state("WAIT",Infinite)# Add "WAIT" stateself.insert_state("PROC",1)# Add "PROC" state with duration 1self.insert_input_port("in")# Add input port "in"self.insert_output_port("next")# Add output port "next"self.proc_num=f"proc{proc_num}"# Processor numberself.user=None# Current user being processed#self.proc_user = [] # List of processed users
[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 """_time=self.global_timeifport=="in":self.user=msg.retrieve()[0]self._cur_state="PROC"# Transition state to "PROC"self.update_state("PROC",self.user.get_service_time())# Update "PROC" state duration
[docs]defoutput(self,msg_deliver):""" Generates the output message when in the "PROC" state. Returns: SysMessage: The output message """_time=self.global_timemsg=Noneifself._cur_state=="PROC":cur_time=self.global_timeself.user.calc_wait_time(cur_time)# Calculate wait time#self.proc_user.append(self.user) # Add user to processed list#print(f"[A][processed] ID:{self.user.get_id()} Time:{_time}")msg=SysMessage(self.get_name(),"next")msg.insert(self.proc_num)# Insert processor numbermsg.insert(self.user)msg_deliver.insert_message(msg)returnmsg_deliver
[docs]defint_trans(self):"""Handles internal transitions based on the current state."""ifself._cur_state=="PROC":self._cur_state="WAIT"# Transition state to "WAIT"
def__del__(self):#"""Destructor to print the log of processed users."""#print(f"[{self.get_name()}-{self.proc_num} log]")#print("user-name, process_time, arrival_time, done_time, wait_time")#for user in self.proc_user:# print(user)passdef__str__(self):""" Returns a string representation of the BankAccountant. Returns: str: String representation """returnf">> {self.get_name()}, State:{self._cur_state}, {self.user}"