Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Visualizer: add node for vertices with incoming edges too
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Apr 20, 2024
1 parent d9b07a6 commit 2d90570
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 13 additions & 1 deletion lib/mosaik/graph/visualizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ def to_dot
# Set of visited edges (to avoid duplicates in undirected graphs)
visited = Set.new

# List of vertices with incoming or outgoing edges
coupled_vertices = graph
.vertices
.values
.select { |v| v.edges.any? } + graph
.vertices
.values
.map { |v| v.edges.keys }
.flatten(2)
.uniq
.map { |id| graph.find_vertex(id) }

[
graph.directed ? "digraph {" : "graph {",
(if graph.clusters.any?
Expand All @@ -41,7 +53,7 @@ def to_dot
.values
.map do |vertex|
[
("\"#{vertex.id}\" [shape=circle, width=1, fixedsize=true, fontsize=12, style=filled, fillcolor=lightblue]" if vertex.edges.any? || !options[:hide_uncoupled]),
("\"#{vertex.id}\" [shape=circle, width=1, fixedsize=true, fontsize=12, style=filled, fillcolor=lightblue]" if vertex.in?(coupled_vertices) || !options[:hide_uncoupled]),
*vertex
.edges
.flat_map do |key, edges|
Expand Down
6 changes: 2 additions & 4 deletions spec/mosaik/graph/visualizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,13 @@
graph.add_vertex("vertex1")
graph.add_vertex("vertex2")
graph.add_vertex("vertex3")
graph.add_edge("vertex1", "vertex2")
graph.add_edge("vertex2", "vertex3", foo: "bar", baz: "bat")
graph.add_edge("vertex1", "vertex2", foo: "bar", baz: "bat")

expect(visualizer.to_dot).to eq <<~DOT
digraph {
"vertex1" [shape=circle, width=1, fixedsize=true, fontsize=12, style=filled, fillcolor=lightblue]
"vertex1" -> "vertex2"
"vertex1" -> "vertex2" [label="foo: bar, baz: bat"]
"vertex2" [shape=circle, width=1, fixedsize=true, fontsize=12, style=filled, fillcolor=lightblue]
"vertex2" -> "vertex3" [label="foo: bar, baz: bat"]
}
DOT
end
Expand Down

0 comments on commit 2d90570

Please sign in to comment.