Skip to content

Commit 8ffa959

Browse files
committed
refactoring and partial consolidation
1 parent 6e6973f commit 8ffa959

File tree

2 files changed

+62
-38
lines changed

2 files changed

+62
-38
lines changed

src/GraphsDFG/entities/GraphsDFG.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,23 @@ mutable struct GraphsDFG{T <: AbstractParams} <: AbstractDFG
2121
labelDict::Dict{Symbol, Int64}
2222
addHistory::Vector{Symbol} #TODO: Discuss more - is this an audit trail?
2323
solverParams::T # Solver parameters
24+
# GraphsDFG{T}(x...) where T <: AbstractParams = new{T}(x...)
2425
end
2526

27+
28+
GraphsDFG( g::FGType=Graphs.incdict(GraphsNode,is_directed=false),
29+
d::String="Graphs.jl implementation",
30+
n::Int64=0,
31+
l::Dict{Symbol, Int64}=Dict{Symbol, Int64}(),
32+
a::Vector{Symbol}=Symbol[];
33+
userId::String = "UserID",
34+
robotId::String = "robotID",
35+
sessionId::String = "sessionID",
36+
userData::Dict{Symbol, String} = Dict{Symbol, String}(),
37+
robotData::Dict{Symbol, String} = Dict{Symbol, String}(),
38+
sessionData::Dict{Symbol, String} = Dict{Symbol, String}(),
39+
params::T=NoSolverParams()) where T <: AbstractParams = GraphsDFG{T}(g, d, userId, robotId, sessionId, userData, robotData, sessionData, n, l, a, params)
40+
2641
GraphsDFG{T}( g::FGType=Graphs.incdict(GraphsNode,is_directed=false),
2742
d::String="Graphs.jl implementation",
2843
n::Int64=0,

src/needsahome.jl

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export buildSubgraphFromLabels!
1+
export buildSubgraphFromLabels!, buildSubgraphFromLabels!_SPECIAL
2+
export buildSubgraphFromLabels
23

34

45
"""
@@ -18,91 +19,99 @@ Related
1819
getVariableIds, _copyIntoGraph!
1920
"""
2021
function buildSubgraphFromLabels!(dfg::G,
21-
syms::Vector{Symbol},
22-
destType::Type{<:AbstractDFG}=GraphsDFG;
23-
solvable::Int=0)::G where G <: AbstractDFG
22+
syms::Vector{Symbol};
23+
subfg::AbstractDFG=(G <: InMemoryDFGTypes ? G : GraphsDFG)(params=getSolverParams(dfg)),
24+
solvable::Int=0,
25+
allowedFactors::Union{Nothing, Vector{Symbol}}=nothing )::G where G <: AbstractDFG
2426
#
25-
#Same type
26-
LocalGraphType = G <: InMemoryDFGTypes ? G : destType{typeof(getSolverParms(dfg))}
27-
# data structure for cliq sub graph
28-
cliqSubFg = LocalGraphType(params=getSolverParams(dfg))
2927

3028
# add a little too many variables (since we need the factors)
3129
for sym in syms
3230
if solvable <= getSolvable(dfg, sym)
33-
getSubgraphAroundNode(dfg, getVariable(dfg, sym), 2, false, cliqSubFg, solvable=solvable)
31+
getSubgraphAroundNode(dfg, getVariable(dfg, sym), 2, false, subfg, solvable=solvable)
3432
end
3533
end
3634

3735
# remove excessive variables that were copied by neighbors distance 2
38-
currVars = getVariableIds(cliqSubFg)
36+
currVars = getVariableIds(subfg)
3937
toDelVars = setdiff(currVars, syms)
4038
for dv in toDelVars
4139
# delete any neighboring factors first
42-
for fc in lsf(cliqSubFg, dv)
43-
deleteFactor!(cliqSubFg, fc)
40+
for fc in lsf(subfg, dv)
41+
deleteFactor!(subfg, fc)
4442
end
4543

4644
# and the variable itself
47-
deleteVariable!(cliqSubFg, dv)
45+
deleteVariable!(subfg, dv)
4846
end
4947

50-
return cliqSubFg
48+
return subfg
49+
end
50+
51+
function buildSubgraphFromLabels(dfg::G,
52+
syms::Vector{Symbol};
53+
subfg::AbstractDFG=(G <: InMemoryDFGTypes ? G : GraphsDFG)(params=getSolverParams(dfg)),
54+
solvable::Int=0,
55+
allowedFactors::Union{Nothing, Vector{Symbol}}=nothing )::G where G <: AbstractDFG
56+
#
57+
@warn "Deprecated buildSubgraphFromLabels, use buildSubgraphFromLabels! instead."
58+
buildSubgraphFromLabels(dfg, syms, subfg=subfg, solvable=solvable, allowedFactors=allowedFactors )
5159
end
5260

