Commit | Line | Data |
951ddfe8 |
1 | package Text::Tradition::HasStemma; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use Moose::Role; |
a47b32d9 |
6 | use Date::Parse; |
951ddfe8 |
7 | use Text::Tradition::Stemma; |
98f22390 |
8 | use Text::Tradition::StemmaUtil qw/ parse_newick /; |
951ddfe8 |
9 | |
10 | =head1 NAME |
11 | |
a445ce40 |
12 | Text::Tradition::HasStemma - add-on to associate stemma hypotheses to |
13 | Text::Tradition objects |
951ddfe8 |
14 | |
15 | =head1 DESCRIPTION |
16 | |
a445ce40 |
17 | It is often the case that, for a given text tradition, the order of copying |
18 | of the witnesses can or should be reconstructed (or at least the attempt |
19 | should be made.) This class is a role that can be applied to |
20 | Text::Tradition objects to record stemma hypotheses. See the documentation |
21 | for L<Text::Tradition::Stemma> for more information. |
951ddfe8 |
22 | |
23 | =head1 METHODS |
24 | |
25 | =head2 stemmata |
26 | |
27 | Return a list of all stemmata associated with the tradition. |
28 | |
29 | =head2 stemma_count |
30 | |
31 | Return the number of stemma hypotheses defined for this tradition. |
32 | |
33 | =head2 stemma( $idx ) |
34 | |
35 | Return the L<Text::Tradition::Stemma> object identified by the given index. |
36 | |
37 | =head2 clear_stemmata |
38 | |
39 | Delete all stemma hypotheses associated with this tradition. |
40 | |
98f22390 |
41 | =head2 has_stemweb_jobid |
42 | |
43 | Returns true if there is currently a Stemweb job ID, indicating that a |
44 | stemma tree calculation from the Stemweb service is in process. |
45 | |
46 | =head2 stemweb_jobid |
47 | |
48 | Return the currently-running job ID (if any) for calculation of Stemweb |
49 | trees. |
50 | |
51 | =head2 set_stemweb_jobid( $jobid ) |
52 | |
53 | Record a job ID for a Stemweb calculation. |
54 | |
951ddfe8 |
55 | =cut |
56 | |
57 | has 'stemmata' => ( |
58 | traits => ['Array'], |
59 | isa => 'ArrayRef[Text::Tradition::Stemma]', |
60 | handles => { |
61 | stemmata => 'elements', |
62 | _add_stemma => 'push', |
63 | stemma => 'get', |
64 | stemma_count => 'count', |
65 | clear_stemmata => 'clear', |
66 | }, |
67 | default => sub { [] }, |
68 | ); |
69 | |
98f22390 |
70 | has 'stemweb_jobid' => ( |
71 | is => 'ro', |
72 | isa => 'Str', |
73 | writer => 'set_stemweb_jobid', |
74 | predicate => 'has_stemweb_jobid', |
75 | clearer => '_clear_stemweb_jobid', |
76 | ); |
77 | |
78 | before 'set_stemweb_jobid' => sub { |
79 | my( $self ) = shift; |
80 | if( $self->has_stemweb_jobid ) { |
81 | $self->throw( "Tradition already has a Stemweb jobid: " |
82 | . $self->stemweb_jobid ); |
83 | } |
84 | }; |
951ddfe8 |
85 | |
86 | =head2 add_stemma( $dotfile ) |
87 | |
88 | Initializes a Text::Tradition::Stemma object from the given dotfile, |
89 | and associates it with the tradition. |
90 | |
91 | =begin testing |
92 | |
93 | use Text::Tradition; |
94 | |
95 | my $t = Text::Tradition->new( |
96 | 'name' => 'simple test', |
97 | 'input' => 'Tabular', |
98 | 'file' => 't/data/simple.txt', |
99 | ); |
951ddfe8 |
100 | is( $t->stemma_count, 0, "No stemmas added yet" ); |
101 | my $s; |
102 | ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" ); |
103 | is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" ); |
104 | is( $t->stemma_count, 1, "Tradition claims to have a stemma" ); |
105 | is( $t->stemma(0), $s, "Tradition hands back the right stemma" ); |
106 | |
107 | =end testing |
108 | |
109 | =cut |
110 | |
111 | sub add_stemma { |
112 | my $self = shift; |
113 | my %opts = @_; |
114 | my $stemma_fh; |
115 | if( $opts{'dotfile'} ) { |
116 | open $stemma_fh, '<', $opts{'dotfile'} |
117 | or warn "Could not open file " . $opts{'dotfile'}; |
118 | } elsif( $opts{'dot'} ) { |
119 | my $str = $opts{'dot'}; |
120 | open $stemma_fh, '<', \$str; |
121 | } |
122 | # Assume utf-8 |
123 | binmode $stemma_fh, ':utf8'; |
124 | my $stemma = Text::Tradition::Stemma->new( |
125 | 'dot' => $stemma_fh ); |
126 | $self->_add_stemma( $stemma ) if $stemma; |
127 | return $stemma; |
128 | } |
129 | |
98f22390 |
130 | =head2 record_stemweb_result( $format, $data ) |
131 | |
132 | Records the result returned by a Stemweb calculation, and clears any |
738620c7 |
133 | existing job ID. Returns any new stemmata that were created. |
98f22390 |
134 | |
a47b32d9 |
135 | =begin testing |
136 | |
137 | use Text::Tradition; |
138 | use JSON qw/ from_json /; |
139 | |
140 | my $t = Text::Tradition->new( |
141 | 'name' => 'Stemweb test', |
142 | 'input' => 'Self', |
143 | 'file' => 't/data/besoin.xml', |
144 | 'stemweb_jobid' => '4', |
145 | ); |
146 | |
147 | is( $t->stemma_count, 0, "No stemmas added yet" ); |
148 | |
149 | 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"}' ); |
738620c7 |
150 | my $newst = $t->record_stemweb_result( $answer ); |
151 | is( scalar @$newst, 1, "New stemma was returned from record_stemweb_result" ); |
152 | is( $newst->[0], $t->stemma(0), "Answer has the right object" ); |
a47b32d9 |
153 | ok( !$t->has_stemweb_jobid, "Job ID was removed from tradition" ); |
154 | is( $t->stemma_count, 1, "Tradition has new stemma" ); |
155 | ok( $t->stemma(0)->is_undirected, "New stemma is undirected as it should be" ); |
156 | is( $t->stemma(0)->identifier, "RHM 1382777054_0", "Stemma has correct identifier" ); |
738620c7 |
157 | is( $t->stemma(0)->from_jobid, 4, "New stemma has correct associated job ID" ); |
a47b32d9 |
158 | |
159 | |
160 | =end testing |
98f22390 |
161 | |
162 | =cut |
163 | |
164 | sub record_stemweb_result { |
a47b32d9 |
165 | my( $self, $answer ) = @_; |
738620c7 |
166 | my $jobid = $self->stemweb_jobid; |
167 | my $stemmata = []; |
a47b32d9 |
168 | if( $answer->{format} eq 'dot' ) { |
169 | $self->add_stemma( dot => $answer->{result} ); |
170 | } elsif( $answer->{format} eq 'newick' ) { |
738620c7 |
171 | $stemmata = parse_newick( $answer->{result} ); |
a47b32d9 |
172 | my $title = sprintf( "%s %d", $answer->{algorithm}, |
173 | str2time( $answer->{start_time} ) ); |
174 | my $i = 0; |
98f22390 |
175 | foreach my $stemma ( @$stemmata ) { |
a47b32d9 |
176 | my $ititle = $title . "_$i"; $i++; |
177 | $stemma->set_identifier( $ititle ); |
738620c7 |
178 | $stemma->_set_from_jobid( $jobid ); |
98f22390 |
179 | $self->_add_stemma( $stemma ); |
180 | } |
98f22390 |
181 | } else { |
a47b32d9 |
182 | $self->throw( "Cannot parse tree results with format " . $answer->{format} ); |
98f22390 |
183 | } |
a47b32d9 |
184 | $self->_clear_stemweb_jobid(); |
738620c7 |
185 | return $stemmata; |
98f22390 |
186 | } |
187 | |
951ddfe8 |
188 | 1; |
189 | |
190 | =head1 LICENSE |
191 | |
192 | This package is free software and is provided "as is" without express |
193 | or implied warranty. You can redistribute it and/or modify it under |
194 | the same terms as Perl itself. |
195 | |
196 | =head1 AUTHOR |
197 | |
198 | Tara L Andrews E<lt>aurum@cpan.orgE<gt> |