lib/Treex/Block/Util/DefinedAttr.pm

filter: Statementstmt 31.7% 13 / 41 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.0% 2 / 5 Podpod 33.3% 1 / 3 totaltotal 20.5% 16 / 78 CC 14 SCAR 34.3
CC
14
Cov
20%
check_tree
42.0
BUILD
30.0
process_zone
30.0
CRAP 30.8
« lib/Treex/Core/DocumentReader/ZoneReader.pm ↑ Summary lib/Treex/Core/TredView/Vallex.pm »
1 package Treex::Block::Util::DefinedAttr;
2 {
3 1 $Treex::Block::Util::DefinedAttr::VERSION = '0.08633_1';
4 }
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 extends 'Treex::Core::Block';
8  
9 1 has [qw(tnode anode nnode pnode)] => (
10 is => 'ro',
11 documentation => 'comma separated attributes of nodes on a given layer',
12 );
13  
14 1 has 'on_error' => (
15 is => 'ro',
16 isa => enum( [qw(warn die)] ),
17 default => 'warn',
18 documentation => 'What to do if undefined attributes are found: warn or die',
19 );
20  
21 1 has 'message' => (
22 is => 'ro',
23 isa => 'Str',
24 default => '',
25 documentation => 'What to print',
26 );
27  
28 sub BUILD {
29 0 ▶ my ($self) = @_;
Subroutine BUILD: not called CC=5 SCAR=34.0
Pod undocumented
30 0 ▶ if ( !$self->tnode && !$self->anode && !$self->nnode && !$self->pnode ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->tnode or $self->anode or $self->nnode or $self->pnode) 0 0 0
Condition0%0/9
$self->tnode or $self->anode or $self->nnode or $self->pnode
l!l&&r!l&&!r
000
$self->tnode or $self->anode or $self->nnode
l!l&&r!l&&!r
000
$self->tnode or $self->anode
l!l&&r!l&&!r
000
MC/DC0%0/4
$self->tnode or $self->anode or $self->nnode or $self->pnode
$self->tnode $self->anode $self->nnode $self->pnode
Truth table0%0/5
A or B or C or D
A= $self->tnodeB= $self->anodeC= $self->nnodeD= $self->pnode
ABCDresult
1XXX1
01XX1
001X1
00011
00000
31 0 log_fatal 'One of parameters tnode, anode, nnode, pnode must be specified.';
32 }
33 0 return;
34 }
35  
36 sub process_zone {
37 0 ▶ my ( $self, $zone ) = @_;
Subroutine process_zone: not called CC=5 SCAR=34.0
Pod documented
38 0 ▶ if ( $self->anode ) { $self->check_tree( $zone, 'a', $self->anode ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->anode) 0 0 0
39 0 ▶ if ( $self->tnode ) { $self->check_tree( $zone, 't', $self->tnode ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tnode) 0 0 0
40 0 ▶ if ( $self->nnode ) { $self->check_tree( $zone, 'n', $self->nnode ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nnode) 0 0 0
41 0 ▶ if ( $self->pnode ) { $self->check_tree( $zone, 'p', $self->pnode ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pnode) 0 0 0
42 0 return;
43 }
44  
45 sub check_tree {
46 0 ▶ my ( $self, $zone, $layer, $attrs ) = @_;
Subroutine check_tree: not called CC=6 SCAR=37.4
Pod undocumented
47 0 my $tree = $zone->get_tree($layer);
48 0 ▶ if ( !defined $tree ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tree) 0 0 0
49 0 log_warn "No $layer-tree in zone " . $zone->get_label();
50 0 return;
51 }
52  
53 0 foreach my $node ( $tree->get_descendants() ) {
54 0 foreach my $name ( split /,/, $attrs ) {
55 0 my $value = $node->get_attr($name);
56 0 ▶ if ( !defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
57 0 my $address = $node->get_address();
58 0 my $msg = "${layer}node\t$address\tundefined attr_name=$name\t" . $self->message;
59 0 ▶ log_fatal($msg) if $self->on_error eq 'die';
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_error eq "die" 0 0 0
60 0 log_warn($msg);
61 }
62 }
63 }
64 0 return;
65 }
66  
67 1 1;
68  
69 __END__
70  
71 =for Pod::Coverage check_tree
72  
73 =head1 NAME
74  
75 Treex::Block::Util::DefinedAttr - Special block for checking C<undef> attributes
76  
77 =head1 VERSION
78  
79 version 0.08633_1
80  
81 =head1 SYNOPSIS
82  
83 # on the command line:
84 treex Util::DefinedAttr anode=lemma on_error=die -- myfile.treex
85  
86 # in a scenario:
87 Util::SetGlobal language=en selector=T
88 Util::DefinedAttr tnode=t_lemma,nodetype,formeme
89 Block::XY
90 Util::DefinedAttr anode=lemma on_error=die message="after block XY"
91  
92  
93 =head1 DESCRIPTION
94  
95 Warns/dies if a given attribute is undefined (in any node).
96  
97 One of parameters C<tnode>, C<anode>, C<nnode>, C<pnode> must be always specified.
98 By default: C<on_error=warn> and C<message> is empty.
99  
100  
101 =head1 COPYRIGHT AND LICENSE
102  
103 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
104  
105 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« lib/Treex/Core/DocumentReader/ZoneReader.pm ↑ Summary lib/Treex/Core/TredView/Vallex.pm »