Skip to content

Commit f1426cd

Browse files
authored
Merge pull request #973 from JuliaRobotics/23Q1/fix/unpackfactordata
fix unpackFactor on factorData, one use of JSON2
2 parents 16cc933 + 5e7a230 commit f1426cd

File tree

4 files changed

+44
-71
lines changed

4 files changed

+44
-71
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
99
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1010
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
1111
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
12+
JSON2 = "2535ab7d-5cd8-5a07-80ac-9b1792aadce3"
1213
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
1314
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1415
ManifoldsBase = "3362f125-f0bb-47a3-aa74-596ffd7ef2fb"
@@ -31,6 +32,7 @@ Distributions = "0.23, 0.24, 0.25"
3132
DocStringExtensions = "0.8, 0.9"
3233
GraphPlot = "0.5.0"
3334
Graphs = "1.4"
35+
JSON2 = "0.3"
3436
JSON3 = "1"
3537
ManifoldsBase = "0.12, 0.13, 0.14"
3638
OrderedCollections = "1.4"

src/DistributedFactorGraphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ using Dates
2222
using TimeZones
2323
using Distributions
2424
using Reexport
25+
using JSON2 # only for unpacking factorData via NamedTuples, TODO drop dependency
2526
using JSON3
2627
using StructTypes
2728
using LinearAlgebra

src/entities/DFGVariable.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ end
157157

158158
StructTypes.StructType(::Type{MeanMaxPPE}) = StructTypes.UnorderedStruct()
159159
StructTypes.idproperty(::Type{MeanMaxPPE}) = :id
160-
StructTypes.omitempties(::Type{MeanMaxPPE}) = (:id,)
160+
StructTypes.omitempties(::Type{MeanMaxPPE}) = (:id,:createdTimestamp,:lastUpdatedTimestamp)
161161

162162
##------------------------------------------------------------------------------
163163
## Constructors

src/services/Serialization.jl

Lines changed: 40 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ end
3535
typeModuleName(varT::Type{<:InferenceVariable}) = typeModuleName(varT())
3636

3737
"""
38-
$(SIGNATURES)
38+
$(SIGNATURES)
3939
Get a type from the serialization module.
4040
"""
4141
function getTypeFromSerializationModule(_typeString::AbstractString)
@@ -113,7 +113,7 @@ function unpackVariableNodeData(d::PackedVariableNodeData)
113113
vals = Vector{getPointType(T)}(undef, length(val_))
114114
# vals = getPoint.(T, val_)
115115
for (i,v) in enumerate(val_)
116-
vals[i] = getPoint(T, v)
116+
vals[i] = getPoint(T, v)
117117
end
118118

119119
r4 = d.dimbw
@@ -191,6 +191,7 @@ function unpackVariable(variable::PackedVariable; skipVersionCheck::Bool=false)
191191
solvable=variable.solvable )
192192
end
193193

194+
194195
##==============================================================================
195196
## Factor Packing and unpacking
196197
##==============================================================================
@@ -251,16 +252,20 @@ end
251252
function fncStringToData(packtype::Type{<:AbstractPackedFactor}, data::String)
252253

253254
# Convert string to Named Tuples for kwargs
254-
# packed = JSON3.read(data, GenericFunctionNodeData{packtype})
255-
fncData = JSON3.read(data, Dict{String,Any})
255+
# packed_ = JSON3.read(data, GenericFunctionNodeData{packtype})
256+
fncData = JSON3.read(data) #, NamedTuple) # Dict{String,Any})
257+
256258

257259
# data isa AbstractString ? JSON2.read(data) : data
260+
# JSON3.generate_type(packed.fnc)
261+
# JSON3.generate_type(fncData["fnc"])
258262

259263
# TODO use kwdef constructors instead,
260-
@show fncData["fnc"]
261-
packT = JSON3.read(JSON3.write(fncData["fnc"]), packtype)
262-
# nt = NamedTuple{}
263-
# packtype(;fncData["fnc"]...)
264+
# @info "user factor" fncData["fnc"]
265+
restring = JSON3.write(fncData["fnc"])
266+
# @show restring
267+
# packT = JSON3.read(restring, packtype)
268+
packT = packtype(;JSON2.read(restring)...)
264269

