-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphsDFGSerialization.jl
More file actions
107 lines (90 loc) · 3.73 KB
/
GraphsDFGSerialization.jl
File metadata and controls
107 lines (90 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using InteractiveUtils
@kwdef struct PackedGraphsDFG{T <: AbstractParams}
description::String
addHistory::Vector{Symbol}
solverParams::T
solverParams_type::String = string(nameof(typeof(solverParams)))
typePackedVariable::Bool = false # Are variables packed or full
typePackedFactor::Bool = false # Are factors packed or full
blobStores::Union{Nothing, Dict{Symbol, FolderStore{Vector{UInt8}}}}
graphLabel::Symbol
graphTags::Vector{Symbol}
graphMetadata::Dict{Symbol, SmallDataTypes}
graphBlobEntries::OrderedDict{Symbol, Blobentry}
agent::Agent
end
StructTypes.StructType(::Type{PackedGraphsDFG}) = StructTypes.AbstractType()
StructTypes.subtypekey(::Type{PackedGraphsDFG}) = :solverParams_type
#TODO look at StructTypes.@register_struct_subtype when new StructTypes.jl is tagged (for type field)
function StructTypes.subtypes(::Type{PackedGraphsDFG})
subs = subtypes(AbstractParams)
return NamedTuple(map(s -> nameof(s) => PackedGraphsDFG{s}, subs))
end
getTypeDFGVariables(fg::GraphsDFG{<:AbstractParams, T, <:AbstractDFGFactor}) where {T} = T
getTypeDFGFactors(fg::GraphsDFG{<:AbstractParams, <:AbstractDFGVariable, T}) where {T} = T
##
"""
$(SIGNATURES)
Packing function to serialize DFG metadata from.
"""
function packDFGMetadata(fg::GraphsDFG)
commonfields = intersect(fieldnames(PackedGraphsDFG), fieldnames(GraphsDFG))
setdiff!(commonfields, [:blobStores])
blobStores = Dict{Symbol, FolderStore{Vector{UInt8}}}()
foreach(values(fg.blobStores)) do store
if store isa FolderStore{Vector{UInt8}}
blobStores[getLabel(store)] = store
else
@warn "Blobstore $(getLabel(store)) of type $(typeof(store)) is not supported yet and will not be saved"
end
end
props = (k => getproperty(fg, k) for k in commonfields)
return PackedGraphsDFG(;
typePackedVariable = getTypeDFGVariables(fg) == VariableDFG,
typePackedFactor = getTypeDFGFactors(fg) == FactorDFG,
blobStores,
props...,
)
end
function unpackDFGMetadata(packed::PackedGraphsDFG)
commonfields = intersect(fieldnames(GraphsDFG), fieldnames(PackedGraphsDFG))
setdiff!(commonfields, [:blobStores])
blobStores = packed.blobStores
#TODO add 'CanSerialize' trait to blobstores and also serialize NvaBlobStores
_isfolderstorepath(s) = false
_isfolderstorepath(s::FolderStore) = ispath(s.folder)
# FIXME escalate to keyword
for (ks, bs) in blobStores
if !_isfolderstorepath(bs)
delete!(blobStores, ks)
@warn("Unable to load blobstore, $ks from $(bs.folder)")
end
end
props = (k => getproperty(packed, k) for k in commonfields)
VT = if isnothing(packed.typePackedVariable) || !packed.typePackedVariable
VariableCompute
else
VariableDFG
end
FT = if isnothing(packed.typePackedFactor) || !packed.typePackedFactor
FactorCompute
else
FactorDFG
end
# VT = isnothing(packed.typePackedVariable) || packed.typePackedVariable ? Variable : VariableCompute
# FT = isnothing(packed.typePackedFactor) || packed.typePackedFactor ? FactorDFG : FactorCompute
props = filter!(collect(props)) do (k, v)
return !isnothing(v)
end
return GraphsDFG{typeof(packed.solverParams), VT, FT}(; blobStores, props...)
end
function unpackDFGMetadata!(dfg::GraphsDFG, packed::PackedGraphsDFG)
commonfields = intersect(fieldnames(GraphsDFG), fieldnames(PackedGraphsDFG))
setdiff!(commonfields, [:blobStores])
!isnothing(packed.blobStores) && merge!(dfg.blobStores, packed.blobStores)
props = (k => getproperty(packed, k) for k in commonfields)
foreach(props) do (k, v)
return setproperty!(dfg, k, v)
end
return dfg
end