1 package Text::Tradition::HasStemma;
7 use Text::Tradition::Stemma;
11 Text::Tradition::HasStemma - add-on to associate stemma hypotheses to
12 Text::Tradition objects
16 It is often the case that, for a given text tradition, the order of copying
17 of the witnesses can or should be reconstructed (or at least the attempt
18 should be made.) This class is a role that can be applied to
19 Text::Tradition objects to record stemma hypotheses. See the documentation
20 for L<Text::Tradition::Stemma> for more information.
26 Return a list of all stemmata associated with the tradition.
30 Return the number of stemma hypotheses defined for this tradition.
34 Return the L<Text::Tradition::Stemma> object identified by the given index.
38 Delete all stemma hypotheses associated with this tradition.
40 =head2 has_stemweb_jobid
42 Returns true if there is currently a Stemweb job ID, indicating that a
43 stemma tree calculation from the Stemweb service is in process.
47 Return the currently-running job ID (if any) for calculation of Stemweb
50 =head2 set_stemweb_jobid( $jobid )
52 Record a job ID for a Stemweb calculation.
58 isa => 'ArrayRef[Text::Tradition::Stemma]',
60 stemmata => 'elements',
61 _add_stemma => 'push',
63 stemma_count => 'count',
64 clear_stemmata => 'clear',
66 default => sub { [] },
69 has 'stemweb_jobid' => (
72 writer => 'set_stemweb_jobid',
73 predicate => 'has_stemweb_jobid',
74 clearer => '_clear_stemweb_jobid',
77 before 'set_stemweb_jobid' => sub {
79 if( $self->has_stemweb_jobid ) {
80 $self->throw( "Tradition already has a Stemweb jobid: "
81 . $self->stemweb_jobid );
85 =head2 add_stemma( dotfile => $dotfile )
86 =head2 add_stemma( dot => $dotstring )
87 =head2 add_stemma( $stemma_obj )
89 Initializes a Text::Tradition::Stemma object from the given dotfile,
90 and associates it with the tradition.
96 my $t = Text::Tradition->new(
97 'name' => 'simple test',
99 'file' => 't/data/simple.txt',
101 is( $t->stemma_count, 0, "No stemmas added yet" );
103 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
104 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
105 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
106 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
115 if( ref( @_ ) eq 'Text::Tradition::Stemma' ) {
118 $stemma = Text::Tradition::Stemma->new( @_ );
120 $self->_add_stemma( $stemma ) if $stemma;
124 =head2 record_stemweb_result( $format, $data )
126 Records the result returned by a Stemweb calculation, and clears any
127 existing job ID. Returns any new stemmata that were created.
132 use JSON qw/ from_json /;
134 my $t = Text::Tradition->new(
135 'name' => 'Stemweb test',
137 'file' => 't/data/besoin.xml',
138 'stemweb_jobid' => '4',
141 is( $t->stemma_count, 0, "No stemmas added yet" );
143 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"}' );
144 my $newst = $t->record_stemweb_result( $answer );
145 is( scalar @$newst, 1, "New stemma was returned from record_stemweb_result" );
146 is( $newst->[0], $t->stemma(0), "Answer has the right object" );
147 ok( !$t->has_stemweb_jobid, "Job ID was removed from tradition" );
148 is( $t->stemma_count, 1, "Tradition has new stemma" );
149 ok( $t->stemma(0)->is_undirected, "New stemma is undirected as it should be" );
150 is( $t->stemma(0)->identifier, "RHM 1382784254_0", "Stemma has correct identifier" );
151 is( $t->stemma(0)->from_jobid, 4, "New stemma has correct associated job ID" );
158 sub record_stemweb_result {
159 my( $self, $answer ) = @_;
160 my $jobid = $self->stemweb_jobid;
162 if( $answer->{format} eq 'dot' ) {
163 $self->add_stemma( dot => $answer->{result} );
164 } elsif( $answer->{format} eq 'newick' ) {
165 $stemmata = Text::Tradition::Stemma->new_from_newick( $answer->{result} );
166 my $title = sprintf( "%s %d", $answer->{algorithm},
167 str2time( $answer->{start_time}, 'UTC' ) );
169 foreach my $stemma ( @$stemmata ) {
170 my $ititle = $title . "_$i"; $i++;
171 $stemma->set_identifier( $ititle );
172 $stemma->_set_from_jobid( $jobid );
173 $self->_add_stemma( $stemma );
176 $self->throw( "Cannot parse tree results with format " . $answer->{format} );
178 $self->_clear_stemweb_jobid();
186 This package is free software and is provided "as is" without express
187 or implied warranty. You can redistribute it and/or modify it under
188 the same terms as Perl itself.
192 Tara L Andrews E<lt>aurum@cpan.orgE<gt>