| Akron | 7dacd01 | 2020-05-27 12:18:57 +0200 | [diff] [blame] | 1 | package KorAP::VirtualCorpus::And; |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use base 'KorAP::VirtualCorpus'; |
| 5 | |
| 6 | |
| 7 | # TODO: |
| 8 | # Support comments! |
| 9 | |
| 10 | |
| 11 | # Constructor |
| 12 | sub new { |
| 13 | my $class = shift; |
| 14 | bless { ops => [@_] }, $class; |
| 15 | }; |
| 16 | |
| 17 | |
| 18 | # Get koral type |
| 19 | sub koral_type { |
| 20 | return 'And'; |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | # Get operands |
| 25 | sub operands { |
| 26 | shift->{ops}; |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | # Flatten group |
| 31 | sub flatten { |
| 32 | my $self = shift; |
| 33 | |
| 34 | my @ops; |
| 35 | |
| 36 | foreach (@{$self->{ops}}) { |
| 37 | if ($_->koral_type eq 'And') { |
| 38 | push @ops, @{$_->operands}; |
| 39 | } |
| 40 | |
| 41 | else { |
| 42 | push @ops, $_->flatten; |
| 43 | }; |
| 44 | }; |
| 45 | |
| 46 | $self->{ops} = \@ops; |
| 47 | |
| 48 | return $self; |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | # Serialize fragment |
| 53 | sub _to_fragment { |
| 54 | my $self = shift; |
| 55 | my $json = '{'; |
| 56 | $json .= '"@type":"koral:docGroup",'; |
| 57 | $json .= '"operation":"operation:and",'; |
| 58 | $json .= '"operands":['; |
| 59 | $json .= join(',', map { $_->_to_fragment } @{$self->{ops}}); |
| 60 | $json .= ']'; |
| 61 | |
| 62 | # Set at the end, when all comments are done |
| 63 | $json .= $self->_commentparam_to_string; |
| 64 | $json .= '}'; |
| 65 | return $json; |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | 1; |