Skip to content

Commit 4c6fc41

Browse files
authored
Merge pull request #827 from JuliaRobotics/22Q1/refac/_packSolverData
2 parents 9229c7e + 4282886 commit 4c6fc41

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

src/CloudGraphsDFG/services/CommonFunctions.jl

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ Note: Individual values are serialized if they are not already.
134134
If the additional properties is provided, the additional
135135
properties will be added in verbatim when serializing.
136136
137+
Returns: `::String`
138+
137139
Related
138140
139141
_Neo4jTo???
140142
"""
141-
function _structToNeo4jProps(inst::Union{User, Robot, Session, PVND, N, APPE, ABDE},
142-
addProps::Dict{String, String}=Dict{String, String}();
143-
cypherNodeName::String="subnode")::String where
143+
function _structToNeo4jProps(inst::Union{<:User, <:Robot, <:Session, PVND, N, APPE, ABDE},
144+
addProps::Dict{<:AbstractString, <:AbstractString}=Dict{String, String}();
145+
cypherNodeName::String="subnode") where
144146
{N <: DFGNode, APPE <: AbstractPointParametricEst, ABDE <: AbstractDataEntry, PVND <: PackedVariableNodeData}
145147
props = Dict{String, String}()
146148
io = IOBuffer()
@@ -156,7 +158,7 @@ function _structToNeo4jProps(inst::Union{User, Robot, Session, PVND, N, APPE, AB
156158
val = "\"$(string(field))\""
157159
end
158160
# TODO: Switch this to decorator pattern
159-
if typeof(inst) <: DFGNode
161+
if inst isa DFGNode # typeof(inst) <: DFGNode
160162
# Variables
161163
fieldname == :solverDataDict && continue
162164
fieldname == :ppeDict && continue
@@ -178,23 +180,12 @@ function _structToNeo4jProps(inst::Union{User, Robot, Session, PVND, N, APPE, AB
178180
# TODO: Consolidate with packFactor in Serialization.jl - https://github.com/JuliaRobotics/DistributedFactorGraphs.jl/issues/525
179181
if fieldname == :solverData
180182
fnctype = getSolverData(inst).fnc.usrfnc!
181-
try
182-
packtype = convertPackedType(fnctype)
183-
packed = convert(PackedFunctionNodeData{packtype}, getSolverData(inst))
184-
packedJson = JSON2.write(packed)
185-
val = "\"$(replace(packedJson, "\"" => "\\\""))\"" # Escape slashes too
186-
fieldname = :data #Keeping with FileDFG format
187-
catch ex
188-
io = IOBuffer()
189-
showerror(io, ex, catch_backtrace())
190-
err = String(take!(io))
191-
msg = "Error while packing '$(inst.label)' as '$fnctype', please check the unpacking/packing converters for this factor - \r\n$err"
192-
error(msg)
193-
end
183+
val = _packSolverData( inst, fnctype; replaceBackslashes=true )
184+
fieldname = :data #Keeping with FileDFG format
194185
end
195186
end
196187
# Fallback, default to JSON2
197-
if val == nothing
188+
if val === nothing
198189
val = JSON2.write(field)
199190
end
200191
write(io, "$cypherNodeName.$fieldname=$val,")

src/services/Serialization.jl

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
# TODO dev and debugging, used by some of the DFG drivers
3+
export _packSolverData
4+
25
# For all types that pack their type into their own structure (e.g. PPE)
36
const TYPEKEY = "_type"
47

@@ -288,26 +291,39 @@ end
288291
##==============================================================================
289292

290293

291-
function packFactor(dfg::G, f::DFGFactor)::Dict{String, Any} where G <: AbstractDFG
292-
# Construct the properties to save
293-
props = Dict{String, Any}()
294-
props["label"] = string(f.label)
295-
props["timestamp"] = Dates.format(f.timestamp, "yyyy-mm-ddTHH:MM:SS.ssszzz")
296-
props["nstime"] = string(f.nstime.value)
297-
props["tags"] = JSON2.write(f.tags)
298-
# Pack the node data
299-
fnctype = getSolverData(f).fnc.usrfnc!
294+
function _packSolverData(
295+
f::DFGFactor,
296+
fnctype::AbstractFactor;
297+
replaceBackslashes::Bool=false )
298+
#
299+
packtype = convertPackedType(fnctype)
300300
try
301-
packtype = convertPackedType(fnctype)
302-
packed = convert(PackedFunctionNodeData{packtype}, getSolverData(f))
303-
props["data"] = JSON2.write(packed)
301+
packed = convert( PackedFunctionNodeData{packtype}, getSolverData(f) )
302+
packedJson = JSON2.write(packed)
303+
if replaceBackslashes
304+
return "\"$(replace(packedJson, "\"" => "\\\""))\"" # Escape slashes too
305+
end
306+
return packedJson
304307
catch ex
305308
io = IOBuffer()
306309
showerror(io, ex, catch_backtrace())
307310
err = String(take!(io))
308311
msg = "Error while packing '$(f.label)' as '$fnctype', please check the unpacking/packing converters for this factor - \r\n$err"
309312
error(msg)
310313
end
314+
end
315+
316+
# returns ::Dict{String, <:Any}
317+
function packFactor(dfg::AbstractDFG, f::DFGFactor)
318+
# Construct the properties to save
319+
props = Dict{String, Any}()
320+
props["label"] = string(f.label)
321+
props["timestamp"] = Dates.format(f.timestamp, "yyyy-mm-ddTHH:MM:SS.ssszzz")
322+
props["nstime"] = string(f.nstime.value)
323+
props["tags"] = JSON2.write(f.tags)
324+
# Pack the node data
325+
fnctype = getSolverData(f).fnc.usrfnc!
326+
props["data"] = _packSolverData(f, fnctype)
311327
# Include the type
312328
props["fnctype"] = String(_getname(fnctype))
313329
props["_variableOrderSymbols"] = JSON2.write(f._variableOrderSymbols)

0 commit comments

Comments
 (0)