continued doc and testing drive; rationalize GraphML a little
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / GraphML.pm
CommitLineData
e58153d6 1package Text::Tradition::Parser::GraphML;
b49c4318 2
3use strict;
4use warnings;
e867486f 5use Exporter 'import';
6use vars qw/ @EXPORT_OK $xpc /;
7
b49c4318 8use XML::LibXML;
9use XML::LibXML::XPathContext;
10
e867486f 11@EXPORT_OK = qw/ graphml_parse populate_witness_path /;
12
2ceca8c3 13=head1 NAME
b49c4318 14
2ceca8c3 15Text::Tradition::Parser::GraphML
16
17=head1 DESCRIPTION
18
19Parser module for Text::Tradition, given a GraphML file that describes
ec3f9144 20a collation graph. Returns the information about the graph that has
21been parsed out from the GraphML. This module is meant to be used
22with a module (e.g. CollateX or Self) that interprets the specific
23GraphML conventions of the source program.
2ceca8c3 24
25=head1 METHODS
26
e867486f 27=head2 B<graphml_parse>( $init_opts )
2ceca8c3 28
dfc37e38 29parse( $init_opts );
2ceca8c3 30
dfc37e38 31Takes a set of Tradition initialization options, among which should be either
32'file' or 'string'; parses that file or string and returns a list of nodes, edges,
ec3f9144 33and their associated data.
2ceca8c3 34
35=cut
b49c4318 36
ec3f9144 37# Return graph -> nodeid -> { key1/val1, key2/val2, key3/val3 ... }
38# -> edgeid -> { source, target, wit1/val1, wit2/val2 ...}
4a8828f0 39
e867486f 40sub graphml_parse {
dfc37e38 41 my( $opts ) = @_;
ec3f9144 42
43 my $graph_hash = { 'nodes' => [],
910a0a6d 44 'edges' => [] };
e867486f 45
b49c4318 46 my $parser = XML::LibXML->new();
dfc37e38 47 my $doc;
48 if( exists $opts->{'string'} ) {
49 $doc = $parser->parse_string( $opts->{'string'} );
50 } elsif ( exists $opts->{'file'} ) {
51 $doc = $parser->parse_file( $opts->{'file'} );
52 } else {
53 warn "Could not find string or file option to parse";
54 return;
55 }
56
e867486f 57 my( $graphattr, $nodedata, $witnesses ) = ( {}, {}, {} );
8e1394aa 58 my $graphml = $doc->documentElement();
4a8828f0 59 $xpc = XML::LibXML::XPathContext->new( $graphml );
b49c4318 60 $xpc->registerNs( 'g', 'http://graphml.graphdrawing.org/xmlns' );
61
62 # First get the ID keys, for witnesses and for collation data
b49c4318 63 foreach my $k ( $xpc->findnodes( '//g:key' ) ) {
910a0a6d 64 # Each key has a 'for' attribute; the edge keys are witnesses, and
65 # the node keys contain an ID and string for each node.
66 my $keyid = $k->getAttribute( 'id' );
67 my $keyname = $k->getAttribute( 'attr.name' );
68
e309421a 69 # Keep track of the XML identifiers for the data carried
70 # in each node element.
71 my $dtype = $k->getAttribute( 'for' );
72 if( $dtype eq 'graph' ) {
73 $graphattr->{$keyid} = $keyname;
74 } elsif( $dtype eq 'node' ) {
75 $nodedata->{$keyid} = $keyname;
910a0a6d 76 } else {
77 $witnesses->{$keyid} = $keyname;
78 }
b49c4318 79 }
80
81 my $graph_el = $xpc->find( '/g:graphml/g:graph' )->[0];
82
ec3f9144 83 my $node_reg = {};
e309421a 84
85 # Read in graph globals (if any).
86 print STDERR "Reading graphml global data\n";
87 foreach my $dkey ( keys %$graphattr ) {
88 my $keyname = $graphattr->{$dkey};
89 my $keyvalue = _lookup_node_data( $graph_el, $dkey );
90 $graph_hash->{'global'}->{$keyname} = $keyvalue;
91 }
ec3f9144 92
94c00c71 93 # Add the nodes to the graph hash.
94 print STDERR "Reading graphml nodes\n";
b49c4318 95 my @nodes = $xpc->findnodes( '//g:node' );
96 foreach my $n ( @nodes ) {
910a0a6d 97 # Could use a better way of registering these
98 my $node_hash = {};
99 foreach my $dkey ( keys %$nodedata ) {
100 my $keyname = $nodedata->{$dkey};
101 my $keyvalue = _lookup_node_data( $n, $dkey );
102 $node_hash->{$keyname} = $keyvalue if defined $keyvalue;
103 }
104 $node_reg->{$n->getAttribute( 'id' )} = $node_hash;
105 push( @{$graph_hash->{'nodes'}}, $node_hash );
b49c4318 106 }
910a0a6d 107
ec3f9144 108 # Now add the edges, and cross-ref with the node objects.
94c00c71 109 print STDERR "Reading graphml edges\n";
b49c4318 110 my @edges = $xpc->findnodes( '//g:edge' );
111 foreach my $e ( @edges ) {
910a0a6d 112 my $from = $e->getAttribute('source');
113 my $to = $e->getAttribute('target');
114
115 # We don't know whether the edge data is one per witness
116 # or one per witness type, or something else. So we just
117 # save it and let our calling parser decide.
118 my $edge_hash = {
119 'source' => $node_reg->{$from},
120 'target' => $node_reg->{$to},
121 };
122 foreach my $wkey( keys %$witnesses ) {
123 my $wname = $witnesses->{$wkey};
124 my $wlabel = _lookup_node_data( $e, $wkey );
125 $edge_hash->{$wname} = $wlabel if $wlabel;
126 }
127 push( @{$graph_hash->{'edges'}}, $edge_hash );
b49c4318 128 }
ec3f9144 129 return $graph_hash;
4a8828f0 130}
b49c4318 131
e867486f 132=head2 B<populate_witness_path>( $tradition )
133
134Given a tradition, populate the 'path' and 'uncorrected_path' attributes
135of all of its witnesses. Useful for all formats based on the graph itself.
136
137=cut
138
139sub populate_witness_path {
140 my ( $tradition, $ante_corr ) = @_;
141 my $c = $tradition->collation;
142 print STDERR "Walking paths for witnesses\n";
143 foreach my $wit ( $tradition->witnesses ) {
144 my @path = $c->reading_sequence( $c->start, $c->end, $wit->sigil );
145 $wit->path( \@path );
146 if( $ante_corr->{$wit->sigil} ) {
147 # Get the uncorrected path too
148 my @uc = $c->reading_sequence( $c->start, $c->end,
149 $wit->sigil . $c->ac_label, $wit->sigil );
150 $wit->uncorrected_path( \@uc );
151 }
152 }
153}
154
4a8828f0 155sub _lookup_node_data {
ec3f9144 156 my( $xmlnode, $key ) = @_;
4a8828f0 157 my $lookup_xpath = './g:data[@key="%s"]/child::text()';
94c00c71 158 my $data = $xpc->find( sprintf( $lookup_xpath, $key ), $xmlnode );
159 # If we get back an empty nodelist, we return undef.
160 if( ref( $data ) ) {
161 return undef unless $data->size;
162 return $data->to_literal->value;
163 }
164 # Otherwise we got back a value. Return it.
4a8828f0 165 return $data;
b49c4318 166}
167
2ceca8c3 168=back
169
170=head1 LICENSE
171
172This package is free software and is provided "as is" without express
173or implied warranty. You can redistribute it and/or modify it under
174the same terms as Perl itself.
175
176=head1 AUTHOR
177
178Tara L Andrews, aurum@cpan.org
179
180=cut
181
b49c4318 1821;