implement relationship logical propagation
[scpubgit/stemmatology.git] / base / t / text_tradition_collation_relationshipstore.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5 $| = 1;
6
7
8
9 # =begin testing
10 {
11 use Text::Tradition;
12 use TryCatch;
13
14 use_ok( 'Text::Tradition::Collation::RelationshipStore' );
15
16 # Add some relationships, and delete them
17
18 my $cxfile = 't/data/Collatex-16.xml';
19 my $t = Text::Tradition->new( 
20         'name'  => 'inline', 
21         'input' => 'CollateX',
22         'file'  => $cxfile,
23         );
24 my $c = $t->collation;
25
26 my @v1 = $c->add_relationship( 'n21', 'n22', { 'type' => 'lexical' } );
27 is( scalar @v1, 1, "Added a single relationship" );
28 is( $v1[0]->[0], 'n21', "Got correct node 1" );
29 is( $v1[0]->[1], 'n22', "Got correct node 2" );
30 my @v2 = $c->add_relationship( 'n24', 'n23', 
31         { 'type' => 'spelling', 'scope' => 'global' } );
32 is( scalar @v2, 2, "Added a global relationship with two instances" );
33 @v1 = $c->del_relationship( 'n22', 'n21' );
34 is( scalar @v1, 1, "Deleted first relationship" );
35 @v2 = $c->del_relationship( 'n12', 'n13' );
36 is( scalar @v2, 2, "Deleted second global relationship" );
37 my @v3 = $c->del_relationship( 'n1', 'n2' );
38 is( scalar @v3, 0, "Nothing deleted on non-existent relationship" );
39 }
40
41
42
43 # =begin testing
44 {
45 use Test::Warn;
46 use Text::Tradition;
47 use TryCatch;
48
49 my $t1;
50 warnings_exist {
51         $t1 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
52 } [qr/Cannot set relationship on a meta reading/],
53         "Got expected relationship drop warning on parse";
54
55 # Test 1.1: try to equate nodes that are prevented with an intermediate collation
56 ok( $t1, "Parsed test fragment file" );
57 my $c1 = $t1->collation;
58 my $trel = $c1->get_relationship( 'r9.2', 'r9.3' );
59 is( ref( $trel ), 'Text::Tradition::Collation::Relationship',
60         "Troublesome relationship exists" );
61 is( $trel->type, 'collated', "Troublesome relationship is a collation" );
62
63 # Try to make the link we want
64 try {
65         $c1->add_relationship( 'r8.6', 'r10.3', { 'type' => 'orthographic' } );
66         ok( 1, "Added cross-collation relationship as expected" );
67 } catch( Text::Tradition::Error $e ) {
68         ok( 0, "Existing collation blocked equivalence relationship: " . $e->message );
69 }
70
71 try {
72         $c1->calculate_ranks();
73         ok( 1, "Successfully calculated ranks" );
74 } catch ( Text::Tradition::Error $e ) {
75         ok( 0, "Collation now has a cycle: " . $e->message );
76 }
77
78 # Test 1.2: attempt merge of an identical reading
79 try {
80         $c1->merge_readings( 'r9.3', 'r11.5' );
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
87 # Test 1.3: attempt relationship with a meta reading (should fail)
88 try {
89         $c1->add_relationship( 'r8.1', 'r9.2', { 'type' => 'collated' } );
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
96 # Test 1.4: try to break a relationship near a meta reading
97 $c1->add_relationship( 'r7.6', 'r7.3', { type => 'orthographic' } );
98 try {
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
106 # Test 2.1: try to equate nodes that are prevented with a real intermediate
107 # equivalence
108 my $t2;
109 warnings_exist {
110         $t2 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
111 } [qr/Cannot set relationship on a meta reading/],
112         "Got expected relationship drop warning on parse";
113 my $c2 = $t2->collation;
114 $c2->add_relationship( 'r9.2', 'r9.3', { 'type' => 'lexical' } );
115 my $trel2 = $c2->get_relationship( 'r9.2', 'r9.3' );
116 is( ref( $trel2 ), 'Text::Tradition::Collation::Relationship',
117         "Created blocking relationship" );
118 is( $trel2->type, 'lexical', "Blocking relationship is not a collation" );
119 # This time the link ought to fail
120 try {
121         $c2->add_relationship( 'r8.6', 'r10.3', { 'type' => 'orthographic' } );
122         ok( 0, "Added cross-equivalent bad relationship" );
123 } catch ( Text::Tradition::Error $e ) {
124         like( $e->message, qr/witness loop/,
125                 "Existing equivalence blocked crossing relationship" );
126 }
127
128 try {
129         $c2->calculate_ranks();
130         ok( 1, "Successfully calculated ranks" );
131 } catch ( Text::Tradition::Error $e ) {
132         ok( 0, "Collation now has a cycle: " . $e->message );
133 }
134
135 # Test 3.1: make a straightforward pair of transpositions.
136 my $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
138 my $c3 = $t3->collation;
139 try {
140         $c3->add_relationship( 'r36.4', 'r38.3', { 'type' => 'transposition' } );
141         ok( 1, "Added straightforward transposition" );
142 } catch ( Text::Tradition::Error $e ) {
143         ok( 0, "Failed to add normal transposition: " . $e->message );
144 }
145 try {
146         $c3->add_relationship( 'r36.3', 'r38.2', { 'type' => 'transposition' } );
147         ok( 1, "Added straightforward transposition complement" );
148 } catch ( Text::Tradition::Error $e ) {
149         ok( 0, "Failed to add normal transposition complement: " . $e->message );
150 }
151
152 # Test 3.2: try to make a transposition that could be a parallel.
153 try {
154         $c3->add_relationship( 'r28.2', 'r29.2', { 'type' => 'transposition' } );
155         ok( 0, "Added bad colocated transposition" );
156 } catch ( Text::Tradition::Error $e ) {
157         like( $e->message, qr/Readings appear to be colocated/,
158                 "Prevented bad colocated transposition" );
159 }
160
161 # Test 3.3: make the parallel, and then make the transposition again.
162 try {
163         $c3->add_relationship( 'r28.3', 'r29.3', { 'type' => 'orthographic' } );
164         ok( 1, "Equated identical readings for transposition" );
165 } catch ( Text::Tradition::Error $e ) {
166         ok( 0, "Failed to equate identical readings: " . $e->message );
167 }
168 try {
169         $c3->add_relationship( 'r28.2', 'r29.2', { 'type' => 'transposition' } );
170         ok( 1, "Added straightforward transposition complement" );
171 } catch ( Text::Tradition::Error $e ) {
172         ok( 0, "Failed to add normal transposition complement: " . $e->message );
173 }
174
175 # Test 4: make a global relationship that involves re-ranking a node first, when 
176 # the prior rank has a potential match too
177 my $t4 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/globalrel_test.xml' );
178 my $c4 = $t4->collation;
179 # Can we even add the relationship?
180 try {
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?
190 is( $c4->reading('r463.2')->rank, $c4->reading('r463.4')->rank, 
191         "Expected readings now at same rank" );
192         
193 # Test group 5: relationship transitivity.
194 my $t5 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/john.xml' );
195 my $c5 = $t5->collation;
196
197 # Test 5.1: make a grammatical link to an orthographically-linked reading
198 $c5->add_relationship( 'r13.5', 'r13.2', { type => 'orthographic' } );
199 $c5->add_relationship( 'r13.1', 'r13.2', { type => 'grammatical', propagate => 1 } );
200 my $impliedrel = $c5->get_relationship( 'r13.1', 'r13.5' );
201 ok( $impliedrel, 'Relationship was made between indirectly linked readings' );
202 if( $impliedrel ) {
203         is( $impliedrel->type, 'grammatical', 'Implicit inbound relationship has the correct type' );
204 }
205
206 # Test 5.2: make another orthographic link, see if the grammatical one propagates
207 $c5->add_relationship( 'r13.3', 'r13.5', { type => 'orthographic', propagate => 1 } );
208 foreach my $rdg ( qw/ r13.3 r13.5 / ) {
209         my $newgram = $c5->get_relationship( 'r13.1', $rdg );
210         ok( $newgram, 'Relationship was propagaged up between indirectly linked readings' );
211         if( $newgram ) {
212                 is( $newgram->type, 'grammatical', 'Implicit outbound relationship has the correct type' );
213         }
214 }
215 my $neworth = $c5->get_relationship( 'r13.2', 'r13.3' );
216 ok( $neworth, 'Relationship was made between indirectly linked siblings' );
217 if( $neworth ) {
218         is( $neworth->type, 'orthographic', 'Implicit direct relationship has the correct type' );
219 }
220
221 # Test 5.3: make an intermediate (spelling) link to the remaining node
222 $c5->add_relationship( 'r13.4', 'r13.2', { type => 'spelling', propagate => 1 } );
223 # Should be linked grammatically to 12.1, spelling-wise to the rest
224 my $newgram = $c5->get_relationship( 'r13.4', 'r13.1' );
225 ok( $newgram, 'Relationship was made between indirectly linked readings' );
226 if( $newgram ) {
227         is( $newgram->type, 'grammatical', 'Implicit intermediate-out relationship has the correct type' );
228 }
229 foreach my $rdg ( qw/ r13.3 r13.5 / ) {
230         my $newspel = $c5->get_relationship( 'r13.4', $rdg );
231         ok( $newspel, 'Relationship was made between indirectly linked readings' );
232         if( $newspel ) {
233                 is( $newspel->type, 'spelling', 'Implicit intermediate-in relationship has the correct type' );
234         }
235 }
236
237 # Test 5.4: add a parallel but not sibling relationship
238 $c5->add_relationship( 'r13.6', 'r13.2', { type => 'lexical', propagate => 1 } );
239 ok( !$c5->get_relationship( 'r13.6', 'r13.1' ), 
240         "Lexical relationship did not affect grammatical" );
241 foreach my $rdg ( qw/ r13.3 r13.4 r13.5 / ) {
242         my $newlex = $c5->get_relationship( 'r13.6', $rdg );
243         ok( $newlex, 'Parallel was made between indirectly linked readings' );
244         if( $newlex ) {
245                 is( $newlex->type, 'lexical', 'Implicit parallel-down relationship has the correct type' );
246         }
247 }
248
249 # Test 5.5: try it with non-colocated relationships
250 my $numrel = scalar $c5->relationships;
251 $c5->add_relationship( 'r62.1', 'r64.1', { type => 'transposition', propagate => 1 } );
252 is( scalar $c5->relationships, $numrel+1, 
253         "Adding non-colo relationship did not propagate" );
254 # Add a pivot point
255 $c5->add_relationship( 'r61.1', 'r61.5', { type => 'orthographic' } );
256 # Add a third transposed node
257 $c5->add_relationship( 'r62.1', 'r60.3', { type => 'transposition', propagate => 1 } );
258 my $newtrans = $c5->get_relationship( 'r64.1', 'r60.3' );
259 ok( $newtrans, 'Non-colo relationship was made between indirectly linked readings' );
260 if( $newtrans ) {
261         is( $newtrans->type, 'transposition', 'Implicit non-colo relationship has the correct type' );
262 }
263 is( scalar $c5->relationships, $numrel+4, 
264         "Adding non-colo relationship only propagated on non-colos" );
265 }
266
267
268
269
270 1;