return the new reading generated on duplication
[scpubgit/stemmatology.git] / base / t / text_tradition_collation_relationshipstore.t
CommitLineData
b0b4421a 1#!/usr/bin/perl -w
2
3use strict;
4use Test::More 'no_plan';
5$| = 1;
6
7
8
9# =begin testing
10{
11use Text::Tradition;
ee801e17 12use TryCatch;
b0b4421a 13
14use_ok( 'Text::Tradition::Collation::RelationshipStore' );
ee801e17 15
16# Add some relationships, and delete them
17
18my $cxfile = 't/data/Collatex-16.xml';
19my $t = Text::Tradition->new(
56772e8c 20 'name' => 'inline',
21 'input' => 'CollateX',
22 'file' => $cxfile,
23 );
ee801e17 24my $c = $t->collation;
25
f8331a4d 26my @v1 = $c->add_relationship( 'n21', 'n22', { 'type' => 'lexical' } );
ee801e17 27is( scalar @v1, 1, "Added a single relationship" );
28is( $v1[0]->[0], 'n21', "Got correct node 1" );
29is( $v1[0]->[1], 'n22', "Got correct node 2" );
679f17e1 30my @v2 = $c->add_relationship( 'n24', 'n23',
ee801e17 31 { 'type' => 'spelling', 'scope' => 'global' } );
32is( scalar @v2, 2, "Added a global relationship with two instances" );
33@v1 = $c->del_relationship( 'n22', 'n21' );
34is( scalar @v1, 1, "Deleted first relationship" );
679f17e1 35@v2 = $c->del_relationship( 'n12', 'n13' );
ee801e17 36is( scalar @v2, 2, "Deleted second global relationship" );
681893aa 37my @v3 = $c->del_relationship( 'n1', 'n2' );
38is( scalar @v3, 0, "Nothing deleted on non-existent relationship" );
b0b4421a 39}
40
41
42
6d381462 43# =begin testing
44{
56772e8c 45use Test::Warn;
6d381462 46use Text::Tradition;
47use TryCatch;
48
56772e8c 49my $t1;
e92d4229 50warnings_exist {
56772e8c 51 $t1 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
e92d4229 52} [qr/Cannot set relationship on a meta reading/],
56772e8c 53 "Got expected relationship drop warning on parse";
54
176badfe 55# Test 1.1: try to equate nodes that are prevented with an intermediate collation
6d381462 56ok( $t1, "Parsed test fragment file" );
57my $c1 = $t1->collation;
10e4b1ac 58my $trel = $c1->get_relationship( 'r9.2', 'r9.3' );
6d381462 59is( ref( $trel ), 'Text::Tradition::Collation::Relationship',
60 "Troublesome relationship exists" );
61is( $trel->type, 'collated', "Troublesome relationship is a collation" );
62
63# Try to make the link we want
64try {
10e4b1ac 65 $c1->add_relationship( 'r8.6', 'r10.3', { 'type' => 'orthographic' } );
6d381462 66 ok( 1, "Added cross-collation relationship as expected" );
176badfe 67} catch( Text::Tradition::Error $e ) {
68 ok( 0, "Existing collation blocked equivalence relationship: " . $e->message );
6d381462 69}
70
71try {
72 $c1->calculate_ranks();
73 ok( 1, "Successfully calculated ranks" );
176badfe 74} catch ( Text::Tradition::Error $e ) {
75 ok( 0, "Collation now has a cycle: " . $e->message );
6d381462 76}
77
176badfe 78# Test 1.2: attempt merge of an identical reading
359944f7 79try {
10e4b1ac 80 $c1->merge_readings( 'r9.3', 'r11.5' );
359944f7 81 ok( 1, "Successfully merged reading 'pontifex'" );
82} catch ( Text::Tradition::Error $e ) {
83 ok( 0, "Merge of mergeable readings failed: $e->message" );
84
85}
86
176badfe 87# Test 1.3: attempt relationship with a meta reading (should fail)
88try {
10e4b1ac 89 $c1->add_relationship( 'r8.1', 'r9.2', { 'type' => 'collated' } );
176badfe 90 ok( 0, "Allowed a meta-reading to be used in a relationship" );
91} catch ( Text::Tradition::Error $e ) {
92 is( $e->message, 'Cannot set relationship on a meta reading',
93 "Relationship link prevented for a meta reading" );
94}
95
beb47b16 96# Test 1.4: try to break a relationship near a meta reading
97$c1->add_relationship( 'r7.6', 'r7.3', { type => 'orthographic' } );
98try {
99 $c1->del_relationship( 'r7.6', 'r7.7' );
100 $c1->del_relationship( 'r7.6', 'r7.3' );
101 ok( 1, "Relationship broken with a meta reading as neighbor" );
102} catch {
103 ok( 0, "Relationship deletion failed with a meta reading as neighbor" );
104}
105
176badfe 106# Test 2.1: try to equate nodes that are prevented with a real intermediate
6d381462 107# equivalence
56772e8c 108my $t2;
e92d4229 109warnings_exist {
56772e8c 110 $t2 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
e92d4229 111} [qr/Cannot set relationship on a meta reading/],
56772e8c 112 "Got expected relationship drop warning on parse";
6d381462 113my $c2 = $t2->collation;
10e4b1ac 114$c2->add_relationship( 'r9.2', 'r9.3', { 'type' => 'lexical' } );
115my $trel2 = $c2->get_relationship( 'r9.2', 'r9.3' );
6d381462 116is( ref( $trel2 ), 'Text::Tradition::Collation::Relationship',
117 "Created blocking relationship" );
118is( $trel2->type, 'lexical', "Blocking relationship is not a collation" );
119# This time the link ought to fail
120try {
10e4b1ac 121 $c2->add_relationship( 'r8.6', 'r10.3', { 'type' => 'orthographic' } );
cc31ebaa 122 ok( 0, "Added cross-equivalent bad relationship" );
176badfe 123} catch ( Text::Tradition::Error $e ) {
124 like( $e->message, qr/witness loop/,
125 "Existing equivalence blocked crossing relationship" );
6d381462 126}
127
128try {
129 $c2->calculate_ranks();
130 ok( 1, "Successfully calculated ranks" );
176badfe 131} catch ( Text::Tradition::Error $e ) {
132 ok( 0, "Collation now has a cycle: " . $e->message );
6d381462 133}
cc31ebaa 134
176badfe 135# Test 3.1: make a straightforward pair of transpositions.
cc31ebaa 136my $t3 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/lf2.xml' );
137# Test 1: try to equate nodes that are prevented with an intermediate collation
138my $c3 = $t3->collation;
139try {
10e4b1ac 140 $c3->add_relationship( 'r36.4', 'r38.3', { 'type' => 'transposition' } );
cc31ebaa 141 ok( 1, "Added straightforward transposition" );
176badfe 142} catch ( Text::Tradition::Error $e ) {
143 ok( 0, "Failed to add normal transposition: " . $e->message );
cc31ebaa 144}
145try {
10e4b1ac 146 $c3->add_relationship( 'r36.3', 'r38.2', { 'type' => 'transposition' } );
cc31ebaa 147 ok( 1, "Added straightforward transposition complement" );
176badfe 148} catch ( Text::Tradition::Error $e ) {
149 ok( 0, "Failed to add normal transposition complement: " . $e->message );
cc31ebaa 150}
151
176badfe 152# Test 3.2: try to make a transposition that could be a parallel.
cc31ebaa 153try {
10e4b1ac 154 $c3->add_relationship( 'r28.2', 'r29.2', { 'type' => 'transposition' } );
cc31ebaa 155 ok( 0, "Added bad colocated transposition" );
176badfe 156} catch ( Text::Tradition::Error $e ) {
157 like( $e->message, qr/Readings appear to be colocated/,
158 "Prevented bad colocated transposition" );
cc31ebaa 159}
160
176badfe 161# Test 3.3: make the parallel, and then make the transposition again.
cc31ebaa 162try {
10e4b1ac 163 $c3->add_relationship( 'r28.3', 'r29.3', { 'type' => 'orthographic' } );
cc31ebaa 164 ok( 1, "Equated identical readings for transposition" );
176badfe 165} catch ( Text::Tradition::Error $e ) {
166 ok( 0, "Failed to equate identical readings: " . $e->message );
cc31ebaa 167}
168try {
10e4b1ac 169 $c3->add_relationship( 'r28.2', 'r29.2', { 'type' => 'transposition' } );
cc31ebaa 170 ok( 1, "Added straightforward transposition complement" );
176badfe 171} catch ( Text::Tradition::Error $e ) {
172 ok( 0, "Failed to add normal transposition complement: " . $e->message );
cc31ebaa 173}
98a66507 174
9e9b7540 175# Test 4: make a global relationship that involves re-ranking a node first, when
98a66507 176# the prior rank has a potential match too
177my $t4 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/globalrel_test.xml' );
178my $c4 = $t4->collation;
179# Can we even add the relationship?
180try {
181 $c4->add_relationship( 'r463.2', 'r463.4',
182 { type => 'orthographic', scope => 'global' } );
183 ok( 1, "Added global relationship without error" );
184} catch ( Text::Tradition::Error $e ) {
185 ok( 0, "Failed to add global relationship when same-rank alternative exists: "
186 . $e->message );
187}
188$c4->calculate_ranks();
189# Do our readings now share a rank?
190is( $c4->reading('r463.2')->rank, $c4->reading('r463.4')->rank,
191 "Expected readings now at same rank" );
9e9b7540 192
193# Test group 5: relationship transitivity.
194my $t5 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/john.xml' );
195my $c5 = $t5->collation;
0e4e4e4b 196# Test 5.0: propagate all existing transitive rels and make sure it succeeds
197my $orignumrels = scalar $c5->relationships();
198try {
199 $c5->relations->propagate_all_relationships();
200 ok( 1, "Propagated all existing transitive relationships" );
201} catch ( Text::Tradition::Error $err ) {
202 ok( 0, "Failed to propagate all existing relationships: " . $err->message );
203}
204ok( scalar( $c5->relationships ) > $orignumrels, "Added some relationships in propagation" );
9e9b7540 205
206# Test 5.1: make a grammatical link to an orthographically-linked reading
207$c5->add_relationship( 'r13.5', 'r13.2', { type => 'orthographic' } );
208$c5->add_relationship( 'r13.1', 'r13.2', { type => 'grammatical', propagate => 1 } );
209my $impliedrel = $c5->get_relationship( 'r13.1', 'r13.5' );
210ok( $impliedrel, 'Relationship was made between indirectly linked readings' );
211if( $impliedrel ) {
212 is( $impliedrel->type, 'grammatical', 'Implicit inbound relationship has the correct type' );
213}
214
215# Test 5.2: make another orthographic link, see if the grammatical one propagates
216$c5->add_relationship( 'r13.3', 'r13.5', { type => 'orthographic', propagate => 1 } );
217foreach my $rdg ( qw/ r13.3 r13.5 / ) {
218 my $newgram = $c5->get_relationship( 'r13.1', $rdg );
219 ok( $newgram, 'Relationship was propagaged up between indirectly linked readings' );
220 if( $newgram ) {
221 is( $newgram->type, 'grammatical', 'Implicit outbound relationship has the correct type' );
222 }
223}
224my $neworth = $c5->get_relationship( 'r13.2', 'r13.3' );
225ok( $neworth, 'Relationship was made between indirectly linked siblings' );
226if( $neworth ) {
227 is( $neworth->type, 'orthographic', 'Implicit direct relationship has the correct type' );
228}
229
230# Test 5.3: make an intermediate (spelling) link to the remaining node
231$c5->add_relationship( 'r13.4', 'r13.2', { type => 'spelling', propagate => 1 } );
232# Should be linked grammatically to 12.1, spelling-wise to the rest
233my $newgram = $c5->get_relationship( 'r13.4', 'r13.1' );
234ok( $newgram, 'Relationship was made between indirectly linked readings' );
235if( $newgram ) {
236 is( $newgram->type, 'grammatical', 'Implicit intermediate-out relationship has the correct type' );
237}
238foreach my $rdg ( qw/ r13.3 r13.5 / ) {
239 my $newspel = $c5->get_relationship( 'r13.4', $rdg );
240 ok( $newspel, 'Relationship was made between indirectly linked readings' );
241 if( $newspel ) {
242 is( $newspel->type, 'spelling', 'Implicit intermediate-in relationship has the correct type' );
243 }
244}
245
52179f61 246# Test 5.4: delete a spelling relationship, add it again, make sure it doesn't
247# throw and make sure all the relationships are the same
248my $numrel = scalar $c5->relationships;
249$c5->del_relationship( 'r13.4', 'r13.2' );
250try {
251 $c5->add_relationship( 'r13.4', 'r13.2', { type => 'spelling', propagate => 1 } );
252 ok( 1, "Managed not to throw an exception re-adding the relationship" );
253} catch( Text::Tradition::Error $e ) {
254 ok( 0, "Threw an exception trying to re-add our intermediate relationship: " . $e->message );
255}
256is( $numrel, scalar $c5->relationships, "Number of relationships did not change" );
257foreach my $rdg ( qw/ r13.2 r13.3 r13.5 / ) {
258 my $newspel = $c5->get_relationship( 'r13.4', $rdg );
259 ok( $newspel, 'Relationship was made between indirectly linked readings' );
260 if( $newspel ) {
261 is( $newspel->type, 'spelling', 'Reinstated intermediate-in relationship has the correct type' );
262 }
263}
264my $stillgram = $c5->get_relationship( 'r13.4', 'r13.1' );
265ok( $stillgram, 'Relationship was made between indirectly linked readings' );
266if( $stillgram ) {
267 is( $stillgram->type, 'grammatical', 'Reinstated intermediate-out relationship has the correct type' );
268}
269
270# Test 5.5: add a parallel but not sibling relationship
9e9b7540 271$c5->add_relationship( 'r13.6', 'r13.2', { type => 'lexical', propagate => 1 } );
272ok( !$c5->get_relationship( 'r13.6', 'r13.1' ),
273 "Lexical relationship did not affect grammatical" );
274foreach my $rdg ( qw/ r13.3 r13.4 r13.5 / ) {
275 my $newlex = $c5->get_relationship( 'r13.6', $rdg );
276 ok( $newlex, 'Parallel was made between indirectly linked readings' );
277 if( $newlex ) {
278 is( $newlex->type, 'lexical', 'Implicit parallel-down relationship has the correct type' );
279 }
280}
281
52179f61 282# Test 5.6: try it with non-colocated relationships
283$numrel = scalar $c5->relationships;
9e9b7540 284$c5->add_relationship( 'r62.1', 'r64.1', { type => 'transposition', propagate => 1 } );
285is( scalar $c5->relationships, $numrel+1,
286 "Adding non-colo relationship did not propagate" );
287# Add a pivot point
288$c5->add_relationship( 'r61.1', 'r61.5', { type => 'orthographic' } );
289# Add a third transposed node
290$c5->add_relationship( 'r62.1', 'r60.3', { type => 'transposition', propagate => 1 } );
291my $newtrans = $c5->get_relationship( 'r64.1', 'r60.3' );
292ok( $newtrans, 'Non-colo relationship was made between indirectly linked readings' );
293if( $newtrans ) {
294 is( $newtrans->type, 'transposition', 'Implicit non-colo relationship has the correct type' );
295}
296is( scalar $c5->relationships, $numrel+4,
297 "Adding non-colo relationship only propagated on non-colos" );
0e4e4e4b 298
299# Test 5.7: ensure that attempts to cross boundaries on bindlevel-equal
300# relationships fail.
301try {
302 $c5->add_relationship( 'r39.6', 'r41.1', { type => 'grammatical', propagate => 1 } );
303 ok( 0, "Did not prevent add of conflicting relationship level" );
304} catch( Text::Tradition::Error $err ) {
305 like( $err->message, qr/Conflicting existing relationship/, "Got correct error message trying to add conflicting relationship level" );
306}
307
308# Test 5.8: ensure that weak relationships don't interfere
309$c5->add_relationship( 'r50.1', 'r50.2', { type => 'collated' } );
310$c5->add_relationship( 'r50.3', 'r50.4', { type => 'orthographic' } );
311try {
312 $c5->add_relationship( 'r50.4', 'r50.1', { type => 'grammatical', propagate => 1 } );
313 ok( 1, "Collation did not interfere with new relationship add" );
314} catch( Text::Tradition::Error $err ) {
315 ok( 0, "Collation interfered with new relationship add: " . $err->message );
316}
317my $crel = $c5->get_relationship( 'r50.1', 'r50.2' );
318ok( $crel, "Original relationship still exists" );
319if( $crel ) {
320 is( $crel->type, 'collated', "Original relationship still a collation" );
321}
322
323try {
324 $c5->add_relationship( 'r50.1', 'r51.1', { type => 'spelling', propagate => 1 } );
325 ok( 1, "Collation did not interfere with relationship re-ranking" );
326} catch( Text::Tradition::Error $err ) {
327 ok( 0, "Collation interfered with relationship re-ranking: " . $err->message );
328}
329$crel = $c5->get_relationship( 'r50.1', 'r50.2' );
330ok( !$crel, "Collation relationship now gone" );
331
332# Test 5.9: ensure that strong non-transitive relationships don't interfere
333$c5->add_relationship( 'r66.1', 'r66.4', { type => 'grammatical' } );
334$c5->add_relationship( 'r66.2', 'r66.4', { type => 'uncertain', propagate => 1 } );
335try {
336 $c5->add_relationship( 'r66.1', 'r66.3', { type => 'grammatical', propagate => 1 } );
337 ok( 1, "Non-transitive relationship did not block grammatical add" );
338} catch( Text::Tradition::Error $err ) {
339 ok( 0, "Non-transitive relationship blocked grammatical add: " . $err->message );
340}
341is( scalar $c5->related_readings( 'r66.4' ), 3, "Reading 66.4 has all its links" );
342is( scalar $c5->related_readings( 'r66.2' ), 1, "Reading 66.2 has only one link" );
343is( scalar $c5->related_readings( 'r66.1' ), 2, "Reading 66.1 has all its links" );
344is( scalar $c5->related_readings( 'r66.3' ), 2, "Reading 66.3 has all its links" );
6d381462 345}
346
347
348
b0b4421a 349
3501;