Skip to content

Commit 257ebc8

Browse files
authored
Merge pull request #170 from JuliaRobotics/feature/IIFTests_cgdfg
Fixing CGDFG and 0.5 cleanup
2 parents 10ab05b + 508a1c0 commit 257ebc8

23 files changed

+647
-367
lines changed

Project.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DistributedFactorGraphs"
22
uuid = "b5cc3c7e-6572-11e9-2517-99fb8daf2f04"
3-
version = "0.4.1"
3+
version = "0.5.0"
44

55
[deps]
66
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
@@ -13,6 +13,7 @@ JSON2 = "2535ab7d-5cd8-5a07-80ac-9b1792aadce3"
1313
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
1414
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1515
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
16+
Neo4j = "d2adbeaf-5838-5367-8a2f-e46d570981db"
1617
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1718
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1819
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
@@ -22,15 +23,15 @@ Distributions = "≥ 0.18"
2223
DocStringExtensions = "≥ 0.7"
2324
Graphs = "≥ 0.10.3"
2425
MetaGraphs = "≥ 0.6.4"
26+
Neo4j = "≥ 2.0.0"
2527
Reexport = "≥ 0.2"
2628
Requires = "≥ 0.5"
2729
julia = "0.7, 1"
2830

2931
[extras]
3032
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3133
GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231"
32-
Neo4j = "d2adbeaf-5838-5367-8a2f-e46d570981db"
3334
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
3435

3536
[targets]
36-
test = ["Test", "GraphPlot", "Neo4j", "Pkg"]
37+
test = ["Test", "GraphPlot", "Pkg"]

src/CloudGraphsDFG/CloudGraphsDFG.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ include("entities/CloudGraphsDFG.jl")
55

66
# Services
77
include("services/CommonFunctions.jl")
8+
include("services/CGStructure.jl")
89
include("services/CloudGraphsDFG.jl")
910

1011
# Exports
1112
export Neo4jInstance, CloudGraphsDFG
1213
export exists
1314
export clearSession!
14-
export getLabelDict, getDescription, setDescription, getInnerGraph, getAddHistory, getSolverParams, setSolverParams
15+
export getLabelDict, getDescription, setDescription, getAddHistory, getSolverParams, setSolverParams
1516

