"""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 a BehaviorExecutor, an object for executing a BehaviorModel."""from.definitionimportInfinitefrom.executorimportExecutor
[docs]classBehaviorExecutor(Executor):""" A decorated form of the BehaviorModel, ready to be executed by the SysExecutor. Manages the simulation time of the BehaviorModel and the information in the SysExecutor. Args: itime (int or Infinite): Time of instance creation dtime (int or Infinite): Time of instance destruction ename (str): SysExecutor name behavior_model (ModelType.BEHAVIORAL): Behavior Model """def__init__(self,itime=Infinite,dtime=Infinite,ename="default",behavior_model=None,parent=None):super().__init__(itime,dtime,ename,behavior_model,parent)self._next_event_t=0# Next event timeself._cur_state=""# Current state of the behavior executorself.request_time=float("inf")# Request time initialized to infinityself.behavior_model=behavior_model#Behavior Modelself._cancel_reschedule_f=False#cancel reschedule flag
[docs]defset_global_time(self,gtime):"""Sets the global time for the executor and behavior model"""self.global_time=gtimeself.behavior_model.set_global_time(gtime)
[docs]defget_core_model(self):"""Returns the core behavior model"""returnself.behavior_model
def__str__(self):"""Returns a string representation of the executor"""returnf"[N]:{self.get_name()}, [S]:{self._cur_state}"
[docs]defget_name(self):"""Returns the name of the behavior model"""returnself.behavior_model.get_name()
[docs]defget_engine_name(self):"""Returns the name of the engine"""returnself.engine_name
[docs]defset_engine_name(self,engine_name):"""Sets the name of the engine"""self.engine_name=engine_name
[docs]defget_create_time(self):"""Returns the instance creation time"""returnself._instance_t
[docs]defget_destruct_time(self):"""Returns the destruction time"""returnself._destruct_t
[docs]defget_obj_id(self):"""Returns the object ID of the behavior model"""returnself.behavior_model.get_obj_id()
# State management
[docs]defget_cur_state(self):"""Returns the current state of the executor"""returnself._cur_state
[docs]definit_state(self,state):"""Initializes the state of the executor"""self._cur_state=state
# External Transition
[docs]defext_trans(self,port,msg):"""Handles external transition based on port and message"""ifself.behavior_model.get_cancel_flag():self._cancel_reschedule_f=Trueself.behavior_model.ext_trans(port,msg)
[docs]defoutput(self,msg_deliver):"""Executes the output function of the behavior model"""returnself.behavior_model.output(msg_deliver)
# Time Advance Function
[docs]deftime_advance(self):"""Returns the time advance value for the current state"""ifself.behavior_model._cur_stateinself.behavior_model._states:returnself.behavior_model._states[self.behavior_model._cur_state]return-1
[docs]defset_req_time(self,global_time):"""Sets the request time based on the global time and time advance"""self.set_global_time(global_time)ifself.time_advance()==Infinite:self._next_event_t=Infiniteself.request_time=Infiniteelse:ifself._cancel_reschedule_f:self.request_time=min(self._next_event_t,global_time+self.time_advance())else:self.request_time=global_time+self.time_advance()
[docs]defget_req_time(self):"""Returns the request time and resets the cancel flag if necessary"""ifself._cancel_reschedule_f:self._cancel_reschedule_f=Falseself.behavior_model.reset_cancel_flag()self._next_event_t=self.request_timereturnself.request_time