Fix graphml output / input format
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / GraphML.pm
CommitLineData
e58153d6 1package Text::Tradition::Parser::GraphML;
b49c4318 2
3use strict;
4use warnings;
b49c4318 5use XML::LibXML;
6use XML::LibXML::XPathContext;
7
2ceca8c3 8=head1 NAME
b49c4318 9
2ceca8c3 10Text::Tradition::Parser::GraphML
11
12=head1 DESCRIPTION
13
14Parser module for Text::Tradition, given a GraphML file that describes
ec3f9144 15a collation graph. Returns the information about the graph that has
16been parsed out from the GraphML. This module is meant to be used
17with a module (e.g. CollateX or Self) that interprets the specific
18GraphML conventions of the source program.
2ceca8c3 19
20=head1 METHODS
21
22=over
23
24=item B<parse>
25
ec3f9144 26parse( $graphml_string );
2ceca8c3 27
ec3f9144 28Takes a string containing the GraphML; returns a list of nodes, edges,
29and their associated data.
2ceca8c3 30
31=cut
b49c4318 32
ec3f9144 33use vars qw/ $xpc $nodedata $witnesses /;
b5054ca9 34
ec3f9144 35# Return graph -> nodeid -> { key1/val1, key2/val2, key3/val3 ... }
36# -> edgeid -> { source, target, wit1/val1, wit2/val2 ...}
4a8828f0 37
b49c4318 38sub parse {
ec3f9144 39 my( $graphml_str ) = @_;
40
41 my $graph_hash = { 'nodes' => [],
910a0a6d 42 'edges' => [] };
b49c4318 43
44 my $parser = XML::LibXML->new();
45 my $doc = $parser->parse_string( $graphml_str );
8e1394aa 46 my $graphml = $doc->documentElement();
4a8828f0 47 $xpc = XML::LibXML::XPathContext->new( $graphml );
b49c4318 48 $xpc->registerNs( 'g', 'http://graphml.graphdrawing.org/xmlns' );
49
50 # First get the ID keys, for witnesses and for collation data
b49c4318 51 foreach my $k ( $xpc->findnodes( '//g:key' ) ) {
910a0a6d 52 # Each key has a 'for' attribute; the edge keys are witnesses, and
53 # the node keys contain an ID and string for each node.
54 my $keyid = $k->getAttribute( 'id' );
55 my $keyname = $k->getAttribute( 'attr.name' );
56
57 if( $k->getAttribute( 'for' ) eq 'node' ) {
58 # Keep track of the XML identifiers for the data carried
59 # in each node element.
60 $nodedata->{$keyid} = $keyname
61 } else {
62 $witnesses->{$keyid} = $keyname;
63 }
b49c4318 64 }
65
66 my $graph_el = $xpc->find( '/g:graphml/g:graph' )->[0];
67
ec3f9144 68 my $node_reg = {};
69
94c00c71 70 # Add the nodes to the graph hash.
71 print STDERR "Reading graphml nodes\n";
b49c4318 72 my @nodes = $xpc->findnodes( '//g:node' );
73 foreach my $n ( @nodes ) {
910a0a6d 74 # Could use a better way of registering these
75 my $node_hash = {};
76 foreach my $dkey ( keys %$nodedata ) {
77 my $keyname = $nodedata->{$dkey};
78 my $keyvalue = _lookup_node_data( $n, $dkey );
79 $node_hash->{$keyname} = $keyvalue if defined $keyvalue;
80 }
81 $node_reg->{$n->getAttribute( 'id' )} = $node_hash;
82 push( @{$graph_hash->{'nodes'}}, $node_hash );
b49c4318 83 }
910a0a6d 84
ec3f9144 85 # Now add the edges, and cross-ref with the node objects.
94c00c71 86 print STDERR "Reading graphml edges\n";
b49c4318 87 my @edges = $xpc->findnodes( '//g:edge' );
88 foreach my $e ( @edges ) {
910a0a6d 89 my $from = $e->getAttribute('source');
90 my $to = $e->getAttribute('target');
91
92 # We don't know whether the edge data is one per witness
93 # or one per witness type, or something else. So we just
94 # save it and let our calling parser decide.
95 my $edge_hash = {
96 'source' => $node_reg->{$from},
97 'target' => $node_reg->{$to},
98 };
99 foreach my $wkey( keys %$witnesses ) {
100 my $wname = $witnesses->{$wkey};
101 my $wlabel = _lookup_node_data( $e, $wkey );
102 $edge_hash->{$wname} = $wlabel if $wlabel;
103 }
104 push( @{$graph_hash->{'edges'}}, $edge_hash );
b49c4318 105 }
ec3f9144 106 return $graph_hash;
4a8828f0 107}
b49c4318 108
4a8828f0 109sub _lookup_node_data {
ec3f9144 110 my( $xmlnode, $key ) = @_;
4a8828f0 111 my $lookup_xpath = './g:data[@key="%s"]/child::text()';
94c00c71 112 my $data = $xpc->find( sprintf( $lookup_xpath, $key ), $xmlnode );
113 # If we get back an empty nodelist, we return undef.
114 if( ref( $data ) ) {
115 return undef unless $data->size;
116 return $data->to_literal->value;
117 }
118 # Otherwise we got back a value. Return it.
4a8828f0 119 return $data;
b49c4318 120}
121
2ceca8c3 122=back
123
124=head1 LICENSE
125
126This package is free software and is provided "as is" without express
127or implied warranty. You can redistribute it and/or modify it under
128the same terms as Perl itself.
129
130=head1 AUTHOR
131
132Tara L Andrews, aurum@cpan.org
133
134=cut
135
b49c4318 1361;