79
79
80
80
typeModuleName (varT:: Type{<:InferenceVariable} ) = typeModuleName (varT ())
81
81
82
- function getTypeFromSerializationModule (variableTypeString:: String )
82
+ function getTypeFromSerializationModule (_typeString:: AbstractString )
83
+ @debug " DFG converting type string to Julia type" _typeString
83
84
try
84
85
# split the type at last `.`
85
- split_st = split (variableTypeString , r" \. (?!.*\. )" )
86
+ split_st = split (_typeString , r" \. (?!.*\. )" )
86
87
# if module is specified look for the module in main, otherwise use Main
87
88
if length (split_st) == 2
88
89
m = getfield (Main, Symbol (split_st[1 ]))
@@ -102,7 +103,7 @@ function getTypeFromSerializationModule(variableTypeString::String)
102
103
return ret
103
104
104
105
catch ex
105
- @error " Unable to deserialize soft type $(variableTypeString ) "
106
+ @error " Unable to deserialize type $(_typeString ) "
106
107
io = IOBuffer ()
107
108
showerror (io, ex, catch_backtrace ())
108
109
err = String (take! (io))
@@ -111,6 +112,25 @@ function getTypeFromSerializationModule(variableTypeString::String)
111
112
nothing
112
113
end
113
114
115
+ """
116
+ $(SIGNATURES)
117
+ Get a type from the serialization module inside DFG.
118
+ """
119
+ function getTypeFromSerializationModule (dfg:: G , moduleType:: Symbol ) where G <: AbstractDFG
120
+ @warn " Deprecating getTypeFromSerializationModule(dfg,symbol), use getTypeFromSerializationModule(string) instead." maxlog= 10
121
+ st = nothing
122
+ try
123
+ st = getfield (Main, Symbol (moduleType))
124
+ catch ex
125
+ @error " Unable to deserialize packed variableType $(moduleType) "
126
+ io = IOBuffer ()
127
+ showerror (io, ex, catch_backtrace ())
128
+ err = String (take! (io))
129
+ @error (err)
130
+ end
131
+ return st
132
+ end
133
+
114
134
# #==============================================================================
115
135
# # Variable Packing and unpacking
116
136
# #==============================================================================
@@ -131,12 +151,69 @@ function packVariable(dfg::AbstractDFG, v::DFGVariable)
131
151
return props:: Dict{String, Any}
132
152
end
133
153
154
+ """
155
+ $(SIGNATURES)
156
+
157
+ Common unpack a Dict{String, Any} into a PPE.
158
+ """
159
+ function _unpackPPE (
160
+ packedPPE:: Dict{String, Any} ;
161
+ _type = pop! (packedPPE, " _type" ) # required for generic use
162
+ )
163
+ #
164
+ # Cleanup Zoned timestamp, which is always UTC
165
+ if packedPPE[" lastUpdatedTimestamp" ][end ] == ' Z'
166
+ packedPPE[" lastUpdatedTimestamp" ] = packedPPE[" lastUpdatedTimestamp" ][1 : end - 1 ]
167
+ end
168
+
169
+ # !haskey(packedPPE, "_type") && error("Cannot find type key '_type' in packed PPE data")
170
+ if (_type === nothing || _type == " " )
171
+ @warn " Cannot deserialize PPE, unknown type key, trying DistributedFactorGraphs.MeanMaxPPE" _type
172
+ _type = " DistributedFactorGraphs.MeanMaxPPE"
173
+ end
174
+ ppeType = getTypeFromSerializationModule (_type)
175
+
176
+ ppe = Unmarshal. unmarshal (
177
+ ppeType,
178
+ packedPPE
179
+ )
180
+ # _pk = Symbol(packedPPE["solveKey"])
181
+ # ppe = MeanMaxPPE(;
182
+ # solveKey=_pk,
183
+ # suggested=float.(pd["suggested"]),
184
+ # max=float.(pd["max"]),
185
+ # mean=float.(pd["mean"]),
186
+ # lastUpdatedTimestamp=DateTime(string(pd["lastUpdatedTimestamp"]))
187
+ # )
188
+
189
+ return ppe
190
+ end
191
+
192
+ """
193
+ $(SIGNATURES)
194
+
195
+ Unpack a Dict{String, Any} into a PPE.
196
+
197
+ Notes:
198
+ - returns `::VariableNodeData`
199
+ """
200
+ function _unpackVariableNodeData (
201
+ dfg:: AbstractDFG ,
202
+ packedDict:: Dict{String, Any}
203
+ )
204
+ #
205
+ packedVND = Unmarshal. unmarshal (PackedVariableNodeData, packedDict)
206
+ return unpackVariableNodeData (dfg, packedVND)
207
+ end
208
+
134
209
# returns a DFGVariable
135
- function unpackVariable (dfg:: G ,
210
+ function unpackVariable (dfg:: AbstractDFG ,
136
211
packedProps:: Dict{String, Any} ;
137
212
unpackPPEs:: Bool = true ,
138
213
unpackSolverData:: Bool = true ,
139
- unpackBigData:: Bool = true ) where G <: AbstractDFG
214
+ unpackBigData:: Bool = haskey (packedProps," dataEntryType" ) && haskey (packedProps, " dataEntry" )
215
+ )
216
+ #
140
217
@debug " Unpacking variable:\r\n $packedProps "
141
218
# Version checking.
142
219
_versionCheck (packedProps)
@@ -146,32 +223,51 @@ function unpackVariable(dfg::G,
146
223
# Parse it
147
224
timestamp = ZonedDateTime (packedProps[" timestamp" ])
148
225
nstime = Nanosecond (get (packedProps, " nstime" , 0 ))
149
- # Supporting string serialization using packVariable and CGDFG serialization (Vector{String})
150
- if packedProps[" tags" ] isa String
151
- tags = JSON2. read (packedProps[" tags" ], Vector{Symbol})
226
+
227
+ # FIXME , drop nested packing, see DFG #867
228
+ # string serialization using packVariable and CGDFG serialization (Vector{String})
229
+ tags = if packedProps[" tags" ] isa String
230
+ JSON2. read (packedProps[" tags" ], Vector{Symbol})
152
231
else
153
- tags = Symbol .(packedProps[" tags" ])
232
+ Symbol .(packedProps[" tags" ])
154
233
end
155
- ppeDict = unpackPPEs ? JSON2. read (packedProps[" ppeDict" ], Dict{Symbol, MeanMaxPPE}) : Dict {Symbol, MeanMaxPPE} ()
156
- smallData = JSON2. read (packedProps[" smallData" ], Dict{Symbol, SmallDataTypes})
157
234
158
- variableTypeString = if haskey (packedProps, " softtype" )
159
- # TODO Deprecate, remove in v0.12
160
- @warn " Packed field `softtype` is deprecated and replaced with `variableType`"
161
- packedProps[" softtype" ]
235
+ # FIXME , drop nested packing, see DFG #867
236
+ ppeDict = if unpackPPEs && haskey (packedProps," ppesDict" )
237
+ JSON2. read (packedProps[" ppeDict" ], Dict{Symbol, MeanMaxPPE})
238
+ elseif unpackPPEs && haskey (packedProps," ppes" ) && packedProps[" ppes" ] isa AbstractVector
239
+ # these different cases are not well covered in tests, but first fix #867
240
+ # TODO dont hardcode the ppeType (which is already discovered for each entry in _updatePPE)
241
+ ppedict = Dict {Symbol, MeanMaxPPE} ()
242
+ for pd in packedProps[" ppes" ]
243
+ _type = get (pd, " _type" , " DistributedFactorGraphs.MeanMaxPPE" )
244
+ ppedict[Symbol (pd[" solveKey" ])] = _unpackPPE (pd; _type)
245
+ end
246
+ ppedict
162
247
else
163
- packedProps[ " variableType " ]
248
+ Dict {Symbol, MeanMaxPPE} ()
164
249
end
165
250
251
+ smallData = JSON2. read (packedProps[" smallData" ], Dict{Symbol, SmallDataTypes})
252
+
253
+ variableTypeString = packedProps[" variableType" ]
254
+
166
255
variableType = getTypeFromSerializationModule (variableTypeString)
167
256
isnothing (variableType) && error (" Cannot deserialize variableType '$variableTypeString ' in variable '$label '" )
168
257
pointType = getPointType (variableType)
169
258
170
- if unpackSolverData
259
+ # FIXME , drop nested packing, see DFG #867
260
+ solverData = if unpackSolverData && haskey (packedProps, " solverDataDict" )
171
261
packed = JSON2. read (packedProps[" solverDataDict" ], Dict{String, PackedVariableNodeData})
172
- solverData = Dict {Symbol, VariableNodeData{variableType, pointType}} (Symbol .(keys (packed)) .=> map (p -> unpackVariableNodeData (dfg, p), values (packed)))
262
+ Dict {Symbol, VariableNodeData{variableType, pointType}} (Symbol .(keys (packed)) .=> map (p -> unpackVariableNodeData (dfg, p), values (packed)))
263
+ elseif unpackPPEs && haskey (packedProps," solverData" ) && packedProps[" solverData" ] isa AbstractVector
264
+ solverdict = Dict {Symbol, VariableNodeData{variableType, pointType}} ()
265
+ for sd in packedProps[" solverData" ]
266
+ solverdict[Symbol (sd[" solveKey" ])] = _unpackVariableNodeData (dfg, sd)
267
+ end
268
+ solverdict
173
269
else
174
- solverData = Dict {Symbol, VariableNodeData{variableType, pointType}} ()
270
+ Dict {Symbol, VariableNodeData{variableType, pointType}} ()
175
271
end
176
272
# Rebuild DFGVariable using the first solver variableType in solverData
177
273
# @info "dbg Serialization 171" variableType Symbol(packedProps["label"]) timestamp nstime ppeDict solverData smallData Dict{Symbol,AbstractDataEntry}() Ref(packedProps["solvable"])
@@ -191,23 +287,12 @@ function unpackVariable(dfg::G,
191
287
# Now rehydrate complete DataEntry type.
192
288
if unpackBigData
193
289
# TODO Deprecate - for backward compatibility between v0.8 and v0.9, remove in v0.10
194
- if haskey (packedProps, " bigDataElemType" )
195
- @warn " `bigDataElemType` is deprecate, please save data again with new version that uses `dataEntryType`"
196
- dataElemTypes = JSON2. read (packedProps[" bigDataElemType" ], Dict{Symbol, Symbol})
197
- else
198
- dataElemTypes = JSON2. read (packedProps[" dataEntryType" ], Dict{Symbol, Symbol})
199
- for (k,name) in dataElemTypes
200
- dataElemTypes[k] = Symbol (split (string (name), ' .' )[end ])
201
- end
290
+ dataElemTypes = JSON2. read (packedProps[" dataEntryType" ], Dict{Symbol, Symbol})
291
+ for (k,name) in dataElemTypes
292
+ dataElemTypes[k] = Symbol (split (string (name), ' .' )[end ])
202
293
end
203
294
204
- # TODO Deprecate - for backward compatibility between v0.8 and v0.9, remove in v0.10
205
- if haskey (packedProps, " bigData" )
206
- @warn " `bigData` is deprecate, please save data again with new version"
207
- dataIntermed = JSON2. read (packedProps[" bigData" ], Dict{Symbol, String})
208
- else
209
- dataIntermed = JSON2. read (packedProps[" dataEntry" ], Dict{Symbol, String})
210
- end
295
+ dataIntermed = JSON2. read (packedProps[" dataEntry" ], Dict{Symbol, String})
211
296
212
297
for (k,bdeInter) in dataIntermed
213
298
interm = JSON. parse (bdeInter)
452
537
# # Serialization
453
538
# #==============================================================================
454
539
455
- """
456
- $(SIGNATURES)
457
- Get a type from the serialization module inside DFG.
458
- """
459
- function getTypeFromSerializationModule (dfg:: G , moduleType:: Symbol ) where G <: AbstractDFG
460
- st = nothing
461
- try
462
- st = getfield (Main, Symbol (moduleType))
463
- catch ex
464
- @error " Unable to deserialize packed variableType $(moduleType) "
465
- io = IOBuffer ()
466
- showerror (io, ex, catch_backtrace ())
467
- err = String (take! (io))
468
- @error (err)
469
- end
470
- return st
471
- end
540
+
0 commit comments