1 package Traditions::Parser::GraphML;
7 use XML::LibXML::XPathContext;
10 # Takes a GraphML string; returns a Graph::Easy object.
13 my( $graph, $graphml_str ) = @_;
15 my $parser = XML::LibXML->new();
16 my $doc = $parser->parse_string( $graphml_str );
17 my $collation = $doc->documentElement();
18 my $xpc = XML::LibXML::XPathContext->new( $collation );
19 $xpc->registerNs( 'g', 'http://graphml.graphdrawing.org/xmlns' );
21 # First get the ID keys, for witnesses and for collation data
24 foreach my $k ( $xpc->findnodes( '//g:key' ) ) {
25 # Each key has a 'for' attribute; the edge keys are witnesses, and
26 # the node keys contain an ID and string for each node.
28 if( $k->getAttribute( 'for' ) eq 'node' ) {
29 $nodedata{ $k->getAttribute( 'attr.name' ) } = $k->getAttribute( 'id' );
31 $witnesses{ $k->getAttribute( 'id' ) } = $k->getAttribute( 'attr.name' );
35 my $graph_el = $xpc->find( '/g:graphml/g:graph' )->[0];
37 # Add the nodes to the graph. First delete the start node, because
38 # GraphML graphs will have their own start nodes.
39 $graph->del_node( $graph->start() );
40 # Map from XML IDs to node name/identity
42 # Keep track of whatever extra info we're passed
44 my @nodes = $xpc->findnodes( '//g:node' );
45 foreach my $n ( @nodes ) {
46 my $lookup_xpath = './g:data[@key="%s"]/child::text()';
47 my $id = $xpc->findvalue( sprintf( $lookup_xpath, $nodedata{'number'} ), $n );
48 my $label = $xpc->findvalue( sprintf( $lookup_xpath, $nodedata{'token'} ), $n );
49 my $gnode = $graph->add_node( $id );
50 $node_name{ $n->getAttribute('id') } = $id;
51 $gnode->set_attribute( 'label', $label );
53 # Now get the rest of the data
55 my @keys = grep { $_ !~ /^(number|token)$/ } keys( %nodedata );
56 foreach my $k ( @keys ) {
57 my $data = $xpc->findvalue( sprintf( $lookup_xpath, $nodedata{ $k } ), $n );
58 $extra->{ $k } = $data;
60 $extra_data->{ $id } = $extra;
64 my @edges = $xpc->findnodes( '//g:edge' );
65 foreach my $e ( @edges ) {
66 my $from = $node_name{ $e->getAttribute('source') };
67 my $to = $node_name{ $e->getAttribute('target') };
68 # Label according to the witnesses present.
69 my @wit_ids = $xpc->findnodes( './g:data/attribute::key', $e );
70 my @wit_names = map { $witnesses{ $_->getValue() } } @wit_ids;
71 my $label = join( ', ', @wit_names );
73 $graph->add_edge( $from, $to, $label );
76 ## Reverse the node_name hash so that we have two-way lookup.
77 my %node_id = reverse %node_name;
79 ## Record the nodes that are marked as transposed.
80 my $tr_xpath = '//g:node[g:data[@key="' . $nodedata{'identity'} . '"]]';
81 my $transposition_nodes = $xpc->find( $tr_xpath );
82 foreach my $tn ( @$transposition_nodes ) {
83 my $id_xpath = sprintf( './g:data[@key="%s"]/text()',
84 $nodedata{'identity'} );
85 $graph->set_identical_node( $node_name{ $tn->getAttribute( 'id' ) },
86 $node_name{ $xpc->findvalue( $id_xpath,
91 # Find the beginning and end nodes of the graph. The beginning node
92 # has no incoming edges; the end node has no outgoing edges.
93 my( $begin_node, $end_node );
94 foreach my $gnode ( $graph->nodes() ) {
95 print STDERR "Checking node " . $gnode->name . "\n";
96 my @outgoing = $gnode->outgoing();
97 my @incoming = $gnode->incoming();
99 unless( scalar @incoming ) {
100 warn "Already have a beginning node" if $begin_node;
101 my $node_xml_id = $node_id{ $gnode->name() };
102 my @bn = $xpc->findnodes( '//g:node[@id="' . $node_xml_id . '"]' );
103 warn "XPath did not find a node for id $node_xml_id"
105 $begin_node = $bn[0];
106 $graph->start( $gnode );
107 $node_name{ 0 } = '#START#';
108 $node_id{'#START#'} = 0;
110 unless( scalar @outgoing ) {
111 warn "Already have an ending node" if $end_node;
112 my $node_xml_id = $node_id{ $gnode->name() };
113 my @bn = $xpc->findnodes( '//g:node[@id="' . $node_xml_id . '"]' );
114 warn "XPath did not find a node for id $node_xml_id"
120 # Now for each witness, walk the path through the graph.
121 # Then we need to find the common nodes.
122 # TODO This method is going to fall down if we have a very gappy
123 # text in the collation.
124 # TODO think about whether it makes more sense to do this in the
125 # XML or in the graph. Right now it's the XML.
128 foreach my $wit ( keys %witnesses ) {
129 my $node_id = $begin_node->getAttribute('id');
130 my @wit_path = ( $node_name{ $node_id } );
131 # TODO Detect loops at some point
132 while( $node_id != $end_node->getAttribute('id') ) {
133 # Find the node which is the target of the edge whose
134 # source is $node_id and applies to this witness.
135 my $xpath_expr = '//g:edge[child::g:data[@key="'
136 . $wit . '"] and attribute::source="'
138 my $next_edge = $xpc->find( $xpath_expr, $graph_el )->[0];
139 $node_id = $next_edge->getAttribute('target');
140 push( @wit_path, $node_name{ $node_id } );
142 $paths->{ $witnesses{ $wit }} = \@wit_path;
143 if( @common_nodes ) {
145 foreach my $n ( @wit_path) {
146 push( @cn, $n ) if grep { $_ eq $n } @common_nodes;
149 push( @common_nodes, @cn );
151 push( @common_nodes, @wit_path );
155 # Mark all the nodes as either common or not.
156 foreach my $cn ( @common_nodes ) {
157 print STDERR "Setting $cn as common node\n";
158 $graph->node( $cn )->set_attribute( 'class', 'common' );
160 foreach my $n ( $graph->nodes() ) {
161 $n->set_attribute( 'class', 'variant' )
162 unless $n->get_attribute( 'class' ) eq 'common';
165 # Now calculate graph positions.
166 $graph->make_positions( \@common_nodes, $paths );