Skip to content

Commit 506905f

Browse files
authored
Merge pull request #124 from JuliaRobotics/feature/cloudSparseAdj
Sparse cloud adj matrix
2 parents 1ac721a + 423f854 commit 506905f

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

src/CloudGraphsDFG/services/CloudGraphsDFG.jl

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -718,25 +718,11 @@ Optionally provide a distance to specify the number of edges should be followed.
718718
Optionally provide an existing subgraph addToDFG, the extracted nodes will be copied into this graph. By default a new subgraph will be created.
719719
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.
720720
"""
721-
function getSubgraphAroundNode(dfg::CloudGraphsDFG, node::DFGNode, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::Union{Nothing, CloudGraphsDFG}=nothing)::CloudGraphsDFG
721+
function getSubgraphAroundNode(dfg::CloudGraphsDFG, node::DFGNode, distance::Int64=1, includeOrphanFactors::Bool=false, addToDFG::AbstractDFG=_getDuplicatedEmptyDFG(dfg))::AbstractDFG
722722
distance < 1 && error("getSubgraphAroundNode() only works for distance > 0")
723723

724724
# Making a copy session if not specified
725-
if addToDFG == nothing
726-
addToDFG = _getDuplicatedEmptyDFG(dfg)
727-
end
728-
729-
# Thank you Neo4j for 0..* awesomeness!!
730-
neighborList = _getLabelsFromCyphonQuery(dfg.neo4jInstance, "(n:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):$(node.label))-[FACTORGRAPH*0..$distance]-(node:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId))")
731-
732-
# Copy the section of graph we want
733-
_copyIntoGraph!(dfg, addToDFG, neighborList, includeOrphanFactors)
734-
return addToDFG
735-
end
736-
737-
function getSubgraphAroundNode(dfg::CloudGraphsDFG{<:AbstractParams}, node::DFGNode, distance::Int64, includeOrphanFactors::Bool, addToDFG::MetaGraphsDFG{AbstractParams})::AbstractDFG
738-
distance < 1 && error("getSubgraphAroundNode() only works for distance > 0")
739-
725+
#moved to parameter addToDFG::AbstractDFG=_getDuplicatedEmptyDFG(dfg)
740726

741727
# Thank you Neo4j for 0..* awesomeness!!
742728
neighborList = _getLabelsFromCyphonQuery(dfg.neo4jInstance, "(n:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId):$(node.label))-[FACTORGRAPH*0..$distance]-(node:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId))")
@@ -801,6 +787,33 @@ function getAdjacencyMatrix(dfg::CloudGraphsDFG)::Matrix{Union{Nothing, Symbol}}
801787
return adjMat
802788
end
803789

790+
function getAdjacencyMatrixSparse(dfg::CloudGraphsDFG)::Tuple{SparseMatrixCSC, Vector{Symbol}, Vector{Symbol}}
791+
varLabels = getVariableIds(dfg)
792+
factLabels = sort(getFactorIds(dfg))
793+
vDict = Dict(varLabels .=> [1:length(varLabels)...].+1)
794+
fDict = Dict(factLabels .=> [1:length(factLabels)...].+1)
795+
796+
adjMat = spzeros(Int, length(factLabels)+1, length(varLabels)+1)
797+
798+
# Now ask for all relationships for this session graph
799+
loadtx = transaction(dfg.neo4jInstance.connection)
800+
query = "START n=node(*) MATCH (n:VARIABLE:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId))-[r:FACTORGRAPH]-(m:FACTOR:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId)) RETURN n.label as variable, m.label as factor;"
801+
nodes = loadtx(query; submit=true)
802+
# Have to finish the transaction
803+
commit(loadtx)
804+
if length(nodes.errors) > 0
805+
error(string(nodes.errors))
806+
end
807+
# Add in the relationships
808+
varRel = Symbol.(map(node -> node["row"][1], nodes.results[1]["data"]))
809+
factRel = Symbol.(map(node -> node["row"][2], nodes.results[1]["data"]))
810+
for i = 1:length(varRel)
811+
adjMat[fDict[factRel[i]], vDict[varRel[i]]] = 1
812+
end
813+
814+
return adjMat, varLabels, factLabels
815+
end
816+
804817
# """
805818
# $(SIGNATURES)
806819
# Produces a dot-format of the graph for visualization.

0 commit comments

Comments
 (0)