11
2- function _packVariable (dfg:: G , v:: DFGVariable ):: Dict{String, Any} where G <: AbstractDFG
3- props = Dict {String, Any} ()
4- props[" label" ] = string (v. label)
5- props[" timestamp" ] = string (v. timestamp)
6- props[" tags" ] = JSON2. write (v. tags)
7- props[" estimateDict" ] = JSON2. write (v. estimateDict)
8- props[" solverDataDict" ] = JSON2. write (Dict (keys (v. solverDataDict) .=> map (vnd -> pack (dfg, vnd), values (v. solverDataDict))))
9- props[" smallData" ] = JSON2. write (v. smallData)
10- props[" ready" ] = v. ready
11- props[" backendset" ] = v. backendset
12- return props
13- end
14-
15- function _unpackVariable (dfg:: G , packedProps:: Dict{String, Any} ):: DFGVariable where G <: AbstractDFG
16- label = Symbol (packedProps[" label" ])
17- timestamp = DateTime (packedProps[" timestamp" ])
18- tags = JSON2. read (packedProps[" tags" ], Vector{Symbol})
19- estimateDict = JSON2. read (packedProps[" estimateDict" ], Dict{Symbol, Dict{Symbol, VariableEstimate}})
20- smallData = nothing
21- smallData = JSON2. read (packedProps[" smallData" ], Dict{String, String})
22-
23- packed = JSON2. read (packedProps[" solverDataDict" ], Dict{String, PackedVariableNodeData})
24- solverData = Dict (Symbol .(keys (packed)) .=> map (p -> unpack (dfg, p), values (packed)))
25-
26- # Rebuild DFGVariable
27- variable = DFGVariable (Symbol (packedProps[" label" ]))
28- variable. timestamp = timestamp
29- variable. tags = tags
30- variable. estimateDict = estimateDict
31- variable. solverDataDict = solverData
32- variable. smallData = smallData
33- variable. ready = packedProps[" ready" ]
34- variable. backendset = packedProps[" backendset" ]
35-
36- return variable
37- end
38-
39- function _packFactor (dfg:: G , f:: DFGFactor ):: Dict{String, Any} where G <: AbstractDFG
40- # Construct the properties to save
41- props = Dict {String, Any} ()
42- props[" label" ] = string (f. label)
43- props[" tags" ] = JSON2. write (f. tags)
44- # Pack the node data
45- fnctype = f. data. fnc. usrfnc!
46- try
47- packtype = getfield (_getmodule (fnctype), Symbol (" Packed$(_getname (fnctype)) " ))
48- packed = convert (PackedFunctionNodeData{packtype}, f. data)
49- props[" data" ] = JSON2. write (packed)
50- catch ex
51- io = IOBuffer ()
52- showerror (io, ex, catch_backtrace ())
53- err = String (take! (io))
54- msg = " Error while packing '$(f. label) ' as '$fnctype ', please check the unpacking/packing converters for this factor - \r\n $err "
55- error (msg)
56- end
57- # Include the type
58- props[" fnctype" ] = String (_getname (fnctype))
59- props[" _variableOrderSymbols" ] = JSON2. write (f. _variableOrderSymbols)
60- props[" backendset" ] = f. backendset
61- props[" ready" ] = f. ready
62-
63- return props
64- end
65-
66-
67- function _unpackFactor (dfg:: G , packedProps:: Dict{String, Any} , iifModule):: DFGFactor where G <: AbstractDFG
68- label = packedProps[" label" ]
69- tags = JSON2. read (packedProps[" tags" ], Vector{Symbol})
70-
71- data = packedProps[" data" ]
72- @debug " Decoding $label ..."
73- datatype = packedProps[" fnctype" ]
74- packtype = getfield (Main, Symbol (" Packed" * datatype))
75- packed = nothing
76- fullFactor = nothing
77- try
78- packed = JSON2. read (data, GenericFunctionNodeData{packtype,String})
79- fullFactor = iifModule. decodePackedType (dfg, packed)
80- catch ex
81- io = IOBuffer ()
82- showerror (io, ex, catch_backtrace ())
83- err = String (take! (io))
84- msg = " Error while unpacking '$label ' as '$datatype ', please check the unpacking/packing converters for this factor - \r\n $err "
85- error (msg)
86- end
87-
88- # Include the type
89- _variableOrderSymbols = JSON2. read (packedProps[" _variableOrderSymbols" ], Vector{Symbol})
90- backendset = packedProps[" backendset" ]
91- ready = packedProps[" ready" ]
92-
93- # Rebuild DFGVariable
94- factor = DFGFactor {typeof(fullFactor.fnc), Symbol} (Symbol (label))
95- factor. tags = tags
96- factor. data = fullFactor
97- factor. _variableOrderSymbols = _variableOrderSymbols
98- factor. ready = ready
99- factor. backendset = backendset
100-
101- # GUARANTEED never to bite us in the ass in the future...
102- # ... TODO : refactor if changed: https://github.com/JuliaRobotics/IncrementalInference.jl/issues/350
103- factor. data. fncargvID = deepcopy (_variableOrderSymbols)
104-
105- # Note, once inserted, you still need to call IIF.rebuildFactorMetadata!
106- return factor
107- end
108-
1092function saveDFG (dfg:: G , folder:: String ) where G <: AbstractDFG
1103 variables = getVariables (dfg)
1114 factors = getFactors (dfg)
@@ -123,14 +16,14 @@ function saveDFG(dfg::G, folder::String) where G <: AbstractDFG
12316 map (f -> rm (" $factorFolder /$f " ), readdir (factorFolder))
12417 # Variables
12518 for v in variables
126- vPacked = _packVariable (dfg, v)
19+ vPacked = packVariable (dfg, v)
12720 io = open (" $varFolder /$(v. label) .json" , " w" )
12821 JSON2. write (io, vPacked)
12922 close (io)
13023 end
13124 # Factors
13225 for f in factors
133- fPacked = _packFactor (dfg, f)
26+ fPacked = packFactor (dfg, f)
13427 io = open (" $folder /factors/$(f. label) .json" , " w" )
13528 JSON2. write (io, fPacked)
13629 close (io)
@@ -152,7 +45,7 @@ function loadDFG(folder::String, iifModule, dfgLoadInto::G=GraphsDFG{NoSolverPar
15245 for varFile in varFiles
15346 io = open (" $varFolder /$varFile " )
15447 packedData = JSON2. read (io, Dict{String, Any})
155- push! (variables, _unpackVariable (dfgLoadInto, packedData))
48+ push! (variables, unpackVariable (dfgLoadInto, packedData))
15649 end
15750 @info " Loaded $(length (variables)) variables - $(map (v-> v. label, variables)) "
15851 @info " Inserting variables into graph..."
@@ -162,7 +55,7 @@ function loadDFG(folder::String, iifModule, dfgLoadInto::G=GraphsDFG{NoSolverPar
16255 for factorFile in factorFiles
16356 io = open (" $factorFolder /$factorFile " )
16457 packedData = JSON2. read (io, Dict{String, Any})
165- push! (factors, _unpackFactor (dfgLoadInto, packedData, iifModule))
58+ push! (factors, unpackFactor (dfgLoadInto, packedData, iifModule))
16659 end
16760 @info " Loaded $(length (variables)) factors - $(map (f-> f. label, factors)) "
16861 @info " Inserting factors into graph..."
0 commit comments