Skip to content

Commit b59f877

Browse files
authored
Consolidate FactorDFG with FactorCompute, and rebuild Factor serialization. (#1192)
1 parent efd40d0 commit b59f877

27 files changed

+636
-943
lines changed

docs/extra/CoreAPI.fods

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@
16401640
<text:p>x</text:p>
16411641
</table:table-cell>
16421642
<table:table-cell table:style-name="ce51" office:value-type="string" calcext:value-type="string">
1643-
<text:p>_variableOrderSymbols</text:p>
1643+
<text:p>variableorder</text:p>
16441644
</table:table-cell>
16451645
<table:table-cell/>
16461646
<table:table-cell table:style-name="ce101"/>

src/DataBlobs/entities/BlobEntry.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ StructUtils.@kwarg struct Blobentry
4949
version::VersionNumber = version(Blobentry)
5050
end
5151
version(::Type{Blobentry}) = v"0.1.0"
52-
version(node) = node.version
5352

5453
function Blobentry(label::Symbol, blobstore = :default; kwargs...)
5554
return Blobentry(; label, blobstore, kwargs...)
@@ -122,3 +121,5 @@ function Base.setproperty!(x::Blobentry, f::Symbol, val)
122121
setfield!(x, f, val)
123122
end
124123
end
124+
125+
const Blobentries = OrderedDict{Symbol, Blobentry}

src/Deprecated.jl

Lines changed: 149 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,153 @@
11
## ================================================================================
22
## Deprecated in v0.29
33
##=================================================================================
4+
export FactorCompute
5+
const FactorCompute = FactorDFG
6+
47
function getHash(entry::Blobentry)
58
return error(
69
"Blobentry field :hash has been deprecated; use :crchash or :shahash instead",
710
)
811
end
12+
13+
function getMetadata(node)
14+
return error(
15+
"getMetadata(node::$(typeof(node))) is deprecated; metadata is now stored in bloblets. Use getBloblets instead.",
16+
)
17+
# return JSON.parse(base64decode(f.metadata), Dict{Symbol, MetadataTypes})
18+
end
19+
20+
# getTimestamp
21+
22+
# setTimestamp is deprecated for now we can implement setTimestamp!(dfg, lbl, ts) later.
23+
function setTimestamp(args...; kwargs...)
24+
return error("setTimestamp is obsolete, use addVariable!(..., timestamp=...) instead.")
25+
end
26+
function setTimestamp!(args...; kwargs...)
27+
return error(
28+
"setTimestamp! is not implemented, use addVariable!(..., timestamp=...) instead.",
29+
)
30+
end
31+
32+
##------------------------------------------------------------------------------
33+
## solveInProgress
34+
##------------------------------------------------------------------------------
35+
36+
# getSolveInProgress and isSolveInProgress is deprecated for DFG v1.0, we can bring it back fully implemented when needed.
37+
# """
38+
# $SIGNATURES
39+
40+
# Which variables or factors are currently being used by an active solver. Useful for ensuring atomic transactions.
41+
42+
# DevNotes:
43+
# - Will be renamed to `data.solveinprogress` which will be in VND, not AbstractGraphNode -- see DFG #201
44+
45+
# Related
46+
47+
# isSolvable
48+
# """
49+
function getSolveInProgress(
50+
var::Union{VariableCompute, FactorCompute},
51+
solveKey::Symbol = :default,
52+
)
53+
# Variable
54+
if var isa VariableCompute
55+
if haskey(getSolverDataDict(var), solveKey)
56+
return getSolverDataDict(var)[solveKey].solveInProgress
57+
else
58+
return 0
59+
end
60+
end
61+
# Factor
62+
return getFactorState(var).solveInProgress
63+
end
64+
65+
#TODO missing set solveInProgress and graph level accessor
66+
67+
function isSolveInProgress(
68+
node::Union{VariableCompute, FactorCompute},
69+
solvekey::Symbol = :default,
70+
)
71+
return getSolveInProgress(node, solvekey) > 0
72+
end
73+
74+
"""
75+
$(SIGNATURES)
76+
Get a type from the serialization module.
77+
"""
78+
function getTypeFromSerializationModule(_typeString::AbstractString)
79+
@debug "DFG converting type string to Julia type" _typeString
80+
try
81+
# split the type at last `.`
82+
split_st = split(_typeString, r"\.(?!.*\.)")
83+
#if module is specified look for the module in main, otherwise use Main
84+
if length(split_st) == 2
85+
m = getfield(Main, Symbol(split_st[1]))
86+
else
87+
m = Main
88+
end
89+
noparams = split(split_st[end], r"{")
90+
ret = if 1 < length(noparams)
91+
# fix #671, but does not work with specific module yet
92+
bidx = findfirst(r"{", split_st[end])[1]
93+
error("getTypeFromSerializationModule eval obsolete")
94+
# Core.eval(m, Base.Meta.parse("$(noparams[1])$(split_st[end][bidx:end])"))
95+
else
96+
getfield(m, Symbol(split_st[end]))
97+
end
98+
99+
return ret
100+
101+
catch ex
102+
@error "Unable to deserialize type $(_typeString)"
103+
io = IOBuffer()
104+
showerror(io, ex, catch_backtrace())
105+
err = String(take!(io))
106+
@error(err)
107+
end
108+
return nothing
109+
end
110+
111+
## Version checking
112+
#NOTE fixed really bad function but kept similar as fallback #TODO upgrade to use pkgversion(m::Module)
113+
function _getDFGVersion()
114+
return pkgversion(DistributedFactorGraphs)
115+
end
116+
117+
function _versionCheck(node::Union{<:VariableDFG, <:FactorDFG})
118+
if node._version.minor < _getDFGVersion().minor
119+
@warn "This data was serialized using DFG $(node._version) but you have $(_getDFGVersion()) installed, there may be deserialization issues." maxlog =
120+
10
121+
end
122+
end
123+
124+
refMetadata(node) = node.metadata
125+
126+
function packDistribution end
127+
function unpackDistribution end
128+
129+
getAgentMetadata(args...) = error("getAgentMetadata is obsolete, use Bloblets instead.")
130+
setAgentMetadata!(args...) = error("setAgentMetadata! is obsolete, use Bloblets instead.")
131+
getGraphMetadata(args...) = error("getGraphMetadata is obsolete, use Bloblets instead.")
132+
setGraphMetadata!(args...) = error("setGraphMetadata! is obsolete, use Bloblets instead.")
133+
134+
function setDescription!(args...)
135+
return error("setDescription! was removed and may be implemented later.")
136+
end
137+
138+
# TODO find replacement.
139+
function _getDuplicatedEmptyDFG(
140+
dfg::GraphsDFG{P, V, F},
141+
) where {P <: AbstractDFGParams, V <: AbstractGraphVariable, F <: AbstractGraphFactor}
142+
Base.depwarn("_getDuplicatedEmptyDFG is deprecated.", :_getDuplicatedEmptyDFG)
143+
newDfg = GraphsDFG{P, V, F}(;
144+
agentLabel = getAgentLabel(dfg),
145+
graphLabel = getGraphLabel(dfg),
146+
solverParams = deepcopy(dfg.solverParams),
147+
)
148+
# DFG.setDescription!(newDfg, "(Copy of) $(DFG.getDescription(dfg))")
149+
return newDfg
150+
end
9151
## ================================================================================
10152
## Deprecated in v0.28
11153
##=================================================================================
@@ -124,18 +266,19 @@ function cloneSolveKey!(dfg::AbstractDFG, dest::Symbol, src::Symbol; kw...)
124266
return cloneSolveKey!(dfg, dest, dfg, src; kw...)
125267
end
126268

127-
#TODO not a good function, as it's not complete.
269+
#TODO make a replacement if used a lot... not a good function, as it's not complete.
128270
# """
129271
# $(SIGNATURES)
130272
# Convenience function to get all the metadata of a DFG
131273
# """
132274
function getDFGInfo(dfg::AbstractDFG)
275+
Base.depwarn("getDFGInfo is deprecated and needs a replacement.", :getDFGInfo)
133276
return (
134-
description = getDescription(dfg),
277+
graphDescription = getDescription(dfg),
135278
agentLabel = getAgentLabel(dfg),
136279
graphLabel = getGraphLabel(dfg),
137-
agentMetadata = getAgentMetadata(dfg),
138-
graphMetadata = getGraphMetadata(dfg),
280+
# agentBloblets = getAgentBloblets(dfg),
281+
# graphBloblets = getGraphBloblets(dfg),
139282
solverParams = getSolverParams(dfg),
140283
)
141284
end
@@ -350,16 +493,7 @@ end
350493
@deprecate getFactorFunction(args...) getObservation(args...)
351494
@deprecate getFactorType(args...) getObservation(args...)
352495

353-
#TODO maybe deprecate setMetadata!
354-
function setMetadata!(v::VariableDFG, metadata::Dict{Symbol, MetadataTypes})
355-
return error("FIXME: Metadata is not currently mutable in a Variable")
356-
# v.metadata = base64encode(JSON3.write(metadata))
357-
end
358-
359-
function setMetadata!(v::VariableCompute, metadata::Dict{Symbol, MetadataTypes})
360-
v.smallData !== metadata && empty!(v.smallData)
361-
return merge!(v.smallData, metadata)
362-
end
496+
setMetadata!(args...) = error("setMetadata is obsolete, use Bloblets instead.")
363497

364498
function updateData!(
365499
dfg::AbstractDFG,
@@ -999,7 +1133,7 @@ end
9991133

10001134
# # Deprecated check usefull? # packedFnc = fncStringToData(factor.fnctype, factor.data)
10011135
# # Deprecated check usefull? # decodeType = getFactorOperationalMemoryType(dfg)
1002-
# # Deprecated check usefull? # fullFactorData = decodePackedType(dfg, factor._variableOrderSymbols, decodeType, packedFnc)
1136+
# # Deprecated check usefull? # fullFactorData = decodePackedType(dfg, factor.variableorder, decodeType, packedFnc)
10031137
# function fncStringToData(args...; kwargs...)
10041138
# @warn "fncStringToData is obsolete, called with" args kwargs
10051139
# return error("fncStringToData is obsolete.")

src/DistributedFactorGraphs.jl

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ using Random
2222
using TimeZones
2323
using NanoDates
2424
using JSON
25+
export StructUtils # export for use in macros
2526
using LinearAlgebra
2627
using SparseArrays
2728
using UUIDs
@@ -31,7 +32,7 @@ using ProgressMeter
3132
using SHA
3233
using FileIO
3334

34-
import Distributions
35+
import Distributions #TODO this was unused before (if we move SerializingDistributions.jl out we can maybe remove the Distributions dependency?)
3536
import Tar
3637
import CodecZlib
3738

@@ -78,7 +79,7 @@ export AbstractPackedBelief, PackedBelief
7879
# Variables
7980
export VariableCompute, VariableDFG, VariableSummary, VariableSkeleton
8081
# Factors
81-
export FactorCompute, FactorDFG, FactorSummary, FactorSkeleton
82+
export FactorDFG, FactorSummary, FactorSkeleton
8283

8384
export Blobentry
8485

@@ -274,16 +275,17 @@ export removeTags! #TODO do we want this one
274275
export hasTags
275276

276277
##------------------------------------------------------------------------------
277-
## Metadata
278+
## Bloblets
278279
##------------------------------------------------------------------------------
279-
# currently these refer to variable metadata
280-
export getMetadata
281-
export addMetadata!
282-
export deleteMetadata!
283-
export listMetadata
280+
# currently these refer to variable Bloblets
281+
#TODO Bloblet CRUD
282+
# export getVariableBloblet
283+
# export addVariableBloblet!
284+
# export deleteVariableBloblet!
285+
# export listVariableBloblets
284286

285-
export getAgentMetadata
286-
export getGraphMetadata
287+
# export getAgentBloblet
288+
# export getGraphBloblet
287289

288290
##------------------------------------------------------------------------------
289291
## FileDFG
@@ -396,13 +398,13 @@ const unstable_functions::Vector{Symbol} = [
396398
:pack,
397399
:packDistribution,
398400
:packVariable,
399-
:packFactor,
401+
# :packFactor,
400402
:packBlob,
401403
:packState,
402404
:unpack,
403405
:unpackDistribution,
404406
:unpackVariable,
405-
:unpackFactor,
407+
# :unpackFactor,
406408
:unpackBlob,
407409
:unpackState,
408410
:ls2,
@@ -421,11 +423,13 @@ const unstable_functions::Vector{Symbol} = [
421423
:setSolverParams!,
422424
:setDescription!,
423425
:setSolvable!,
424-
:setTimestamp,
425426
:setTags!,
426427
:setSolvedCount!,
427428
:setMarginalized!,
428429
# no set on these
430+
431+
#deprecated in v0.29
432+
:setTimestamp,
429433
:setMetadata!, # no set, use add merge
430434
:setAgentMetadata!,
431435
:setGraphMetadata!,
@@ -527,7 +531,11 @@ include("entities/Bloblet.jl")
527531
include("DataBlobs/entities/BlobEntry.jl")
528532
include("DataBlobs/entities/BlobStores.jl")
529533

534+
include("serialization/PackedSerialization.jl")
535+
include("serialization/DistributionSerialization.jl")
536+
530537
include("entities/DFGFactor.jl")
538+
# include("serialization/FactorSerialization.jl")
531539

532540
include("entities/DFGVariable.jl")
533541

src/FileDFG/services/FileDFG.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ function saveDFG(folder::AbstractString, dfg::AbstractDFG; saveMetadata::Bool =
4545
end
4646
# Factors
4747
@showprogress "saving factors" for f in factors
48-
fPacked = packFactor(f)
49-
JSON.json("$factorFolder/$(f.label).json", fPacked)
48+
JSON.json("$factorFolder/$(f.label).json", f)
5049
end
5150
#GraphsDFG metadata
5251
if saveMetadata
@@ -144,7 +143,7 @@ function loadDFG!(
144143
end
145144

146145
# extract the factor graph from fileDFG folder
147-
factors = FactorCompute[]
146+
factors = FactorDFG[]
148147
varFolder = "$folder/variables"
149148
factorFolder = "$folder/factors"
150149
# Folder preparations
@@ -177,9 +176,7 @@ function loadDFG!(
177176

178177
# `factors` is not type stable `::Vector{Factor}` or `::Vector{FactorCompute{<:}}` (vector of abstract)
179178
factors = @showprogress 1 "loading factors" asyncmap(factorFiles) do factorFile
180-
jstr = read("$factorFolder/$factorFile", String)
181-
packedfact = JSON.parse(jstr, FactorDFG)
182-
f = usePackedFactor ? packedfact : unpackFactor(packedfact)
179+
f = JSON.parsefile("$factorFolder/$factorFile", FactorDFG)
183180
return addFactor!(dfgLoadInto, f)
184181
end
185182

src/GraphsDFG/GraphsDFG.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,15 @@ using ...DistributedFactorGraphs:
2121
getAgentLabel,
2222
getGraphLabel,
2323
isInitialized,
24-
MetadataTypes
24+
MetadataTypes,
25+
Bloblets,
26+
Blobentries
2527

2628
# import DFG functions to extend
2729
import ...DistributedFactorGraphs:
2830
setSolverParams!,
2931
getFactor,
3032
# getLabelDict,
31-
getAgentMetadata,
32-
setAgentMetadata!,
33-
getGraphMetadata,
34-
setGraphMetadata!,
3533
addVariable!,
3634
getVariable,
3735
getAddHistory,
@@ -56,7 +54,6 @@ import ...DistributedFactorGraphs:
5654
buildSubgraph,
5755
copyGraph!,
5856
getBiadjacencyMatrix,
59-
_getDuplicatedEmptyDFG,
6057
toDot,
6158
toDotFile,
6259
findShortestPathDijkstra,

0 commit comments

Comments
 (0)