working version of rebuild_equivalence
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / RelationshipStore.pm
index c7728cd..8517ac1 100644 (file)
@@ -97,6 +97,7 @@ has 'equivalence_graph' => (
        is => 'ro',
        isa => 'Graph',
        default => sub { Graph->new() },
+       writer => '_reset_equivalence',
        );
        
 has '_node_equivalences' => (
@@ -106,6 +107,7 @@ has '_node_equivalences' => (
                equivalence => 'get',
                set_equivalence => 'set',
                remove_equivalence => 'delete',
+               _clear_equivalence => 'clear',
        },
        );
 
@@ -116,6 +118,7 @@ has '_equivalence_readings' => (
                eqreadings => 'get',
                set_eqreadings => 'set',
                remove_eqreadings => 'delete',
+               _clear_eqreadings => 'clear',
        },
        );
        
@@ -781,8 +784,6 @@ sub add_equivalence_edge {
        my( $self, $source, $target ) = @_;
        my $seq = $self->equivalence( $source );
        my $teq = $self->equivalence( $target );
-       print STDERR "Adding equivalence edge $seq -> $teq for $source -> $target\n"
-               if grep { $_ eq '451,2' } @_;
        $self->equivalence_graph->add_edge( $seq, $teq );
 }
 
@@ -1002,6 +1003,44 @@ sub _find_equiv_without {
        return keys %found;
 }
 
+=head2 rebuild_equivalence
+
+(Re)build the equivalence graph from scratch. Dumps the graph, makes a new one,
+adds all readings and edges, then makes an equivalence for all relationships.
+
+=cut
+
+sub rebuild_equivalence {
+       my $self = shift;
+       my $newgraph = Graph->new();
+       # Set this as the new equivalence graph
+       $self->_reset_equivalence( $newgraph );
+       # Clear out the data hashes
+       $self->_clear_equivalence;
+       $self->_clear_eqreadings;
+       
+       # Add the readings
+       foreach my $r ( $self->collation->readings ) {
+               my $rid = $r->id;
+               $newgraph->add_vertex( $rid );
+               $self->set_equivalence( $rid, $rid );
+               $self->set_eqreadings( $rid, [ $rid ] );
+       }
+
+       # Now add the edges
+       foreach my $e ( $self->collation->paths ) {
+               $self->add_equivalence_edge( @$e );
+       }
+
+       # Now equate the colocated readings. This does no testing; 
+       # it assumes that all preexisting relationships are valid.
+       foreach my $rel ( $self->relationships ) {
+               my $relobj = $self->get_relationship( $rel );
+               next unless $relobj && $relobj->colocated;
+               $self->_make_equivalence( @$rel );
+       }
+}
+
 ### Output logic
 
 sub _as_graphml {