From a972089fecb01c62880c6a2a5bc5cbcb96105580 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Mon, 13 Jun 2011 18:38:56 +0200 Subject: add Graph::GEXF while it's not on CPAN Signed-off-by: franck cuny --- lib/Graph/GEXF/Node.pm | 155 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 lib/Graph/GEXF/Node.pm (limited to 'lib/Graph/GEXF/Node.pm') diff --git a/lib/Graph/GEXF/Node.pm b/lib/Graph/GEXF/Node.pm new file mode 100644 index 0000000..8f8187a --- /dev/null +++ b/lib/Graph/GEXF/Node.pm @@ -0,0 +1,155 @@ +package Graph::GEXF::Node; + +use Moose; +use Graph::GEXF::Edge; + +with + 'Graph::GEXF::Role::Attributes' => { for => [qw/node/] }, + 'Graph::GEXF::Role::Viz::Color', 'Graph::GEXF::Role::Viz::Position', + 'Graph::GEXF::Role::Viz::Size' => { as => 'size' }, + 'Graph::GEXF::Role::Viz::Shape' => { for => 'node' }; + +has id => ( + is => 'ro', + isa => 'Str', + required => 1, + traits => ['Chained'] +); + +has label => ( + is => 'rw', + isa => 'Str', + traits => ['Chained'] +); + +has edges => ( + traits => ['Hash'], + is => 'rw', + isa => 'HashRef[Graph::GEXF::Edge]', + default => sub { {} }, + handles => { + add_edge => 'set', + has_link_to => 'exists', + all_edges => 'keys', + get_edge => 'get', + } +); + +sub link_to { + my $self = shift; + my @nodes_id = @_; + + foreach my $node_id (@nodes_id) { + my $edge; + if ( ref $node_id ) { + $edge = Graph::GEXF::Edge->new( + source => $self->id, + target => $node_id->{target}, + weight => $node_id->{weight} + ); + $self->add_edge( $node_id->{target} => $edge ); + } + else { + $edge = Graph::GEXF::Edge->new( + source => $self->id, + target => $node_id, + ); + $self->add_edge( $node_id => $edge ); + } + } +} + +sub attribute { + my ($self, $attribute_name, $value) = @_; + +# return 0 unless $self->has_node_attribute; + + if (!$self->has_node_attribute($attribute_name)) { + die "this attribute doesn't exists"; + } + + $self->node_attributes->{$attribute_name}->{value} = $value; + + 1; +} + +no Moose; + +1; + +=head1 SYNOPSIS + + my $graph = Graph::GEXF->new(); + + my $n = $graph->add_node(); + +=head1 DESCRIPTION + +=head2 ATTRIBUTES + +=head3 id + + my $n = $graph->add_node(1); + $n->id; # returns 1 + +The B of a node can't be changed once the node is created. + +=head3 label + + $n->label('franckcuny'); + $n->label(); + +Each node has a label. If the B