Graphs with decorated edges

Hello dear rubyists,

I’m working on an application that simulates several components
interacting with each other. I would like to represent this situation
using a graph, where each component is a vertex and the interaction of
2 components is an edge between them.

I’ve taking using RGL [0] to handle graphs and it seems very nice.
However, I don’t find a way to add a label or a list of labels to the
edges.

Have I overlooked this functionality in RGL? Can you suggest an
alternative lib to handle graphs?

Thanks and kind regards,
Ed

[0] http://rgl.rubyforge.org/rgl/index.html


Encontrá a “Tu psicópata favorito” http://tuxmaniac.blogspot.com

The future is not what it used to be.
– Paul Valéry

I’ve just recently worked on something similar (making a picture of
the graph of components configured in an XML file). I ended up using
GRATR, which is just a fork of RGL: http://gratr.rubyforge.org/ It
also didn’t seem to handle the labels for edges, so I used the
DOT::DOTDigraph class directly instead of the actual graph object.

I added the following directly to that class to get the output using
DOT functionality:

class DOT::DOTDigraph
def write_to_graphic_file(fmt=‘png’, dotfile=‘graph’)
src = dotfile + ‘.dot’
dot = dotfile + ‘.’ + fmt

File.open(src, 'w') {|f| f << self.to_s << "\n"}

system( "dot -T#{fmt} #{src} -o #{dot}" )
dot

end
end

Adding nodes looks like this:

@graph << DOT::DOTNode.new(‘name’ => name, ‘label’ => label,
‘fontsize’ => fontsize)

And adding edges looks like this:

@graph << DOT::DOTDirectedArc.new(‘from’ => from, ‘to’ => to, ‘label’
=> label, ‘fontsize’ => fontsize)

There may be a better way to do it, but that’s what I came up with.

-Stephen

Using GRATR, you can add labels to the edges of the graph by adding a
third parameter when you do that add_edge! which is a Hash of options
for that edge. E.g. g.add_edge!(node1, node2, :label => “My edge
label”)

I’ve taking using RGL [0] to handle graphs and it seems very nice.
However, I don’t find a way to add a label or a list of labels to the
edges.

Have I overlooked this functionality in RGL? Can you suggest an
alternative lib to handle graphs?
You can check the wrapper around boost::graph I wrote for one of my
projets.
Check the documentation of the BGL module at
http://www.laas.fr/~sjoyeux/doc/api/roby/

The main features are:

  • a vertex can be any Ruby object
  • a vertex can be part of many graphs (see Vertex#each_graph)
  • edges can be decorated (‘info’ parameter to Graph#link and
    Vertex#[])
  • availability of basic boost::graph algorithms: component, DFS, BFS.
    I
    usually add more when I need them …

You can also check Roby::RelationGraph and
Roby::DirectedRelationSupport,
which are specific extensions to the graph API I use for the Roby
project.

If you find it useful, I can create a separate project for it.