Skip to content

Commit 306090f

Browse files
authored
Merge pull request #109 from JuliaRobotics/maintenance/3Q19/miniup
some code cleanup, more dispatch possible
2 parents ee35907 + f3e8e47 commit 306090f

File tree

11 files changed

+410
-206
lines changed

11 files changed

+410
-206
lines changed

src/CloudGraphsDFG/services/CloudGraphsDFG.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ getDescription(dfg::CloudGraphsDFG) = dfg.description
6363
setDescription(dfg::CloudGraphsDFG, description::String) = dfg.description = description
6464
getAddHistory(dfg::CloudGraphsDFG) = dfg.addHistory
6565
getSolverParams(dfg::CloudGraphsDFG) = dfg.solverParams
66-
function setSolverParams(dfg::CloudGraphsDFG, solverParams::T) where T <: AbstractParams
67-
dfg.solverParams = solverParams
66+
function setSolverParams(dfg::CloudGraphsDFG, solverParams::T)::T where T <: AbstractParams
67+
return dfg.solverParams = solverParams
6868
end
6969

7070
"""
@@ -739,11 +739,11 @@ Get a deep subgraph copy from the DFG given a list of variables and factors.
739739
Optionally provide an existing subgraph addToDFG, the extracted nodes will be copied into this graph. By default a new subgraph will be created.
740740
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.
741741
"""
742-
function getSubgraph(dfg::CloudGraphsDFG, variableFactorLabels::Vector{Symbol}, includeOrphanFactors::Bool=false, addToDFG::Union{Nothing, CloudGraphsDFG}=nothing)::CloudGraphsDFG
742+
function getSubgraph(dfg::CloudGraphsDFG,
743+
variableFactorLabels::Vector{Symbol},
744+
includeOrphanFactors::Bool=false,
745+
addToDFG::G=_getDuplicatedEmptyDFG(dfg) )::G where {G <: AbstractDFG}
743746
# Making a copy session if not specified
744-
if addToDFG == nothing
745-
addToDFG = _getDuplicatedEmptyDFG(dfg)
746-
end
747747

748748
_copyIntoGraph!(dfg, addToDFG, variableFactorLabels, includeOrphanFactors)
749749

src/Common.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
export sortVarNested
32
export isPrior, lsfPriors
43
export getData
@@ -7,6 +6,14 @@ export getFactorType, getfnctype
76
export lsTypes, lsfTypes
87
export lsWho, lsfWho
98

9+
## Utility functions for getting type names and modules (from IncrementalInference)
10+
function _getmodule(t::T) where T
11+
T.name.module
12+
end
13+
function _getname(t::T) where T
14+
T.name.name
15+
end
16+
1017
"""
1118
$(SIGNATURES)
1219
Test if all elements of the string is a number: Ex, "123" is true, "1_2" is false.

src/GraphsDFG/services/GraphsDFG.jl

Lines changed: 47 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@ setDescription(dfg::GraphsDFG, description::String) = dfg.description = descript
2222
getInnerGraph(dfg::GraphsDFG) = dfg.g
2323
getAddHistory(dfg::GraphsDFG) = dfg.addHistory
2424
getSolverParams(dfg::GraphsDFG) = dfg.solverParams
25+
function setSolverParams(dfg::GraphsDFG, solverParams::T) where T <: AbstractParams
26+
dfg.solverParams = solverParams
27+
end
2528

26-
# setSolverParams(dfg::GraphsDFG, solverParams) = dfg.solverParams = solverParams
27-
function setSolverParams(dfg::GraphsDFG, solverParams::P) where P <: AbstractParams
28-
dfg.solverParams = solverParams
29+
"""
30+
$(SIGNATURES)
31+
Gets an empty and unique CloudGraphsDFG derived from an existing DFG.
32+
"""
33+
function _getDuplicatedEmptyDFG(dfg::GraphsDFG)::GraphsDFG
34+
newDfg = GraphsDFG{typeof(dfg.solverParams)}(;
35+
userId=dfg.userId, robotId=dfg.robotId, sessionId=dfg.sessionId,
36+
params=deepcopy(dfg.solverParams))
37+
newDfg.description ="(Copy of) $(dfg.description)"
38+
return newDfg
2939
end
3040

3141
"""
@@ -190,13 +200,6 @@ function deleteVariable!(dfg::GraphsDFG, label::Symbol)::DFGVariable
190200
return variable
191201
end
192202

