X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition%2FCollation%2FRelationshipStore.pm;h=334b5fe5ee5dd8f300d21446fd6acaff9662ff0f;hb=9317e522bb0299ab6bdcfe2d999750719e893407;hp=b5e2e535d610cbeebe10b10d86afa7b5956a4500;hpb=4043968234b0a53a390f184413be94949c7b867d;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition/Collation/RelationshipStore.pm b/lib/Text/Tradition/Collation/RelationshipStore.pm index b5e2e53..334b5fe 100644 --- a/lib/Text/Tradition/Collation/RelationshipStore.pm +++ b/lib/Text/Tradition/Collation/RelationshipStore.pm @@ -23,9 +23,38 @@ general) between readings. =begin testing use Text::Tradition; +use TryCatch; use_ok( 'Text::Tradition::Collation::RelationshipStore' ); +# Add some relationships, and delete them + +my $cxfile = 't/data/Collatex-16.xml'; +my $t = Text::Tradition->new( + 'name' => 'inline', + 'input' => 'CollateX', + 'file' => $cxfile, + ); +my $c = $t->collation; + +my @v1 = $c->add_relationship( 'n21', 'n22', { 'type' => 'meaning' } ); +is( scalar @v1, 1, "Added a single relationship" ); +is( $v1[0]->[0], 'n21', "Got correct node 1" ); +is( $v1[0]->[1], 'n22', "Got correct node 2" ); +my @v2 = $c->add_relationship( 'n9', 'n23', + { 'type' => 'spelling', 'scope' => 'global' } ); +is( scalar @v2, 2, "Added a global relationship with two instances" ); +@v1 = $c->del_relationship( 'n22', 'n21' ); +is( scalar @v1, 1, "Deleted first relationship" ); +@v2 = $c->del_relationship( 'n8', 'n13' ); +is( scalar @v2, 2, "Deleted second global relationship" ); +try { + my @v3 = $c->del_relationship( 'n1', 'n2' ); + ok( 0, "Should have errored on non-existent relationship" ); +} catch( Text::Tradition::Error $e ) { + like( $e->message, qr/No relationship defined/, "Attempt to delete non-existent relationship errored" ); +} + =end testing =head1 METHODS @@ -253,6 +282,31 @@ sub add_relationship { return @pairs_set; } +=head2 del_relationship( $source, $target ) + +Removes the relationship between the given readings. If the relationship is +non-local, removes the relationship everywhere in the graph. + +=cut + +sub del_relationship { + my( $self, $source, $target ) = @_; + my $rel = $self->get_relationship( $source, $target ); + throw( "No relationship defined between $source and $target" ) unless $rel; + my @vectors = ( [ $source, $target ] ); + $self->_remove_relationship( $source, $target ); + if( $rel->nonlocal ) { + # Remove the relationship wherever it occurs. + my @rel_edges = grep { $self->get_relationship( @$_ ) == $rel } + $self->relationships; + foreach my $re ( @rel_edges ) { + $self->_remove_relationship( @$re ); + push( @vectors, $re ); + } + } + return @vectors; +} + =head2 relationship_valid( $source, $target, $type ) Checks whether a relationship of type $type may exist between the readings given