Treex/Block/Util/DefinedAttr.pm

filter: Statementstmt 27.6% 13 / 47 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.0% 2 / 5 Podpod 33.3% 1 / 3 totaltotal 17.3% 16 / 92 CC 18 SCAR 41.5
CC
18
Cov
16%
check_tree
110.0
BUILD
30.0
process_zone
30.0
CRAP 63.7
« Treex/Core/Scenario.pm ↑ Summary Treex/Core/Run.pm »
1 package Treex::Block::Util::DefinedAttr;
2 1 $Treex::Block::Util::DefinedAttr::VERSION = '2.20151210';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 extends 'Treex::Core::Block';
6  
7 1 has [qw(tnode anode nnode pnode)] => (
8 is => 'ro',
9 documentation => 'comma separated attributes of nodes on a given layer',
10 );
11  
12 1 has 'on_error' => (
13 is => 'ro',
14 isa => enum( [qw(warn die ignore)] ),
15 default => 'warn',
16 documentation => 'What to do if undefined attributes are found: warn (default), die, ignore',
17 );
18  
19 1 has 'message' => (
20 is => 'ro',
21 isa => 'Str',
22 default => '',
23 documentation => 'What to print',
24 );
25  
26 1 has fill => (
27 isa => 'Str',
28 is => 'ro',
29 default => 'undef',
30 documentation => 'Comma separated list of attribute values to be filled instead of undef. The default is "undef" which means Perl undef. If this list is shorter than ?node list, the last value is used for the rest. '
31 );
32  
33  
34 sub BUILD {
35 0 ▶ my ($self) = @_;
Subroutine BUILD: not called CC=5 SCAR=34.0
Pod undocumented
36 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
37 0 log_fatal 'One of parameters tnode, anode, nnode, pnode must be specified.';
38 }
39 0 return;
40 }
41  
42 sub process_zone {
43 0 ▶ my ( $self, $zone ) = @_;
Subroutine process_zone: not called CC=5 SCAR=34.0
Pod documented
44 0 ▶ if ( $self->anode ) { $self->check_tree( $zone, 'a', $self->anode ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->anode) 0 0 0
45 0 ▶ if ( $self->tnode ) { $self->check_tree( $zone, 't', $self->tnode ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tnode) 0 0 0
46 0 ▶ if ( $self->nnode ) { $self->check_tree( $zone, 'n', $self->nnode ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nnode) 0 0 0
47 0 ▶ if ( $self->pnode ) { $self->check_tree( $zone, 'p', $self->pnode ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pnode) 0 0 0
48 0 return;
49 }
50  
51 sub check_tree {
52 0 ▶ my ( $self, $zone, $layer, $attrs ) = @_;
Subroutine check_tree: not called CC=10 SCAR=47.0
Pod undocumented
53 0 my $tree = $zone->get_tree($layer);
54 0 ▶ if ( !defined $tree ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tree) 0 0 0
55 0 log_warn "No $layer-tree in zone " . $zone->get_label();
56 0 return;
57 }
58  
59 0 foreach my $node ( $tree->get_descendants() ) {
60 0 my @fill_values = split /,/, $self->fill;
61  
62 # If $self->fill is an empty string, @fill_values will contain no items.
63 0 ▶ @fill_values = ('') if !@fill_values;
Branch: 1 branch
BranchTrueFalseTotal
unless @fill_values 0 0 0
64  
65 0 my $fill_value = shift @fill_values;
66 0 foreach my $name ( split /,/, $attrs ) {
67 0 my $orig_value = $node->get_attr($name);
68 0 ▶ if ( !defined $orig_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $orig_value) 0 0 0
69 0 ▶ if ($self->on_error =~ /die|warn/){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_error =~ /die|warn/) 0 0 0
70 0 my $address = $node->get_address();
71 0 my $msg = "${layer}node\t$address\tundefined attr_name=$name\t" . $self->message;
72 0 ▶ log_fatal($msg) if $self->on_error eq 'die';
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_error eq "die" 0 0 0
73 0 log_warn($msg);
74 }
75 0 ▶ $node->set_attr($name, $fill_value) if $fill_value ne 'undef';
Branch: 1 branch
BranchTrueFalseTotal
if $fill_value ne "undef" 0 0 0
76 }
77 0 ▶ $fill_value = shift @fill_values if @fill_values;
Branch: 1 branch
BranchTrueFalseTotal
if @fill_values 0 0 0
78 }
79 }
80 0 return;
81 }
82  
83 1 1;
84  
85 __END__
86  
87 =pod
88  
89 =encoding utf-8
90  
91 =for Pod::Coverage check_tree
92  
93 =head1 NAME
94  
95 Treex::Block::Util::DefinedAttr - Special block for checking C<undef> attributes
96  
97 =head1 VERSION
98  
99 version 2.20151210
100  
101 =head1 SYNOPSIS
102  
103 # on the command line:
104 treex Util::DefinedAttr anode=lemma on_error=die -- myfile.treex
105  
106 # in a scenario:
107 Util::SetGlobal language=en selector=T
108 Util::DefinedAttr tnode=t_lemma,nodetype,formeme
109 Block::XY
110 Util::DefinedAttr anode=lemma on_error=die message="after block XY"
111  
112  
113 =head1 DESCRIPTION
114  
115 Warns/dies if a given attribute is undefined (in any node).
116  
117 One of parameters C<tnode>, C<anode>, C<nnode>, C<pnode> must be always specified.
118 By default: C<on_error=warn> and C<message> is empty.
119  
120  
121 =head1 COPYRIGHT AND LICENSE
122  
123 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
124  
125 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Treex/Core/Scenario.pm ↑ Summary Treex/Core/Run.pm »