265270
packed = GenericFunctionNodeData{packtype}(
266271
fncData["eliminated"],
@@ -297,82 +302,47 @@ function fncStringToData(fncType::String, data::T) where {T <: AbstractPackedFac
297302
end
298303
end
299304
function fncStringToData(fncType::String, data::Union{String, <:NamedTuple})
305+
# FIXME, should rather just store the data as `PackedFactorX` rather than hard code the type change here???
300306
packtype = DFG.getTypeFromSerializationModule("Packed"*fncType)
301307
fncStringToData(packtype, data)
302308
end
303309

304310

305-
# Returns `::DFGFactor`
306311
function unpackFactor(
307-
dfg::G,
308-
packedFactor::PackedFactor;
312+
dfg::AbstractDFG,
313+
factor::PackedFactor;
309314
skipVersionCheck::Bool=false
310-
) where G <: AbstractDFG
311-
# Version checking.
312-
!skipVersionCheck && _versionCheck(packedFactor)
313-
314-
# id = if haskey(packedProps, "id") && packedProps["id"] !== nothing
315-
# UUID(packedProps["id"]) else nothing end
316-
# label = packedProps["label"]
317-
318-
# # various formats in which the timestamp might be stored
319-
# packedProps["timestamp"] = getStandardZDTString(packedProps["timestamp"])
320-
# timestamp = ZonedDateTime(packedProps["timestamp"])
321-
# nstime = Nanosecond(get(packedProps, "nstime", 0))
322-
323-
# Get the stored tags and variable order
324-
# @assert !(packedProps["tags"] isa String) "unpackFactor expecting JSON only data, packed `tags` should be a vector of strings (not a single string of elements)."
325-
# @assert !(packedProps["_variableOrderSymbols"] isa String) "unpackFactor expecting JSON only data, packed `_variableOrderSymbols` should be a vector of strings (not a single string of elements)."
326-
# tags = Symbol.(packedProps["tags"])
327-
# _variableOrderSymbols = Symbol.(packedProps["_variableOrderSymbols"])
328-
329-
data = packedFactor.data
330-
# if(data isa AbstractString)
331-
# data = JSON3.read(data, NamedTuple) # was a JSON2
332-
# end
333-
datatype = packedFactor.fnctype
334-
@debug "DECODING factor type = '$(datatype)' for factor '$label'"
335-
# packtype = getTypeFromSerializationModule("Packed"*datatype)
336-
337-
# FIXME type instability from nothing to T
338-
packed = nothing
339-
fullFactorData = nothing
315+
)
316+
#
317+
@debug "DECODING factor type = '$(factor.fnctype)' for factor '$(factor.label)'"
318+
!skipVersionCheck && _versionCheck(factor)
340319

320+
fullFactorData = nothing
341321
try
342-
packed = fncStringToData(datatype, data) #convert(GenericFunctionNodeData{packtype}, data)
322+
packedFnc = fncStringToData(factor.fnctype, factor.data)
343323
decodeType = getFactorOperationalMemoryType(dfg)
344-
fullFactorData = decodePackedType(dfg, packedFactor._variableOrderSymbols, decodeType, packed)
324+
fullFactorData = decodePackedType(dfg, factor._variableOrderSymbols, decodeType, packedFnc)
345325
catch ex
346326
io = IOBuffer()
347327
showerror(io, ex, catch_backtrace())
348328
err = String(take!(io))
349-
msg = "Error while unpacking '$(packedFactor.label)' as '$datatype', please check the unpacking/packing converters for this factor - \r\n$err"
329+
msg = "Error while unpacking '$(factor.label)' as '$(factor.fnctype)', please check the unpacking/packing converters for this factor - \r\n$err"
350330
error(msg)
351-
end
352-
353-
# solvable = packedProps["solvable"]
354-
355-
smallData = JSON3.read(base64decode(packedFactor.metadata), Dict{Symbol, DFG.SmallDataTypes})
356-
# Rebuild DFGFactor
357-
#TODO use constuctor to create factor
358-
factor = DFGFactor( packedFactor.label,
359-
packedFactor.timestamp,
360-
Nanosecond(packedFactor.nstime),
361-
Set(packedFactor.tags),
362-
fullFactorData,
363-
packedFactor.solvable,
364-
Tuple(packedFactor._variableOrderSymbols);
365-
packedFactor.id,
366-
smallData)
367-
#
368-
369-
# Note, once inserted, you still need to call rebuildFactorMetadata!
370-
return factor
331+
end
332+
333+
metadata = JSON3.read(base64decode(factor.metadata), Dict{Symbol, DFG.SmallDataTypes})
334+
335+
return DFGFactor(
336+
factor.label,
337+
factor.timestamp,
338+
Nanosecond(factor.nstime),
339+
Set(factor.tags),
340+
fullFactorData,
341+
factor.solvable,
342+
Tuple(factor._variableOrderSymbols),
343+
id=factor.id,
344+
smallData = metadata
345+
)
371346
end
372347

373-
374-
##==============================================================================
375-
## Serialization
376-
##==============================================================================
377-
378-
348+
#

0 commit comments

Comments
 (0)