lib/AI/MicroStructure/MultiList.pm

filter: Statementstmt 88.9% 113 / 127 Branchbran 92.8% 26 / 28 Conditioncond 61.9% 13 / 21 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 86.9% 20 / 23 Podpod 100% 7 / 7 totaltotal 84.0% 184 / 219 CC 23 SCAR 15.1
CC
23
Cov
86%
init
9.6
name
8.3
new
4.0
CRAP 4.5
« lib/AI/MicroStructure/Hypothesis.pm ↑ Summary lib/AI/MicroStructure/ObjectParser.pm »
1 package AI::MicroStructure::MultiList;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use AI::MicroStructure (); # do not export metaname and friends
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use AI::MicroStructure::RemoteList;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use List::Util qw( shuffle );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our @ISA = qw( AI::MicroStructure::RemoteList );
9 1 our $VERSION = '1.000';
10  
11 sub init {
12 1 ▶ my ($self, $data) = @_;
Subroutine init: called CC=9 SCAR=22.6
Pod documented
13 1 my $class = caller(0);
14  
15 1 ▶ $data ||= AI::MicroStructure->load_data($class);
Condition33%1/3
$data ||= "AI::MicroStructure"->load_data($class)
l!l&&r!l&&!r
010
MC/DC0%0/2
$data ||= "AI::MicroStructure"->load_data($class)
$data "AI::MicroStructure"->load_data($class)
Truth table33%1/3
A ||= B
A= $dataB= "AI::MicroStructure"->load_data($class)
ABresult
1X1
011
000
16 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # note: variables mentioned twice to avoid a warning
19  
20 1 ▶ my $sep = ${"$class\::Separator"} = ${"$class\::Separator"} ||= '/';
Condition50%1/2
${"${class}::Separator";} ||= "/"
l!l
01
MC/DC0%0/1
${"${class}::Separator";} ||= "/"
${"${class}::Separator";}
Truth table50%1/2
A
A= ${"${class}::Separator";}
Aresult
11
00
21 1 my $tail = qr/$sep?[^$sep]*$/;
22  
23 # compute all categories
24 1 my @categories = ( [ $data->{names}, '' ] );
25 1 ▶ while ( my ( $h, $k ) = @{ shift @categories or []} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = @{shift @categories or [];}) 12 1 13
shift @categories or [] 1 12 13
26 12 ▶ if ( ref $h eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h eq 'HASH') { } 5 7 12
27 push @categories,
28 5 ▶ map { [ $h->{$_}, ( $k ? "$k$sep$_" : $_ ) ] } keys %$h;
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 7 4 11
29 }
30 else { # leaf
31 7 my @items = split /\s+/, $h;
32 7 ▶ while ($k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k) 15 7 22
33 15 push @{ ${"$class\::MultiList"}{$k} }, @items;
34 15 $k =~ s!$tail!!;
35 }
36 }
37 }
38  
39 1 ▶ ${"$class\::Default"} = ${"$class\::Default"} = $data->{default} || ':all';
Condition50%1/2
$data->{'default'} || ':all'
l!l
10
MC/DC0%0/1
$data->{'default'} || ':all'
$data->{'default'}
Truth table50%1/2
A
A= $data->{'default'}
Aresult
11
00
40 1 ${"$class\::Theme"} = ${"$class\::Theme"} = ( split /::/, $class )[-1];
41  
42 1 *{"$class\::import"} = sub {
43 0 ▶ my $callpkg = caller(0);
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 my $structure = ${"$class\::Theme"};
45 0 my $meta = $class->new;
46 0 ▶ *{"$callpkg\::meta$structure"} = sub { $meta->name(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 1 };
48  
49 1 ${"$class\::meta"} = ${"$class\::meta"} = $class->new();
50 }
51  
52 sub name {
53 42 ▶ my ( $self, $count ) = @_;
Subroutine name: called CC=8 SCAR=21.1
Pod documented
54 42 my $class = ref $self;
55  
56 42 ▶ if ( !$class ) { # called as a class method!
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 42 42
57 0 $class = $self;
58 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 0 $self = ${"$class\::meta"};
60 }
61  
62 42 ▶ if ( defined $count && $count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $count and $count == 0) 14 28 42
Condition100%3/3
defined $count and $count == 0
!ll&&!rl&&r
141414
MC/DC100%2/2
defined $count and $count == 0
defined $count $count == 0
Truth table100%3/3
A and B
A= defined $countB= $count == 0
ABresult
0X0
100
111
63 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
64 return wantarray
65 14 ? shuffle @{ $self->{base} }
66 14 ▶ : scalar @{ $self->{base} };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 0 14
67 }
68  
69 28 ▶ $count ||= 1;
Condition100%2/2
$count ||= 1
l!l
1414
MC/DC100%1/1
$count ||= 1
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
70 28 my $list = $self->{cache};
71 28 ▶ if ( @{ $self->{base} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"base"};}) 26 2 28
72 26 ▶ push @$list, shuffle @{ $self->{base} } while @$list < $count;
Branch: 1 branch
BranchTrueFalseTotal
@$list < $count and do { push @$list, &shuffle(@{$$self{"base"};}) } 36 26 62
73 }
74 28 splice( @$list, 0, $count );
75 }
76  
77 sub new {
78 17 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
79  
80 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 17 my $self = bless { @_, cache => [] }, $class;
82  
83 # compute some defaults
84 17 ▶ $self->{category} ||= ${"$class\::Default"};
Condition66%2/3
$self->{"category"} ||= ${"${class}::Default";}
l!l&&r!l&&!r
1430
MC/DC0%0/2
$self->{"category"} ||= ${"${class}::Default";}
$self->{"category"} ${"${class}::Default";}
Truth table66%2/3
A ||= B
A= $self->{"category"}B= ${"${class}::Default";}
ABresult
1X1
011
000
85  
86 # fall back to last resort (FIXME should we carp()?)
87 1 $self->{category} = ${"$class\::Default"}
88 if $self->{category} ne ':all'
89 17 ▶ && !exists ${"$class\::MultiList"}{ $self->{category} };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"category"} ne ":all" and not exists ${"${class}::MultiList";}{$self->{"category"}} 1 16 17
Condition100%3/3
$self->{"category"} ne ":all" and not exists ${"${class}::MultiList";}{$self->{"category"}}
!ll&&!rl&&r
1151
MC/DC100%2/2
$self->{"category"} ne ":all" and not exists ${"${class}::MultiList";}{$self->{"category"}}
$self->{"category"} ne ":all" not exists ${"${class}::MultiList";}{$self->{"category"}}
Truth table100%3/3
A and not(B)
A= $self->{"category"} ne ":all"B= not exists ${"${class}::MultiList";}{$self->{"category"}}
ABresult
0X0
100
111
90  
91 17 $self->_compute_base();
92 17 return $self;
93 }
94  
95 sub _compute_base {
96 17 ▶ my ($self) = @_;
Subroutine _compute_base: called CC=2 SCAR=6.9
97 17 my $class = ref $self;
98  
99 # compute the base list for this category
100 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 17 my %seen;
102 $self->{base} = [
103 78 grep { !$seen{$_}++ }
104 27 ▶ map { @{ ${"$class\::MultiList"}{$_} } }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'category'} eq ':all' ? : 1 16 17
105 $self->{category} eq ':all'
106 1 ? ( keys %{"$class\::MultiList"} )
107 : ( $self->{category} )
108 17 ];
109 17 return;
110 }
111  
112 14 ▶sub category { $_[0]->{category} }
Subroutine category: called CC=1 SCAR=0.0
Pod documented
113  
114 sub categories {
115 2 ▶ my $class = shift;
Subroutine categories: called CC=2 SCAR=6.9
Pod documented
116 2 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 1 1 2
117  
118 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
119 2 return keys %{"$class\::MultiList"};
120 }
121  
122 sub has_category {
123 6 ▶ my ($class, $category) = @_;
Subroutine has_category: called CC=2 SCAR=6.9
Pod documented
124 6 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 3 3 6
125  
126 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
127 6 return exists ${"$class\::MultiList"}{$category};
128 }
129  
130 sub structure {
131 0 ▶ my $class = ref $_[0] || $_[0];
Subroutine structure: not called CC=2 SCAR=11.6
Pod documented
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
132 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
133 0 return ${"$class\::Theme"};
134 }
135  
136 1 1;
137  
138 __END__
139  
140 =head1 NAME
141  
142 AI::MicroStructure::MultiList - Base class for structures with multiple lists
143  
144 =head1 SYNOPSIS
145  
146 package AI::MicroStructure::digits;
147 use AI::MicroStructure::MultiList;
148 our @ISA = ( AI::MicroStructure::MultiList );
149 __PACKAGE__->init();
150 1;
151  
152 =head1 NAME
153
154 AI::MicroStructure::digits - The numbers structure
155
156 =head1 DESCRIPTION
157
158 You can count on this module. Almost.
159  
160 =cut
161
162 __DATA__
163 # default
164 :all
165 # names primes even
166 two
167 # names primes odd
168 three five seven
169 # names composites even
170 four six eight
171 # names composites odd
172 nine
173 # names other
174 zero one
175  
176 =head1 DESCRIPTION
177  
178 C<AI::MicroStructure::MultiList> is the base class for all structures
179 that are meant to return a random excerpt from a predefined list
180 I<divided in categories>.
181  
182 The category is selected at construction time from:
183  
184 =over 4
185  
186 =item 1.
187  
188 the given C<category> parameter,
189  
190 =item 2.
191  
192 the default category for the selected structure.
193  
194 =back
195  
196 Categories and sub-categories are separated by a C</> character.
197  
198 =head1 METHODS
199  
200 AI::MicroStructure::MultiList offers several methods, so that the subclasses
201 are easy to write (see full example in L<SYNOPSIS>):
202  
203 =over 4
204  
205 =item new( category => $category )
206  
207 The constructor of a single instance. An instance will not repeat items
208 until the list is exhausted.
209  
210 $meta = AI::MicroStructure::digits->new( category => 'primes' );
211 $meta = AI::MicroStructure::digits->new( category => 'primes/odd' );
212  
213 The special category C<:all> will use all the items in all categories.
214  
215 $meta = AI::MicroStructure::digits->new( category => ':all' );
216  
217 If no C<category> parameter is given, C<AI::MicroStructure::MultiList>
218 will use the class default. If the class doesn't define a default,
219 then C<:all> is used.
220  
221 =item init()
222  
223 init() must be called when the subclass is loaded, so as to read the
224 __DATA__ section and fully initialise it.
225  
226 =item name( $count )
227  
228 Return $count names (default: C<1>).
229  
230 Using C<0> will return the whole list in list context, and the size of the
231 list in scalar context (according to the C<category> parameter passed to the
232 constructor).
233  
234 =item category()
235  
236 Return the selected category for this instance.
237  
238 =item categories()
239  
240 Return the categories supported by the structure (except C<:all>).
241  
242 =item has_category( $category )
243  
244 Return a boolean value indicating if the structure contains the given category.
245  
246 =item structure()
247  
248 Return the structure name.
249  
250 =back
251  
252 =head1 AUTHOR
253  
254 Philippe 'BooK' Bruhat, C<< <book@cpan.org> >>
255  
256 =head1 COPYRIGHT & LICENSE
257  
258 Copyright 2006 Philippe 'BooK' Bruhat, All Rights Reserved.
259  
260 This program is free software; you can redistribute it and/or modify it
261 under the same terms as Perl itself.
262  
263 =cut
264  
« lib/AI/MicroStructure/Hypothesis.pm ↑ Summary lib/AI/MicroStructure/ObjectParser.pm »