add method to generate a part of the graph specified by rank range
[scpubgit/stemmatology.git] / t / graph.t
1 #!/usr/bin/perl
2
3 use strict; use warnings;
4 use Test::More;
5 use lib 'lib';
6 use Text::Tradition;
7 use XML::LibXML;
8 use XML::LibXML::XPathContext;
9
10 my $datafile = 't/data/Collatex-16.xml';
11
12 my $tradition = Text::Tradition->new( 
13     'name'  => 'inline', 
14     'input' => 'CollateX',
15     'file'  => $datafile,
16     );
17 my $collation = $tradition->collation;
18
19 # Test the svg creation
20 my $parser = XML::LibXML->new();
21 $parser->load_ext_dtd( 0 );
22 my $svg = $parser->parse_string( $collation->as_svg() );
23 is( $svg->documentElement->nodeName(), 'svg', 'Got an svg document' );
24
25 # Test for the correct number of nodes in the SVG
26 my $svg_xpc = XML::LibXML::XPathContext->new( $svg->documentElement() );
27 $svg_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
28 my @svg_nodes = $svg_xpc->findnodes( '//svg:g[@class="node"]' );
29 is( scalar @svg_nodes, 26, "Correct number of nodes in the graph" );
30
31 # Test for the correct number of edges
32 my @svg_edges = $svg_xpc->findnodes( '//svg:g[@class="edge"]' );
33 is( scalar @svg_edges, 32, "Correct number of edges in the graph" );
34
35 # Test svg creation for a subgraph
36 my $part_svg = $parser->parse_string( $collation->svg_subgraph( 15 ) ); # start, no end
37 is( $part_svg->documentElement->nodeName(), 'svg', "Got an svg subgraph to end" );
38 my $part_xpc = XML::LibXML::XPathContext->new( $part_svg->documentElement() );
39 $part_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
40 @svg_nodes = $part_xpc->findnodes( '//svg:g[@class="node"]' );
41 is( scalar( @svg_nodes ), 9, 
42         "Correct number of nodes in the subgraph" );
43 @svg_edges = $part_xpc->findnodes( '//svg:g[@class="edge"]' );
44 is( scalar( @svg_edges ), 10,
45         "Correct number of edges in the subgraph" );
46 open( OUT, ">test.svg" );
47 print OUT $part_svg->toString();
48 close OUT;
49
50 $part_svg = $parser->parse_string( $collation->svg_subgraph( 10, 13 ) ); # start, no end
51 is( $part_svg->documentElement->nodeName(), 'svg', "Got an svg subgraph in the middle" );
52 $part_xpc = XML::LibXML::XPathContext->new( $part_svg->documentElement() );
53 $part_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
54 @svg_nodes = $part_xpc->findnodes( '//svg:g[@class="node"]' );
55 is( scalar( @svg_nodes ), 9, 
56         "Correct number of nodes in the subgraph" );
57 @svg_edges = $part_xpc->findnodes( '//svg:g[@class="edge"]' );
58 is( scalar( @svg_edges ), 11,
59         "Correct number of edges in the subgraph" );
60
61
62 $part_svg = $parser->parse_string( $collation->svg_subgraph( 0, 5 ) ); # start, no end
63 is( $part_svg->documentElement->nodeName(), 'svg', "Got an svg subgraph from start" );
64 $part_xpc = XML::LibXML::XPathContext->new( $part_svg->documentElement() );
65 $part_xpc->registerNs( 'svg', 'http://www.w3.org/2000/svg' );
66 @svg_nodes = $part_xpc->findnodes( '//svg:g[@class="node"]' );
67 is( scalar( @svg_nodes ), 7, 
68         "Correct number of nodes in the subgraph" );
69 @svg_edges = $part_xpc->findnodes( '//svg:g[@class="edge"]' );
70 is( scalar( @svg_edges ), 7,
71         "Correct number of edges in the subgraph" );
72
73 SKIP: {
74         skip "lemmatization disabled for now", 1;
75         # Test for the correct common nodes
76         my @common_nodes = ( '#START#' );
77         push( @common_nodes, qw/ n1 n5 n6 n7 n12 n16 n19 n20 n27 / );
78         my @expected_nodes = map { [ $_, 1 ] } @common_nodes;
79         foreach my $idx ( qw/2 3 4 8 10 11 13 16 17 18/ ) {
80                 splice( @expected_nodes, $idx, 0, [ "node_null", undef ] );
81         }
82         my @active_nodes = $collation->lemma_readings();
83         subtest 'Initial common points' => \&compare_active;
84         my $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced ... ... ... #';
85         is( make_text( @active_nodes ), $string, "Got the right starting text" );
86         
87         sub compare_active {
88                 is( scalar( @active_nodes ), scalar ( @expected_nodes ), 
89                 "Arrays are same length" );
90         
91                 foreach ( 0 .. scalar(@active_nodes)-1 ) {
92                 is( $active_nodes[$_]->[1], $expected_nodes[$_]->[1], 
93                         "Element has same toggle value" );
94                 if( defined $active_nodes[$_]->[1] ) {
95                         is( $active_nodes[$_]->[0], $expected_nodes[$_]->[0], 
96                         "Active or toggled element has same node name " 
97                         . $active_nodes[$_]->[0] );
98                 }
99                 }
100         }
101         
102         sub make_text {
103                 my @words;
104                 foreach my $n ( @_ ) {
105                 if( $n->[1] ) {
106                         push( @words, $collation->reading( $n->[0] )->text );
107                 } elsif ( !defined $n->[1] ) {
108                         push( @words, '...' );
109                 }
110                 }
111                 return join( ' ', @words );
112         }
113         
114         # Test that the common nodes are marked common
115         foreach my $cn ( @common_nodes ) {
116                 ok( $collation->reading( $cn )->is_common, "Node $cn is marked common" );
117         }
118         
119         # Test the manuscript paths
120         my $wit_a = '# when april with his showers sweet with fruit the drought of march has pierced unto the root #';
121         my $wit_b = '# when showers sweet with april fruit the march of drought has pierced to the root #';
122         my $wit_c = '# when showers sweet with april fruit teh drought of march has pierced teh rood #';
123         is( join( ' ', @{$tradition->witness( "A" )->text} ), $wit_a, "Correct path for witness A" );
124         is( join( ' ', @{$tradition->witness( "B" )->text} ), $wit_b, "Correct path for witness B" );
125         is( join( ' ', @{$tradition->witness( "C" )->text} ), $wit_c, "Correct path for witness C" );
126         
127         # Test the transposition identifiers
128         my $transposition_pools = [ [ 'n2', 'n11' ], [ 'n14', 'n18' ], 
129                                         [ 'n17', 'n15' ] ];
130         my $transposed_nodes = { 'n2' => $transposition_pools->[0],
131                                  'n11' => $transposition_pools->[0],
132                                  'n14' => $transposition_pools->[1],
133                                  'n15' => $transposition_pools->[2],
134                                  'n17' => $transposition_pools->[2],
135                                  'n18' => $transposition_pools->[1],
136         };
137         
138         my $real_transposed_nodes = {};
139         foreach my $r ( $collation->readings ) {
140                 my @same = map { $_->name } @{$r->same_as};
141                 $real_transposed_nodes->{ $r->name } = \@same if @same > 1;
142         }
143                 
144         is_deeply( $real_transposed_nodes, $transposed_nodes, "Found the right transpositions" );
145         
146         # Test turning on a node
147         my @off = $collation->toggle_reading( 'n21' );
148         $expected_nodes[ 16 ] = [ "n21", 1 ];
149         @active_nodes = $collation->lemma_readings( @off );
150         subtest 'Turned on node for new location' => \&compare_active;
151         $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced unto ... ... #';
152         is( make_text( @active_nodes ), $string, "Got the right text" );
153          
154         # Test the toggling effects of same-column
155         @off = $collation->toggle_reading( 'n22' );
156         splice( @expected_nodes, 16, 1, ( [ "n21", 0 ], [ "n22", 1 ] ) );
157         @active_nodes = $collation->lemma_readings( @off );
158         subtest 'Turned on other node in that location' => \&compare_active;
159         $string = '# when ... ... ... showers sweet with ... fruit ... ... of ... has pierced to ... ... #';
160         is( make_text( @active_nodes ), $string, "Got the right text" );
161         
162         # Test the toggling effects of transposition
163         @off = $collation->toggle_reading( 'n14' );
164         # Add the turned on node
165         $expected_nodes[ 11 ] = [ "n14", 1 ];
166         # Remove the 'off' for the previous node
167         splice( @expected_nodes, 16, 1 );
168         @active_nodes = $collation->lemma_readings( @off );
169         subtest 'Turned on transposition node' => \&compare_active;
170         $string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #';
171         is( make_text( @active_nodes ), $string, "Got the right text" );
172         
173         @off = $collation->toggle_reading( 'n18' );
174         # Toggle on the new node
175         $expected_nodes[ 13 ] = [ "n18", 1 ];
176         # Toggle off the transposed node
177         $expected_nodes[ 11 ] = [ "n14", undef ];
178         @active_nodes = $collation->lemma_readings( @off );
179         subtest 'Turned on that node\'s partner' => \&compare_active;
180         $string = '# when ... ... ... showers sweet with ... fruit ... ... of drought has pierced to ... ... #';
181         is( make_text( @active_nodes ), $string, "Got the right text" );
182         
183         @off = $collation->toggle_reading( 'n14' );
184         # Toggle on the new node
185         $expected_nodes[ 11 ] = [ "n14", 1 ];
186         # Toggle off the transposed node
187         $expected_nodes[ 13 ] = [ "n18", undef ];
188         @active_nodes = $collation->lemma_readings( @off );
189         subtest 'Turned on the original node' => \&compare_active;
190         $string = '# when ... ... ... showers sweet with ... fruit ... drought of ... has pierced to ... ... #';
191         is( make_text( @active_nodes ), $string, "Got the right text" );
192         
193         @off = $collation->toggle_reading( 'n15' );
194         # Toggle on the new node, and off with the old
195         splice( @expected_nodes, 11, 1, [ "n14", 0 ], [ "n15", 1 ] );
196         @active_nodes = $collation->lemma_readings( @off );
197         subtest 'Turned on the colocated node' => \&compare_active;
198         $string = '# when ... ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
199         is( make_text( @active_nodes ), $string, "Got the right text" );
200         
201         @off = $collation->toggle_reading( 'n3' );
202         # Toggle on the new node
203         splice( @expected_nodes, 3, 1, [ "n3", 1 ] );
204         # Remove the old toggle-off
205         splice( @expected_nodes, 11, 1 );
206         @active_nodes = $collation->lemma_readings( @off );
207         subtest 'Turned on a singleton node' => \&compare_active;
208         $string = '# when ... with ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
209         is( make_text( @active_nodes ), $string, "Got the right text" );
210         
211         @off = $collation->toggle_reading( 'n3' );
212         # Toggle off this node
213         splice( @expected_nodes, 3, 1, [ "n3", 0 ] );
214         @active_nodes = $collation->lemma_readings( @off );
215         subtest 'Turned off a singleton node' => \&compare_active;
216         $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced to ... ... #';
217         is( make_text( @active_nodes ), $string, "Got the right text" );
218         
219         @off = $collation->toggle_reading( 'n21' );
220         splice( @expected_nodes, 16, 1, ["n22", 0 ], [ "n21", 1 ] );
221         @active_nodes = $collation->lemma_readings( @off );
222         subtest 'Turned on another node after singleton switchoff' => \&compare_active;
223         $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto ... ... #';
224         is( make_text( @active_nodes ), $string, "Got the right text" );
225         
226         # Now start testing some position identifiers
227         # 2. 'april with his' have no colocated
228         # 3. 'april' 2 has no colocated
229         # 4. 'teh' and 'the'
230         # 5. 'drought' & 'march'
231         # 6. 'march' & 'drought'
232         # 7. 'unto' 'the' 'root'...
233         #    'unto can match 'to' or 'teh'
234         #    'the' can match 'teh' or 'rood'
235         #    'root' can mach 'rood'
236         
237         foreach my $cn ( @common_nodes ) {
238                 my $cnr = $collation->reading( $cn );
239                 is( scalar( $collation->same_position_as( $cnr ) ), 0, "Node $cn has no colocations" );
240         }
241         
242         my %expected_colocations = (
243                 'n2' => [],     # april
244                 'n3' => [],     # with
245                 'n4' => [],     # his
246                 'n11' => [],    # april
247                 'n8' => [ 'n13' ],  # teh -> the
248                 'n13' => [ 'n8' ],  # the -> teh
249                 'n14' => [ 'n15' ], # drought -> march
250                 'n18' => [ 'n17' ], # drought -> march
251                 'n17' => [ 'n18' ], # march -> drought
252                 'n15' => [ 'n14' ], # march -> drought
253                 'n21' => [ 'n22', 'n9' ], # unto -> to, teh
254                 'n22' => [ 'n21', 'n9' ], # to -> unto, teh
255                 'n9' => [ 'n21', 'n22', 'n23' ], # teh -> unto, to, the
256                 'n23' => [ 'n25', 'n9' ], # the -> teh, rood
257                 'n25' => [ 'n23', 'n26' ], # rood -> the, root
258                 'n26' => [ 'n25' ], # root -> rood
259         );
260         
261         foreach my $n ( keys %expected_colocations ) {
262                 my $nr = $collation->reading( $n );
263                 my @colocated = sort( map { $_->name } $collation->same_position_as( $nr ) );
264                 is_deeply( \@colocated, $expected_colocations{$n}, "Colocated nodes for $n correct" );
265         }
266         
267         # Test strict colocations
268         $expected_colocations{'n9'} = [];
269         $expected_colocations{'n21'} = ['n22'];
270         $expected_colocations{'n22'} = ['n21'];
271         $expected_colocations{'n23'} = [];
272         $expected_colocations{'n25'} = [];
273         $expected_colocations{'n26'} = [];
274         
275         foreach my $n ( keys %expected_colocations ) {
276                 my $nr = $collation->reading( $n );
277                 my @colocated = sort( map { $_->name } $collation->same_position_as( $nr, 1 ) );
278                 is_deeply( \@colocated, $expected_colocations{$n}, "Strictly colocated nodes for $n correct" );
279         }
280         
281         # Test turning on, then off, an annoyingly overlapping node
282         
283         @off = $collation->toggle_reading( 'n9' );
284         # Remove the old toggle-off
285         splice( @expected_nodes, 16, 1 );
286         splice( @expected_nodes, 17, 0, [ "n9", 1 ] );
287         @active_nodes = $collation->lemma_readings( @off );
288         subtest 'Turned on a node without fixed position' => \&compare_active;
289         $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto teh ... ... #';
290         is( make_text( @active_nodes ), $string, "Got the right text" );
291         
292         @off = $collation->toggle_reading( 'n23' );
293         splice( @expected_nodes, 18, 1, [ "n23", 1 ] );
294         @active_nodes = $collation->lemma_readings( @off );
295         subtest 'Turned on a node colocated to one without fixed position' => \&compare_active;
296         $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto teh the ... #';
297         is( make_text( @active_nodes ), $string, "Got the right text" );
298         
299         @off = $collation->toggle_reading( 'n9' );
300         splice( @expected_nodes, 17, 1, [ "n9", 0 ] );
301         @active_nodes = $collation->lemma_readings( @off );
302         subtest 'Turned on a node colocated to one without fixed position' => \&compare_active;
303         $string = '# when ... ... showers sweet with ... fruit ... march of ... has pierced unto the ... #';
304         is( make_text( @active_nodes ), $string, "Got the right text" );
305         
306         ### Now test relationship madness.
307         
308         my( $result, @relations ) = $collation->add_relationship( 'n25', 'n23', {'type' => 'lexical'} ); # rood -> the
309         ok( $result, "Added relationship between nodes" );
310         is( scalar @relations, 1, "Returned only the one collapse" );
311         is_deeply( $relations[0], [ 'n25', 'n23' ], "Returned the correct collapse" );
312         is( $collation->reading( 'n25' )->position->reference, '9,3', "Harmonized position for n25 correct" );
313         is( $collation->reading( 'n23' )->position->reference, '9,3', "Harmonized position for n23 correct" );
314         is( $collation->reading( 'n9' )->position->reference, '9,2', "Adjusted position for n9 correct" );
315         
316         # Do some yucky hardcoded cleanup to undo this relationship.
317         $collation->reading('n25')->position->max( 4 );
318         $collation->reading('n9')->position->max( 3 );
319         $collation->graph->del_edge( $collation->reading('n25')->edges_to( $collation->reading('n23')) );
320         
321         ( $result, @relations ) = $collation->add_relationship( 'n26', 'n25', {'type' => 'spelling'} ); # root -> rood
322         ok( $result, "Added relationship between nodes" );
323         is( scalar @relations, 1, "Returned only the one collapse" );
324         is_deeply( $relations[0], [ 'n26', 'n25' ], "Returned the correct collapse" );
325         is( $collation->reading( 'n26' )->position->reference, '9,4', "Harmonized position for n26 correct" );
326         is( $collation->reading( 'n25' )->position->reference, '9,4', "Harmonized position for n25 correct" );
327         is( $collation->reading( 'n9' )->position->reference, '9,2-3', "Adjusted position for n9 correct" );
328         
329         ( $result, @relations ) = $collation->add_relationship( 'n15', 'n9', {'type' => 'lexical'} ); # bogus march -> teh
330         ok( !$result, "Refused to add skewed relationship: " . $relations[0] );
331         
332         ( $result, @relations ) = $collation->add_relationship( 'n25', 'n26', {'type' => 'spelling'} ); # root -> rood
333         ok( !$result, "Refused to add dupe relationship: " . $relations[0] );
334         
335         ( $result, @relations ) = $collation->add_relationship( 'n8', 'n13', {'type' => 'spelling', 'global' => 1 } ); # teh -> the
336         ok( $result, "Added global relationship between nodes" );
337         is( scalar @relations, 2, "Returned two relationship creations" );
338         is_deeply( $relations[0], [ 'n8', 'n13' ], "Returned the original collapse" );
339         is_deeply( $relations[1], [ 'n9', 'n23' ], "Returned the other collapse" );
340         is( $collation->reading( 'n8' )->position->reference, '6,2', "Harmonized position for n8 correct" );
341         is( $collation->reading( 'n9' )->position->reference, '9,3', "Harmonized position for n9 correct" );
342 }
343
344 done_testing();