Skip to content

Commit ee04f00

Browse files
authored
Updates for serialization needed for IIFTypes (#1142)
Co-authored-by: Johannes Terblanche <[email protected]>
1 parent 94835a0 commit ee04f00

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

src/Deprecated.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ const VariableNodeData = VariableState
9090
solveKey,
9191
)
9292

93+
@deprecate packVariableNodeData(args...; kwargs...) packVariableState(args...; kwargs...)
94+
@deprecate unpackVariableNodeData(args...; kwargs...) unpackVariableState(
95+
args...;
96+
kwargs...,
97+
)
98+
9399
export updateVariableSolverData!
94100

95101
#TODO possibly completely deprecated or not exported until update verb is standardized

src/DistributedFactorGraphs.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ using StaticArrays
5252

5353
import Base: getindex
5454

55+
using InteractiveUtils: subtypes
56+
5557
##==============================================================================
5658
# Exports
5759
##==============================================================================

src/services/AbstractDFG.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ function rebuildFactorCache!(
121121
factor::AbstractDFGFactor,
122122
neighbors = [],
123123
)
124-
return error("rebuildFactorCache! is not implemented for $(typeof(dfg))")
124+
@warn("rebuildFactorCache! is not implemented for $(typeof(dfg))")
125+
return nothing
125126
end
126127

127128
"""

src/services/DFGFactor.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,15 @@ that contains the information about the factor, such as the measurement, prior,
4848
getObservation(f::FactorCompute) = f.observation
4949
function getObservation(f::FactorDFG)
5050
#FIXME completely refactor to not need getTypeFromSerializationModule and just use StructTypes
51-
packtype = DFG.getTypeFromSerializationModule("Packed" * f.fnctype)
51+
52+
if contains(f.fnctype, ".")
53+
# packed factor contains a module name, just extracting type and ignoring module
54+
fnctype = split(f.fnctype, ".")[end]
55+
else
56+
fnctype = f.fnctype
57+
end
58+
59+
packtype = DFG.getTypeFromSerializationModule("Packed" * fnctype)
5260
return packtype(; JSON3.read(f.observJSON)...)
5361
# return packtype(JSON3.read(f.observJSON))
5462
end

src/services/Serialization.jl

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ end
4040

4141
typeModuleName(varT::Type{<:InferenceVariable}) = typeModuleName(varT())
4242

43+
function parseVariableType(_typeString::AbstractString)
44+
m = match(r"{(\d+)}", _typeString)
45+
if !isnothing(m) #parameters in type
46+
param = parse(Int, m[1])
47+
typeString = _typeString[1:(m.offset - 1)]
48+
else
49+
param = nothing
50+
typeString = _typeString
51+
end
52+
53+
all_subtypes = Dict(map(s -> nameof(s) => s, subtypes(InferenceVariable)))
54+
55+
subtype = get(all_subtypes, Symbol(split(typeString, ".")[end]), nothing)
56+
57+
if isnothing(subtype)
58+
error("Unable to deserialize type $(_typeString), not found")
59+
return nothing
60+
end
61+
62+
if isnothing(param)
63+
# no parameters, just return the type
64+
return subtype
65+
else
66+
# return the type with parameters
67+
return subtype{param}
68+
end
69+
end
70+
4371
"""
4472
$(SIGNATURES)
4573
Get a type from the serialization module.
@@ -123,7 +151,7 @@ function unpackVariableState(d::PackedVariableState)
123151
# Figuring out the variableType
124152
# TODO deprecated remove in v0.11 - for backward compatibility for saved variableTypes.
125153
ststring = string(split(d.variableType, "(")[1])
126-
T = getTypeFromSerializationModule(ststring)
154+
T = parseVariableType(ststring)
127155
isnothing(T) && error(
128156
"The variable doesn't seem to have a variableType. It needs to set up with an InferenceVariable from IIF. This will happen if you use DFG to add serialized variables directly and try use them. Please use IncrementalInference.addVariable().",
129157
)
@@ -206,7 +234,7 @@ function unpackVariable(variable::VariableDFG; skipVersionCheck::Bool = false)
206234
!skipVersionCheck && _versionCheck(variable)
207235

208236
# Variable and point type
209-
variableType = DFG.getTypeFromSerializationModule(variable.variableType)
237+
variableType = parseVariableType(variable.variableType)
210238
isnothing(variableType) && error(
211239
"Cannot deserialize variableType '$(variable.variableType)' in variable '$(variable.label)'",
212240
)
@@ -249,15 +277,19 @@ VariableDFG(v::VariableCompute) = packVariable(v)
249277

250278
# returns FactorDFG
251279
function packFactor(f::FactorCompute)
252-
fnctype = getObservation(f)
280+
obstype = typeof(getObservation(f))
281+
fnctype = string(parentmodule(obstype), ".", nameof(obstype))
282+
253283
return FactorDFG(;
254284
id = f.id,
255285
label = f.label,
256286
tags = f.tags,
257287
_variableOrderSymbols = f._variableOrderSymbols,
258288
timestamp = f.timestamp,
259289
nstime = string(f.nstime.value),
260-
fnctype = String(_getname(fnctype)),
290+
#TODO fully test include module name in factor fnctype, see #1140
291+
# fnctype = String(_getname(getObservation(f))),
292+
fnctype,
261293
solvable = getSolvable(f),
262294
metadata = base64encode(JSON3.write(f.smallData)),
263295
# Pack the node data
@@ -284,8 +316,7 @@ function unpackObservation(factor::FactorDFG)
284316
#TODO change to unpack: observ = unpack(observpacked)
285317
# currently the observation type is stored in the factor and this complicates unpacking of seperate observations
286318
observpacked = getObservation(factor)
287-
packtype = DFG.getTypeFromSerializationModule("Packed" * factor.fnctype)
288-
return convert(convertStructType(packtype), observpacked)
319+
return convert(convertStructType(typeof(observpacked)), observpacked)
289320
else
290321
rethrow()
291322
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ if get(ENV, "IIF_TEST", "true") == "true"
6464
Pkg.add(
6565
#FIXME This is a temporary fix to use the refactored factor branch.
6666
# PackageSpec(; name = "IncrementalInference", rev = "upstream/dfg_integration_test"),
67-
PackageSpec(; name = "IncrementalInference", rev = "refac/factor"),
67+
PackageSpec(; name = "IncrementalInference", rev = "develop"),
6868
)
6969
@info "------------------------------------------------------------------------"
7070
@info "These tests are using IncrementalInference to do additional driver tests"

0 commit comments

Comments
 (0)