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

Commit

Permalink
Render graph to file and png
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Mar 31, 2024
1 parent 91f507d commit 17489ad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/mosaik/commands/extract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def call
.new(options, graph)
.tap(&:validate)
.call

# Write graph to file
debug graph.to_dot
graph.to_png("mosaik")

info "Graph written to mosaik.dot and rendered to mosaik.png"
end

private
Expand Down
4 changes: 4 additions & 0 deletions lib/mosaik/graph/edge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def initialize(attributes = {})
@attributes = attributes
end

def to_dot
attributes.map { |k, v| "#{k}=#{v}" }.join(", ")
end

def inspect
"#<#{self.class.name} attributes=#{attributes.map { |k, v| "#{k}: #{v}" }.join(',')}>"
end
Expand Down
13 changes: 13 additions & 0 deletions lib/mosaik/graph/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ def tsort_each_child(vertex, &)
end
end

def to_dot
<<~DOT
digraph {
#{vertices.values.map(&:to_dot).join("\n ")}
}
DOT
end

def to_png(file)
File.write("#{file}.dot", to_dot)
system("dot -Tpng #{file}.dot -o #{file}.png")
end

def inspect
"#<#{self.class.name} vertices=#{vertices.values.map(&:inspect)}>"
end
Expand Down
8 changes: 8 additions & 0 deletions lib/mosaik/graph/vertex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def remove_edge(id)
edges.delete(id)
end

def to_dot
edges.map do |to, edges|
edges.map do |edge|
"\"#{id}\" -> \"#{to}\" [#{edge.to_dot}]"
end
end.flatten.join("\n ")
end

def inspect
"#<#{self.class.name} id=#{id} attributes=#{attributes.map { |k, v| "#{k}: #{v}" }.join(',')} edges=#{edges.count}>"
end
Expand Down

0 comments on commit 17489ad

Please sign in to comment.