Skip to content

Commit 3549c4d

Browse files
authored
Merge pull request #10 from JuliaRobotics/feature/variablefactordefinition
Feature/variablefactordefinition
2 parents a541f65 + 1dcc191 commit 3549c4d

File tree

6 files changed

+123
-18
lines changed

6 files changed

+123
-18
lines changed

REQUIRE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ DocStringExtensions 0.4.1
55
Requires 0.5.2
66
JSON2
77
Graphs
8+
# Caesar/IIF specific imports
9+
Distributions
10+
Dates

src/DistributedFactorGraphs.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module DistributedFactorGraphs
33
using Base
44
using DocStringExtensions
55
using Requires
6+
using Dates
7+
using Distributions
68

79
# Entities
810
include("entities/AbstractTypes.jl")
@@ -13,6 +15,8 @@ export AbstractDFG
1315
export DFGNode
1416
export DFGFactor
1517
export DFGVariable
18+
export label, timestamp, tags, estimates, estimate, solverData, solverDataDict, id, smallData, bigData
19+
export label, data, id
1620

1721
# Include the Graphs.jl API.
1822
include("services/GraphsDFG.jl")

src/entities/DFGFactor.jl

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1+
"""
2+
$(TYPEDEF)
3+
"""
4+
mutable struct GenericFunctionNodeData{T, S}
5+
fncargvID::Array{Int,1}
6+
eliminated::Bool
7+
potentialused::Bool
8+
edgeIDs::Array{Int,1}
9+
frommodule::S #Union{Symbol, AbstractString}
10+
fnc::T
11+
multihypo::String # likely to moved when GenericWrapParam is refactored
12+
certainhypo::Vector{Int}
13+
GenericFunctionNodeData{T, S}() where {T, S} = new{T,S}()
14+
GenericFunctionNodeData{T, S}(x1, x2, x3, x4, x5::S, x6::T, x7::String="", x8::Vector{Int}=Int[]) where {T, S} = new{T,S}(x1, x2, x3, x4, x5, x6, x7, x8)
15+
GenericFunctionNodeData(x1, x2, x3, x4, x5::S, x6::T, x7::String="", x8::Vector{Int}=Int[]) where {T, S} = new{T,S}(x1, x2, x3, x4, x5, x6, x7, x8)
16+
# GenericFunctionNodeData(x1, x2, x3, x4, x5::S, x6::T, x7::String) where {T, S} = new{T,S}(x1, x2, x3, x4, x5, x6, x7)
17+
end
18+
119
"""
220
$(SIGNATURES)
321
Fundamental structure for a DFG factor.
422
"""
5-
mutable struct DFGFactor <: DFGNode
23+
mutable struct DFGFactor{T, S} <: DFGNode
624
label::Symbol
7-
#TODO: Populate
25+
data::GenericFunctionNodeData{T, S}
826
_internalId::Int64
9-
DFGFactor(label::Symbol) = new(label, 0)
10-
DFGFactor(label::Symbol, _internalId::Int64) = new(label, _internalId)
27+
DFGFactor{T, S}(label::Symbol) where {T, S} = new{T, S}(label, GenericFunctionNodeData{T, S}(), 0)
28+
DFGFactor{T, S}(label::Symbol, _internalId::Int64) where {T, S} = new{T, S}(label, GenericFunctionNodeData{T, S}(), _internalId)
1129
end
30+
31+
label(f::F) where F <: DFGFactor = f.label
32+
data(f::F) where F <: DFGFactor = f.data
33+
id(f::F) where F <: DFGFactor = f._internalId

src/entities/DFGVariable.jl

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,74 @@
1+
"""
2+
$(TYPEDEF)
3+
"""
4+
mutable struct VariableNodeData
5+
val::Array{Float64,2}
6+
bw::Array{Float64,2}
7+
BayesNetOutVertIDs::Array{Int,1}
8+
dimIDs::Array{Int,1} # Likely deprecate
9+
dims::Int
10+
eliminated::Bool
11+
BayesNetVertID::Int
12+
separator::Array{Int,1}
13+
groundtruth::Union{Nothing, Dict{ Tuple{Symbol, Vector{Float64}} } } # not packed yet
14+
softtype
15+
initialized::Bool
16+
ismargin::Bool
17+
dontmargin::Bool
18+
VariableNodeData() = new()
19+
# function VariableNodeData(x1,x2,x3,x4,x5,x6,x7,x8,x9)
20+
# @warn "Deprecated use of VariableNodeData(11 param), use 13 parameters instead"
21+
# new(x1,x2,x3,x4,x5,x6,x7,x8,x9, nothing, true, false, false) # TODO ensure this is initialized true is working for most cases
22+
# end
23+
VariableNodeData(x1::Array{Float64,2},
24+
x2::Array{Float64,2},
25+
x3::Vector{Int},
26+
x4::Vector{Int},
27+
x5::Int,
28+
x6::Bool,
29+
x7::Int,
30+
x8::Vector{Int},
31+
x9::Union{Nothing, Dict{ Tuple{Symbol, Vector{Float64}} } },
32+
x10,
33+
x11::Bool,
34+
x12::Bool,
35+
x13::Bool) =
36+
new(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13)
37+
end
38+
39+
struct VariableEstimate
40+
estimate::Vector{Float64}
41+
type::Symbol
42+
key::Symbol
43+
end
144

