load extensions statically to avoid bad object wrapping interactions
[scpubgit/stemmatology.git] / analysis / lib / Text / Tradition / HasStemma.pm
CommitLineData
951ddfe8 1package Text::Tradition::HasStemma;
2
3use strict;
4use warnings;
5use Moose::Role;
6use Text::Tradition::Stemma;
7
8=head1 NAME
9
a445ce40 10Text::Tradition::HasStemma - add-on to associate stemma hypotheses to
11Text::Tradition objects
951ddfe8 12
13=head1 DESCRIPTION
14
a445ce40 15It is often the case that, for a given text tradition, the order of copying
16of the witnesses can or should be reconstructed (or at least the attempt
17should be made.) This class is a role that can be applied to
18Text::Tradition objects to record stemma hypotheses. See the documentation
19for L<Text::Tradition::Stemma> for more information.
951ddfe8 20
21=head1 METHODS
22
23=head2 stemmata
24
25Return a list of all stemmata associated with the tradition.
26
27=head2 stemma_count
28
29Return the number of stemma hypotheses defined for this tradition.
30
31=head2 stemma( $idx )
32
33Return the L<Text::Tradition::Stemma> object identified by the given index.
34
35=head2 clear_stemmata
36
37Delete all stemma hypotheses associated with this tradition.
38
39=cut
40
41has 'stemmata' => (
42 traits => ['Array'],
43 isa => 'ArrayRef[Text::Tradition::Stemma]',
44 handles => {
45 stemmata => 'elements',
46 _add_stemma => 'push',
47 stemma => 'get',
48 stemma_count => 'count',
49 clear_stemmata => 'clear',
50 },
51 default => sub { [] },
52 );
53
54
55=head2 add_stemma( $dotfile )
56
57Initializes a Text::Tradition::Stemma object from the given dotfile,
58and associates it with the tradition.
59
60=begin testing
61
62use Text::Tradition;
63
64my $t = Text::Tradition->new(
65 'name' => 'simple test',
66 'input' => 'Tabular',
67 'file' => 't/data/simple.txt',
68 );
951ddfe8 69is( $t->stemma_count, 0, "No stemmas added yet" );
70my $s;
71ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
72is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
73is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
74is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
75
76=end testing
77
78=cut
79
80sub add_stemma {
81 my $self = shift;
82 my %opts = @_;
83 my $stemma_fh;
84 if( $opts{'dotfile'} ) {
85 open $stemma_fh, '<', $opts{'dotfile'}
86 or warn "Could not open file " . $opts{'dotfile'};
87 } elsif( $opts{'dot'} ) {
88 my $str = $opts{'dot'};
89 open $stemma_fh, '<', \$str;
90 }
91 # Assume utf-8
92 binmode $stemma_fh, ':utf8';
93 my $stemma = Text::Tradition::Stemma->new(
94 'dot' => $stemma_fh );
95 $self->_add_stemma( $stemma ) if $stemma;
96 return $stemma;
97}
98
991;
100
101=head1 LICENSE
102
103This package is free software and is provided "as is" without express
104or implied warranty. You can redistribute it and/or modify it under
105the same terms as Perl itself.
106
107=head1 AUTHOR
108
109Tara L Andrews E<lt>aurum@cpan.orgE<gt>