1617
export getAddHistory, getDescription, getLabelDict
1718
export addVariable!, addFactor!
1819
export ls, lsf, getVariables, getFactors, getVariableIds, getFactorIds
1920
export getVariable, getFactor
20-
export updateVariable!, updateFactor!, updateVariableSolverData!
21+
export updateVariable!, updateFactor!, mergeUpdateVariableSolverData!
2122
export deleteVariable!, deleteFactor!
2223
export getAdjacencyMatrix
2324
export getNeighbors
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Additional exports
2+
export copySession!
3+
# Please be careful with these
4+
# With great power comes great "Oh crap, I deleted everything..."
5+
export clearSession!!, clearRobot!!, clearUser!!
6+
7+
8+
"""
9+
$(SIGNATURES)
10+
DANGER: Clears the whole session from the database.
11+
"""
12+
function clearSession!!(dfg::CloudGraphsDFG)::Nothing
13+
# Perform detach+deletion
14+
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId)) detach delete node ")
15+
16+
# Clearing history
17+
dfg.addHistory = Symbol[]
18+
empty!(dfg.variableCache)
19+
empty!(dfg.factorCache)
20+
empty!(dfg.labelDict)
21+
return nothing
22+
end
23+
24+
"""
25+
$(SIGNATURES)
26+
DANGER: Clears the whole robot + sessions from the database.
27+
"""
28+
function clearRobot!!(dfg::CloudGraphsDFG)::Nothing
29+
# Perform detach+deletion
30+
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId):$(dfg.robotId)) detach delete node ")
31+
32+
# Clearing history
33+
dfg.addHistory = Symbol[]
34+
empty!(dfg.variableCache)
35+
empty!(dfg.factorCache)
36+
empty!(dfg.labelDict)
37+
return nothing
38+
end
39+
40+
"""
41+
$(SIGNATURES)
42+
DANGER: Clears the whole user + robot + sessions from the database.
43+
"""
44+
function clearUser!!(dfg::CloudGraphsDFG)::Nothing
45+
# Perform detach+deletion
46+
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId)) detach delete node ")
47+
48+
# Clearing history
49+
dfg.addHistory = Symbol[]
50+
empty!(dfg.variableCache)
51+
empty!(dfg.factorCache)
52+
empty!(dfg.labelDict)
53+
return nothing
54+
end
55+
56+
"""
57+
$(SIGNATURES)
58+
DANGER: Copies and overwrites the destination session.
59+
If no destination specified then it creates a unique one.
60+
"""
61+
function copySession!(sourceDFG::CloudGraphsDFG, destDFG::Union{Nothing, CloudGraphsDFG})::CloudGraphsDFG
62+
if destDFG == nothing
63+
destDFG = _getDuplicatedEmptyDFG(sourceDFG)
64+
end
65+
_copyIntoGraph!(sourceDFG, destDFG, union(getVariableIds(sourceDFG), getFactorIds(sourceDFG)), true)
66+
return destDFG
67+
end
68+
"""
69+
$(SIGNATURES)
70+
DANGER: Copies the source to a new unique destination.
71+
"""
72+
copySession!(sourceDFG::CloudGraphsDFG)::CloudGraphsDFG = copySession!(sourceDFG, nothing)
73+
74+
75+
getUserData(dfg::CloudGraphsDFG)::Dict{Symbol, String} = _getNodeProperty(dfg.neo4jInstance, [dfg.userId, "USER"])
76+
function setUserData(dfg::CloudGraphsDFG, data::Dict{Symbol, String})::Bool
77+
error("Not implemented yet")
78+
return true
79+
end
80+
getRobotData(dfg::CloudGraphsDFG)::Dict{Symbol, String} = _getNodeProperty(dfg.neo4jInstance, [dfg.userId, dfg.robotId, "ROBOT"])
81+
function setRobotData(dfg::CloudGraphsDFG, data::Dict{Symbol, String})::Bool
82+
error("Not implemented yet")
83+
return true
84+
end
85+
getSessionData(dfg::CloudGraphsDFG)::Dict{Symbol, String} = _getNodeProperty(dfg.neo4jInstance, [dfg.userId, dfg.robotId, dfg.sessionId, "SESSION"])
86+
function setSessionData(dfg::CloudGraphsDFG, data::Dict{Symbol, String})::Bool
87+
error("Not implemented yet")
88+
return true
89+
end

src/CloudGraphsDFG/services/CloudGraphsDFG.jl

Lines changed: 30 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
# Additional exports
2-
export copySession!
3-
# Please be careful with these
4-
# With great power comes great "Oh crap, I deleted everything..."
5-
export clearSession!!, clearRobot!!, clearUser!!
6-
7-
function _getNodeType(dfg::CloudGraphsDFG, nodeLabel::Symbol)::Symbol
8-
dfg.useCache && haskey(dfg.variableDict, nodeLabel) && return :VARIABLE
9-
dfg.useCache && haskey(dfg.factorDict, nodeLabel) && return :FACTOR
10-
nodeId = nothing
11-
if dfg.useCache && haskey(dfg.labelDict)
12-
nodeId = dfg.labelDict[nodeLabel]
13-
else
14-
nodeId = _tryGetNeoNodeIdFromNodeLabel(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, nodeLabel)
15-
end
16-
# Erk, little expensive to do this...
17-
nodeId == nothing && error("Cannot find node with label '$nodeLabel'!")
18-
labels = getnodelabels(getnode(dfg.neo4jInstance.graph, nodeId))
19-
"VARIABLE" in labels && return :VARIABLE
20-
"FACTOR" in labels && return :FACTOR
21-
error("Node with label '$nodeLabel' has neither FACTOR nor VARIABLE labels")
22-
end
23-
24-
## End
251

