@@ -9,7 +9,6 @@ import JSON.Writer: StructuralContext, JSONContext, show_json
9
9
import JSON. Serializations: CommonSerialization, StandardSerialization
10
10
JSON. show_json (io:: JSONContext , serialization:: CommonSerialization , uuid:: UUID ) = print (io. io, " \" $uuid \" " )
11
11
12
-
13
12
# # Utility functions for ZonedDateTime
14
13
15
14
# Regex parser that converts clauses like ":59.82-" to well formatted ":59.820-"
@@ -40,6 +39,43 @@ function standardizeZDTStrings!(T, interm::Dict)
40
39
nothing
41
40
end
42
41
42
+ function string2ZonedDateTime (stringTimestamp)
43
+ # ss = split(stringTimestamp, r"(T[0-9.:]*?\K(?=[-+Zz]))|[\[\]]")
44
+ ss = split (stringTimestamp, r" T[\d .:]{5,12}?\K (?=[-+Zz])" )
45
+ length (ss) != 2 && error (" Misformed zoned timestamp string $stringTimestamp " )
46
+ ZonedDateTime (DateTime (ss[1 ]), TimeZone (ss[2 ]))
47
+ end
48
+
49
+ # Softtype module.type string functions
50
+ function typeModuleName (softtype:: InferenceVariable )
51
+ io = IOBuffer ()
52
+ ioc = IOContext (io, :module => DistributedFactorGraphs)
53
+ show (ioc, typeof (softtype))
54
+ return String (take! (io))
55
+ end
56
+
57
+ function getTypeFromSerializationModule (softtypeString:: String )
58
+ try
59
+ # split the type at last `.`
60
+ split_st = split (softtypeString, r" \. (?!.*\. )" )
61
+ # if module is specified look for the module in main, otherwise use Main
62
+ if length (split_st) == 2
63
+ m = getfield (Main, Symbol (split_st[1 ]))
64
+ else
65
+ m = Main
66
+ end
67
+ return getfield (m, Symbol (split_st[end ]))
68
+
69
+ catch ex
70
+ @error " Unable to deserialize soft type $(softtypeString) "
71
+ io = IOBuffer ()
72
+ showerror (io, ex, catch_backtrace ())
73
+ err = String (take! (io))
74
+ @error (err)
75
+ end
76
+ nothing
77
+ end
78
+
43
79
# #==============================================================================
44
80
# # Variable Packing and unpacking
45
81
# #==============================================================================
@@ -53,9 +89,8 @@ function packVariable(dfg::G, v::DFGVariable)::Dict{String, Any} where G <: Abst
53
89
props[" solverDataDict" ] = JSON2. write (Dict (keys (v. solverDataDict) .=> map (vnd -> packVariableNodeData (dfg, vnd), values (v. solverDataDict))))
54
90
props[" smallData" ] = JSON2. write (v. smallData)
55
91
props[" solvable" ] = v. solvable
56
- props[" softtype" ] = string ( typeof ( getSofttype (v) ))
92
+ props[" softtype" ] = typeModuleName ( getSofttype (v))
57
93
props[" dataEntry" ] = JSON2. write (Dict (keys (v. dataDict) .=> map (bde -> JSON. json (bde), values (v. dataDict))))
58
-
59
94
props[" dataEntryType" ] = JSON2. write (Dict (keys (v. dataDict) .=> map (bde -> typeof (bde), values (v. dataDict))))
60
95
return props
61
96
end
@@ -82,8 +117,8 @@ function unpackVariable(dfg::G,
82
117
smallData = JSON2. read (packedProps[" smallData" ], Dict{Symbol, SmallDataTypes})
83
118
84
119
softtypeString = packedProps[" softtype" ]
85
- softtype = getTypeFromSerializationModule (dfg, Symbol ( softtypeString) )
86
- softtype == nothing && error (" Cannot deserialize softtype '$softtypeString ' in variable '$label '" )
120
+ softtype = getTypeFromSerializationModule (softtypeString)
121
+ isnothing ( softtype) && error (" Cannot deserialize softtype '$softtypeString ' in variable '$label '" )
87
122
88
123
if unpackSolverData
89
124
packed = JSON2. read (packedProps[" solverDataDict" ], Dict{String, PackedVariableNodeData})
@@ -134,7 +169,7 @@ function packVariableNodeData(dfg::G, d::VariableNodeData)::PackedVariableNodeDa
134
169
d. BayesNetOutVertIDs,
135
170
d. dimIDs, d. dims, d. eliminated,
136
171
d. BayesNetVertID, d. separator,
137
- d . softtype != nothing ? string (d. softtype) : nothing ,
172
+ typeModuleName (d. softtype),
138
173
d. initialized,
139
174
d. inferdim,
140
175
d. ismargin,
@@ -153,14 +188,10 @@ function unpackVariableNodeData(dfg::G, d::PackedVariableNodeData)::VariableNode
153
188
c4 = r4 > 0 ? floor (Int,length (d. vecbw)/ r4) : 0
154
189
M4 = reshape (d. vecbw,r4,c4)
155
190
156
- # TODO -- allow out of module type allocation (future feature, not currently in use)
157
191
@debug " Dispatching conversion packed variable -> variable for type $(string (d. softtype)) "
158
192
# Figuring out the softtype
159
- unpackedTypeName = split (d. softtype, " (" )[1 ]
160
- unpackedTypeName = split (unpackedTypeName, ' .' )[end ]
161
- @debug " DECODING Softtype = $unpackedTypeName "
162
- st = getTypeFromSerializationModule (dfg, Symbol (unpackedTypeName))
163
- 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()." )
193
+ st = getTypeFromSerializationModule (d. softtype)
194
+ 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()." )
164
195
165
196
return VariableNodeData {st} (M3,M4, d. BayesNetOutVertIDs,
166
197
d. dimIDs, d. dims, d. eliminated, d. BayesNetVertID, d. separator,
0 commit comments