@@ -10,6 +10,34 @@ import JSON.Serializations: CommonSerialization, StandardSerialization
10
10
JSON. show_json (io:: JSONContext , serialization:: CommonSerialization , uuid:: UUID ) = print (io. io, " \" $uuid \" " )
11
11
12
12
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
13
41
# #==============================================================================
14
42
# # Variable Packing and unpacking
15
43
# #==============================================================================
@@ -23,9 +51,7 @@ function packVariable(dfg::G, v::DFGVariable)::Dict{String, Any} where G <: Abst
23
51
props[" solverDataDict" ] = JSON2. write (Dict (keys (v. solverDataDict) .=> map (vnd -> packVariableNodeData (dfg, vnd), values (v. solverDataDict))))
24
52
props[" smallData" ] = JSON2. write (v. smallData)
25
53
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))
29
55
props[" dataEntry" ] = JSON2. write (Dict (keys (v. dataDict) .=> map (bde -> JSON. json (bde), values (v. dataDict))))
30
56
31
57
props[" dataEntryType" ] = JSON2. write (Dict (keys (v. dataDict) .=> map (bde -> typeof (bde), values (v. dataDict))))
@@ -88,8 +114,8 @@ function unpackVariable(dfg::G,
88
114
smallData = JSON2. read (packedProps[" smallData" ], Dict{Symbol, SmallDataTypes})
89
115
90
116
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 '" )
93
119
94
120
if unpackSolverData
95
121
packed = JSON2. read (packedProps[" solverDataDict" ], Dict{String, PackedVariableNodeData})
@@ -139,7 +165,7 @@ function packVariableNodeData(dfg::G, d::VariableNodeData)::PackedVariableNodeDa
139
165
d. BayesNetOutVertIDs,
140
166
d. dimIDs, d. dims, d. eliminated,
141
167
d. BayesNetVertID, d. separator,
142
- d . softtype != nothing ? string (d. softtype) : nothing ,
168
+ typeModuleName (d. softtype),
143
169
d. initialized,
144
170
d. inferdim,
145
171
d. ismargin,
@@ -158,14 +184,10 @@ function unpackVariableNodeData(dfg::G, d::PackedVariableNodeData)::VariableNode
158
184
c4 = r4 > 0 ? floor (Int,length (d. vecbw)/ r4) : 0
159
185
M4 = reshape (d. vecbw,r4,c4)
160
186
161
- # TODO -- allow out of module type allocation (future feature, not currently in use)
162
187
@debug " Dispatching conversion packed variable -> variable for type $(string (d. softtype)) "
163
188
# 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()." )
169
191
170
192
return VariableNodeData {st} (M3,M4, d. BayesNetOutVertIDs,
171
193
d. dimIDs, d. dims, d. eliminated, d. BayesNetVertID, d. separator,
0 commit comments