checkpoint, not sure what is here
[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' => [],
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' ) ) {
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.
b5054ca9 54 my $keyid = $k->getAttribute( 'id' );
55 my $keyname = $k->getAttribute( 'attr.name' );
b49c4318 56
57 if( $k->getAttribute( 'for' ) eq 'node' ) {
ec3f9144 58 # Keep track of the XML identifiers for the data carried
59 # in each node element.
60 $nodedata->{$keyid} = $keyname
b49c4318 61 } else {
ec3f9144 62 $witnesses->{$keyid} = $keyname;
b49c4318 63 }
64 }
65
66 my $graph_el = $xpc->find( '/g:graphml/g:graph' )->[0];
67
ec3f9144 68 my $node_reg = {};
69
70 # Add the nodes to the graph hash.
b49c4318 71 my @nodes = $xpc->findnodes( '//g:node' );
72 foreach my $n ( @nodes ) {
b5054ca9 73 # Could use a better way of registering these
ec3f9144 74 my $node_hash = {};
75 foreach my $dkey ( keys %$nodedata ) {
76 my $keyname = $nodedata->{$dkey};
77 my $keyvalue = _lookup_node_data( $n, $dkey );
f6066bac 78 $node_hash->{$keyname} = $keyvalue if defined $keyvalue;
b49c4318 79 }
ec3f9144 80 $node_reg->{$n->getAttribute( 'id' )} = $node_hash;
81 push( @{$graph_hash->{'nodes'}}, $node_hash );
b49c4318 82 }
83
ec3f9144 84 # Now add the edges, and cross-ref with the node objects.
b49c4318 85 my @edges = $xpc->findnodes( '//g:edge' );
86 foreach my $e ( @edges ) {
ec3f9144 87 my $from = $e->getAttribute('source');
88 my $to = $e->getAttribute('target');
89
90 # We don't know whether the edge data is one per witness
91 # or one per witness type, or something else. So we just
92 # save it and let our calling parser decide.
93 my $edge_hash = {
94 'source' => $node_reg->{$from},
95 'target' => $node_reg->{$to},
96 };
97 foreach my $wkey( keys %$witnesses ) {
98 my $wname = $witnesses->{$wkey};
99 my $wlabel = _lookup_node_data( $e, $wkey );
100 $edge_hash->{$wname} = $wlabel if $wlabel;
b49c4318 101 }
ec3f9144 102 push( @{$graph_hash->{'edges'}}, $edge_hash );
b49c4318 103 }
ec3f9144 104 return $graph_hash;
4a8828f0 105}
b49c4318 106
4a8828f0 107sub _lookup_node_data {
ec3f9144 108 my( $xmlnode, $key ) = @_;
4a8828f0 109 my $lookup_xpath = './g:data[@key="%s"]/child::text()';
ec3f9144 110 my $data = $xpc->findvalue( sprintf( $lookup_xpath, $key ), $xmlnode );
4a8828f0 111 return $data;
b49c4318 112}
113
2ceca8c3 114=back
115
116=head1 LICENSE
117
118This package is free software and is provided "as is" without express
119or implied warranty. You can redistribute it and/or modify it under
120the same terms as Perl itself.
121
122=head1 AUTHOR
123
124Tara L Andrews, aurum@cpan.org
125
126=cut
127
b49c4318 1281;