Skip to content

Commit f6c124d

Browse files
committed
Fixing CGDFG
1 parent a2e85ec commit f6c124d

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

src/CloudGraphsDFG/services/CloudGraphsDFG.jl

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,6 @@ export copySession!
44
# With great power comes great "Oh crap, I deleted everything..."
55
export clearSession!!, clearRobot!!, clearUser!!
66

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-
247
## End
258

269
"""
@@ -157,6 +140,22 @@ DANGER: Copies the source to a new unique destination.
157140
"""
158141
copySession!(sourceDFG::CloudGraphsDFG)::CloudGraphsDFG = copySession!(sourceDFG, nothing)
159142

143+
"""
144+
$SIGNATURES
145+
146+
Return whether `sym::Symbol` represents a variable vertex in the graph.
147+
"""
148+
isVariable(dfg::CloudGraphsDFG, sym::Symbol)::Bool =
149+
"VARIABLE" in _getNodeTags(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, sym)
150+
151+
"""
152+
$SIGNATURES
153+
154+
Return whether `sym::Symbol` represents a factor vertex in the graph.
155+
"""
156+
isFactor(dfg::CloudGraphsDFG, sym::Symbol)::Bool =
157+
"FACTOR" in _getNodeTags(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, sym)
158+
160159
"""
161160
$(SIGNATURES)
162161
Add a DFGVariable to a DFG.
@@ -659,15 +658,16 @@ hasOrphans(dfg::CloudGraphsDFG)::Bool = !isFullyConnected(dfg)
659658
Retrieve a list of labels of the immediate neighbors around a given variable or factor.
660659
"""
661660
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 "
661+
query = "(n:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):$(node.label))--(node) where (node:VARIABLE or node:FACTOR) "
663662
if ready != nothing || backendset != nothing
664663
if ready != nothing
665-
query = query * "and node.ready = $(ready)"
664+
query = query * " and node.ready = $(ready)"
666665
end
667666
if backendset != nothing
668-
query = query * "and node.backendset = $(backendset)"
667+
query = query * " and node.backendset = $(backendset)"
669668
end
670669
end
670+
@debug "[Query] $query"
671671
neighbors = _getLabelsFromCyphonQuery(dfg.neo4jInstance, query)
672672
# If factor, need to do variable ordering
673673
if T <: DFGFactor
@@ -681,21 +681,22 @@ end
681681
Retrieve a list of labels of the immediate neighbors around a given variable or factor specified by its label.
682682
"""
683683
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 "
684+
query = "(n:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):$(label))--(node) where (node:VARIABLE or node:FACTOR) "
685685
if ready != nothing || backendset != nothing
686686
if ready != nothing
687-
query = query * "and node.ready = $(ready)"
687+
query = query * " and node.ready = $(ready)"
688688
end
689689
if backendset != nothing
690-
query = query * "and node.backendset = $(backendset)"
690+
query = query * " and node.backendset = $(backendset)"
691691
end
692692
end
693+
@debug "[Query] $query"
693694
neighbors = _getLabelsFromCyphonQuery(dfg.neo4jInstance, query)
694695
# If factor, need to do variable ordering
695-
if _getNodeType(dfg, label) == :FACTOR
696-
# TODO: Implement shortcut for this.
697-
factor = getFactor(dfg, label)
698-
neighbors = intersect(factor._variableOrderSymbols, neighbors)
696+
if isFactor(dfg, label)
697+
# Server is authority
698+
serverOrder = Symbol.(JSON2.read(_getNodeProperty(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, label, "_variableOrderSymbols")))
699+
neighbors = intersect(serverOrder, neighbors)
699700
end
700701
return neighbors
701702
end

src/CloudGraphsDFG/services/CommonFunctions.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ function _getLabelsFromCyphonQuery(neo4jInstance::Neo4jInstance, matchCondition:
3333
return Symbol.(nodeIds)
3434
end
3535

36+
"""
37+
$(SIGNATURES)
38+
Get a node property - returns nothing if not found
39+
"""
40+
function _getNodeProperty(neo4jInstance::Neo4jInstance, userId::String, robotId::String, sessionId::String, nodeId::Symbol, property::String)
41+
query = "match (n:$userId:$robotId:$sessionId:$nodeId) return n.$property"
42+
result = DistributedFactorGraphs._queryNeo4j(neo4jInstance, query)
43+
44+
return result.results[1]["data"][1]["row"][1]
45+
end
46+
47+
"""
48+
$(SIGNATURES)
49+
Get a node's tags
50+
"""
51+
function _getNodeTags(neo4jInstance::Neo4jInstance, userId::String, robotId::String, sessionId::String, nodeId::Symbol)::Union{Nothing, Vector{String}}
52+
query = "match (n:$userId:$robotId:$sessionId:$nodeId) return labels(n)"
53+
result = DistributedFactorGraphs._queryNeo4j(neo4jInstance, query)
54+
length(result.results[1]["data"]) != 1 && return nothing
55+
return result.results[1]["data"][1]["row"][1]
56+
end
57+
3658
"""
3759
$(SIGNATURES)
3860
Returns the list of CloudGraph nodes that matches the Cyphon query.

0 commit comments

Comments
 (0)