Merge branch 'master' of github.com:tla/stemmatology
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / RelationshipStore.pm
index b5e2e53..334b5fe 100644 (file)
@@ -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