Skip to content

Commit 1d12804

Browse files
authored
Option with removed nodeCounter and labelDict (#68)
* removed nodeCounter and labelDict it is now passed to the MetaGraphs dict :label nodeCounter just returs the number of nodes nv(G) * up requrement for MetaGraphs to 0.6.4 * doing codecov first since coveralls repo don't exist. rest of caesar uses codecov, is both needed
1 parent 11f455f commit 1d12804

File tree

4 files changed

+56
-62
lines changed

4 files changed

+56
-62
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ notifications:
1818
matrix:
1919
allow_failures:
2020
- julia: nightly
21-
- julia: 1.2
2221

2322
# script:
2423
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
@@ -34,7 +33,7 @@ matrix:
3433
# - julia --project --color=yes --check-bounds=yes -e 'using Pkg; Pkg.add(PackageSpec(name="MetaGraphs", rev="master")); Pkg.build(); Pkg.test(coverage=true)'
3534

3635
after_success:
37-
- julia -e 'using Pkg; cd(Pkg.dir("DistributedFactorGraphs")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder()); Codecov.submit(process_folder())'
36+
- julia -e 'using Pkg; cd(Pkg.dir("DistributedFactorGraphs")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder()); Coveralls.submit(process_folder())'
3837

3938
jobs:
4039
include:

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Graphs = "≥ 0.10.3"
2323
Reexport = "≥ 0.2"
2424
Requires = "≥ 0.5"
2525
julia = "0.7, 1"
26-
MetaGraphs = "≥ 0.6.3"
26+
MetaGraphs = "≥ 0.6.4"
2727

2828
[extras]
2929
IncrementalInference = "904591bb-b899-562f-9e6f-b8df64c7d480"

src/LightGraphsDFG/entities/LightGraphsDFG.jl

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ mutable struct LightGraphsDFG{T <: AbstractParams} <: AbstractDFG
1616
userId::String
1717
robotId::String
1818
sessionId::String
19-
#TODO Remove nodeCounter
20-
nodeCounter::Int64 #TODO pos 'n paar van die veranderlikes dalk aan na MetaGraphs calls
21-
#TODO verander na label vector of gebruik matagrapsh sin
22-
labelDict::Dict{Symbol, Int64}
19+
#NOTE Removed nodeCounter
20+
# nodeCounter::Int64
21+
#NOTE using matagraphs labels
22+
# labelDict::Dict{Symbol, Int64}
2323
addHistory::Vector{Symbol} #TODO: Discuss more - is this an audit trail?
2424
solverParams::T # Solver parameters
2525
end
@@ -29,15 +29,27 @@ function LightGraphsDFG{T}(g::LFGType=MetaGraph(),
2929
d::String="LightGraphs.jl implementation",
3030
userId::String="User ID",
3131
robotId::String="Robot ID",
32-
sessionId::String="Session ID",
33-
n::Int64=0,
34-
l::Dict{Symbol, Int64}=Dict{Symbol, Int64}(),
35-
a::Vector{Symbol}=Symbol[];
32+
sessionId::String="Session ID";
3633
params::T=NoSolverParams()) where T <: AbstractParams
3734
set_prop!(g, :description, d)
3835
set_prop!(g, :userId, userId)
3936
set_prop!(g, :robotId, robotId)
4037
set_prop!(g, :sessionId, sessionId)
4138
set_indexing_prop!(g, :label)
42-
LightGraphsDFG{T}(g, d, userId, robotId, sessionId, n, l, a, params)
39+
LightGraphsDFG{T}(g, d, userId, robotId, sessionId, Symbol[], params)
40+
end
41+
42+
Base.propertynames(x::LightGraphsDFG, private::Bool=false) =
43+
(:g, :description, :userId, :robotId, :sessionId, :nodeCounter, :labelDict, :addHistory, :solverParams)
44+
# (private ? fieldnames(typeof(x)) : ())...)
45+
46+
Base.getproperty(x::LightGraphsDFG,f::Symbol) = begin
47+
if f == :nodeCounter
48+
nv(x.g)
49+
elseif f == :labelDict
50+
@warn "Read only! using internal labelDict"
51+
x.g.metaindex[:label]
52+
else
53+
getfield(x,f)
54+
end
4355
end

src/LightGraphsDFG/services/LightGraphsDFG.jl

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Accessors
2-
getLabelDict(dfg::LightGraphsDFG) = dfg.labelDict
2+
getLabelDict(dfg::LightGraphsDFG) = dfg.g.metaindex[:label]
33
getDescription(dfg::LightGraphsDFG) = dfg.description
44
setDescription(dfg::LightGraphsDFG, description::String) = dfg.description = description
55
getInnerGraph(dfg::LightGraphsDFG) = dfg.g
@@ -16,20 +16,19 @@ end
1616
True if the variable or factor exists in the graph.
1717
"""
1818
function exists(dfg::LightGraphsDFG, node::N) where N <: DFGNode
19-
return haskey(dfg.labelDict, node.label)
19+
return haskey(dfg.g.metaindex[:label], node.label)
2020
end
21-
exists(dfg::LightGraphsDFG, nId::Symbol) = haskey(dfg.labelDict, nId)
21+
exists(dfg::LightGraphsDFG, nId::Symbol) = haskey(dfg.g.metaindex[:label], nId)
2222

2323

2424
"""
2525
$(SIGNATURES)
2626
Add a DFGVariable to a DFG.
2727
"""
2828
function addVariable!(dfg::LightGraphsDFG, variable::DFGVariable)::Bool
29-
if haskey(dfg.labelDict, variable.label)
29+
if haskey(dfg.g.metaindex[:label], variable.label)
3030
error("Variable '$(variable.label)' already exists in the factor graph")
3131
end
32-
dfg.nodeCounter += 1
3332

3433
#NOTE Internal ID always set to zero as it is not needed?
3534
variable._internalId = 0
@@ -43,8 +42,6 @@ function addVariable!(dfg::LightGraphsDFG, variable::DFGVariable)::Bool
4342
MetaGraphs.add_vertex!(dfg.g, :label, variable.label) || return false
4443
MetaGraphs.set_props!(dfg.g, nv(dfg.g), props) || return false
4544

46-
#TODO die ID gaan heeltyd verander, ek dink sover gebruik label direk as index
47-
push!(dfg.labelDict, variable.label=>variable._internalId)
4845
# Track insertion
4946
push!(dfg.addHistory, variable.label)
5047

@@ -56,43 +53,37 @@ end
5653
Add a DFGFactor to a DFG.
5754
"""
5855
function addFactor!(dfg::LightGraphsDFG, variables::Vector{DFGVariable}, factor::DFGFactor)::Bool
59-
if haskey(dfg.labelDict, factor.label)
56+
if haskey(dfg.g.metaindex[:label], factor.label)
6057
error("Factor '$(factor.label)' already exists in the factor graph")
6158
end
6259
for v in variables
63-
if !(v.label in keys(dfg.labelDict))
60+
if !(v.label in keys(dfg.g.metaindex[:label]))
6461
error("Variable '$(v.label)' not found in graph when creating Factor '$(factor.label)'")
6562
end
6663
end
67-
dfg.nodeCounter += 1
68-
factor._internalId = dfg.nodeCounter
64+
65+
#NOTE Internal ID always set to zero as it is not needed?
66+
factor._internalId = 0
6967
factor._variableOrderSymbols = map(v->v.label, variables)
70-
# fNode = LightGraphsNode(dfg.nodeCounter, factor)
71-
# f = Graphs.add_vertex!(dfg.g, fNode)
7268

73-
#TODO something like this or the next props definition
69+
#NOTE something like this or the next props definition
7470
# props = Dict{:Symbol, Any}()
7571
# props[:tags] = factor.tags
7672
# props[:factor] = factor
7773

7874
props = Dict(:factor=>factor)
7975

80-
retval = MetaGraphs.add_vertex!(dfg.g, :label, factor.label)
81-
retval && set_props!(dfg.g, nv(dfg.g), props)
76+
MetaGraphs.add_vertex!(dfg.g, :label, factor.label) || return false
77+
set_props!(dfg.g, nv(dfg.g), props) || return false
8278

8379
# Add index
84-
push!(dfg.labelDict, factor.label=>factor._internalId)
80+
# push!(dfg.labels, factor.label)
8581
# Add the edges...
8682
for variable in variables
87-
# v = dfg.g.vertices[variable._internalId]
88-
# edge = Graphs.make_edge(dfg.g, v, f)
89-
# Graphs.add_edge!(dfg.g, edge)
90-
retval && MetaGraphs.add_edge!(dfg.g, dfg.g[variable.label,:label], dfg.g[factor.label,:label])
83+
MetaGraphs.add_edge!(dfg.g, dfg.g[variable.label,:label], dfg.g[factor.label,:label]) || return false
9184
end
92-
# Track insertion
93-
# push!(dfg.addHistory, factor.label)
9485

95-
return retval
86+
return true
9687
end
9788

9889
"""
@@ -124,7 +115,7 @@ function getVariable(dfg::LightGraphsDFG, label::Union{Symbol, String})::DFGVari
124115
if typeof(label) == String
125116
label = Symbol(label)
126117
end
127-
if !haskey(dfg.labelDict, label)
118+
if !haskey(dfg.g.metaindex[:label], label)
128119
error("Variable label '$(label)' does not exist in the factor graph")
129120
end
130121
return get_prop(dfg.g, dfg.g[label,:label], :variable)
@@ -135,14 +126,14 @@ end
135126
Get a DFGFactor from a DFG using its underlying integer ID.
136127
"""
137128
function getFactor(dfg::LightGraphsDFG, factorId::Int64)::DFGFactor
138-
# if !(factorId in values(dfg.labelDict))
129+
# if !(factorId in values(dfg.g.metaindex[:label]))
139130
# error("Factor ID '$(factorId)' does not exist in the factor graph")
140131
# end
141132
return get_prop(dfg.g, factorId, :factor)
142133
end
143134

144135
function getFactor(g::MetaGraph, factorId::Int64)::DFGFactor
145-
# if !(factorId in values(dfg.labelDict))
136+
# if !(factorId in values(dfg.g.metaindex[:label]))
146137
# error("Factor ID '$(factorId)' does not exist in the factor graph")
147138
# end
148139
return get_prop(g, factorId, :factor)
@@ -156,7 +147,7 @@ function getFactor(dfg::LightGraphsDFG, label::Union{Symbol, String})::DFGFactor
156147
if typeof(label) == String
157148
label = Symbol(label)
158149
end
159-
if !haskey(dfg.labelDict, label)
150+
if !haskey(dfg.g.metaindex[:label], label)
160151
error("Factor label '$(label)' does not exist in the factor graph")
161152
end
162153
return get_prop(dfg.g, dfg.g[label,:label], :factor)
@@ -167,10 +158,9 @@ end
167158
Update a complete DFGVariable in the DFG.
168159
"""
169160
function updateVariable!(dfg::LightGraphsDFG, variable::DFGVariable)::DFGVariable
170-
if !haskey(dfg.labelDict, variable.label)
161+
if !haskey(dfg.g.metaindex[:label], variable.label)
171162
error("Variable label '$(variable.label)' does not exist in the factor graph")
172163
end
173-
# dfg.g.vertices[dfg.labelDict[variable.label]].dfgNode = variable
174164
set_prop!(dfg.g, dfg.g[variable.label,:label], :variable, variable)
175165
return variable
176166
end
@@ -180,10 +170,9 @@ end
180170
Update a complete DFGFactor in the DFG.
181171
"""
182172
function updateFactor!(dfg::LightGraphsDFG, factor::DFGFactor)::DFGFactor
183-
if !haskey(dfg.labelDict, factor.label)
173+
if !haskey(dfg.g.metaindex[:label], factor.label)
184174
error("Factor label '$(factor.label)' does not exist in the factor graph")
185175
end
186-
# dfg.g.vertices[dfg.labelDict[factor.label]].dfgNode = factor
187176
set_prop!(dfg.g, dfg.g[factor.label,:label], :factor, factor)
188177
return factor
189178
end
@@ -193,14 +182,12 @@ end
193182
Delete a DFGVariable from the DFG using its label.
194183
"""
195184
function deleteVariable!(dfg::LightGraphsDFG, label::Symbol)::DFGVariable
196-
if !haskey(dfg.labelDict, label)
185+
if !haskey(dfg.g.metaindex[:label], label)
197186
error("Variable label '$(label)' does not exist in the factor graph")
198187
end
199-
# variable = dfg.g.vertices[dfg.labelDict[label]].dfgNode
200-
# delete_vertex!(dfg.g.vertices[dfg.labelDict[label]], dfg.g)
201188
variable = get_prop(dfg.g, dfg.g[label,:label], :variable)
202189
rem_vertex!(dfg.g, dfg.g[label,:label])
203-
delete!(dfg.labelDict, label)
190+
204191
return variable
205192
end
206193

@@ -216,14 +203,11 @@ deleteVariable!(dfg::LightGraphsDFG, variable::DFGVariable)::DFGVariable = delet
216203
Delete a DFGFactor from the DFG using its label.
217204
"""
218205
function deleteFactor!(dfg::LightGraphsDFG, label::Symbol)::DFGFactor
219-
if !haskey(dfg.labelDict, label)
206+
if !haskey(dfg.g.metaindex[:label], label)
220207
error("Factor label '$(label)' does not exist in the factor graph")
221208
end
222-
# factor = dfg.g.vertices[dfg.labelDict[label]].dfgNode
223-
# delete_vertex!(dfg.g.vertices[dfg.labelDict[label]], dfg.g)
224209
factor = get_prop(dfg.g, dfg.g[label,:label], :factor)
225210
MetaGraphs.rem_vertex!(dfg.g, dfg.g[label,:label])
226-
delete!(dfg.labelDict, label)
227211
return factor
228212
end
229213

@@ -359,11 +343,10 @@ end
359343
Retrieve a list of labels of the immediate neighbors around a given variable or factor.
360344
"""
361345
function getNeighbors(dfg::LightGraphsDFG, node::T; ready::Union{Nothing, Int}=nothing, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol} where T <: DFGNode
362-
if !haskey(dfg.labelDict, node.label)
346+
if !haskey(dfg.g.metaindex[:label], node.label)
363347
error("Variable/factor with label '$(node.label)' does not exist in the factor graph")
364348
end
365-
# vert = dfg.g.vertices[dfg.labelDict[node.label]]
366-
# neighbors = in_neighbors(vert, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
349+
367350
neighbors = map(idx->get_prop(dfg.g, idx, :label), LightGraphs.neighbors(dfg.g, dfg.g[node.label,:label]))
368351
# Additional filtering
369352
neighbors = ready != nothing ? filter(lbl -> _isready(dfg, dfg.g[lbl,:label], ready), neighbors) : neighbors
@@ -375,14 +358,14 @@ function getNeighbors(dfg::LightGraphsDFG, node::T; ready::Union{Nothing, Int}=n
375358
return order
376359
end
377360

378-
return neighbors#map(n -> n.dfgNode.label, neighbors)
361+
return neighbors
379362
end
380363
"""
381364
$(SIGNATURES)
382365
Retrieve a list of labels of the immediate neighbors around a given variable or factor specified by its label.
383366
"""
384367
function getNeighbors(dfg::LightGraphsDFG, label::Symbol; ready::Union{Nothing, Int}=nothing, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol} where T <: DFGNode
385-
if !haskey(dfg.labelDict, label)
368+
if !haskey(dfg.g.metaindex[:label], label)
386369
error("Variable/factor with label '$(label)' does not exist in the factor graph")
387370
end
388371

@@ -433,14 +416,14 @@ function _copyIntoGraph!(sourceDFG::LightGraphsDFG, destDFG::LightGraphsDFG, ns:
433416

434417
# Now we have to add all variables first,
435418
for v in variables
436-
if !haskey(destDFG.labelDict, v.label)
419+
if !haskey(destDFG.g.metaindex[:label], v.label)
437420
addVariable!(destDFG, deepcopy(v))
438421
end
439422
end
440423

441424
# And then all factors to the destDFG.
442425
for f in factors
443-
if !haskey(destDFG.labelDict, f.label)
426+
if !haskey(destDFG.g.metaindex[:label], f.label)
444427
# Get the original factor variables (we need them to create it)
445428
# variables = in_neighbors(factor, sourceDFG.g)
446429
# variables = getNeighbors(sourceDFG, f)
@@ -450,7 +433,7 @@ function _copyIntoGraph!(sourceDFG::LightGraphsDFG, destDFG::LightGraphsDFG, ns:
450433
# Find the labels and associated variables in our new subgraph
451434
factVariables = DFGVariable[]
452435
for v in variables
453-
if haskey(destDFG.labelDict, v.label)
436+
if haskey(destDFG.g.metaindex[:label], v.label)
454437
push!(factVariables, getVariable(destDFG, v.label))
455438
#otherwise ignore
456439
end
@@ -474,7 +457,7 @@ Optionally provide an existing subgraph addToDFG, the extracted nodes will be co
474457
Note: By default orphaned factors (where the subgraph does not contain all the related variables) are not returned. Set includeOrphanFactors to return the orphans irrespective of whether the subgraph contains all the variables.
475458
"""
476459
function getSubgraphAroundNode(dfg::LightGraphsDFG{P}, node::T, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::LightGraphsDFG=LightGraphsDFG{P}())::LightGraphsDFG where {P <: AbstractParams, T <: DFGNode}
477-
if !haskey(dfg.labelDict, node.label)
460+
if !haskey(dfg.g.metaindex[:label], node.label)
478461
error("Variable/factor with label '$(node.label)' does not exist in the factor graph")
479462
end
480463

@@ -495,7 +478,7 @@ Note: By default orphaned factors (where the subgraph does not contain all the r
495478
"""
496479
function getSubgraph(dfg::LightGraphsDFG, variableFactorLabels::Vector{Symbol}, includeOrphanFactors::Bool=false, addToDFG::LightGraphsDFG=LightGraphsDFG())::LightGraphsDFG
497480
for label in variableFactorLabels
498-
if !haskey(dfg.labelDict, label)
481+
if !haskey(dfg.g.metaindex[:label], label)
499482
error("Variable/factor with label '$(label)' does not exist in the factor graph")
500483
end
501484
end

0 commit comments

Comments
 (0)