193-
#Alias
194-
"""
195-
$(SIGNATURES)
196-
Delete a referenced DFGVariable from the DFG.
197-
"""
198-
deleteVariable!(dfg::GraphsDFG, variable::DFGVariable)::DFGVariable = deleteVariable!(dfg, variable.label)
199-
200203
"""
201204
$(SIGNATURES)
202205
Delete a DFGFactor from the DFG using its label.
@@ -211,13 +214,6 @@ function deleteFactor!(dfg::GraphsDFG, label::Symbol)::DFGFactor
211214
return factor
212215
end
213216

214-
# Alias
215-
"""
216-
$(SIGNATURES)
217-
Delete the referened DFGFactor from the DFG.
218-
"""
219-
deleteFactor!(dfg::GraphsDFG, factor::DFGFactor)::DFGFactor = deleteFactor!(dfg, factor.label)
220-
221217
"""
222218
$(SIGNATURES)
223219
List the DFGVariables in the DFG.
@@ -235,34 +231,6 @@ function getVariables(dfg::GraphsDFG, regexFilter::Union{Nothing, Regex}=nothing
235231
return variables
236232
end
237233

238-
"""
239-
$(SIGNATURES)
240-
Get a list of IDs of the DFGVariables in the DFG.
241-
Optionally specify a label regular expression to retrieves a subset of the variables.
242-
243-
Example
244-
```julia
245-
getVariableIds(dfg, r"l", tags=[:APRILTAG;])
246-
```
247-
248-
Related
249-
250-
ls
251-
"""
252-
function getVariableIds(dfg::GraphsDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[])::Vector{Symbol}
253-
vars = getVariables(dfg, regexFilter, tags=tags)
254-
# mask = map(v -> length(intersect(v.tags, tags)) > 0, vars )
255-
map(v -> v.label, vars)
256-
end
257-
258-
# Alias
259-
"""
260-
$(SIGNATURES)
261-
List the DFGVariables in the DFG.
262-
Optionally specify a label regular expression to retrieves a subset of the variables.
263-
"""
264-
ls(dfg::GraphsDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[])::Vector{Symbol} = getVariableIds(dfg, regexFilter, tags=tags)
265-
266234
"""
267235
$(SIGNATURES)
268236
List the DFGFactors in the DFG.
@@ -276,29 +244,6 @@ function getFactors(dfg::GraphsDFG, regexFilter::Union{Nothing, Regex}=nothing):
276244
return factors
277245
end
278246

279-
"""
280-
$(SIGNATURES)
281-
Get a list of the IDs of the DFGFactors in the DFG.
282-
Optionally specify a label regular expression to retrieves a subset of the factors.
283-
"""
284-
getFactorIds(dfg::GraphsDFG, regexFilter::Union{Nothing, Regex}=nothing)::Vector{Symbol} = map(f -> f.label, getFactors(dfg, regexFilter))
285-
286-
"""
287-
$(SIGNATURES)
288-
List the DFGFactors in the DFG.
289-
Optionally specify a label regular expression to retrieves a subset of the factors.
290-
"""
291-
# Alias
292-
lsf(dfg::GraphsDFG, regexFilter::Union{Nothing, Regex}=nothing)::Vector{Symbol} = getFactorIds(dfg, regexFilter)
293-
294-
"""
295-
$(SIGNATURES)
296-
Alias for getNeighbors - returns neighbors around a given node label.
297-
"""
298-
function lsf(dfg::GraphsDFG, label::Symbol)::Vector{Symbol}
299-
return getNeighbors(dfg, label)
300-
end
301-
302247
"""
303248
$(SIGNATURES)
304249
Checks if the graph is fully connected, returns true if so.
@@ -307,14 +252,6 @@ function isFullyConnected(dfg::GraphsDFG)::Bool
307252
return length(Graphs.connected_components(dfg.g)) == 1
308253
end
309254

310-
#Alias
311-
"""
312-
$(SIGNATURES)
313-
Checks if the graph is not fully connected, returns true if it is not contiguous.
314-
"""
315-
hasOrphans(dfg::GraphsDFG)::Bool = !isFullyConnected(dfg)
316-
317-
318255
"""
319256
$(SIGNATURES)
320257
Retrieve a list of labels of the immediate neighbors around a given variable or factor.
@@ -359,56 +296,40 @@ function getNeighbors(dfg::GraphsDFG, label::Symbol; ready::Union{Nothing, Int}=
359296
return map(n -> n.dfgNode.label, neighbors)
360297
end
361298

362-
# Aliases
363-
"""
364-
$(SIGNATURES)
365-
Retrieve a list of labels of the immediate neighbors around a given variable or factor.
366-
"""
367-
function ls(dfg::GraphsDFG, node::T)::Vector{Symbol} where T <: DFGNode
368-
return getNeighbors(dfg, node)
369-
end
370-
"""
371-
$(SIGNATURES)
372-
Retrieve a list of labels of the immediate neighbors around a given variable or factor specified by its label.
373-
"""
374-
function ls(dfg::GraphsDFG, label::Symbol)::Vector{Symbol} where T <: DFGNode
375-
return getNeighbors(dfg, label)
376-
end
377-
378-
function _copyIntoGraph!(sourceDFG::GraphsDFG, destDFG::GraphsDFG, variableFactorLabels::Vector{Symbol}, includeOrphanFactors::Bool=false)::Nothing
379-
# Split into variables and factors
380-
verts = map(id -> sourceDFG.g.vertices[sourceDFG.labelDict[id]], variableFactorLabels)
381-
sourceVariables = filter(n -> n.dfgNode isa DFGVariable, verts)
382-
sourceFactors = filter(n -> n.dfgNode isa DFGFactor, verts)
383-
384-
# Now we have to add all variables first,
385-
for variable in sourceVariables
386-
if !haskey(destDFG.labelDict, variable.dfgNode.label)
387-
addVariable!(destDFG, deepcopy(variable.dfgNode))
388-
end
389-
end
390-
# And then all factors to the destDFG.
391-
for factor in sourceFactors
392-
if !haskey(destDFG.labelDict, factor.dfgNode.label)
393-
# Get the original factor variables (we need them to create it)
394-
neighVarIds = getNeighbors(sourceDFG, factor.dfgNode.label) #OLD: in_neighbors(factor, sourceDFG.g)
395-
# Find the labels and associated neighVarIds in our new subgraph
396-
factVariables = DFGVariable[]
397-
for neighVarId in neighVarIds
398-
if haskey(destDFG.labelDict, neighVarId)
399-
push!(factVariables, getVariable(destDFG, neighVarId))
400-
#otherwise ignore
401-
end
402-
end
403-
404-
# Only if we have all of them should we add it (otherwise strange things may happen on evaluation)
405-
if includeOrphanFactors || length(factVariables) == length(neighVarIds)
406-
addFactor!(destDFG, factVariables, deepcopy(factor.dfgNode))
407-
end
408-
end
409-
end
410-
return nothing
411-
end
299+
# function _copyIntoGraph!(sourceDFG::GraphsDFG, destDFG::GraphsDFG, variableFactorLabels::Vector{Symbol}, includeOrphanFactors::Bool=false)::Nothing
300+
# # Split into variables and factors
301+
# verts = map(id -> sourceDFG.g.vertices[sourceDFG.labelDict[id]], variableFactorLabels)
302+
# sourceVariables = filter(n -> n.dfgNode isa DFGVariable, verts)
303+
# sourceFactors = filter(n -> n.dfgNode isa DFGFactor, verts)
304+
#
305+
# # Now we have to add all variables first,
306+
# for variable in sourceVariables
307+
# if !haskey(destDFG.labelDict, variable.dfgNode.label)
308+
# addVariable!(destDFG, deepcopy(variable.dfgNode))
309+
# end
310+
# end
311+
# # And then all factors to the destDFG.
312+
# for factor in sourceFactors
313+
# if !haskey(destDFG.labelDict, factor.dfgNode.label)
314+
# # Get the original factor variables (we need them to create it)
315+
# neighVarIds = getNeighbors(sourceDFG, factor.dfgNode.label) #OLD: in_neighbors(factor, sourceDFG.g)
316+
# # Find the labels and associated neighVarIds in our new subgraph
317+
# factVariables = DFGVariable[]
318+
# for neighVarId in neighVarIds
319+
# if haskey(destDFG.labelDict, neighVarId)
320+
# push!(factVariables, getVariable(destDFG, neighVarId))
321+
# #otherwise ignore
322+
# end
323+
# end
324+
#
325+
# # Only if we have all of them should we add it (otherwise strange things may happen on evaluation)
326+
# if includeOrphanFactors || length(factVariables) == length(neighVarIds)
327+
# addFactor!(destDFG, factVariables, deepcopy(factor.dfgNode))
328+
# end
329+
# end
330+
# end
331+
# return nothing
332+
# end
412333

413334
"""
414335
$(SIGNATURES)
@@ -445,23 +366,6 @@ function getSubgraphAroundNode(dfg::GraphsDFG{P}, node::T, distance::Int64=1, in
445366
return addToDFG
446367
end
447368

448-
"""
449-
$(SIGNATURES)
450-
Get a deep subgraph copy from the DFG given a list of variables and factors.
451-
Optionally provide an existing subgraph addToDFG, the extracted nodes will be copied into this graph. By default a new subgraph will be created.
452-
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.
453-
"""
454-
function getSubgraph(dfg::GraphsDFG, variableFactorLabels::Vector{Symbol}, includeOrphanFactors::Bool=false, addToDFG::GraphsDFG=GraphsDFG{AbstractParams}())::GraphsDFG
455-
for label in variableFactorLabels
456-
if !haskey(dfg.labelDict, label)
457-
error("Variable/factor with label '$(label)' does not exist in the factor graph")
458-
end
459-
end
460-
461-
_copyIntoGraph!(dfg, addToDFG, variableFactorLabels, includeOrphanFactors)
462-
return addToDFG
463-
end
464-
465369
"""
466370
$(SIGNATURES)
467371
Get an adjacency matrix for the DFG, returned as a Matrix{Union{Nothing, Symbol}}.

src/LightDFG/entities/LightDFG.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ Base.getproperty(x::LightDFG,f::Symbol) = begin
3737
@error "Depreciated? returning number of nodes"
3838
nv(x.g)
3939
elseif f == :labelDict
40-
@error "Depreciated? Concider using exists(dfg,label) instead. Returing internals copy"
40+
@error "Depreciated? Consider using exists(dfg,label) instead. Returning internals copy"
41+
#TODO: https://github.com/JuliaRobotics/DistributedFactorGraphs.jl/issues/111
4142
copy(x.g.labels.sym_int)
4243
else
4344
getfield(x,f)

0 commit comments

Comments
 (0)