implement persistent equivalence graph for relationship tracking
[scpubgit/stemmatology.git] / 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(
20 'name' => 'inline',
21 'input' => 'CollateX',
22 'file' => $cxfile,
23 );
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{
45use Text::Tradition;
46use TryCatch;
47
48my $t1 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
49# Test 1: try to equate nodes that are prevented with an intermediate collation
50ok( $t1, "Parsed test fragment file" );
51my $c1 = $t1->collation;
52my $trel = $c1->get_relationship( '9,2', '9,3' );
53is( ref( $trel ), 'Text::Tradition::Collation::Relationship',
54 "Troublesome relationship exists" );
55is( $trel->type, 'collated', "Troublesome relationship is a collation" );
56
57# Try to make the link we want
58try {
59 $c1->add_relationship( '8,6', '10,3', { 'type' => 'orthographic' } );
60 ok( 1, "Added cross-collation relationship as expected" );
61} catch {
62 ok( 0, "Existing collation blocked equivalence relationship" );
63}
64
65try {
66 $c1->calculate_ranks();
67 ok( 1, "Successfully calculated ranks" );
68} catch {
69 ok( 0, "Collation now has a cycle" );
70}
71
359944f7 72# Now attempt merge of an identical reading
73try {
74 $c1->merge_readings( '9,3', '11,5' );
75 ok( 1, "Successfully merged reading 'pontifex'" );
76} catch ( Text::Tradition::Error $e ) {
77 ok( 0, "Merge of mergeable readings failed: $e->message" );
78
79}
80
6d381462 81# Test 2: try to equate nodes that are prevented with a real intermediate
82# equivalence
6d381462 83my $t2 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/legendfrag.xml' );
6d381462 84my $c2 = $t2->collation;
85$c2->add_relationship( '9,2', '9,3', { 'type' => 'lexical' } );
86my $trel2 = $c2->get_relationship( '9,2', '9,3' );
87is( ref( $trel2 ), 'Text::Tradition::Collation::Relationship',
88 "Created blocking relationship" );
89is( $trel2->type, 'lexical', "Blocking relationship is not a collation" );
90# This time the link ought to fail
91try {
92 $c2->add_relationship( '8,6', '10,3', { 'type' => 'orthographic' } );
cc31ebaa 93 ok( 0, "Added cross-equivalent bad relationship" );
6d381462 94} catch {
cc31ebaa 95 ok( 1, "Existing equivalence blocked crossing relationship" );
6d381462 96}
97
98try {
99 $c2->calculate_ranks();
100 ok( 1, "Successfully calculated ranks" );
101} catch {
102 ok( 0, "Collation now has a cycle" );
103}
cc31ebaa 104
105# Test 3: make a straightforward pair of transpositions.
106my $t3 = Text::Tradition->new( 'input' => 'Self', 'file' => 't/data/lf2.xml' );
107# Test 1: try to equate nodes that are prevented with an intermediate collation
108my $c3 = $t3->collation;
109try {
110 $c3->add_relationship( '36,4', '38,3', { 'type' => 'transposition' } );
111 ok( 1, "Added straightforward transposition" );
112} catch {
113 ok( 0, "Failed to add normal transposition" );
114}
115try {
116 $c3->add_relationship( '36,3', '38,2', { 'type' => 'transposition' } );
117 ok( 1, "Added straightforward transposition complement" );
118} catch {
119 ok( 0, "Failed to add normal transposition complement" );
120}
121
122# Test 4: try to make a transposition that could be a parallel.
123try {
124 $c3->add_relationship( '28,2', '29,2', { 'type' => 'transposition' } );
125 ok( 0, "Added bad colocated transposition" );
126} catch {
127 ok( 1, "Prevented bad colocated transposition" );
128}
129
130# Test 5: make the parallel, and then make the transposition again.
131try {
132 $c3->add_relationship( '28,3', '29,3', { 'type' => 'orthographic' } );
133 ok( 1, "Equated identical readings for transposition" );
134} catch {
135 ok( 0, "Failed to equate identical readings" );
136}
137try {
138 $c3->add_relationship( '28,2', '29,2', { 'type' => 'transposition' } );
139 ok( 1, "Added straightforward transposition complement" );
140} catch {
141 ok( 0, "Failed to add normal transposition complement" );
142}
6d381462 143}
144
145
146
b0b4421a 147
1481;