Skip to content

Commit e045162

Browse files
committed
Save/load module with softtype
1 parent 211af7c commit e045162

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

src/services/Serialization.jl

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@ import JSON.Serializations: CommonSerialization, StandardSerialization
1010
JSON.show_json(io::JSONContext, serialization::CommonSerialization, uuid::UUID) = print(io.io, "\"$uuid\"")
1111

1212

13+
function typeModuleName(softtype::InferenceVariable)
14+
io = IOBuffer()
15+
ioc = IOContext(io, :module=>DistributedFactorGraphs)
16+
show(ioc, typeof(softtype))
17+
return String(take!(io))
18+
end
19+
20+
function getTypeFromSerializationModule(softtypeString::String)
21+
try
22+
# split the type at last `.`
23+
split_st = split(softtypeString, r"\.(?!.*\.)")
24+
#if module is specified look for the module in main, otherwise use Main
25+
if length(split_st) == 2
26+
m = getfield(Main, Symbol(split_st[1]))
27+
else
28+
m = Main
29+
end
30+
return getfield(m, Symbol(split_st[end]))
31+
32+
catch ex
33+
@error "Unable to deserialize soft type $(softtypeString)"
34+
io = IOBuffer()
35+
showerror(io, ex, catch_backtrace())
36+
err = String(take!(io))
37+
@error(err)
38+
end
39+
nothing
40+
end
1341
##==============================================================================
1442
## Variable Packing and unpacking
1543
##==============================================================================
@@ -23,9 +51,7 @@ function packVariable(dfg::G, v::DFGVariable)::Dict{String, Any} where G <: Abst
2351
props["solverDataDict"] = JSON2.write(Dict(keys(v.solverDataDict) .=> map(vnd -> packVariableNodeData(dfg, vnd), values(v.solverDataDict))))
2452
props["smallData"] = JSON2.write(v.smallData)
2553
props["solvable"] = v.solvable
26-
props["softtype"] = string(typeof(getSofttype(v)))
27-
# props["bigData"] = JSON2.write(Dict(keys(v.dataDict) .=> map(bde -> JSON2.write(bde), values(v.dataDict))))
28-
# props["bigDataElemType"] = JSON2.write(Dict(keys(v.dataDict) .=> map(bde -> typeof(bde), values(v.dataDict))))
54+
props["softtype"] = typeModuleName(getSofttype(v))
2955
props["dataEntry"] = JSON2.write(Dict(keys(v.dataDict) .=> map(bde -> JSON.json(bde), values(v.dataDict))))
3056

3157
props["dataEntryType"] = JSON2.write(Dict(keys(v.dataDict) .=> map(bde -> typeof(bde), values(v.dataDict))))
@@ -88,8 +114,8 @@ function unpackVariable(dfg::G,
88114
smallData = JSON2.read(packedProps["smallData"], Dict{Symbol, SmallDataTypes})
89115

90116
softtypeString = packedProps["softtype"]
91-
softtype = getTypeFromSerializationModule(dfg, Symbol(softtypeString))
92-
softtype == nothing && error("Cannot deserialize softtype '$softtypeString' in variable '$label'")
117+
softtype = getTypeFromSerializationModule(softtypeString)
118+
isnothing(softtype) && error("Cannot deserialize softtype '$softtypeString' in variable '$label'")
93119

94120
if unpackSolverData
95121
packed = JSON2.read(packedProps["solverDataDict"], Dict{String, PackedVariableNodeData})
@@ -139,7 +165,7 @@ function packVariableNodeData(dfg::G, d::VariableNodeData)::PackedVariableNodeDa
139165
d.BayesNetOutVertIDs,
140166
d.dimIDs, d.dims, d.eliminated,
141167
d.BayesNetVertID, d.separator,
142-
d.softtype != nothing ? string(d.softtype) : nothing,
168+
typeModuleName(d.softtype),
143169
d.initialized,
144170
d.inferdim,
145171
d.ismargin,
@@ -158,14 +184,10 @@ function unpackVariableNodeData(dfg::G, d::PackedVariableNodeData)::VariableNode
158184
c4 = r4 > 0 ? floor(Int,length(d.vecbw)/r4) : 0
159185
M4 = reshape(d.vecbw,r4,c4)
160186

161-
# TODO -- allow out of module type allocation (future feature, not currently in use)
162187
@debug "Dispatching conversion packed variable -> variable for type $(string(d.softtype))"
163188
# Figuring out the softtype
164-
unpackedTypeName = split(d.softtype, "(")[1]
165-
unpackedTypeName = split(unpackedTypeName, '.')[end]
166-
@debug "DECODING Softtype = $unpackedTypeName"
167-
st = getTypeFromSerializationModule(dfg, Symbol(unpackedTypeName))
168-
st == nothing && error("The variable doesn't seem to have a softtype. 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().")
189+
st = getTypeFromSerializationModule(d.softtype)
190+
isnothing(st) && error("The variable doesn't seem to have a softtype. 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().")
169191

170192
return VariableNodeData{st}(M3,M4, d.BayesNetOutVertIDs,
171193
d.dimIDs, d.dims, d.eliminated, d.BayesNetVertID, d.separator,

0 commit comments

Comments
 (0)