| 1 | package Treex::View::Node; | ||
| 2 | our $AUTHORITY = 'cpan:MICHALS'; | ||
| 3 | $Treex::View::Node::VERSION = '1.0.0'; | ||
| 4 | # ABSTRACT: Wrapper around Treex::Core::Node | ||
| 5 | |||
| 6 | 0 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | BEGIN { $ENV{NO_FS_CLASSES} = 1; } | ||
| 8 | use Treex::PML::Schema; | ||
| 9 | use Treex::PML::Instance; | ||
| 10 | use Treex::Core::TredView; | ||
| 11 | use Scalar::Util qw(blessed); | ||
| 12 | use namespace::autoclean; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Treex::View::Node - This is L<Treex::Core::Node> wrapper | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use Treex::View::Node; | ||
| 21 | my $root = Treex::View::Node->new( node => $treex_root ); | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Wrapper around regular L<Treex::Core::Node> to provide L<TO_JSON> | ||
| 26 | method used for converting Treex structure to simple Perl hashes and | ||
| 27 | arrays. | ||
| 28 | |||
| 29 | =head1 IMPORTED CONSTANTS | ||
| 30 | |||
| 31 | =over 2 | ||
| 32 | |||
| 33 | =item PML_STRUCTURE_DECL | ||
| 34 | |||
| 35 | =item PML_CONTAINER_DECL | ||
| 36 | |||
| 37 | =item PML_SEQUENCE_DECL | ||
| 38 | |||
| 39 | =item PML_LIST_DECL | ||
| 40 | |||
| 41 | =item PML_ALT_DECL | ||
| 42 | |||
| 43 | =item PML_CDATA_DECL | ||
| 44 | |||
| 45 | =item PML_CHOICE_DECL | ||
| 46 | |||
| 47 | =item PML_CONSTANT_DECL | ||
| 48 | |||
| 49 | =item PML_ELEMENT_DECL | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | =head1 METHODS | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | has 'node' => ( is => 'ro', isa => 'Treex::Core::Node', required => 1 ); | ||
| 58 | |||
| 59 | has 'labels' => ( | ||
| 60 | is => 'rw', | ||
| 61 | isa => 'Treex::Core::TredView::Labels', | ||
| 62 | ); | ||
| 63 | |||
| 64 | =head2 traverse_data | ||
| 65 | |||
| 66 | Will traverse data and dumps structures to hashes and arrays | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub traverse_data { | ||
| 71 | my ( $self, $decl, $value ) = @_; | ||
| 72 | my $data; | ||
| 73 | my $decl_is = $decl->get_decl_type; | ||
| 74 | |||
| 75 | if ( $decl_is == PML_STRUCTURE_DECL ) { | ||
| 76 | my @members = grep { | ||
| 77 | ( !defined( $_->get_role ) or $_->get_role ne '#CHILDNODES' ) | ||
| 78 | and ( ( $_->get_content_decl && $_->get_content_decl->get_role || '' ) ne '#TREES' ) | ||
| 79 | } $decl->get_members; | ||
| 80 | $data = {}; | ||
| 81 | for (@members) { | ||
| 82 | my $n = $_->get_knit_name; | ||
| 83 | my $v = $value->{$n}; | ||
| 84 | my $d = $_->get_knit_content_decl; | ||
| 85 | $data->{$n} = ( defined($v) and !$d->is_atomic ) ? $self->traverse_data( $d, $v ) : $v; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | elsif ( $decl_is == PML_CONTAINER_DECL ) { | ||
| 89 | my @attrs = $decl->get_attributes; | ||
| 90 | $data = {}; | ||
| 91 | for (@attrs) { | ||
| 92 | my $n = $_->get_name; | ||
| 93 | $data->{$n} = $value->{$n}; | ||
| 94 | } | ||
| 95 | my $knit_content_decl = $decl->get_knit_content_decl; | ||
| 96 | my $content_decl = $decl->get_content_decl; | ||
| 97 | my $role = ( $content_decl && $content_decl->get_role ) || ''; | ||
| 98 | if ( defined($content_decl) and $role ne '#CHILDNODES' and $role ne '#TREES' ) { | ||
| 99 | if ( $knit_content_decl and defined( $value->{'#content'} ) ) { | ||
| 100 | $data->{'#content'} | ||
| 101 | = $knit_content_decl->is_atomic | ||
| 102 | ? $value->{'#content'} | ||
| 103 | : $self->traverse_data( $knit_content_decl, $value->{'#content'} ); | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | $data->{'#content'} = undef; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | elsif ( $decl_is == PML_SEQUENCE_DECL ) { | ||
| 111 | my @elems = $decl->get_elements; | ||
| 112 | $data = []; | ||
| 113 | my $idx = 0; | ||
| 114 | my %pos; | ||
| 115 | for (@elems) { | ||
| 116 | $pos{ $_->get_name } = $idx++; | ||
| 117 | push @{$data}, [ $_->get_name, ]; | ||
| 118 | } | ||
| 119 | for ( @{ $value->elements_list } ) { | ||
| 120 | my $n = $_->name; | ||
| 121 | my $v = $_->value; | ||
| 122 | my $e = $decl->get_element_by_name($n); | ||
| 123 | my $p = $pos{$n}; # position | ||
| 124 | |||
| 125 | push @{ $data->[$p] }, $self->traverse_data( $e, $v ); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | elsif ( $decl_is == PML_ELEMENT_DECL ) { | ||
| 129 | my $content_decl = $decl->get_knit_content_decl; | ||
| 130 | my $compact = !defined($value) or $content_decl->is_atomic; | ||
| 131 | $data = { '#value' => $compact ? $value : $self->traverse_data( $content_decl, $value ) }; | ||
| 132 | } | ||
| 133 | elsif ( $decl_is == PML_LIST_DECL || $decl_is == PML_ALT_DECL ) { | ||
| 134 | if ( $decl_is == PML_ALT_DECL and ( !blessed $value or !$value->isa('Treex::PML::Alt') ) ) { | ||
| 135 | $value = Treex::PML::Alt->new($value); | ||
| 136 | } | ||
| 137 | my $content_decl = $decl->get_knit_content_decl; | ||
| 138 | my $atomic = $content_decl->is_atomic; | ||
| 139 | my $ordered = ( $decl_is == PML_LIST_DECL and $decl->is_ordered ); | ||
| 140 | my $i = 0; | ||
| 141 | $data = []; | ||
| 142 | for my $v ( $value->values ) { | ||
| 143 | push @$data, | ||
| 144 | { | ||
| 145 | '#value' => $atomic ? $v : $self->traverse_data( $content_decl, $v ), | ||
| 146 | ( $ordered ? ( '#pos' => $i++ ) : () ), | ||
| 147 | }; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | elsif ( $decl_is == PML_CHOICE_DECL || $decl_is == PML_CONSTANT_DECL || $decl_is == PML_CDATA_DECL ) { | ||
| 151 | confess("Traversing atomic type"); | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | die "Unhandled data type: $decl"; | ||
| 155 | } | ||
| 156 | |||
| 157 | return $data; | ||
| 158 | } | ||
| 159 | |||
| 160 | =head2 TO_JSON | ||
| 161 | |||
| 162 | Called by L<JSON> package while converting blessed items | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub TO_JSON { | ||
| 167 | my $self = shift; | ||
| 168 | |||
| 169 | my $n = $self->node; | ||
| 170 | my $data = { | ||
| 171 | id => $n->id, | ||
| 172 | depth => int( $n->level ), | ||
| 173 | ( $n->does('Treex::Core::Node::Ordered') ? ( order => int( $n->ord ) ) : () ), # force ord to be integer | ||
| 174 | data => $self->traverse_data( $n->type, $n ), | ||
| 175 | }; | ||
| 176 | |||
| 177 | $n->deserialize_wild; # We want to see wild :) | ||
| 178 | if ( $n->wild ) { | ||
| 179 | $data->{data}->{wild_dump} = $n->wild; | ||
| 180 | } | ||
| 181 | |||
| 182 | ## some fake values to stop warnings | ||
| 183 | $n->{'_shift_down'} = 0; | ||
| 184 | $n->{'_shift_right'} = 0; | ||
| 185 | $n->{_tree_depth} = 0; | ||
| 186 | $n->{_depth} = 0; | ||
| 187 | |||
| 188 | if ( $n->is_root ) { | ||
| 189 | $n->{_precomputed_labels} = $self->labels->root_labels($n); | ||
| 190 | $n->{_precomputed_hint} = ''; | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | $n->{_precomputed_buffer} = $self->labels->node_labels( $n, $n->get_layer ); | ||
| 194 | $n->{_precomputed_hint} = Treex::Core::TredView->node_hint( $n, $n->get_layer ); | ||
| 195 | $self->labels->set_labels($n); | ||
| 196 | } | ||
| 197 | $data->{labels} = $n->{_precomputed_labels}; | ||
| 198 | $data->{hint} = $n->{_precomputed_hint}; | ||
| 199 | $data->{parent} = $n->parent ? $n->parent->id : undef; | ||
| 200 | $data->{firstson} = $n->firstson ? $n->firstson->id : undef; | ||
| 201 | $data->{rbrother} = $n->rbrother ? $n->rbrother->id : undef; | ||
| 202 | $data->{lbrother} = $n->lbrother ? $n->lbrother->id : undef; | ||
| 203 | |||
| 204 | #my @children = $n->is_leaf ? () : (map {__PACKAGE__->new(node=>$_, labels=>$self->labels)} $n->children); | ||
| 205 | #$data->{children} = \@children if @children; | ||
| 206 | |||
| 207 | return $data; | ||
| 208 | } | ||
| 209 | |||
| 210 | __PACKAGE__->meta->make_immutable(); | ||
| 211 | 1; | ||
| 212 | |||
| 213 | __END__ | ||