pgraph: Filter graph should not include unfiltered vertices
This adds a test for another sneaky aspect of filter graph: It should not pull in a vertex because another across the edge was included. If we want this behaviour, then we need a different function or a config option for this filter function.
This commit is contained in:
@@ -512,7 +512,7 @@ func (g *Graph) FilterGraph(name string, vertices []Vertex) (*Graph, error) {
|
||||
}
|
||||
for k2, e := range x {
|
||||
//log.Printf("Filter: %s -> %s # %s", k1.Name, k2.Name, e.Name)
|
||||
if contains || VertexContains(k2, vertices) {
|
||||
if contains && VertexContains(k2, vertices) {
|
||||
newGraph.AddEdge(k1, k2, e)
|
||||
}
|
||||
}
|
||||
@@ -538,7 +538,8 @@ func (g *Graph) DisconnectedGraphs() ([]*Graph, error) {
|
||||
// dfs through the graph
|
||||
dfs := g.DFS(start)
|
||||
// filter all the collected elements into a new graph
|
||||
newgraph, err := g.FilterGraph(g.Name, dfs)
|
||||
// TODO: is this method of filtering correct here? && or || ?
|
||||
newGraph, err := g.FilterGraph(g.Name, dfs)
|
||||
if err != nil {
|
||||
return nil, errwrap.Wrapf(err, "could not run DisconnectedGraphs() properly")
|
||||
}
|
||||
@@ -546,7 +547,7 @@ func (g *Graph) DisconnectedGraphs() ([]*Graph, error) {
|
||||
d = append(d, dfs...) // extend
|
||||
|
||||
// append this new graph to the list
|
||||
graphs = append(graphs, newgraph)
|
||||
graphs = append(graphs, newGraph)
|
||||
|
||||
// if we've found all the elements, then we're done
|
||||
// otherwise loop through to continue...
|
||||
|
||||
@@ -213,6 +213,33 @@ func TestFilterGraph2(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterGraph3(t *testing.T) {
|
||||
G, _ := NewGraph("g5")
|
||||
v1 := NV("v1")
|
||||
v2 := NV("v2")
|
||||
v3 := NV("v3")
|
||||
v4 := NV("v4")
|
||||
v5 := NV("v5")
|
||||
e1 := NE("e1")
|
||||
e2 := NE("e2")
|
||||
G.AddEdge(v1, v2, e1)
|
||||
G.AddEdge(v3, v4, e2)
|
||||
G.AddVertex(v5)
|
||||
|
||||
save := []Vertex{v1, v2, v3}
|
||||
out, err := G.FilterGraph("new g5", save)
|
||||
if err != nil {
|
||||
t.Errorf("failed with: %v", err)
|
||||
}
|
||||
|
||||
if c, i := len(save), out.NumVertices(); c != i {
|
||||
t.Errorf("should have %d vertices instead of: %d", c, i)
|
||||
}
|
||||
if c, i := 1, out.NumEdges(); c != i {
|
||||
t.Errorf("should have %d edges instead of: %d", c, i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisconnectedGraphs1(t *testing.T) {
|
||||
G, _ := NewGraph("g6")
|
||||
v1 := NV("v1")
|
||||
|
||||
Reference in New Issue
Block a user