Subject: | from_graph feature request |
Hi Ton,
For the Graph that I'm using I have already generated a unique identifier numbered list (like $num in from_graph) as I need to keep track of vertices as they are added and removed. For this reason I can't have from_graph assign what is left in the list a new number. I was wondering if you would consider the following as a inclusion in a future build?
I'm not familiar with XS or Classes within perl so I've just removed that part (do you have an example of how to use it), but here is a sample of code to explain what I mean.
#!/usr/bin/perl
# Example - Paul Matthews 3/9/05
use strict;
use warnings;
use Graph;
use Graph::Layout::Aesthetic::Topology;
use Graph::Layout::Aesthetic::Monitor::GnuPlot;
use Carp;
my $debug = 1;
#Next unique vertex number
my $id_num = 0;
sub Get_ID_Num {
return $id_num++;
}
#Make a graph
my $g = Graph->new();
#Make some vertices
$g->add_vertex("V1");
$g->set_vertex_attribute("V1", "id", Get_ID_Num());
$g->add_vertex("V2");
$g->set_vertex_attribute("V2", "id", Get_ID_Num());
$g->add_vertex("V3");
$g->set_vertex_attribute("V3", "id", Get_ID_Num());
$g->add_vertex("V4");
$g->set_vertex_attribute("V4", "id", Get_ID_Num());
#Make some edges
$g->add_edge("V1", "V2");
$g->add_edge("V2", "V3");
$g->add_edge("V3", "V4");
$g->add_edge("V4", "V1");
sub from_graph {
my ($graph, %params) = @_;
my $attribute = delete $params{id_attribute};
print "attribute : $attribute\n" if $debug;
croak "Unknown parameter ", join(", ", keys %params) if %params;
my ($num, $f, $t);
# Set up a mapping from vertices to numbers
my $nr = 0;
foreach my $v ( $graph->vertices ) {
print "vertex : $v\n" if $debug;
$num->{$v} = $graph->get_vertex_attribute($v, $attribute);
}
$nr = keys %$num;
my $topo = Graph::Layout::Aesthetic::Topology->new_vertices($nr);
foreach my $e ( $graph->edges ) {
($f, $t) = @{$e};
print "edge $f ($num->{$f}) - $t ($num->{$t})\n" if $debug;
$topo->add_edge($num->{$f}, $num->{$t});
}
$topo->finish;
return $topo;
}
my $topology = from_graph($g, id_attribute => "id");
my $aglo = Graph::Layout::Aesthetic->new($topology);
my $monitor = Graph::Layout::Aesthetic::Monitor::GnuPlot->new();
while (1) {
$aglo->gloss(monitor => $monitor);
}
Regards Paul