245
"""
346
$(SIGNATURES)
447
Fundamental structure for a DFG variable.
548
"""
649
mutable struct DFGVariable <: DFGNode
750
label::Symbol
8-
#TODO: Populate
51+
timestamp::DateTime
52+
tags::Vector{Symbol}
53+
estimateDict::Dict{Symbol, VariableEstimate}
54+
solverDataDict::Dict{Symbol, VariableNodeData}
55+
smallData::Any
56+
bigData::Any
957
_internalId::Int64
10-
DFGVariable(label::Symbol) = new(label, 0)
11-
DFGVariable(label::Symbol, _internalId::Int64) = new(label, _internalId)
58+
DFGVariable(label::Symbol, _internalId::Int64) = new(label, now(), Symbol[], Dict{Symbol, VariableEstimate}(), Dict{Symbol, VariableNodeData}(:default => VariableNodeData()), nothing, nothing, _internalId)
59+
DFGVariable(label::Symbol) = new(label, now(), Symbol[], Dict{Symbol, VariableEstimate}(), Dict{Symbol, VariableNodeData}(:default => VariableNodeData()), nothing, nothing, 0)
1260
end
61+
62+
# Accessors
63+
label(v::DFGVariable) = v.label
64+
timestamp(v::DFGVariable) = v.timestamp
65+
tags(v::DFGVariable) = v.tags
66+
estimates(v::DFGVariable) = v.estimateDict
67+
estimate(v::DFGVariable, key::Symbol=:default) = haskey(v.estimateDict, key) ? v.estimateDict[key] : nothing
68+
#solverData(v::DFGVariable) = haskey(v.solverDataDict, :default) ? v.solverDataDict[:default] : nothing
69+
solverData(v::DFGVariable, key::Symbol=:default) = haskey(v.solverDataDict, key) ? v.solverDataDict[key] : nothing
70+
solverDataDict(v::DFGVariable) = v.solverDataDict
71+
id(v::DFGVariable) = v._internalId
72+
# Todo: Complete this.
73+
smallData(v::DFGVariable) = v.smallData
74+
bigData(v::DFGVariable) = v.bigData

src/services/GraphsDFG.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import Graphs: attributes, vertex_index
2222
function attributes(v::GraphsNode, g::T)::AttributeDict where T <:GenericIncidenceList
2323
AttributeDict(
2424
"label" => v.dfgNode.label,
25-
"color" => typeof(v.dfgNode) == DFGVariable ? "red" : "blue",
26-
"shape" => typeof(v.dfgNode) == DFGVariable ? "box" : "ellipse",
27-
"fillcolor" => typeof(v.dfgNode) == DFGVariable ? "red" : "blue"
25+
"color" => v.dfgNode isa DFGVariable ? "red" : "blue",
26+
"shape" => v.dfgNode isa DFGVariable ? "box" : "ellipse",
27+
"fillcolor" => v.dfgNode isa DFGVariable ? "red" : "blue"
2828
)
2929
end
3030

