@@ -141,6 +141,10 @@ function getVariable(dfg::LightGraphsDFG, variableId::Int64)::DFGVariable
141
141
return get_prop (dfg. g, variableId, :variable )
142
142
end
143
143
144
+ function getVariable (g:: MetaGraph , variableId:: Int64 ):: DFGVariable
145
+ return get_prop (g, variableId, :variable )
146
+ end
147
+
144
148
"""
145
149
$(SIGNATURES)
146
150
Get a DFGVariable from a DFG using its label.
@@ -166,6 +170,13 @@ function getFactor(dfg::LightGraphsDFG, factorId::Int64)::DFGFactor
166
170
return get_prop (dfg. g, factorId, :factor )
167
171
end
168
172
173
+ function getFactor (g:: MetaGraph , factorId:: Int64 ):: DFGFactor
174
+ # if !(factorId in values(dfg.labelDict))
175
+ # error("Factor ID '$(factorId)' does not exist in the factor graph")
176
+ # end
177
+ return get_prop (g, factorId, :factor )
178
+ end
179
+
169
180
"""
170
181
$(SIGNATURES)
171
182
Get a DFGFactor from a DFG using its label.
@@ -422,43 +433,54 @@ function ls(dfg::LightGraphsDFG, label::Symbol)::Vector{Symbol} where T <: DFGNo
422
433
return getNeighbors (dfg, label)
423
434
end
424
435
425
- function _copyIntoGraph! (sourceDFG:: LightGraphsDFG , destDFG:: LightGraphsDFG , variableFactorLabels:: Vector{Symbol} , includeOrphanFactors:: Bool = false ):: Nothing
426
- # Split into variables and factors
427
- verts = map (id -> sourceDFG. g. vertices[sourceDFG. labelDict[id]], variableFactorLabels)
428
- sourceVariables = filter (n -> n. dfgNode isa DFGVariable, verts)
429
- sourceFactors = filter (n -> n. dfgNode isa DFGFactor, verts)
436
+ function _copyIntoGraph! (sourceDFG:: LightGraphsDFG , destDFG:: LightGraphsDFG , ns:: Vector{Int} , includeOrphanFactors:: Bool = false ):: Nothing
437
+ # Split into variables and factors
438
+ subgraph = sourceDFG. g[ns]
439
+ sourceVariableIds = collect (filter_vertices (subgraph, :variable ))
440
+ sourceFactorIds = collect (filter_vertices (subgraph, :factor ))
441
+ # or copy out of sourceDFG.
442
+ # sourceFactorIds = intersect(ns, collect(filter_vertices(sourceDFG.g, :factor)))
443
+
444
+ # get factor and variable nodes
445
+ variables = map (vId-> getVariable (subgraph, vId), sourceVariableIds)
446
+ factors = map (fId-> getFactor (subgraph, fId), sourceFactorIds)
447
+
448
+ # Now we have to add all variables first,
449
+ for v in variables
450
+ if ! haskey (destDFG. labelDict, v. label)
451
+ addVariable! (destDFG, deepcopy (v))
452
+ end
453
+ end
430
454
431
- # Now we have to add all variables first,
432
- for variable in sourceVariables
433
- if ! haskey (destDFG. labelDict, variable. dfgNode. label)
434
- addVariable! (destDFG, deepcopy (variable. dfgNode))
435
- end
436
- end
437
455
# And then all factors to the destDFG.
438
- for factor in sourceFactors
439
- if ! haskey (destDFG. labelDict, factor . dfgNode . label)
456
+ for f in factors
457
+ if ! haskey (destDFG. labelDict, f . label)
440
458
# Get the original factor variables (we need them to create it)
441
- variables = in_neighbors (factor, sourceDFG. g)
459
+ # variables = in_neighbors(factor, sourceDFG.g)
460
+ # variables = getNeighbors(sourceDFG, f)
461
+ varIds = LightGraphs. neighbors (sourceDFG. g, sourceDFG. g[f. label,:label ])
462
+ variables = map (vId-> getVariable (sourceDFG. g, vId), varIds)
463
+
442
464
# Find the labels and associated variables in our new subgraph
443
465
factVariables = DFGVariable[]
444
- for variable in variables
445
- if haskey (destDFG. labelDict, variable . dfgNode . label)
446
- push! (factVariables, getVariable (destDFG, variable . dfgNode . label))
466
+ for v in variables
467
+ if haskey (destDFG. labelDict, v . label)
468
+ push! (factVariables, getVariable (destDFG, v . label))
447
469
# otherwise ignore
448
470
end
449
471
end
450
472
451
473
# Only if we have all of them should we add it (otherwise strange things may happen on evaluation)
452
474
if includeOrphanFactors || length (factVariables) == length (variables)
453
- addFactor! (destDFG, factVariables, deepcopy (factor . dfgNode ))
475
+ addFactor! (destDFG, factVariables, deepcopy (f ))
454
476
end
455
477
end
456
478
end
479
+ @show " Using ME!"
457
480
return nothing
458
481
end
459
482
460
483
461
- # TODO TODO TODO TODO TODO TODO TODO TODO
462
484
"""
463
485
$(SIGNATURES)
464
486
Retrieve a deep subgraph copy around a given variable or factor.
@@ -472,25 +494,29 @@ function getSubgraphAroundNode(dfg::LightGraphsDFG{P}, node::T, distance::Int64=
472
494
end
473
495
474
496
# Build a list of all unique neighbors inside 'distance'
475
- neighborList = Dict {Symbol, Any} ()
476
- push! (neighborList, node. label => dfg. g. vertices[dfg. labelDict[node. label]])
477
- curList = Dict {Symbol, Any} (node. label => dfg. g. vertices[dfg. labelDict[node. label]])
478
- for dist in 1 : distance
479
- newNeighbors = Dict {Symbol, Any} ()
480
- for (key, node) in curList
481
- neighbors = in_neighbors (node, dfg. g) # Don't use out_neighbors! It enforces directiveness even if we don't want it
482
- for neighbor in neighbors
483
- if ! haskey (neighborList, neighbor. dfgNode. label)
484
- push! (neighborList, neighbor. dfgNode. label => neighbor)
485
- push! (newNeighbors, neighbor. dfgNode. label => neighbor)
486
- end
487
- end
488
- end
489
- curList = newNeighbors
490
- end
491
-
492
- # Copy the section of graph we want
493
- _copyIntoGraph! (dfg, addToDFG, collect (keys (neighborList)), includeOrphanFactors)
497
+ # neighborList = Dict{Symbol, Any}()
498
+ # push!(neighborList, node.label => dfg.g.vertices[dfg.labelDict[node.label]])
499
+ # curList = Dict{Symbol, Any}(node.label => dfg.g.vertices[dfg.labelDict[node.label]])
500
+ # for dist in 1:distance
501
+ # newNeighbors = Dict{Symbol, Any}()
502
+ # for (key, node) in curList
503
+ # neighbors = in_neighbors(node, dfg.g) #Don't use out_neighbors! It enforces directiveness even if we don't want it
504
+ # for neighbor in neighbors
505
+ # if !haskey(neighborList, neighbor.dfgNode.label)
506
+ # push!(neighborList, neighbor.dfgNode.label => neighbor)
507
+ # push!(newNeighbors, neighbor.dfgNode.label => neighbor)
508
+ # end
509
+ # end
510
+ # end
511
+ # curList = newNeighbors
512
+ # end
513
+ # Copy the section of graph we want
514
+ # _copyIntoGraph!(dfg, addToDFG, collect(keys(neighborList)), includeOrphanFactors)
515
+
516
+ ns = neighborhood (dfg. g, dfg. g[node. label,:label ], distance)
517
+ # _copyIntoGraph!(dfg, addToDFG, map(n->get_prop(dfg.g, n, :label), ns), includeOrphanFactors)
518
+ # TODO overwrite copyIntoGraph for speed with Int
519
+ _copyIntoGraph! (dfg, addToDFG, ns, includeOrphanFactors)
494
520
return addToDFG
495
521
end
496
522
0 commit comments