262
"""
273
$(SIGNATURES)
@@ -92,70 +68,20 @@ function exists(dfg::CloudGraphsDFG, node::N) where N <: DFGNode
9268
end
9369

9470
"""
95-
$(SIGNATURES)
96-
DANGER: Clears the whole session from the database.
97-
"""
98-
function clearSession!!(dfg::CloudGraphsDFG)::Nothing
99-
# Perform detach+deletion
100-
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId)) detach delete node ")
71+
$SIGNATURES
10172
102-
# Clearing history
103-
dfg.addHistory = Symbol[]
104-
empty!(dfg.variableCache)
105-
empty!(dfg.factorCache)
106-
empty!(dfg.labelDict)
107-
return nothing
108-
end
109-
110-
"""
111-
$(SIGNATURES)
112-
DANGER: Clears the whole robot + sessions from the database.
73+
Return whether `sym::Symbol` represents a variable vertex in the graph.
11374
"""
114-
function clearRobot!!(dfg::CloudGraphsDFG)::Nothing
115-
# Perform detach+deletion
116-
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId):$(dfg.robotId)) detach delete node ")
117-
118-
# Clearing history
119-
dfg.addHistory = Symbol[]
120-
empty!(dfg.variableCache)
121-
empty!(dfg.factorCache)
122-
empty!(dfg.labelDict)
123-
return nothing
124-
end
75+
isVariable(dfg::CloudGraphsDFG, sym::Symbol)::Bool =
76+
"VARIABLE" in _getNodeTags(dfg.neo4jInstance, [dfg.userId, dfg.robotId, dfg.sessionId, String(sym)])
12577

12678
"""
127-
$(SIGNATURES)
128-
DANGER: Clears the whole user + robot + sessions from the database.
129-
"""
130-
function clearUser!!(dfg::CloudGraphsDFG)::Nothing
131-
# Perform detach+deletion
132-
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId)) detach delete node ")
133-
134-
# Clearing history
135-
dfg.addHistory = Symbol[]
136-
empty!(dfg.variableCache)
137-
empty!(dfg.factorCache)
138-
empty!(dfg.labelDict)
139-
return nothing
140-
end
79+
$SIGNATURES
14180
81+
Return whether `sym::Symbol` represents a factor vertex in the graph.
14282
"""
143-
$(SIGNATURES)
144-
DANGER: Copies and overwrites the destination session.
145-
If no destination specified then it creates a unique one.
146-
"""
147-
function copySession!(sourceDFG::CloudGraphsDFG, destDFG::Union{Nothing, CloudGraphsDFG})::CloudGraphsDFG
148-
if destDFG == nothing
149-
destDFG = _getDuplicatedEmptyDFG(sourceDFG)
150-
end
151-
_copyIntoGraph!(sourceDFG, destDFG, union(getVariableIds(sourceDFG), getFactorIds(sourceDFG)), true)
152-
return destDFG
153-
end
154-
"""
155-
$(SIGNATURES)
156-
DANGER: Copies the source to a new unique destination.
157-
"""
158-
copySession!(sourceDFG::CloudGraphsDFG)::CloudGraphsDFG = copySession!(sourceDFG, nothing)
83+
isFactor(dfg::CloudGraphsDFG, sym::Symbol)::Bool =
84+
"FACTOR" in _getNodeTags(dfg.neo4jInstance, [dfg.userId, dfg.robotId, dfg.sessionId, String(sym)])
15985

16086
"""
16187
$(SIGNATURES)
@@ -392,13 +318,17 @@ end
392318
$(SIGNATURES)
393319
Update solver and estimate data for a variable (variable can be from another graph).
394320
"""
395-
function updateVariableSolverData!(dfg::CloudGraphsDFG, sourceVariable::DFGVariable)::DFGVariable
321+
function mergeUpdateVariableSolverData!(dfg::CloudGraphsDFG, sourceVariable::DFGVariable)::DFGVariable
396322
if !exists(dfg, sourceVariable)
397323
error("Source variable '$(sourceVariable.label)' doesn't exist in the graph.")
398324
end
399-
nodeId = _tryGetNeoNodeIdFromNodeLabel(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, sourceVariable.label)
400-
Neo4j.setnodeproperty(dfg.neo4jInstance.graph, nodeId, "estimateDict", JSON2.write(sourceVariable.estimateDict))
401-
Neo4j.setnodeproperty(dfg.neo4jInstance.graph, nodeId, "solverDataDict", JSON2.write(Dict(keys(sourceVariable.solverDataDict) .=> map(vnd -> pack(dfg, vnd), values(sourceVariable.solverDataDict)))))
325+
var = getVariable(dfg, sourceVariable.label)
326+
newEsts = merge!(var.estimateDict, deepcopy(sourceVariable.estimateDict))
327+
newSolveData = merge!(var.solverDataDict, sourceVariable.solverDataDict)
328+
Neo4j.setnodeproperty(dfg.neo4jInstance.graph, var._internalId, "estimateDict",
329+
JSON2.write(newEsts))
330+
Neo4j.setnodeproperty(dfg.neo4jInstance.graph, var._internalId, "solverDataDict",
331+
JSON2.write(Dict(keys(newSolveData) .=> map(vnd -> pack(dfg, vnd), values(newSolveData)))))
402332
return sourceVariable
403333
end
404334

@@ -659,19 +589,20 @@ hasOrphans(dfg::CloudGraphsDFG)::Bool = !isFullyConnected(dfg)
659589
Retrieve a list of labels of the immediate neighbors around a given variable or factor.
660590
"""
661591
function getNeighbors(dfg::CloudGraphsDFG, node::T; ready::Union{Nothing, Int}=nothing, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol} where T <: DFGNode
662-
query = "(n:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):$(node.label))--(node) where node:VARIABLE or node:FACTOR "
592+
query = "(n:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):$(node.label))--(node) where (node:VARIABLE or node:FACTOR) "
663593
if ready != nothing || backendset != nothing
664594
if ready != nothing
665-
query = query * "and node.ready = $(ready)"
595+
query = query * " and node.ready = $(ready)"
666596
end
667597
if backendset != nothing
668-
query = query * "and node.backendset = $(backendset)"
598+
query = query * " and node.backendset = $(backendset)"
669599
end
670600
end
601+
@debug "[Query] $query"
671602
neighbors = _getLabelsFromCyphonQuery(dfg.neo4jInstance, query)
672603
# If factor, need to do variable ordering
673-
if T == DFGFactor
674-
order = intersect(node._variableOrderSymbols, map(v->v.dfgNode.label, neighbors))
604+
if T <: DFGFactor
605+
neighbors = intersect(node._variableOrderSymbols, neighbors)
675606
end
676607
return neighbors
677608
end
@@ -681,20 +612,22 @@ end
681612
Retrieve a list of labels of the immediate neighbors around a given variable or factor specified by its label.
682613
"""
683614
function getNeighbors(dfg::CloudGraphsDFG, label::Symbol; ready::Union{Nothing, Int}=nothing, backendset::Union{Nothing, Int}=nothing)::Vector{Symbol}
684-
query = "(n:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):$(label))--(node) where node:VARIABLE or node:FACTOR "
615+
query = "(n:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):$(label))--(node) where (node:VARIABLE or node:FACTOR) "
685616
if ready != nothing || backendset != nothing
686617
if ready != nothing
687-
query = query * "and node.ready = $(ready)"
618+
query = query * " and node.ready = $(ready)"
688619
end
689620
if backendset != nothing
690-
query = query * "and node.backendset = $(backendset)"
621+
query = query * " and node.backendset = $(backendset)"
691622
end
692623
end
624+
@debug "[Query] $query"
693625
neighbors = _getLabelsFromCyphonQuery(dfg.neo4jInstance, query)
694626
# If factor, need to do variable ordering
695-
if _getNodeType(dfg, label) == :FACTOR
696-
factor = getFactor(dfg, label)
697-
order = intersect(factor._variableOrderSymbols, neighbors)
627+
if isFactor(dfg, label)
628+
# Server is authority
629+
serverOrder = Symbol.(JSON2.read(_getNodeProperty(dfg.neo4jInstance, [dfg.userId, dfg.robotId, dfg.sessionId, String(label)], "_variableOrderSymbols")))
630+
neighbors = intersect(serverOrder, neighbors)
698631
end
699632
return neighbors
700633
end

0 commit comments

Comments
 (0)