| 1 | package Treex::Block::Util::DefinedAttr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | $Treex::Block::Util::DefinedAttr::VERSION = '2.20210102'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ( !$self->tnode && !$self->anode && !$self->nnode && !$self->pnode ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$self->tnode or $self->anode or $self->nnode or $self->pnode
$self->tnode or $self->anode or $self->nnode
$self->tnode or $self->anode
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | log_fatal 'One of parameters tnode, anode, nnode, pnode must be specified.'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub process_zone { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_zone: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my ( $self, $zone ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ( $self->anode ) { $self->check_tree( $zone, 'a', $self->anode ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ( $self->tnode ) { $self->check_tree( $zone, 't', $self->tnode ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( $self->nnode ) { $self->check_tree( $zone, 'n', $self->nnode ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( $self->pnode ) { $self->check_tree( $zone, 'p', $self->pnode ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub check_tree { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_tree: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my ( $self, $zone, $layer, $attrs ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $tree = $zone->get_tree($layer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ( !defined $tree ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ($self->on_error =~ /die|warn/){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | log_warn($msg); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | $node->set_attr($name, $fill_value) if $fill_value ne 'undef'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $fill_value = shift @fill_values if @fill_values; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||