From: Tara L Andrews Date: Fri, 30 Sep 2011 00:28:02 +0000 (+0200) Subject: Parse a known stemma hypothesis from a dotfile X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e05997e2120f6f7c2bf4e3375cf1b6fec9d116e4;p=scpubgit%2Fstemmatology.git Parse a known stemma hypothesis from a dotfile --- diff --git a/lib/Text/Tradition/Stemma.pm b/lib/Text/Tradition/Stemma.pm index 5928016..df8cf4a 100644 --- a/lib/Text/Tradition/Stemma.pm +++ b/lib/Text/Tradition/Stemma.pm @@ -5,6 +5,8 @@ use File::Temp; use IPC::Run qw/ run /; use Moose; use Text::Tradition::Collation::Position; +use Graph; +use Graph::Reader::Dot; has collation => ( is => 'ro', @@ -18,7 +20,50 @@ has character_matrix => ( writer => '_save_character_matrix', predicate => 'has_character_matrix', ); - + +has graph => ( + is => 'rw', + isa => 'Graph', + predicate => 'has_graph', + ); + +has apsp => ( + is => 'rw', + isa => 'Graph', + ); + +sub BUILD { + my( $self, $args ) = @_; + # If we have been handed a dotfile, initialize it into a graph. + if( exists $args->{'dot'} ) { + my $reader = Graph::Reader::Dot->new(); + my $graph = $reader->read_graph( $args->{'dot'} ); + $graph + ? $self->graph( $graph ) + : warn "Failed to parse dot file " . $args->{'dot'}; + } + + # If we have a graph, calculate all the shortest paths between nodes, + # disregarding direction. + if( $self->has_graph ) { + my $undirected; + if( $self->graph->is_directed ) { + # Make an undirected version. + $undirected = Graph->new( 'undirected' => 1 ); + foreach my $v ( $self->graph->vertices ) { + $undirected->add_vertex( $v ); + } + foreach my $e ( $self->graph->edges ) { + $undirected->add_edge( @$e ); + } + } else { + $undirected = $self->graph; + } + $self->apsp( $undirected->APSP_Floyd_Warshall() ); + } +} + + sub make_character_matrix { my $self = shift; unless( $self->collation->linear ) {