Skip to content

Commit ebf4049

Browse files
committed
Abstract getSubgraphAroundNode
1 parent bea5266 commit ebf4049

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/needsahome.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function buildSubgraphFromLabels!(dfg::G,
2525
syms::Vector{Symbol};
2626
subfg::AbstractDFG=(G <: InMemoryDFGTypes ? G : GraphsDFG)(params=getSolverParams(dfg)),
2727
solvable::Int=0,
28-
allowedFactors::Union{Nothing, Vector{Symbol}}=nothing )::G where G <: AbstractDFG
28+
allowedFactors::Union{Nothing, Vector{Symbol}}=nothing )::AbstractDFG where G <: AbstractDFG
2929
#
3030

3131
# add a little too many variables (since we need the factors)

src/services/AbstractDFG.jl

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,36 @@ Optionally provide an existing subgraph addToDFG, the extracted nodes will be co
426426
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.
427427
Note: Always returns the node at the center, but filters around it if solvable is set.
428428
"""
429-
function getSubgraphAroundNode(dfg::G, node::T, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::H=_getDuplicatedEmptyDFG(dfg); solvable::Int=0)::G where {G <: AbstractDFG, H <: AbstractDFG, P <: AbstractParams, T <: DFGNode}
430-
error("getSubgraphAroundNode not implemented for $(typeof(dfg))")
429+
function getSubgraphAroundNode(dfg::G, node::T, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::H=_getDuplicatedEmptyDFG(dfg); solvable::Int=0)::H where {G <: AbstractDFG, H <: AbstractDFG, P <: AbstractParams, T <: DFGNode}
430+
# error("getSubgraphAroundNode not implemented for $(typeof(dfg))")
431+
if !exists(dfg, node.label)
432+
error("Variable/factor with label '$(node.label)' does not exist in the factor graph")
433+
end
434+
435+
# Build a list of all unique neighbors inside 'distance'
436+
neighborList = Dict{Symbol, Any}()
437+
push!(neighborList, node.label => getVariable(dfg, node.label))
438+
curList = Dict{Symbol, Any}(node.label => getVariable(dfg, node.label))
439+
for dist in 1:distance
440+
newNeighbors = Dict{Symbol, Any}()
441+
for (key, node) in curList
442+
neighbors = getNeighbors(dfg, node, solvable=solvable)
443+
for neighbor in neighbors
444+
if isVariable(dfg, neighbor)
445+
push!(neighborList, neighbor => getVariable(dfg,neighbor))
446+
push!(newNeighbors, neighbor => getVariable(dfg,neighbor))
447+
else
448+
push!(neighborList, neighbor => getFactor(dfg,neighbor))
449+
push!(newNeighbors, neighbor => getFactor(dfg,neighbor))
450+
end
451+
end
452+
end
453+
curList = newNeighbors
454+
end
455+
456+
# Copy the section of graph we want
457+
_copyIntoGraph!(dfg, addToDFG, collect(keys(neighborList)), includeOrphanFactors)
458+
return addToDFG
431459
end
432460

433461
"""

0 commit comments

Comments
 (0)