@@ -12,6 +12,30 @@ function _packVariable(dfg::G, v::DFGVariable)::Dict{String, Any} where G <: Abs
12
12
return props
13
13
end
14
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, 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
+
15
39
function _packFactor (dfg:: G , f:: DFGFactor ):: Dict{String, Any} where G <: AbstractDFG
16
40
# Construct the properties to save
17
41
props = Dict {String, Any} ()
@@ -31,6 +55,39 @@ function _packFactor(dfg::G, f::DFGFactor)::Dict{String, Any} where G <: Abstrac
31
55
return props
32
56
end
33
57
58
+
59
+ function _unpackFactor (dfg:: G , packedProps:: Dict{String, Any} , iifModule):: DFGFactor where G <: AbstractDFG
60
+ label = packedProps[" label" ]
61
+ tags = JSON2. read (packedProps[" tags" ], Vector{Symbol})
62
+
63
+ data = packedProps[" data" ]
64
+ datatype = packedProps[" fnctype" ]
65
+ packtype = getfield (Main, Symbol (" Packed" * datatype))
66
+ packed = JSON2. read (data, GenericFunctionNodeData{packtype,String})
67
+ fullFactor = iifModule. decodePackedType (dfg, packed)
68
+ # fullFactor = dfg.decodePackedTypeFunc(dfg, packed)
69
+
70
+ # Include the type
71
+ _variableOrderSymbols = JSON2. read (packedProps[" _variableOrderSymbols" ], Vector{Symbol})
72
+ backendset = packedProps[" backendset" ]
73
+ ready = packedProps[" ready" ]
74
+
75
+ # Rebuild DFGVariable
76
+ factor = DFGFactor {typeof(fullFactor.fnc), Symbol} (Symbol (label))
77
+ factor. tags = tags
78
+ factor. data = fullFactor
79
+ factor. _variableOrderSymbols = _variableOrderSymbols
80
+ factor. ready = ready
81
+ factor. backendset = backendset
82
+
83
+ # GUARANTEED never to bite us in the ass in the future...
84
+ # ... TODO : refactor if changed: https://github.com/JuliaRobotics/IncrementalInference.jl/issues/350
85
+ getData (factor). fncargvID = _variableOrderSymbols
86
+
87
+ # Note, once inserted, you still need to call IIF.rebuildFactorMetadata!
88
+ return factor
89
+ end
90
+
34
91
function saveDFG (dfg:: G , folder:: String ) where G <: AbstractDFG
35
92
variables = getVariables (dfg)
36
93
factors = getFactors (dfg)
@@ -62,6 +119,43 @@ function saveDFG(dfg::G, folder::String) where G <: AbstractDFG
62
119
end
63
120
end
64
121
65
- function loadDFG (folderName:: String , dfgLoadInto:: G = GraphsDFG {NoSolverParams} ()) where G <: AbstractDFG
122
+ function loadDFG (folder:: String , iifModule, dfgLoadInto:: G = GraphsDFG {NoSolverParams} ()) where G <: AbstractDFG
123
+ variables = DFGVariable[]
124
+ factors = DFGFactor[]
125
+ varFolder = " $folder /variables"
126
+ factorFolder = " $folder /factors"
127
+ # Folder preparations
128
+ ! isdir (folder) && error (" Can't load DFG graph - folder '$folder ' doesn't exist" )
129
+ ! isdir (varFolder) && error (" Can't load DFG graph - folder '$folder ' doesn't exist" )
130
+ ! isdir (factorFolder) && error (" Can't load DFG graph - folder '$folder ' doesn't exist" )
131
+
132
+ varFiles = readdir (varFolder)
133
+ factorFiles = readdir (factorFolder)
134
+ for varFile in varFiles
135
+ io = open (" $varFolder /$varFile " )
136
+ packedData = JSON2. read (io, Dict{String, Any})
137
+ push! (variables, _unpackVariable (dfgLoadInto, packedData))
138
+ end
139
+ @info " Loaded $(length (variables)) variables - $(map (v-> v. label, variables)) "
140
+ @info " Inserting variables into graph..."
141
+ # Adding variables
142
+ map (v-> addVariable! (dfgLoadInto, v), variables)
143
+
144
+ for factorFile in factorFiles
145
+ io = open (" $factorFolder /$factorFile " )
146
+ packedData = JSON2. read (io, Dict{String, Any})
147
+ push! (factors, _unpackFactor (dfgLoadInto, packedData, iifModule))
148
+ end
149
+ @info " Loaded $(length (variables)) factors - $(map (f-> f. label, factors)) "
150
+ @info " Inserting factors into graph..."
151
+ # # Adding factors
152
+ map (f-> addFactor! (dfgLoadInto, f. _variableOrderSymbols, f), factors)
153
+
154
+ # Finally, rebuild the CCW's for the factors to completely reinflate them
155
+ @info " Rebuilding CCW's for the factors..."
156
+ for factor in factors
157
+ iifModule. rebuildFactorMetadata! (dfgLoadInto, factor)
158
+ end
159
+
66
160
return dfgLoadInto
67
161
end
0 commit comments