we can parse our own graph output now
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / GraphML.pm
1 package Text::Tradition::Parser::GraphML;
2
3 use strict;
4 use warnings;
5 use XML::LibXML;
6 use XML::LibXML::XPathContext;
7
8 =head1 NAME
9
10 Text::Tradition::Parser::GraphML
11
12 =head1 DESCRIPTION
13
14 Parser module for Text::Tradition, given a GraphML file that describes
15 a collation graph.  Returns the information about the graph that has
16 been parsed out from the GraphML.  This module is meant to be used
17 with a module (e.g. CollateX or Self) that interprets the specific
18 GraphML conventions of the source program.
19
20 =head1 METHODS
21
22 =over
23
24 =item B<parse>
25
26 parse( $graphml_string );
27
28 Takes a string containing the GraphML; returns a list of nodes, edges,
29 and their associated data.
30
31 =cut
32
33 use vars qw/ $xpc $nodedata $witnesses /;
34
35 # Return graph -> nodeid -> { key1/val1, key2/val2, key3/val3 ... }
36 #              -> edgeid -> { source, target, wit1/val1, wit2/val2 ...}
37
38 sub parse {
39     my( $graphml_str ) = @_;
40
41     my $graph_hash = { 'nodes' => [],
42                        'edges' => [] };
43
44     my $parser = XML::LibXML->new();
45     my $doc = $parser->parse_string( $graphml_str );
46     my $graphml = $doc->documentElement();
47     $xpc = XML::LibXML::XPathContext->new( $graphml );
48     $xpc->registerNs( 'g', 'http://graphml.graphdrawing.org/xmlns' );
49     
50     # First get the ID keys, for witnesses and for collation data
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.
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         }
64     }
65
66     my $graph_el = $xpc->find( '/g:graphml/g:graph' )->[0];
67
68     my $node_reg = {};
69
70     # Add the nodes to the graph hash. 
71     my @nodes = $xpc->findnodes( '//g:node' );
72     foreach my $n ( @nodes ) {
73         # Could use a better way of registering these
74         my $node_hash = {};
75         foreach my $dkey ( keys %$nodedata ) {
76             my $keyname = $nodedata->{$dkey};
77             my $keyvalue = _lookup_node_data( $n, $dkey );
78             $node_hash->{$keyname} = $keyvalue if $keyvalue;
79         }
80         $node_reg->{$n->getAttribute( 'id' )} = $node_hash;
81         push( @{$graph_hash->{'nodes'}}, $node_hash );
82     }
83         
84     # Now add the edges, and cross-ref with the node objects.
85     my @edges = $xpc->findnodes( '//g:edge' );
86     foreach my $e ( @edges ) {
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;
101         }
102         push( @{$graph_hash->{'edges'}}, $edge_hash );
103     }
104     return $graph_hash;
105 }
106
107 sub _lookup_node_data {
108     my( $xmlnode, $key ) = @_;
109     my $lookup_xpath = './g:data[@key="%s"]/child::text()';
110     my $data = $xpc->findvalue( sprintf( $lookup_xpath, $key ), $xmlnode );
111     return $data;
112 }
113     
114 =back
115
116 =head1 LICENSE
117
118 This package is free software and is provided "as is" without express
119 or implied warranty.  You can redistribute it and/or modify it under
120 the same terms as Perl itself.
121
122 =head1 AUTHOR
123
124 Tara L Andrews, aurum@cpan.org
125
126 =cut
127
128 1;