Fix graphml output / input format
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / Self.pm
CommitLineData
32014ec9 1package Text::Tradition::Parser::Self;
2
3use strict;
4use warnings;
5use Text::Tradition::Parser::GraphML;
6
7=head1 NAME
8
9Text::Tradition::Parser::GraphML
10
11=head1 DESCRIPTION
12
13Parser module for Text::Tradition to read in its own GraphML output format.
14TODO document what this format is.
15
16=head1 METHODS
17
18=over
19
20=item B<parse>
21
22parse( $graph, $graphml_string );
23
24Takes an initialized Text::Tradition::Graph object and a string
25containing the GraphML; creates the appropriate nodes and edges on the
26graph.
27
28=cut
29
94c00c71 30# TODO share these with Collation.pm somehow
31my( $IDKEY, $TOKENKEY, $TRANSPOS_KEY, $RANK_KEY, $CLASS_KEY,
32 $SOURCE_KEY, $TARGET_KEY, $WITNESS_KEY, $EXTRA_KEY, $RELATIONSHIP_KEY )
33 = qw/ name reading identical rank class
34 source target witness extra relationship/;
32014ec9 35
36sub parse {
37 my( $tradition, $graphml_str ) = @_;
94c00c71 38
39 # TODO this is begging for stream parsing instead of multiple loops.
32014ec9 40 my $graph_data = Text::Tradition::Parser::GraphML::parse( $graphml_str );
41
42 my $collation = $tradition->collation;
43 my %witnesses;
44
45 # Add the nodes to the graph.
910a0a6d 46 # TODO Are we adding extra start/end nodes?
32014ec9 47
48 my $extra_data = {}; # Keep track of data that needs to be processed
49 # after the nodes & edges are created.
f6066bac 50 print STDERR "Adding graph nodes\n";
32014ec9 51 foreach my $n ( @{$graph_data->{'nodes'}} ) {
94c00c71 52 # First extract the data that we can use without reference to
53 # anything else.
54 my %node_data = %$n; # Need $n itself untouched for edge processing
910a0a6d 55 my $nodeid = delete $node_data{$IDKEY};
56 my $reading = delete $node_data{$TOKENKEY};
94c00c71 57 my $class = delete $node_data{$CLASS_KEY} || '';
58 my $rank = delete $node_data{$RANK_KEY};
59
60 # Create the node. Current valid classes are common and meta.
61 # Everything else is a normal reading.
62 my $gnode = $collation->add_reading( $nodeid );
63 $gnode->text( $reading );
64 $gnode->make_common if $class eq 'common';
65 $gnode->is_meta( 1 ) if $class eq 'meta';
66 $gnode->rank( $rank ) if defined $rank;
67
68 # Now save the data that we need for post-processing,
910a0a6d 69 # if it exists.
70 if ( keys %node_data ) {
94c00c71 71 $extra_data->{$nodeid} = \%node_data
910a0a6d 72 }
32014ec9 73 }
910a0a6d 74
32014ec9 75 # Now add the edges.
f6066bac 76 print STDERR "Adding graph edges\n";
94c00c71 77 $DB::single = 1;
32014ec9 78 foreach my $e ( @{$graph_data->{'edges'}} ) {
94c00c71 79 my $from = $e->{$SOURCE_KEY};
80 my $to = $e->{$TARGET_KEY};
81 my $class = $e->{$CLASS_KEY};
82
83 # We may have more information depending on the class.
84 if( $class eq 'path' ) {
85 # We need the witness, and whether it is an 'extra' reading path.
86 my $wit = $e->{$WITNESS_KEY};
87 warn "No witness label on path edge!" unless $wit;
88 my $extra = $e->{$EXTRA_KEY};
89 my $label = $wit . ( $extra ? $collation->ac_label : '' );
90 $collation->add_path( $from->{$IDKEY}, $to->{$IDKEY}, $label );
91 # Add the witness if we don't have it already.
92 unless( $witnesses{$wit} ) {
93 $tradition->add_witness( sigil => $wit );
94 $witnesses{$wit} = 1;
95 }
96 } elsif( $class eq 'relationship' ) {
97 # We need the relationship type.
98 my $rel = $e->{$RELATIONSHIP_KEY};
99 warn "No relationship type for relationship edge!" unless $rel;
100 $collation->add_relationship( $rel, $from->{$IDKEY}, $to->{$IDKEY} );
101 }
32014ec9 102 }
103
104 ## Deal with node information (transposition, relationships, etc.) that
105 ## needs to be processed after all the nodes are created.
94c00c71 106 print STDERR "Adding second-pass node data\n";
f6066bac 107 my $linear = undef;
32014ec9 108 foreach my $nkey ( keys %$extra_data ) {
910a0a6d 109 foreach my $edkey ( keys %{$extra_data->{$nkey}} ) {
110 my $this_reading = $collation->reading( $nkey );
111 if( $edkey eq $TRANSPOS_KEY ) {
94c00c71 112 $DB::single = 1;
910a0a6d 113 my $other_reading = $collation->reading( $extra_data->{$nkey}->{$edkey} );
114 # We evidently have a linear graph.
115 $linear = 1;
116 $this_reading->set_identical( $other_reading );
910a0a6d 117 } else {
118 warn "Unfamiliar reading node data $edkey for $nkey";
119 }
120 }
32014ec9 121 }
f6066bac 122 $collation->linear( $linear );
d9e873d0 123 # TODO We probably need to set the $witness->path arrays for each wit.
32014ec9 124}
125
126=back
127
128=head1 LICENSE
129
130This package is free software and is provided "as is" without express
131or implied warranty. You can redistribute it and/or modify it under
132the same terms as Perl itself.
133
134=head1 AUTHOR
135
136Tara L Andrews, aurum@cpan.org
137
138=cut
139
1401;