@@ -226,7 +226,7 @@ List the DFGVariables in the DFG.
226226
Optionally specify a label regular expression to retrieves a subset of the variables.
227227
"""
228228
function ls(dfg::GraphsDFG, regexFilter::Union{Nothing, Regex}=nothing)::Vector{DFGVariable}
229-
variables = map(v -> v.dfgNode, filter(n -> typeof(n.dfgNode) == DFGVariable, collect(values(dfg.g.vertices))))
229+
variables = map(v -> v.dfgNode, filter(n -> n.dfgNode isa DFGVariable, vertices(dfg.g)))
230230
if regexFilter != nothing
231231
variables = filter(v -> occursin(regexFilter, String(v.label)), variables)
232232
end
@@ -247,7 +247,7 @@ List the DFGFactors in the DFG.
247247
Optionally specify a label regular expression to retrieves a subset of the factors.
248248
"""
249249
function lsf(dfg::GraphsDFG, regexFilter::Union{Nothing, Regex}=nothing)::Vector{DFGFactor}
250-
factors = map(v -> v.dfgNode, filter(n -> typeof(n.dfgNode) == DFGFactor, collect(values(dfg.g.vertices))))
250+
factors = map(v -> v.dfgNode, filter(n -> n.dfgNode isa DFGFactor, vertices(dfg.g)))
251251
if regexFilter != nothing
252252
factors = filter(f -> occursin(regexFilter, String(f.label)), factors)
253253
end
@@ -321,8 +321,8 @@ end
321321
function _copyIntoGraph!(sourceDFG::GraphsDFG, destDFG::GraphsDFG, variableFactorLabels::Vector{Symbol}, includeOrphanFactors::Bool=false)::Nothing
322322
# Split into variables and factors
323323
verts = map(id -> sourceDFG.g.vertices[sourceDFG.labelDict[id]], variableFactorLabels)
324-
sourceVariables = filter(n -> typeof(n.dfgNode) == DFGVariable, verts)
325-
sourceFactors = filter(n -> typeof(n.dfgNode) == DFGFactor, verts)
324+
sourceVariables = filter(n -> n.dfgNode isa DFGVariable, verts)
325+
sourceFactors = filter(n -> n.dfgNode isa DFGFactor, verts)
326326

327327
# Now we have to add all variables first,
328328
for variable in sourceVariables

test/interfaceTests.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
dfg = testDFGAPI()
22
v1 = DFGVariable(:a)
33
v2 = DFGVariable(:b)
4-
f1 = DFGFactor(:f1)
4+
f1 = DFGFactor{Int, :Symbol}(:f1)
55
@testset "Creating Graphs" begin
66
global dfg,v1,v2,f1
77
addVariable!(dfg, v1)
88
@test_throws Exception addVariable!(dfg, v1)
99
addVariable!(dfg, v2)
1010
addFactor!(dfg, [v1, v2], f1)
11-
@test_throws Exception addFactor!(dfg, DFGFactor("f2"), [v1, DFGVariable("Nope")])
11+
@test_throws Exception addFactor!(dfg, DFGFactor{Int, :Symbol}("f2"), [v1, DFGVariable("Nope")])
1212
end
1313

1414
@testset "Listing Nodes" begin
@@ -21,7 +21,7 @@ end
2121
end
2222

2323
# Gets
24-
@testset "Gets and Sets" begin
24+
@testset "Gets, Sets, and Accessors" begin
2525
global dfg,v1,v2,f1
2626
@test getVariable(dfg, v1.label) == v1
2727
@test getFactor(dfg, f1.label) == f1
@@ -35,6 +35,20 @@ end
3535
@test updateVariable!(dfg, v1Prime) != v1
3636
f1Prime = deepcopy(f1)
3737
@test updateFactor!(dfg, f1Prime) != f1
38+
39+
# Accessors
40+
@test label(v1) == v1.label
41+
@test timestamp(v1) == v1.timestamp
42+
@test estimates(v1) == v1.estimateDict
43+
@test estimate(v1, :notfound) == nothing
44+
@test solverData(v1) == v1.solverDataDict[:default]
45+
@test solverData(v1, :default) == v1.solverDataDict[:default]
46+
@test solverDataDict(v1) == v1.solverDataDict
47+
@test id(v1) == v1._internalId
48+
49+
@test label(f1) == f1.label
50+
@test data(f1) == f1.data
51+
@test id(f1) == f1._internalId
3852
end
3953

4054
# Deletions
@@ -71,7 +85,7 @@ numNodes = 10
7185
dfg = testDFGAPI()
7286
verts = map(n -> DFGVariable(Symbol("x$n")), 1:numNodes)
7387
map(v -> addVariable!(dfg, v), verts)
74-
map(n -> addFactor!(dfg, [verts[n], verts[n+1]], DFGFactor(Symbol("x$(n)x$(n+1)f1"))), 1:(numNodes-1))
88+
map(n -> addFactor!(dfg, [verts[n], verts[n+1]], DFGFactor{Int, :Symbol}(Symbol("x$(n)x$(n+1)f1"))), 1:(numNodes-1))
7589
# map(n -> addFactor!(dfg, [verts[n], verts[n+2]], DFGFactor(Symbol("x$(n)x$(n+2)f2"))), 1:2:(numNodes-2))
7690

7791
@testset "Getting Neighbors" begin

0 commit comments

Comments
 (0)