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