| 1 | package Data::Stag::GraphHandler; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Data::Stag::GraphHandler - Graph Handler | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | my $p = Data::Stag->parser; | ||
| 10 | $p->handler('graph'); | ||
| 11 | $p->handler->mapping([vertex=>[ | ||
| 12 | |||
| 13 | $p->parse | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | |||
| 20 | =head1 PUBLIC METHODS - | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use base qw(Data::Stag::BaseHandler); | ||
| 26 | |||
| 27 | use vars qw($VERSION); | ||
| 28 | $VERSION="0.14"; | ||
| 29 | use Graph; | ||
| 30 | |||
| 31 | sub init { | ||
| 32 | my $self = shift; | ||
| 33 | $self->SUPER::init(@_); | ||
| 34 | $self->graph(new Graph); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub graph { | ||
| 38 | my $self = shift; | ||
| 39 | $self->{graph} = shift if @_; | ||
| 40 | return $self->{graph}; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub mapping { | ||
| 44 | my $self = shift; | ||
| 45 | $self->{_mapping} = shift if @_; | ||
| 46 | return $self->{_mapping}; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | sub graph_edges { | ||
| 51 | my $self = shift; | ||
| 52 | $self->{_graph_edges} = shift if @_; | ||
| 53 | return $self->{_graph_edges}; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub catch_end { | ||
| 57 | my $self = shift; | ||
| 58 | my $ev = shift; | ||
| 59 | my $node = shift; | ||
| 60 | |||
| 61 | my $g = $self->graph; | ||
| 62 | |||
| 63 | my $map = $self->mapping; | ||
| 64 | if ($map) { | ||
| 65 | my $uid_tag = $map->get_uid; | ||
| 66 | my @vertexmaps = $map->getnode_vertex; | ||
| 67 | my @edgemaps = $map->getnode_edge; | ||
| 68 | my ($emap) = grep {$ev eq $_->get_tag} @edgemaps; | ||
| 69 | if ($emap) { | ||
| 70 | my $parent_tag = $emap->get_parent; | ||
| 71 | my $child_tag = $emap->get_child; | ||
| 72 | |||
| 73 | my $parent = $node->get($parent_tag); | ||
| 74 | my $child = $node->get($child_tag); | ||
| 75 | if (!@vertexmaps) { | ||
| 76 | # graph defined purely by edges | ||
| 77 | $g->add_vertex($parent); | ||
| 78 | $g->add_vertex($child); | ||
| 79 | } | ||
| 80 | $g->add_edge($parent, $child); | ||
| 81 | $self->_set_attributes($g, $node, [$parent, $child], $emap->get_att); | ||
| 82 | } | ||
| 83 | my ($vmap) = grep {$ev eq $_->get_tag} @vertexmaps; | ||
| 84 | if ($vmap) { | ||
| 85 | my $uid = $node->get($uid_tag); | ||
| 86 | $g->add_vertex($uid); | ||
| 87 | $self->_set_attributes($g, $node, [$uid], $vmap->get_att); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | return; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _set_attributes { | ||
| 94 | my $self = shift; | ||
| 95 | my ($g, $node, $idref, @atts) = @_; | ||
| 96 | |||
| 97 | if (!scalar(@atts) || | ||
| 98 | grep {$_ eq '*'} @atts) { | ||
| 99 | my %atth = map { $_->name => 1 } $node->kids; | ||
| 100 | @atts = keys %atth; | ||
| 101 | } | ||
| 102 | foreach my $att (@atts) { | ||
| 103 | my @v = $node->get($att); | ||
| 104 | foreach my $v (@v) { | ||
| 105 | $g->set_attribute($att, @$idref, $v); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; |