"""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, SnapshotManager, that manages snapshots of the association between BehaivorModel and Model. """fromdillimportdumpimportjsonimportosfrom.snapshot_factoryimportSnapshotFactory
[docs]classSnapshotManager:"""SnapshotManager performs snapshots or restores snapshotted data. It manages the models you want to snapshot and the SnapshotCondition for those models. It snapshots simulations (the directory where all the models and their releases are stored). It also manages the RestoreHandler, which performs a restore of the model or simulation. """def__init__(self,restore_handler=None):""" Args: restore_handler (RestoreHandler, optional): If you want to restore snapshotted simulation, specify a RestoreHandler as a parameter. Defaults to None. """self.snapshot_condition_map={}self.restore_handler=restore_handler
[docs]defget_engine(self):"""Returns the SysExecutor. Returns: Restored SysExecutor """ifself.restore_handler:returnself.restore_handler.get_engine()else:returnNone
[docs]defregister_snapshot_condition(self,_name,_snapshot_condition):"""Associate the model you want to take a snapshot of with the model's SnapshotCondition. Args: _name (str): BehaivorModel name _snapshot_condition (SnapshotCondition): Concrete SnapshotCondition """self.snapshot_condition_map[_name]=_snapshot_condition
[docs]defget_snapshot_factory(self):"""Creates a SnapshotFactory with the SnapshotConditions entered by the user. Returns: SnapshotFactory: SnapshotFactory that generates a SnapshotExecutor that takes the snapshot """returnSnapshotFactory(self.snapshot_condition_map)
[docs]defload_snapshot(self,name,shotmodel):"""_summary_ Args: name (str): The name of the snapshot Model to restore. shotmodel (Binary): Data from snapshotted models. Returns: restore_handler.load_snapshot(name, shotmodel) : Restored BehaviorModel """ifself.restore_handler:returnself.restore_handler.load_snapshot(name,shotmodel)else:returnNone
[docs]defsnapshot_simulation(self,relation_map,model_map,name,directory_path="."):""" Takes a snapshot of the simulation. Snapshot simulation model information and relationships to the “directory_path/name” location. Args: relation_map (dict): The relation map of SysExecutor model_map (dict): The model map of SysExecutor name (str): The name of Simulation directory_path (str): The directory path to save the snapshot """relation={}forkey,valueinrelation_map.items():ifkey[0]:port_key=((key[0].get_name(),key[1]))else:port_key=keylst=[]formodelinvalue:ifmodel[0]isnotNone:data=(model[0].get_name(),model[1])lst.append(tuple(data))else:lst.append(model)relation[port_key]=lstpath=f"{directory_path}/{name}"ifnotos.path.exists(f"{path}"):os.makedirs(path)withopen(f"{path}/relation_map.json","w")asf:relation={str(key):str(value)forkey,valueinrelation.items()}json.dump(relation,f)dump_model={}withopen(f"{path}/model_map.json","w")asf:dump_model["model_name"]=list(model_map.keys())#dump_model["model_name"].remove('dc')json.dump(dump_model,f)forkey,valueinmodel_map.items():#if key == 'dc':# continuewithopen(f"{path}/{key}.simx","wb")asf:dump(value[0].get_core_model().model_snapshot(),f)return