@@ -195,8 +195,11 @@ class TraceInfo:
195195 - all_traces: all valid traces encountered in trace exploration, up until the point they could not go any further
196196 - previous_length: used to identify backtracking
197197 """
198+ ROBOTMBT_MODEL_VERSION = '1.0.0'
198199
199- def __init__ (self ):
200+ def __init__ (self , name : str = '' ):
201+ self .ROBOTMBT_MODEL_VERSION : str = TraceInfo .ROBOTMBT_MODEL_VERSION
202+ self .model_name : str = name
200203 self .current_trace : list [tuple [ScenarioInfo , StateInfo ]] = []
201204 self .all_traces : list [list [tuple [ScenarioInfo , StateInfo ]]] = []
202205 self .previous_length : int = 0
@@ -256,14 +259,14 @@ def _sanity_check(self, scen: ScenarioInfo, state: StateInfo, after: str):
256259 logger .warn (
257260 f'TraceInfo got out of sync after { after } \n Expected state: { prev_state } \n Actual state: { state } ' )
258261
259- def export_graph (self , suite_name : str , dir : str = '' , atest : bool = False ) -> str | None :
262+ def export_graph (self , dir : str = '' , atest : bool = False ) -> str | None :
260263 encoded_instance = jsonpickle .encode (self )
261- name = suite_name .lower ().replace (' ' , '_' )
264+ name = self . model_name .lower ().replace (' ' , '_' )
262265 if atest :
263266 '''
264267 temporary file to not accidentally overwrite an existing file
265268 mkstemp() is not ideal but given Python's limitations this is the easiest solution
266- as temporary file, a different method, is problematic on Windows
269+ as temporary file, a different method, is problematic on Windows
267270 https://stackoverflow.com/a/57015383
268271 '''
269272 fd , dir = tempfile .mkstemp ()
@@ -282,6 +285,22 @@ def export_graph(self, suite_name: str, dir: str = '', atest: bool = False) -> s
282285 f .write (encoded_instance )
283286 return None
284287
288+ @staticmethod
289+ def import_graph_from_file (file_path : str ) -> TraceInfo :
290+ try :
291+ with open (file_path , "r" ) as f :
292+ traceinfo = jsonpickle .decode (f .read ())
293+ traceinfo .ROBOTMBT_MODEL_VERSION # trigger AttributeError if non-robotmbt data
294+ except OSError :
295+ raise
296+ except Exception :
297+ raise TypeError (f"Contents from '{ file_path } ' could not be loaded as RobotMBT graph data" )
298+
299+ if str (traceinfo .ROBOTMBT_MODEL_VERSION ).split ('.' )[0 ] != TraceInfo .ROBOTMBT_MODEL_VERSION .split ('.' )[0 ]:
300+ # raise if major version differs
301+ raise ValueError ("Graph data is from an incompatible RobotMBT version" )
302+ return traceinfo
303+
285304 @staticmethod
286305 def stringify_pair (pair : tuple [ScenarioInfo , StateInfo ]) -> str :
287306 """
0 commit comments