PML/Schema/Struct.pm

filter: Statementstmt 38.3% 46 / 120 Branchbran 6.5% 3 / 46 Conditioncond 16.6% 3 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 62.5% 15 / 24 Podpod 93.3% 14 / 15 totaltotal 35.3% 83 / 235 CC 32 SCAR 56.9
CC
32
Cov
28%
validate_object
650.0
get_member_names
6.0
get_attribute_names
6.0
CRAP 296.8
« PML/Backend/TEIXML.pm ↑ Summary PML/Instance/Writer.pm »
1 package Treex::PML::Schema::Struct;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 $VERSION='2.29'; # version template
9 }
10 9 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use Treex::PML::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use base qw( Treex::PML::Schema::Decl );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use UNIVERSAL::DOES;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 Treex::PML::Schema::Struct - implements declaration of a structure.
20  
21 =head1 INHERITANCE
22  
23 This class inherits from L<Treex::PML::Schema::Decl>.
24  
25 =head1 METHODS
26  
27 See the super-class for the complete list.
28  
29 =over 3
30  
31 =item $decl->get_decl_type ()
32  
33 Returns the constant PML_STRUCTURE_DECL.
34  
35 =item $decl->get_decl_type_str ()
36  
37 Returns the string 'structure'.
38  
39 =item $decl->get_structure_name ()
40  
41 Return declared structure name (if any).
42  
43 =item $decl->get_content_decl ()
44  
45 Returns undef.
46  
47 =item $decl->is_atomic ()
48  
49 Returns 0.
50  
51 =cut
52  
53  
54  
55 0 ▶sub is_atomic { 0 }
Subroutine is_atomic: not called CC=1 SCAR=6.9
Pod documented
56 772 ▶sub get_decl_type { return PML_STRUCTURE_DECL; }
Subroutine get_decl_type: called CC=1 SCAR=0.0
Pod documented
57 0 ▶sub get_decl_type_str { return 'structure'; }
Subroutine get_decl_type_str: not called CC=1 SCAR=6.9
Pod documented
58 0 ▶sub get_content_decl { return(undef); }
Subroutine get_content_decl: not called CC=1 SCAR=6.9
Pod documented
59 0 ▶sub get_structure_name { return $_[0]->{name}; }
Subroutine get_structure_name: not called CC=1 SCAR=6.9
Pod documented
60  
61 184 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
62 184 my ($self,$opts)=@_;
63 184 $self->{-parent}{-decl} = 'structure';
64 }
65  
66 =item $decl->get_members ()
67  
68 Return a list of the associated member declarations
69 (C<Treex::PML::Schema::Member>).
70  
71 =cut
72  
73 355 ▶sub get_members {
Subroutine get_members: called CC=2 SCAR=7.0
Pod documented
74 355 my $members = $_[0]->{member};
75 355 ▶ return $members ? map { $_->[0] } sort { $a->[1]<=> $b->[1] } map { [ $_, $_->{'-#'} ] } values %$members : ();
Branch: 1 branch
BranchTrueFalseTotal
$members ? : 355 0 355
76 }
77  
78 =item $decl->get_member_names ()
79  
80 Return a list of names of all members of the structure.
81  
82 =cut
83  
84 0 ▶sub get_member_names {
Subroutine get_member_names: not called CC=2 SCAR=17.9
Pod documented
85 0 my $members = $_[0]->{member};
86 0 ▶ return $members ? map { $_->[0] } sort { $a->[1]<=> $b->[1] } map { [ $_, $members->{$_}->{'-#'} ] } keys %$members : ();
Branch: 1 branch
BranchTrueFalseTotal
$members ? : 0 0 0
87 }
88  
89 =item $decl->get_member_by_name (name)
90  
91 Return the declaration of the member with a given name.
92  
93 =cut
94  
95 50 ▶sub get_member_by_name {
Subroutine get_member_by_name: called CC=2 SCAR=7.1
Pod documented
96 50 my ($self, $name) = @_;
97 50 my $members = $_[0]->{member};
98 50 ▶ return $members ? $members->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$members ? : 50 0 50
99 }
100  
101 =item $decl->get_attributes ()
102  
103 Return a list of member declarations (C<Treex::PML::Schema::Member>) declared
104 as attributes.
105  
106 =cut
107  
108 43 ▶sub get_attributes {
Subroutine get_attributes: called CC=2 SCAR=7.0
Pod documented
109 43 my $members = $_[0]->{member};
110 24 return $members ? map { $_->[0] } sort { $a->[1]<=> $b->[1] } map { [ $_, $_->{'-#'} ] }
111 43 ▶ grep { $_->{as_attribute} } values %$members : ();
Branch: 1 branch
BranchTrueFalseTotal
$members ? : 43 0 43
112 }
113  
114 =item $decl->get_attribute_names ()
115  
116 Return a list of names of all members of the structure declared as
117 attributes.
118  
119 =cut
120  
121 0 ▶sub get_attribute_names {
Subroutine get_attribute_names: not called CC=2 SCAR=17.9
Pod documented
122 0 my $members = $_[0]->{member};
123 0 return $members ? map { $_->[0] } sort { $a->[1]<=> $b->[1] } map { [ $_, $members->{$_}->{'-#'} ] }
124 0 ▶ grep { $_->{as_attribute} } keys %$members : ();
Branch: 1 branch
BranchTrueFalseTotal
$members ? : 0 0 0
125 }
126  
127  
128  
129 =item $decl->find_members_by_content_decl (decl)
130  
131 Lookup and return those member declarations whose content declaration
132 is decl.
133  
134 =cut
135  
136 0 ▶sub find_members_by_content_decl {
Subroutine find_members_by_content_decl: not called CC=1 SCAR=6.9
Pod documented
137 0 my ($self, $decl) = @_;
138 0 return grep { $decl == $_->get_content_decl } $self->get_members;
139 }
140  
141 =item $decl->find_members_by_type_name (name)
142  
143 Lookup and return those member declarations whose content is specified
144 via a reference to the named type with a given name.
145  
146 =cut
147  
148 0 ▶sub find_members_by_type_name {
Subroutine find_members_by_type_name: not called CC=2 SCAR=17.9
Pod documented
149 0 my ($self, $type_name) = @_;
150 # using directly $member->{type}
151 0 ▶ return grep { defined($_->{type}) and $_->{type} eq $type_name } $self->get_members;
Condition0%0/3
defined $_->{'type'} && $_->{'type'} eq $type_name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_->{'type'} && $_->{'type'} eq $type_name
defined $_->{'type'} $_->{'type'} eq $type_name
Truth table0%0/3
A && B
A= defined $_->{'type'}B= $_->{'type'} eq $type_name
ABresult
0X0
100
111
152 }
153  
154 =item $decl->find_members_by_role (role)
155  
156 Lookup and return declarations of all members with a given role.
157  
158 =cut
159  
160 218 ▶sub find_members_by_role {
Subroutine find_members_by_role: called CC=2 SCAR=6.9
Pod documented
161 218 my ($self, $role) = @_;
162 # using directly $member->{role}
163 218 ▶ return grep { defined($_->{role}) and $_->{role} eq $role } $self->get_members;
Condition100%3/3
defined $_->{'role'} && $_->{'role'} eq $role
!ll&&!rl&&r
45424743
MC/DC100%2/2
defined $_->{'role'} && $_->{'role'} eq $role
defined $_->{'role'} $_->{'role'} eq $role
Truth table100%3/3
A && B
A= defined $_->{'role'}B= $_->{'role'} eq $role
ABresult
0X0
100
111
164 }
165  
166 0 ▶sub validate_object {
Subroutine validate_object: not called CC=25 SCAR=64.8
Pod documented
167 0 my ($self,$object,$opts) = @_;
168  
169 0 my ($path,$tag,$flags);
170 0 my $log = [];
171 0 ▶ if (ref($opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts) 0 0 0
172 0 $flags = $opts->{flags};
173 0 $path = $opts->{path};
174 0 $tag = $opts->{tag};
175 0 ▶ $path.="/".$tag if $tag ne q{};
Branch: 1 branch
BranchTrueFalseTotal
if $tag ne "" 0 0 0
176 }
177  
178 0 my $members = $self->get_members;
179 0 ▶ if (!UNIVERSAL::isa($object,'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not UNIVERSAL::isa($object, 'HASH')) { } 0 0 0
180 0 push @$log, "$path: Unexpected content of the structure '$self->{name}': '$object'";
181 } else {
182 0 my @members = $self->get_members;
183 0 foreach my $member (grep { $_->is_attribute } @members) {
184 0 my $name = $member->get_name;
185 0 ▶ if (ref $object->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object->{$name}) 0 0 0
186 0 push @$log,"$path/$name: invalid content for member declared as attribute: ".ref($object->{$name});
187 }
188 }
189 0 foreach my $member (@members) {
190 0 my $name = $member->get_name;
191 0 my $role = $member->get_role;
192 0 my $mtype = $member->get_content_decl;
193 0 my $val = $object->{$name};
194 0 my $knit_name = $member->get_knit_name;
195 0 ▶ if ($role eq '#CHILDNODES' and !($flags & PML_VALIDATE_NO_TREES)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($role eq '#CHILDNODES' and not $flags & 1) { } 0 0 0
elsif ($name ne $knit_name) { } 0 0 0
elsif ($val ne '') { } 0 0 0
elsif ($member->is_required) { } 0 0 0
Condition0%0/3
$role eq "#CHILDNODES" and not $flags & 1
!ll&&!rl&&r
000
MC/DC0%0/2
$role eq "#CHILDNODES" and not $flags & 1
$role eq "#CHILDNODES" not $flags & 1
Truth table0%0/3
A and not(B)
A= $role eq "#CHILDNODES"B= not $flags & 1
ABresult
0X0
100
111
196 0 ▶ if (not UNIVERSAL::DOES::does($object,'Treex::PML::Node')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::DOES::does($object, "Treex::PML::Node")) 0 0 0
197 0 push @$log, "$path/$name: #CHILDNODES member on a non-node object: $object";
198 }
199 0 ▶ unless ($flags & PML_VALIDATE_NO_CHILDNODES) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flags & 2) 0 0 0
200 0 my $content;
201 0 my $mtype_is = $mtype->get_decl_type;
202 0 ▶ if ($mtype_is == PML_SEQUENCE_DECL) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mtype_is == 5) { } 0 0 0
elsif ($mtype_is == 6) { } 0 0 0
203 0 $content = Treex::PML::Factory->createSeq([map { Treex::PML::Seq::Element->new($_->{'#name'},$_) } $object->children]);
204 } elsif ($mtype_is == PML_LIST_DECL) {
205 0 $content = Treex::PML::Factory->createList([$object->children],1);
206 } else {
207 0 push @$log, "$path: #CHILDNODES should be either a list or sequence type";
208 }
209 0 $mtype->validate_object($content,
210 { flags => $flags,
211 path => $path,
212 tag => $name,
213 log => $log,
214 } );
215 }
216 } elsif ($name ne $knit_name) {
217 0 my $knit_val = $object->{$knit_name};
218 0 my $mtype;
219 0 ▶ if ($knit_val ne q{} and $val ne q{}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($knit_val ne '' and $val ne '') { } 0 0 0
elsif ($val ne '') { } 0 0 0
Condition0%0/3
$knit_val ne "" and $val ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$knit_val ne "" and $val ne ""
$knit_val ne "" $val ne ""
Truth table0%0/3
A and B
A= $knit_val ne ""B= $val ne ""
ABresult
0X0
100
111
220 0 push @$log, "$path/$knit_name: both '$name' and '$knit_name' are present for a #KNIT member";
221 } elsif ($val ne q{}) {
222 0 $knit_name = $name;
223 0 $knit_val = $val;
224 0 $mtype = $member->get_content_decl;
225 } else {
226 0 $mtype = $member->get_knit_content_decl;
227 }
228 0 ▶ if (defined $mtype) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mtype) { } 0 0 0
229 0 ▶ if ($knit_val ne q{} or $member->is_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knit_val ne "" or $member->is_required) 0 0 0
Condition0%0/3
$knit_val ne "" or $member->is_required
l!l&&r!l&&!r
000
MC/DC0%0/2
$knit_val ne "" or $member->is_required
$knit_val ne "" $member->is_required
Truth table0%0/3
A or B
A= $knit_val ne ""B= $member->is_required
ABresult
1X1
011
000
230 0 $mtype->validate_object($knit_val,
231 { flags => $flags,
232 path => $path,
233 tag => $knit_name,
234 log => $log
235 });
236 }
237 } else {
238 0 push @$log, "$path/$knit_name: can't determine data type of the #KNIT member";
239 }
240 } elsif ($val ne q{}) {
241 0 $mtype->validate_object($val,
242 { flags => $flags,
243 path => $path,
244 tag => $name,
245 log => $log,
246 } );
247 } elsif ($member->is_required) {
248 0 push @$log, "$path/$name: CDATA member declared as required cannot be empty!";
249 }
250 }
251 }
252 0 ▶ if ($opts and ref($opts->{log})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts and ref $opts->{"log"}) 0 0 0
Condition0%0/3
$opts and ref $opts->{"log"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts and ref $opts->{"log"}
$opts ref $opts->{"log"}
Truth table0%0/3
A and B
A= $optsB= ref $opts->{"log"}
ABresult
0X0
100
111
253 0 push @{$opts->{log}}, @$log;
254 }
255 0 ▶ return @$log ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@$log ? : 0 0 0
256 }
257  
258 =back
259  
260 =cut
261  
262  
263 9 1;
264 __END__
« PML/Backend/TEIXML.pm ↑ Summary PML/Instance/Writer.pm »