1
1
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
-
109
2
function saveDFG (dfg:: G , folder:: String ) where G <: AbstractDFG
110
3
variables = getVariables (dfg)
111
4
factors = getFactors (dfg)
@@ -123,14 +16,14 @@ function saveDFG(dfg::G, folder::String) where G <: AbstractDFG
123
16
map (f -> rm (" $factorFolder /$f " ), readdir (factorFolder))
124
17
# Variables
125
18
for v in variables
126
- vPacked = _packVariable (dfg, v)
19
+ vPacked = packVariable (dfg, v)
127
20
io = open (" $varFolder /$(v. label) .json" , " w" )
128
21
JSON2. write (io, vPacked)
129
22
close (io)
130
23
end
131
24
# Factors
132
25
for f in factors
133
- fPacked = _packFactor (dfg, f)
26
+ fPacked = packFactor (dfg, f)
134
27
io = open (" $folder /factors/$(f. label) .json" , " w" )
135
28
JSON2. write (io, fPacked)
136
29
close (io)
@@ -152,7 +45,7 @@ function loadDFG(folder::String, iifModule, dfgLoadInto::G=GraphsDFG{NoSolverPar
152
45
for varFile in varFiles
153
46
io = open (" $varFolder /$varFile " )
154
47
packedData = JSON2. read (io, Dict{String, Any})
155
- push! (variables, _unpackVariable (dfgLoadInto, packedData))
48
+ push! (variables, unpackVariable (dfgLoadInto, packedData))
156
49
end
157
50
@info " Loaded $(length (variables)) variables - $(map (v-> v. label, variables)) "
158
51
@info " Inserting variables into graph..."
@@ -162,7 +55,7 @@ function loadDFG(folder::String, iifModule, dfgLoadInto::G=GraphsDFG{NoSolverPar
162
55
for factorFile in factorFiles
163
56
io = open (" $factorFolder /$factorFile " )
164
57
packedData = JSON2. read (io, Dict{String, Any})
165
- push! (factors, _unpackFactor (dfgLoadInto, packedData, iifModule))
58
+ push! (factors, unpackFactor (dfgLoadInto, packedData, iifModule))
166
59
end
167
60
@info " Loaded $(length (variables)) factors - $(map (f-> f. label, factors)) "
168
61
@info " Inserting factors into graph..."
0 commit comments