5361

5462
"""
5563
$SIGNATURES
5664
57-
IIF clique specific version of building subgraphs.
58-
59-
Notes
60-
- Special snowflake that adds only factors related to `frontals`.
65+
IIF clique specific version of building subgraphs. This is was an unfortunate rewrite of the existing `buildSubgraphFromLabels!` function above. Currently halfway consolidated. Tests required to ensure these two functions can be reduced to and will perform the same in both.
6166
6267
DevNotes
6368
- DF: Could we somehow better consolidate the functionality of this method into `buildSubgraphFromLabels!` above, which in turn should be consolidated as SamC suggests.
6469
- Since this function has happened more than once, it seems the name `buildSubgraphFromLabels!` might stick around, even if it just becomes a wrapper.
6570
6671
Related
6772
68-
_copyIntoGraph!
73+
buildSubgraphFromLabels!, _copyIntoGraph!, getVariableIds
6974
"""
70-
function buildSubgraphFromLabels!(dfg::AbstractDFG,
71-
cliqSubFg::AbstractDFG,
72-
frontals::Vector{Symbol},
73-
separators::Vector{Symbol};
74-
solvable::Int=0)
75+
function buildSubgraphFromLabels!_SPECIAL(dfg::G,
76+
# frontals::Vector{Symbol},
77+
syms::Vector{Symbol};
78+
subfg::AbstractDFG=(G <: InMemoryDFGTypes ? G : GraphsDFG)(params=getSolverParams(dfg)),
79+
solvable::Int=0,
80+
allowedFactors::Union{Nothing, Vector{Symbol}}=nothing )::G where G <: AbstractDFG
7581
#
76-
for sym in separators
77-
(solvable <= getSolvable(dfg, sym)) && DFG.addVariable!(cliqSubFg, deepcopy(DFG.getVariable(dfg, sym)))
78-
end
82+
# for sym in separators
83+
# (solvable <= getSolvable(dfg, sym)) && DFG.addVariable!(subfg, deepcopy(DFG.getVariable(dfg, sym)))
84+
# end
7985

8086
addfac = Symbol[]
81-
for sym in frontals
87+
for sym in syms # frontals
8288
if solvable <= getSolvable(dfg, sym)
83-
DFG.addVariable!(cliqSubFg, deepcopy(DFG.getVariable(dfg, sym)))
84-
append!(addfac, getNeighbors(dfg,sym))
89+
DFG.addVariable!(subfg, deepcopy(DFG.getVariable(dfg, sym)))
90+
append!(addfac, getNeighbors(dfg, sym, solvable=solvable))
8591
end
8692
end
8793

88-
allvars = ls(cliqSubFg)
89-
for sym in addfac
94+
# allowable factors as intersect between connected an user list
95+
usefac = allowedFactors == nothing ? addfac : intersect(allowedFactors, addfac)
96+
97+
allvars = ls(subfg)
98+
for sym in usefac
9099
fac = DFG.getFactor(dfg, sym)
91100
vos = fac._variableOrderSymbols
92101
#TODO don't add duplicates to start with
93-
if !exists(cliqSubFg,fac) && vos allvars && solvable <= getSolvable(dfg, sym)
94-
DFG.addFactor!(cliqSubFg, fac._variableOrderSymbols, deepcopy(fac))
102+
if !exists(subfg,fac) && (vos allvars) && (solvable <= getSolvable(dfg, sym))
103+
DFG.addFactor!(subfg, fac._variableOrderSymbols, deepcopy(fac))
95104
end
96105
end
97106

98107
# remove orphans
99-
for fct in DFG.getFactors(cliqSubFg)
108+
for fct in DFG.getFactors(subfg)
100109
# delete any neighboring factors first
101-
if length(getNeighbors(cliqSubFg, fct)) != length(fct._variableOrderSymbols)
102-
DFG.deleteFactor!(cliqSubFg, fc)
110+
if length(getNeighbors(subfg, fct)) != length(fct._variableOrderSymbols)
111+
DFG.deleteFactor!(subfg, fc)
103112
@error "deleteFactor! this should not happen"
104113
end
105114
end
106115

107-
return cliqSubFg
116+
return subfg
108117
end

0 commit comments

Comments
 (0)