"""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 an object SnapshotExecutor that decorates a BehaviorExecutor to snapshot a BehaviorModel. """importdillimportosfrom.executorimportExecutor
[docs]classSnapshotExecutor(Executor):""" Set the model you want to snapshot to a SnapshotExecutor in a form that can be executed by a SystemExecutor. """def__init__(self,behavior_executor,condition,parent):""" Args: behavior_executor (BehaviorExecutor): BehaviorExecutor to decorate """Executor.__init__(self,behavior_executor.get_create_time(),behavior_executor.get_destruct_time(),behavior_executor.get_engine_name(),behavior_executor.get_core_model(),parent)self.behavior_executor=behavior_executorself.condition=condition
[docs]defget_core_model(self):"""Returns BehaviorModel of SnapshotExecutor. Returns: CoreModel: BehaviorModel """returnself.behavior_executor.get_core_model()
def__str__(self):"""Returns the string representation of SnapshotExecutor. Returns: str: String representation """return"SNM"+self.behavior_executor.__str__()
[docs]defget_name(self):"""Returns the name of SnapshotExecutor. Returns: str: Name """returnself.behavior_executor.get_name()
[docs]defget_engine_name(self):"""Returns SysExecutor name of SnapshotExecutor. Returns: str: SysExecutor name """returnself.behavior_executor.get_engine_name()
[docs]defset_engine_name(self,engine_name):"""Sets SysExecutor name of SnapshotExecutor. Args: engine_name (str): SysExecutor name """self.behavior_executor.set_engine_name(engine_name)
[docs]defget_create_time(self):"""Returns the creation time of SnapshotExecutor. Returns: float: Creation time """returnself.behavior_executor.get_create_time()
[docs]defget_destruct_time(self):"""Returns the destruction time of SnapshotExecutor. Returns: float: Destruction time """returnself.behavior_executor.get_destruct_time()
[docs]defget_obj_id(self):"""Return object ID of SnapshotExecutor. Returns: int: Object ID """returnself.behavior_executor.get_obj_id()
# State management
[docs]defget_cur_state(self):"""Returns the current state of SnapshotExecutor. Returns: str: Current state """returnself.behavior_executor.get_cur_state()
[docs]definit_state(self,state):"""Initializes the state of SnapshotExecutor. Args: state (str): The state to set / 설정할 상태 """self.behavior_executor.init_state(state)
# External Transition
[docs]defext_trans(self,port,msg):"""Handles the external transition. Args: port (str): The port name msg (Message): The message """ifself.condition.snapshot_pre_condition_ext(port,msg,self.get_cur_state()):self.snapshot("ext_before")#Conditions created before ext_trans function self.behavior_executor.ext_trans(port,msg)ifself.condition.snapshot_post_condition_ext(port,msg,self.get_cur_state()):self.snapshot("ext_after")
#Conditions generated after ext_trans function # Internal Transition
[docs]defint_trans(self):"""Handles the internal transition."""ifself.condition.snapshot_pre_condition_int(self.get_cur_state()):self.snapshot("int_before")#Conditions created before int_trans function self.behavior_executor.int_trans()ifself.condition.snapshot_post_condition_int(self.get_cur_state()):self.snapshot("int_after")
#Conditions generated after int_trans function# Output Function
[docs]defoutput(self,msg_deliver):"""Handles the output function. Returns: Message: The output message """ifself.condition.snapshot_pre_condition_out(self.get_cur_state()):self.snapshot("output_before")#Conditions created before output function out_msg=self.behavior_executor.output(msg_deliver)ifself.condition.snapshot_post_condition_out(self.get_cur_state(),out_msg):self.snapshot("output_after")#Conditions generated after output functionreturnout_msg
# Time Advanced Function
[docs]deftime_advance(self):"""Returns the time advance value. Returns: float: Time advance value """returnself.behavior_executor.time_advance()
[docs]defset_req_time(self,global_time):"""Sets the request time. Args: global_time (float): Simulation time """ifself.condition.snapshot_time_condition(global_time):self.snapshot("time")#Snapshot conditions over timeself.behavior_executor.set_req_time(global_time)
[docs]defget_req_time(self):"""Returns the request time. Returns: float: Request time """returnself.behavior_executor.get_req_time()
[docs]defsnapshot(self,_prefix,_path="./snapshot"):"""An abstract method that creates a method to take a snapshot. You can use the snapshot method in a conditional method. Use the model_dump method to get the model data in bytes. Save that data to the DB or save it to a file. Args: name (str): The name of the snapshot """model_data=self.model_dump()#model snapshot data(binary type)## snapshot model to simx file (path : ./snapshot/model.simx) ifmodel_data:ifnotos.path.exists(_path):os.makedirs(_path)withopen(f"{_path}/[{_prefix}]{self.behavior_executor.get_name()}.simx","wb")asf:f.write(model_data)
[docs]defmodel_dump(self):"""Dumps BehaviorModel Returns: bytes: The dumped BehaviorModel """returndill.dumps(self.behavior_executor.get_core_model().model_snapshot())
[docs]defget_behavior_executor(self):"""Return BehaviorExecutor. Returns: BehaviorExecutor: The behavior executor / 동작 실행기 """returnself.behavior_executor