Skip to content

Commit dbae574

Browse files
authored
API Standardization through testing (#276)
* API standardization through testing WIP * deprecated clean up * listVariables, listFactors, add update return type * **TODO removed compares == to be fixed later** * fix for julia 1.0 in test * Exists, getVariables/factors isVar/Fac, move attic * no interface tests * TODO compare and confirm pack/unpack changes. Compare skiped, rest pass * IIF_TESTS does not work on julia 1.0
1 parent eea8cb5 commit dbae574

30 files changed

+1116
-503
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ services:
1313
- neo4j
1414

1515
julia:
16-
- 1.0
17-
#FIXME# - 1.2
16+
#FIXME# - 1.0
17+
- 1.2
1818
#FIXME# - 1.3
1919
#FIXME# - nightly
2020

2121
env:
22-
- IIF_TEST=false
22+
- IIF_TEST=true
2323

2424
#FIXME#jobs:
2525
#FIXME# include:

src/CloudGraphsDFG/services/CGStructure.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ function copySession!(sourceDFG::CloudGraphsDFG, destDFG::Union{Nothing, CloudGr
282282
if destDFG == nothing
283283
destDFG = _getDuplicatedEmptyDFG(sourceDFG)
284284
end
285-
_copyIntoGraph!(sourceDFG, destDFG, union(getVariableIds(sourceDFG), getFactorIds(sourceDFG)), true)
285+
_copyIntoGraph!(sourceDFG, destDFG, union(listVariables(sourceDFG), listFactors(sourceDFG)), true)
286286
return destDFG
287287
end
288288
"""

src/CloudGraphsDFG/services/CloudGraphsDFG.jl

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ isVariable(dfg::CloudGraphsDFG, sym::Symbol)::Bool =
6969
isFactor(dfg::CloudGraphsDFG, sym::Symbol)::Bool =
7070
_getNodeCount(dfg.neo4jInstance, ["FACTOR", dfg.userId, dfg.robotId, dfg.sessionId, String(sym)]) == 1
7171

72-
function addVariable!(dfg::CloudGraphsDFG, variable::DFGVariable)::Bool
72+
function addVariable!(dfg::CloudGraphsDFG, variable::DFGVariable)::DFGVariable
7373
if exists(dfg, variable)
7474
error("Variable '$(variable.label)' already exists in the factor graph")
7575
end
@@ -89,10 +89,14 @@ function addVariable!(dfg::CloudGraphsDFG, variable::DFGVariable)::Bool
8989
# Track insertion
9090
push!(dfg.addHistory, variable.label)
9191

92-
return true
92+
return variable
93+
end
94+
95+
function addFactor!(dfg::CloudGraphsDFG, factor::DFGFactor)
96+
addFactor!(dfg, factor._variableOrderSymbols, factor)
9397
end
9498

95-
function addFactor!(dfg::CloudGraphsDFG, variables::Vector{DFGVariable}, factor::DFGFactor)::Bool
99+
function addFactor!(dfg::CloudGraphsDFG, variables::Vector{<:DFGVariable}, factor::DFGFactor)::DFGFactor
96100
if exists(dfg, factor)
97101
error("Factor '$(factor.label)' already exists in the factor graph")
98102
end
@@ -120,10 +124,10 @@ function addFactor!(dfg::CloudGraphsDFG, variables::Vector{DFGVariable}, factor:
120124
# Track insertion only for variables
121125
# push!(dfg.addHistory, factor.label
122126

123-
return true
127+
return factor
124128
end
125129

126-
function addFactor!(dfg::CloudGraphsDFG, variableIds::Vector{Symbol}, factor::DFGFactor)::Bool
130+
function addFactor!(dfg::CloudGraphsDFG, variableIds::Vector{Symbol}, factor::DFGFactor)::DFGFactor
127131
variables = map(vId -> getVariable(dfg, vId), variableIds)
128132
return addFactor!(dfg, variables, factor)
129133
end
@@ -187,7 +191,8 @@ end
187191

188192
function updateVariable!(dfg::CloudGraphsDFG, variable::DFGVariable)::DFGVariable
189193
if !exists(dfg, variable)
190-
error("Variable label '$(variable.label)' does not exist in the factor graph")
194+
@warn "Variable label '$(variable.label)' does not exist in the factor graph, adding"
195+
return addVariable!(dfg, variable)
191196
end
192197
nodeId = _tryGetNeoNodeIdFromNodeLabel(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, variable.label)
193198
# Update the node ID
@@ -217,7 +222,8 @@ end
217222

218223
function updateFactor!(dfg::CloudGraphsDFG, factor::DFGFactor)::DFGFactor
219224
if !exists(dfg, factor)
220-
error("Factor label '$(factor.label)' does not exist in the factor graph")
225+
@warn "Factor label '$(factor.label)' does not exist in the factor graph, adding"
226+
return addFactor!(dfg, factor)
221227
end
222228
nodeId = _tryGetNeoNodeIdFromNodeLabel(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, factor.label)
223229
# Update the _internalId
@@ -316,7 +322,7 @@ end
316322
deleteFactor!(dfg::CloudGraphsDFG, factor::DFGFactor)::DFGFactor = deleteFactor!(dfg, factor.label)
317323

318324
function getVariables(dfg::CloudGraphsDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[], solvable::Int=0)::Vector{DFGVariable}
319-
variableIds = getVariableIds(dfg, regexFilter, tags=tags, solvable=solvable)
325+
variableIds = listVariables(dfg, regexFilter, tags=tags, solvable=solvable)
320326
# TODO: Optimize to use tags in query here!
321327
variables = map(vId->getVariable(dfg, vId), variableIds)
322328
if length(tags) > 0
@@ -326,7 +332,7 @@ function getVariables(dfg::CloudGraphsDFG, regexFilter::Union{Nothing, Regex}=no
326332
return variables
327333
end
328334

329-
function getVariableIds(dfg::CloudGraphsDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[], solvable::Int=0)::Vector{Symbol}
335+
function listVariables(dfg::CloudGraphsDFG, regexFilter::Union{Nothing, Regex}=nothing; tags::Vector{Symbol}=Symbol[], solvable::Int=0)::Vector{Symbol}
330336
# Optimized for DB call
331337
tagsFilter = length(tags) > 0 ? " and "*join("node:".*String.(tags), " or ") : ""
332338
if regexFilter == nothing
@@ -337,11 +343,11 @@ function getVariableIds(dfg::CloudGraphsDFG, regexFilter::Union{Nothing, Regex}=
337343
end
338344

339345
function getFactors(dfg::CloudGraphsDFG, regexFilter::Union{Nothing, Regex}=nothing; solvable::Int=0)::Vector{DFGFactor}
340-
factorIds = getFactorIds(dfg, regexFilter, solvable=solvable)
346+
factorIds = listFactors(dfg, regexFilter, solvable=solvable)
341347
return map(vId->getFactor(dfg, vId), factorIds)
342348
end
343349

344-
function getFactorIds(dfg::CloudGraphsDFG, regexFilter::Union{Nothing, Regex}=nothing; solvable::Int=0)::Vector{Symbol}
350+
function listFactors(dfg::CloudGraphsDFG, regexFilter::Union{Nothing, Regex}=nothing; solvable::Int=0)::Vector{Symbol}
345351
# Optimized for DB call
346352
if regexFilter == nothing
347353
return _getLabelsFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):FACTOR) where node.solvable >= $solvable")
@@ -353,8 +359,8 @@ end
353359
function isFullyConnected(dfg::CloudGraphsDFG)::Bool
354360
# If the total number of nodes == total number of distinct connected nodes, then it is fully connected
355361
# Total nodes
356-
varIds = getVariableIds(dfg)
357-
factIds = getFactorIds(dfg)
362+
varIds = listVariables(dfg)
363+
factIds = listFactors(dfg)
358364
length(varIds) + length(factIds) == 0 && return false
359365

360366
# Total connected nodes - thank you Neo4j for 0..* awesomeness!!
@@ -424,8 +430,8 @@ function getSubgraph(dfg::CloudGraphsDFG,
424430
end
425431

426432
function getIncidenceMatrix(dfg::CloudGraphsDFG; solvable::Int=0)::Matrix{Union{Nothing, Symbol}}
427-
varLabels = sort(getVariableIds(dfg, solvable=solvable))
428-
factLabels = sort(getFactorIds(dfg, solvable=solvable))
433+
varLabels = sort(listVariables(dfg, solvable=solvable))
434+
factLabels = sort(listFactors(dfg, solvable=solvable))
429435
vDict = Dict(varLabels .=> [1:length(varLabels)...].+1)
430436
fDict = Dict(factLabels .=> [1:length(factLabels)...].+1)
431437

@@ -454,8 +460,8 @@ function getIncidenceMatrix(dfg::CloudGraphsDFG; solvable::Int=0)::Matrix{Union{
454460
end
455461

456462
function getIncidenceMatrixSparse(dfg::CloudGraphsDFG; solvable::Int=0)::Tuple{SparseMatrixCSC, Vector{Symbol}, Vector{Symbol}}
457-
varLabels = getVariableIds(dfg, solvable=solvable)
458-
factLabels = getFactorIds(dfg, solvable=solvable)
463+
varLabels = listVariables(dfg, solvable=solvable)
464+
factLabels = listFactors(dfg, solvable=solvable)
459465
vDict = Dict(varLabels .=> [1:length(varLabels)...])
460466
fDict = Dict(factLabels .=> [1:length(factLabels)...])
461467

src/Common.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ DevNotes:
348348
349349
Related
350350
351-
ls, getVariableIds, findClosestTimestamp
351+
ls, listVariables, findClosestTimestamp
352352
"""
353353
function findVariableNearTimestamp(dfg::AbstractDFG,
354354
timest::DateTime,
@@ -359,8 +359,8 @@ function findVariableNearTimestamp(dfg::AbstractDFG,
359359
number::Int=1 )::Vector{Tuple{Vector{Symbol}, Millisecond}}
360360
#
361361
# get the variable labels based on filters
362-
# syms = getVariableIds(dfg, regexFilter, tags=tags, solvable=solvable)
363-
syms = getVariableIds(dfg, regexFilter, tags=tags, solvable=solvable)
362+
# syms = listVariables(dfg, regexFilter, tags=tags, solvable=solvable)
363+
syms = listVariables(dfg, regexFilter, tags=tags, solvable=solvable)
364364
# compile timestamps with label
365365
# vars = map( x->getVariable(dfg, x), syms )
366366
timeset = map(x->(getTimestamp(getVariable(dfg,x)), x), syms)
@@ -406,7 +406,8 @@ end
406406
Add tags to a variable or factor
407407
"""
408408
function addTags!(dfg::InMemoryDFGTypes, sym::Symbol, tags::Vector{Symbol})
409-
union!(getTags(getFactor(fg, sym)), tags)
409+
getFnc = isVariable(dfg,sym) ? getVariable : getFactor
410+
union!(getTags(getFnc(dfg, sym)), tags)
410411
end
411412

412413

src/CommonAccessors.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $SIGNATURES
2222
2323
Set the tags for a node.
2424
"""
25-
function setTags!(f::DataLevel0, tags::Vector{Symbol})
25+
function setTags!(f::DataLevel0, tags::Union{Vector{Symbol},Set{Symbol}})
2626
empty!(f.tags)
2727
union!(f.tags, tags)
2828
end
@@ -53,6 +53,14 @@ end
5353

5454
setTimestamp!(f::FactorDataLevel1, ts::DateTime) = f.timestamp = ts
5555

56+
function setTimestamp(f::DFGFactor, ts::DateTime)
57+
return DFGFactor(f.label, ts, f.tags, f.solverData, f.solvable, f._dfgNodeParams, f._variableOrderSymbols)
58+
end
59+
60+
function setTimestamp(f::DFGFactorSummary, ts::DateTime)
61+
return DFGFactorSummary(f.label, ts, f.tags, f._internalId, f._variableOrderSymbols)
62+
end
63+
5664
"""
5765
$SIGNATURES
5866

src/DFGPlots/DFGPlots.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ More information at [GraphPlot.jl](https://github.com/JuliaGraphs/GraphPlot.jl)
8888
function dfgplot(dfg::AbstractDFG, p::DFGPlotProps = DFGPlotProps())
8989
# TODO implement convert functions
9090
ldfg = LightDFG{NoSolverParams}()
91-
DistributedFactorGraphs._copyIntoGraph!(dfg, ldfg, union(getVariableIds(dfg), getFactorIds(dfg)), true, copyGraphMetadata=false)
91+
DistributedFactorGraphs._copyIntoGraph!(dfg, ldfg, union(listVariables(dfg), listFactors(dfg)), true, copyGraphMetadata=false)
9292
dfgplot(ldfg, p)
9393
end
9494

0 commit comments

Comments
 (0)