use Graph::Easy;
use IPC::Run qw( run binary );
use Text::Tradition::Collation::Reading;
+use Text::Tradition::Collation::Path;
use Moose;
has 'graph' => (
);
has 'collapsed' => (
- is => 'rw',
- isa => 'Bool',
- );
+ is => 'rw',
+ isa => 'Bool',
+ );
+
+has 'linear' => (
+ is => 'rw',
+ isa => 'Bool',
+ default => 1,
+ );
# The collation can be created two ways:
sub BUILD {
my( $self, $args ) = @_;
$self->graph->use_class('node', 'Text::Tradition::Collation::Reading');
+ $self->graph->use_class('edge', 'Text::Tradition::Collation::Path');
# Pass through any graph-specific options.
my $shape = exists( $args->{'shape'} ) ? $args->{'shape'} : 'ellipse';
$self->$orig( @_ );
};
+# Wrapper around paths
+around paths => sub {
+ my $orig = shift;
+ my $self = shift;
+
+ my @result = grep { $_->class eq 'path' } $self->$orig( @_ );
+ return @result;
+};
+
# Wrapper around merge_nodes
sub merge_readings {
return $self->graph->merge_nodes( @_ );
}
+# Extra graph-alike utility
+sub has_path {
+ my( $self, $source, $target, $label ) = @_;
+ my @paths = $source->edges_to( $target );
+ my @relevant = grep { $_->label eq $label } @paths;
+ return scalar @paths;
+}
+
+## Dealing with relationships between readings. This is a different
+## sort of graph edge.
+
+sub add_relationship {
+ my( $self, $type, $source, $target, $global ) = @_;
+ my $rel = Text::Tradition::Collation::Relationship->new(
+ 'sort' => $type,
+ 'global' => $global,
+ 'orig_relation' => [ $source, $target ],
+ );
+ print STDERR sprintf( "Setting relationship %s between readings %s (%s)"
+ . " and %s (%s)\n", $type,
+ $source->label, $source->name,
+ $target->label, $target->name );
+ $self->graph->add_edge( $source, $target, $rel );
+ if( $global ) {
+ # Look for all readings with the source label, and if there are
+ # colocated readings with the target label, join them too.
+ foreach my $r ( $self->readings() ) {
+ next unless $r->label eq $source->label;
+ my @colocated = grep { $_->label eq $target->label }
+ $self->same_position_as( $r );
+ if( @colocated ) {
+ warn "Multiple readings with same label at same position!"
+ if @colocated > 1;
+ my $dup_rel = Text::Tradition::Collation::Relationship->new(
+ 'sort' => $type,
+ 'global' => $global,
+ 'orig_relation' => [ $source, $target ],
+ );
+ $self->graph->add_edge( $r, $colocated[0], $dup_rel );
+ }
+ }
+ }
+}
+
=head2 Output method(s)
=over
my( $self, $recalc ) = @_;
return $self->svg if $self->has_svg;
- $self->collapse_graph_edges();
+ $self->collapse_graph_paths();
$self->_save_graphviz( $self->graph->as_graphviz() )
unless( $self->has_graphviz && !$recalc );
my $in = $self->graphviz;
run( \@cmd, \$in, ">", binary(), \$svg );
$self->{'svg'} = $svg;
- $self->expand_graph_edges();
+ $self->expand_graph_paths();
return $svg;
}
return $graphml;
}
-sub collapse_graph_edges {
+sub collapse_graph_paths {
my $self = shift;
- # Our collation graph has an edge per witness. This is great for
+ # Our collation graph has an path per witness. This is great for
# calculation purposes, but terrible for display. Thus we want to
- # display only one edge between any two nodes.
+ # display only one path between any two nodes.
return if $self->collapsed;
- print STDERR "Collapsing path edges in graph...\n";
+ print STDERR "Collapsing witness paths in graph...\n";
# Don't list out every witness if we have more than half to list.
my $majority = int( scalar( @{$self->tradition->witnesses} ) / 2 ) + 1;
foreach my $node( $self->readings ) {
my $newlabels = {};
# We will visit each node, so we only look ahead.
- foreach my $edge ( $node->outgoing() ) {
- add_hash_entry( $newlabels, $edge->to->name, $edge->name );
- $self->del_path( $edge );
+ foreach my $path ( $node->outgoing() ) {
+ add_hash_entry( $newlabels, $path->to->name, $path->name );
+ $self->del_path( $path );
}
foreach my $newdest ( keys %$newlabels ) {
$label = join( ', ', 'majority', @aclabels );
}
- my $newedge =
+ my $newpath =
$self->add_path( $node, $self->reading( $newdest ), $label );
if( @compressed_wits ) {
- ## TODO fix this hack too.
- $newedge->set_attribute( 'class',
- join( '|', @compressed_wits ) );
+ $newpath->hidden_witnesses( \@compressed_wits );
}
}
}
$self->collapsed( 1 );
}
-sub expand_graph_edges {
+sub expand_graph_paths {
my $self = shift;
- # Our collation graph has only one edge between any two nodes.
+ # Our collation graph has only one path between any two nodes.
# This is great for display, but not so great for analysis.
- # Expand this so that each witness has its own edge between any
+ # Expand this so that each witness has its own path between any
# two reading nodes.
return unless $self->collapsed;
- print STDERR "Expanding path edges in graph...\n";
-
- foreach my $edge( $self->paths ) {
- my $from = $edge->from;
- my $to = $edge->to;
- my @wits = split( /, /, $edge->label );
- if( grep { $_ eq 'majority' } @wits ) {
- push( @wits, split( /\|/, $edge->get_attribute( 'class' ) ) );
+ print STDERR "Expanding witness paths in graph...\n";
+ $DB::single = 1;
+ foreach my $path( $self->paths ) {
+ my $from = $path->from;
+ my $to = $path->to;
+ my @wits = split( /, /, $path->label );
+ if( $path->has_hidden_witnesses ) {
+ push( @wits, @{$path->hidden_witnesses} );
}
- $self->del_path( $edge );
+ $self->del_path( $path );
foreach ( @wits ) {
$self->add_path( $from, $to, $_ );
}
my @common_readings;
foreach my $wit ( @{$self->tradition->witnesses} ) {
+ print STDERR "Making path for " . $wit->sigil . "\n";
$self->make_witness_path( $wit );
@common_readings = _find_common( \@common_readings, $wit->path );
-
- # If we have pre-corrected readings, we need to add paths
- # for those as well.
- if( $wit->has_ante_corr ) {
- my @path = @{$wit->path};
- foreach my $ac ( @{$wit->ante_corr} ) {
- # my( $offset, $length, $items ) = @$ac;
- # Figure out where the path needs to start and
- # end its divergence.
- my $start = $ac->[0] - 1;
- my $end = $ac->[0] + $ac->[1];
- my @chain;
- push( @chain, $path[$start] );
- push( @chain, @{$ac->[2]} );
- push( @chain, $path[$end] );
- $self->make_path_uncorrection( $wit->sigil, @chain );
- }
- @common_readings = _find_common( \@common_readings,
- $wit->uncorrected_path );
- }
+ @common_readings = _find_common( \@common_readings, $wit->uncorrected_path );
}
return @common_readings;
}
sub make_witness_path {
my( $self, $wit ) = @_;
my @chain = @{$wit->path};
- $self->connect_readings_for_witness( $wit->sigil, @chain );
-}
-
-sub make_path_uncorrection {
- my( $self, $sig, @chain ) = @_;
- $sig .= ' (a.c.)';
- $self->connect_readings_for_witness( $sig, @chain );
-}
-
-sub connect_readings_for_witness {
- my( $self, $sig, @chain ) = @_;
+ my $sig = $wit->sigil;
foreach my $idx ( 0 .. $#chain-1 ) {
$self->add_path( $chain[$idx], $chain[$idx+1], $sig );
}
+ @chain = @{$wit->uncorrected_path};
+ foreach my $idx( 0 .. $#chain-1 ) {
+ my $source = $chain[$idx];
+ my $target = $chain[$idx+1];
+ $self->add_path( $source, $target, "$sig (a.c.)" )
+ unless $self->has_path( $source, $target, $sig );
+ }
}
-
sub common_readings {
my $self = shift;
my @common = grep { $_->is_common } $self->readings();
foreach my $di ( 1..scalar(@{$wit_matrix->[$li-1]}) ) {
my $reading = $wit_matrix->[$li-1]->[$di-1];
my $position = "$li,$di";
- $DB::single = 1 unless ref( $reading ) eq 'Text::Tradition::Collation::Reading';
# If we have seen this node before, we need to compare
# its position with what went before.