use strict;
use warnings;
use Moose::Role;
+use Date::Parse;
use Text::Tradition::Stemma;
=head1 NAME
Delete all stemma hypotheses associated with this tradition.
+=head2 has_stemweb_jobid
+
+Returns true if there is currently a Stemweb job ID, indicating that a
+stemma tree calculation from the Stemweb service is in process.
+
+=head2 stemweb_jobid
+
+Return the currently-running job ID (if any) for calculation of Stemweb
+trees.
+
+=head2 set_stemweb_jobid( $jobid )
+
+Record a job ID for a Stemweb calculation.
+
=cut
has 'stemmata' => (
default => sub { [] },
);
+has 'stemweb_jobid' => (
+ is => 'ro',
+ isa => 'Str',
+ writer => 'set_stemweb_jobid',
+ predicate => 'has_stemweb_jobid',
+ clearer => '_clear_stemweb_jobid',
+ );
+
+before 'set_stemweb_jobid' => sub {
+ my( $self ) = shift;
+ if( $self->has_stemweb_jobid ) {
+ $self->throw( "Tradition already has a Stemweb jobid: "
+ . $self->stemweb_jobid );
+ }
+};
-=head2 add_stemma( $dotfile )
+=head2 add_stemma( dotfile => $dotfile )
+=head2 add_stemma( dot => $dotstring )
+=head2 add_stemma( $stemma_obj )
Initializes a Text::Tradition::Stemma object from the given dotfile,
and associates it with the tradition.
sub add_stemma {
my $self = shift;
- my %opts = @_;
- my $stemma_fh;
- if( $opts{'dotfile'} ) {
- open $stemma_fh, '<', $opts{'dotfile'}
- or warn "Could not open file " . $opts{'dotfile'};
- } elsif( $opts{'dot'} ) {
- my $str = $opts{'dot'};
- open $stemma_fh, '<', \$str;
+ my $stemma;
+ if( ref( @_ ) eq 'Text::Tradition::Stemma' ) {
+ $stemma = shift;
+ } else {
+ $stemma = Text::Tradition::Stemma->new( @_ );
}
- # Assume utf-8
- binmode $stemma_fh, ':utf8';
- my $stemma = Text::Tradition::Stemma->new(
- 'dot' => $stemma_fh );
$self->_add_stemma( $stemma ) if $stemma;
return $stemma;
}
+=head2 record_stemweb_result( $format, $data )
+
+Records the result returned by a Stemweb calculation, and clears any
+existing job ID. Returns any new stemmata that were created.
+
+=begin testing
+
+use Text::Tradition;
+use JSON qw/ from_json /;
+
+my $t = Text::Tradition->new(
+ 'name' => 'Stemweb test',
+ 'input' => 'Self',
+ 'file' => 't/data/besoin.xml',
+ 'stemweb_jobid' => '4',
+ );
+
+is( $t->stemma_count, 0, "No stemmas added yet" );
+
+my $answer = from_json( '{"status": 0, "job_id": "4", "algorithm": "RHM", "format": "newick", "start_time": "2013-10-26 10:44:14.050263", "result": "((((((((((((F,U),V),S),T1),T2),A),J),B),L),D),M),C);\n", "end_time": "2013-10-26 10:45:55.398944"}' );
+my $newst = $t->record_stemweb_result( $answer );
+is( scalar @$newst, 1, "New stemma was returned from record_stemweb_result" );
+is( $newst->[0], $t->stemma(0), "Answer has the right object" );
+ok( !$t->has_stemweb_jobid, "Job ID was removed from tradition" );
+is( $t->stemma_count, 1, "Tradition has new stemma" );
+ok( $t->stemma(0)->is_undirected, "New stemma is undirected as it should be" );
+is( $t->stemma(0)->identifier, "RHM 1382784254_0", "Stemma has correct identifier" );
+is( $t->stemma(0)->from_jobid, 4, "New stemma has correct associated job ID" );
+
+
+=end testing
+
+=cut
+
+sub record_stemweb_result {
+ my( $self, $answer ) = @_;
+ my $jobid = $self->stemweb_jobid;
+ my $stemmata = [];
+ if( $answer->{format} eq 'dot' ) {
+ $self->add_stemma( dot => $answer->{result} );
+ } elsif( $answer->{format} eq 'newick' ) {
+ $stemmata = Text::Tradition::Stemma->new_from_newick( $answer->{result} );
+ my $title = sprintf( "%s %d", $answer->{algorithm},
+ str2time( $answer->{start_time}, 'UTC' ) );
+ my $i = 0;
+ foreach my $stemma ( @$stemmata ) {
+ my $ititle = $title . "_$i"; $i++;
+ $stemma->set_identifier( $ititle );
+ $stemma->_set_from_jobid( $jobid );
+ $self->_add_stemma( $stemma );
+ }
+ } else {
+ $self->throw( "Cannot parse tree results with format " . $answer->{format} );
+ }
+ $self->_clear_stemweb_jobid();
+ return $stemmata;
+}
+
1;
=head1 LICENSE