summaryrefslogtreecommitdiff
path: root/lib/Graph/GEXF.pm
blob: f226b065429bbbcd948c40377c2b341094ead1b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package Graph::GEXF;

# ABSTRACT: Manipulate graph file in GEXF

use Moose;

use Data::UUID::LibUUID;
use Moose::Util::TypeConstraints;

use Graph::GEXF::Node;

with
  'Graph::GEXF::Role::XML',
  'Graph::GEXF::Role::Attributes' =>
  { for => [qw/node edge/], with_method => 1 };

=attr visualization (B<Boolean>)

if set to true, the generated graph will includes visualizations informations

=cut

has visualization => (
    is        => 'ro',
    isa       => 'Bool',
    predicate => 'has_visualization',
);

=attr graph_mode (B<static|dynamic>)

Is your graph static or dynamic.

=cut

has graph_mode => (
    is       => 'ro',
    isa      => enum( [qw/static dynamic/] ),
    required => 1,
    default  => 'static',
);

=attr edge_type (B<directed|undirected|mutual|notset>)

The type of the edges

=cut

has edge_type => (
    is       => 'ro',
    isa      => enum( [qw/directed undirected mutual notset/] ),
    required => 1,
    default  => 'directed',
);

=attr nodes

a HashRef of L<Graph::GEXF::Node> objects.

=cut

=method total_nodes

Return the list of nodes attached to the graph

=cut

=method get_node

Return a node

=cut

=method add_node_attribute($name, $type, [$default_value])

Add attributes to node

=method all_nodes

Return all the nodes

=cut

has nodes => (
    traits     => ['Hash'],
    is         => 'rw',
    isa        => 'HashRef[Graph::GEXF::Node]',
    default    => sub { {} },
    auto_deref => 1,
    handles    => {
        _node_exists => 'exists',
        _add_node    => 'set',
        total_nodes  => 'count',
        get_node     => 'get',
        all_nodes    => 'keys',
    },
);

=method add_node

Add a new node to the graph

=cut

sub add_node {
    my $self = shift;
    my ($id, %attributes);

    # TODO should be possible to add a Graph::GEXF::Node too
    
    if ( @_ == 1 ) {
        $id = shift;
    }
    else {
        if ( ( @_ % 2 ) == 0 ) {
            %attributes = @_;
        }
        else {
            $id = shift;
            %attributes = @_;
        }
    }

    if ($id && $self->_node_exists($id)) {
        die "Can't add node wih id $id: already exists";
    }

    $id = new_uuid_string() if !defined $id;

    my $node = Graph::GEXF::Node->new(id => $id);

    map {
        my $attribute = $self->get_node_attribute($_);
        $node->set_node_attribute(
            $_ => {
                id   => $attribute->{id},
                name => $attribute->{name},
                type => $attribute->{type},
            }
        );
    } $self->attributes_node_list;

    $self->_add_node($id => $node);
    $node;
}

1;

=head1 SYNOPSIS

    # create a new graph
    my $graph = Graph::GEXF->new();

    # add some attributes for nodes
    $graph->add_node_attribute('url', 'string');

    # create a new node and set the label
    my $n1 = $graph->add_node(0);
    $n1->label('Gephi');

    my $n2 = $graph->add_node(1);
    $n2->label('WebAtlas');

    my $n3 = $graph->add_node(2);
    $n3->label('RTGI');

    # create relations between nodes
    $n1->link_to(1, 2);
    $n2->link_to(0);
    $n3->link_to(1);

    # set the value for attributes
    $n1->attribute('url' => 'http://gephi.org/');
    $n2->attribute('url' => 'http://webatlas.fr/');
    $n3->attribute('url' => 'http://rtgi.fr/');

    # render the graph in XML
    my $xml = $graph->to_xml;