"""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/LICENSE"""fromdillimportload,loadsfrom.definitionimportModelType,ExecutionTypeimportjsonimportastfrom.system_executorimportSysExecutor
[docs]classRestoreHandler():""" Restore a snapshotted model or project. """def__init__(self,t_resol=1,ex_mode=ExecutionType.V_TIME,name="project",path="./snapshot"):""" Initializes the RestoreHandler with time resolution, execution mode, name, and path. Args: t_resol (float): Time resolution ex_mode (R_TIME or V_TIME): Execution mode(Real time or Virtual time) name (str): Name of SysExecutor path (str, optional): Path to load snapshots """self.path=f"{path}/{name}"self.sim_name=nameself.engine=SysExecutor(t_resol,name,ex_mode,snapshot_manager=None)self.model_map={}pass
[docs]defrestore_engine(self):""" Sets up SysExecutor with the relation map and model map. """withopen(f"{self.path}/relation_map.json","r")asf:relation=json.load(f)relation={ast.literal_eval(key):ast.literal_eval(value)forkey,valueinrelation.items()}model_list={}withopen(f"{self.path}/model_map.json","r")asf:model_list=json.load(f)model_list=model_list["model_name"]self.load_models(model_list)self.relations(relation)
[docs]defload_models(self,model_list):""" Loads models from files and registers them with SysExecutor. Args: model_list (list): List of model names """formodel_nameinmodel_list:withopen(f"{self.path}/{model_name}.simx","rb")asf:model=load(f)self.model_map[model_name]=model["data"]self.engine.register_entity(model["data"])
[docs]defrelations(self,relation_map):""" Sets up coupling relations in SysExecutor. Args: relation_map (dict): The relation map / 관계 맵 """forkey,valueinrelation_map.items():ifkey[0]inself.model_map.keys():output_model=self.model_map[key[0]]else:output_model=self.engineformodelinvalue:ifmodel[0]:input_model=self.model_map[model[0]]else:input_model=self.engineself.engine.coupling_relation(output_model,key[1],input_model,model[1])
[docs]defget_engine(self):"""Returns the SysExecutor. Returns: Restored SysExecutor """self.restore_engine()returnself.engine
[docs]defload_snapshot(self,name,shotmodel):"""Loads BehaviorModel. Args: name (str): The name of Model shotmodel (bytes): Binary data of the model snapshot Returns: object(BehaivorModel): The loaded model Raises: Exception: If the model type is not ModelType.BEHAVIORAL """model_info=loads(shotmodel)ifmodel_info["type"]!=ModelType.BEHAVIORALandmodel_info["type"]!=ModelType.STRUCTURAL:raiseException(f"{model_info['name']} is not of Model type")model=model_info["data"]ifnameisnotNone:model.set_name(name)returnmodel