9 use Text::Tradition::Analysis qw/ run_analysis /;
11 binmode STDOUT, ':utf8';
12 binmode STDERR, ':utf8';
13 eval { no warnings; binmode $DB::OUT, ':utf8'; $DB::deep = 1000 };
15 my $tradition = Text::Tradition->new(
17 'file' => 't/data/besoin.xml' );
18 $tradition->enable_stemmata;
19 $tradition->add_stemma( 'dotfile' => 't/data/besoin.dot' );
21 # Run the analysis of the tradition
22 ## TODO Make proper test db
23 my $calcdsn = 'dbi:SQLite:dbname=t/data/analysis.db';
42 155 => 'genealogical',
43 170 => 'genealogical',
45 205 => 'genealogical',
46 219 => 'genealogical',
47 239 => 'genealogical',
48 244 => 'genealogical',
50 251 => 'genealogical',
51 252 => 'genealogical',
52 293 => 'genealogical',
53 295 => 'genealogical',
54 309 => 'genealogical',
59 318 => 'genealogical',
60 319 => 'genealogical',
64 335 => 'genealogical',
71 406 => 'genealogical',
72 413 => 'genealogical',
76 493 => 'genealogical',
86 632 => 'genealogical',
87 634 => 'genealogical',
88 636 => 'genealogical',
89 685 => 'genealogical',
90 737 => 'genealogical',
101 780 => 'genealogical',
105 837 => 'genealogical',
162 925 => 'genealogical',
164 952 => 'genealogical',
165 954 => 'genealogical',
166 969 => 'genealogical',
167 972 => 'genealogical',
168 973 => 'genealogical',
170 1003 => 'genealogical',
171 1004 => 'genealogical' # check for transp
176 my @all_variant_ranks = sort { $a <=> $b } keys( %expected );
177 # Look through the results
178 my $c = $tradition->collation;
180 my $results = run_analysis( $tradition, %analysis_opts );
182 foreach my $row ( @{$results->{'variants'}} ) {
183 push( @analyzed, $row->{id} );
184 $num_readings{$row->{id}} = scalar @{$row->{'readings'}};
185 my $type = 'genealogical';
186 if( grep { $_->{'is_conflict'} } @{$row->{'readings'}} ) {
188 } elsif( grep { $_->{'is_reverted'} } @{$row->{'readings'}} ) {
191 my $expected = $expected{$row->{'id'}};
192 $expected = 'genealogical' if $expected eq 'type1';
193 is( $type, $expected, "Got expected genealogical result for rank " . $row->{'id'} );
194 # If the row is genealogical, there should be one reading with no parents,
195 # every reading should independently occur exactly once, and the total
196 # number of changes + maybe-changes should equal the total number of
197 # readings who have that one as a parent.
198 ok( !exists $row->{'unsolved'}, "Got a solution for the stated problem" );
199 next if exists $row->{'unsolved'};
200 if( $row->{'genealogical'} ) {
201 # Make the mapping of parent -> child readings
204 foreach my $rdg ( @{$row->{'readings'}} ) {
205 my $parents = $rdg->{'source_parents'} || {};
206 foreach my $p ( keys %$parents ) {
207 push( @{$is_parent{$p}}, $rdg->{'readingid'} );
209 push( @has_no_parent, $rdg->{'readingid'} ) unless keys %$parents;
212 foreach my $rdg ( @{$row->{'readings'}} ) {
213 is( @{$rdg->{'independent_occurrence'}}, 1,
214 "Genealogical reading originates exactly once" );
216 is( @has_no_parent, 1, "Only one genealogical reading lacks a parent" );
219 # Check that run_analysis ran an analysis on all our known variant ranks
220 is_deeply( \@all_variant_ranks, \@analyzed, "Ran analysis for all expected rows" );
222 # Now run it again, excluding type 1 variants.
223 map { delete $expected{$_} if $expected{$_} eq 'type1' } keys %expected;
224 my @useful_variant_ranks = sort { $a <=> $b } keys( %expected );
225 $analysis_opts{'exclude_type1'} = 1;
227 $results = run_analysis( $tradition, %analysis_opts );
228 foreach my $row ( @{$results->{'variants'}} ) {
229 ok( !exists $row->{'unsolved'}, "Got a solution for the stated problem" );
230 next if exists $row->{'unsolved'};
231 push( @analyzed, $row->{id} );
232 my $type = 'genealogical';
233 if( grep { $_->{'is_conflict'} } @{$row->{'readings'}} ) {
235 } elsif( grep { $_->{'is_reverted'} } @{$row->{'readings'}} ) {
238 is( $type, $expected{$row->{'id'}}, "Got expected genealogical result on exclude_type1 run for rank " . $row->{'id'} );
240 is_deeply( \@analyzed, \@useful_variant_ranks, "Ran analysis for all useful rows" );
242 # Now run it again, excluding orthography / spelling.
243 my @merged_exclude = qw/ 76 136 142 155 293 317 319 335 350 361 413 500 515 636 685
245 # 205 now conflicts; it and 493 should also have one fewer reading
246 $expected{205} = 'conflict';
247 $num_readings{205}--;
248 $num_readings{493}--;
249 map { delete $expected{$_} } @merged_exclude;
250 my @merged_remaining = sort { $a <=> $b } keys( %expected );
251 $analysis_opts{'merge_types'} = [ qw/ orthographic spelling / ];
253 $results = run_analysis( $tradition, %analysis_opts );
254 foreach my $row ( @{$results->{'variants'}} ) {
255 push( @analyzed, $row->{id} );
256 ok( !exists $row->{'unsolved'}, "Got a solution for the stated problem" );
257 next if exists $row->{'unsolved'};
258 my $type = 'genealogical';
259 if( grep { $_->{'is_conflict'} } @{$row->{'readings'}} ) {
261 } elsif( grep { $_->{'is_reverted'} } @{$row->{'readings'}} ) {
264 is( $type, $expected{$row->{'id'}}, "Got expected genealogical result on merge_types run for rank " . $row->{'id'} );
265 is( scalar @{$row->{'readings'}}, $num_readings{$row->{id}}, "Got expected number of readings during merge" );
267 is_deeply( \@analyzed, \@merged_remaining, "Ran analysis for all useful unmerged rows" );