| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Analyzer::RewriteSub.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: sub-analysis (LTS, Morph) of rewrite targets | ||
| 6 | |||
| 7 | ##============================================================================== | ||
| 8 | ## Package: Analyzer::RewriteSub | ||
| 9 | ##============================================================================== | ||
| 10 | package DTA::CAB::Analyzer::RewriteSub; | ||
| 11 | use DTA::CAB::Chain; | ||
| 12 | use DTA::CAB::Analyzer ':child'; | ||
| 13 | use DTA::CAB::Analyzer::Morph; | ||
| 14 | use DTA::CAB::Analyzer::LTS; | ||
| 15 | use Carp; | ||
| 16 | use strict; | ||
| 17 | our @ISA = qw(DTA::CAB::Chain); | ||
| 18 | |||
| 19 | ##============================================================================== | ||
| 20 | ## Methods | ||
| 21 | |||
| 22 | ## $obj = CLASS_OR_OBJ->new(chain=>\@analyzers, %args) | ||
| 23 | ## + basic object structure: (see also DTA::CAB::Chain) | ||
| 24 | ## ( | ||
| 25 | ## chain => [$a1, ..., $aN], ##-- sub-analysis chain (e.g. chain=>[$lts,$morph]) | ||
| 26 | ## ) | ||
| 27 | ## + new object structure: | ||
| 28 | ## ( | ||
| 29 | ## rwLabel => $label, ##-- label of source 'rewrite' object (default='rw') | ||
| 30 | ## ) | ||
| 31 | sub new { | ||
| 32 | my $that = shift; | ||
| 33 | my $asub = $that->SUPER::new( | ||
| 34 | ##-- defaults | ||
| 35 | #analysisClass => 'DTA::CAB::Analyzer::Rewrite::Analysis', | ||
| 36 | label => 'rwsub', | ||
| 37 | |||
| 38 | ##-- analysis selection | ||
| 39 | rwLabel => 'rw', | ||
| 40 | |||
| 41 | ##-- user args | ||
| 42 | @_ | ||
| 43 | ); | ||
| 44 | return $asub; | ||
| 45 | } | ||
| 46 | |||
| 47 | ## $doc = $anl->analyzeTypes($doc,\%types,\%opts) | ||
| 48 | ## + perform type-wise analysis of all (text) types in %types (= %{$doc->{types}}) | ||
| 49 | ## + extracts rewrite targets, builds pseudo-type hash, calls sub-chain analyzeTypes(), & expands | ||
| 50 | sub analyzeTypes { | ||
| 51 | my ($asub,$doc,$types,$opts) = @_; | ||
| 52 | return $doc if (!$asub->enabled($opts)); | ||
| 53 | |||
| 54 | ##-- load | ||
| 55 | #$asub->ensureLoaded(); | ||
| 56 | |||
| 57 | ##-- get rewrite target types | ||
| 58 | $types = $doc->types if (!$types); | ||
| 59 | my $rwkey = $asub->{rwLabel}; | ||
| 60 | my $rwtypes = { | ||
| 61 | map { ($_->{hi}=>bless({text=>$_->{hi}},'DTA::CAB::Token')) } | ||
| 62 | map { $_->{$rwkey} ? @{$_->{$rwkey}} : qw() } | ||
| 63 | values(%$types) | ||
| 64 | }; | ||
| 65 | |||
| 66 | ##-- analyze rewrite target types | ||
| 67 | my ($sublabel); | ||
| 68 | foreach (@{$asub->{chain}}) { | ||
| 69 | $sublabel = $_->{label}; | ||
| 70 | next if (defined($opts->{$sublabel}) && !$opts->{$sublabel}); | ||
| 71 | $_->{label} =~ s/^\Q$asub->{label}_\E//; ##-- sanitize label (e.g. "rwsub_morph" --> "morph"), because it's also used as output key | ||
| 72 | $_->analyzeTypes($doc,$rwtypes,$opts); | ||
| 73 | $_->{label} = $sublabel; | ||
| 74 | } | ||
| 75 | |||
| 76 | ##-- delete rewrite target type 'text' | ||
| 77 | delete($_->{text}) foreach (values %$rwtypes); | ||
| 78 | |||
| 79 | ##-- expand rewrite target types | ||
| 80 | my ($rwtyp); | ||
| 81 | foreach (map {$_->{$rwkey} ? @{$_->{$rwkey}} : qw()} values(%$types)) { | ||
| 82 | $rwtyp = $rwtypes->{$_->{hi}}; | ||
| 83 | @$_{keys %$rwtyp} = values %$rwtyp; | ||
| 84 | } | ||
| 85 | |||
| 86 | ##-- return | ||
| 87 | return $doc; | ||
| 88 | } | ||
| 89 | |||
| 90 | ## @keys = $anl->typeKeys() | ||
| 91 | ## + returns list of type-wise keys to be expanded for this analyzer by expandTypes() | ||
| 92 | ## + override returns $anl->{rwLabel} | ||
| 93 | sub typeKeys { | ||
| 94 | return $_[0]{rwLabel}; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | ##------------------------------------------------------------------------ | ||
| 99 | ## Methods: I/O: Input: all | ||
| 100 | |||
| 101 | ## \@analyzers = $ach->chain() | ||
| 102 | ## \@analyzers = $ach->chain(\%opts) | ||
| 103 | ## + get selected analyzer chain | ||
| 104 | ### + NEW: just return $ach->{chain}, since analyzers may still be disabled here (argh) | ||
| 105 | sub chain { | ||
| 106 | my $ach = shift; | ||
| 107 | return $ach->{chain}; | ||
| 108 | #return [grep {$_ && $_->enabled} @{$ach->{chain}}]; | ||
| 109 | } | ||
| 110 | |||
| 111 | ## $bool = $ach->ensureLoaded() | ||
| 112 | ## + returns true if any chain member loads successfully (or if the chain is empty) | ||
| 113 | sub ensureLoaded { | ||
| 114 | my $ach = shift; | ||
| 115 | @{$ach->{chain}} = grep {$_} @{$ach->{chain}}; ##-- hack: chuck undef chain-links here | ||
| 116 | return 1 if (!@{$ach->{chain}}); | ||
| 117 | my $rc = 0; | ||
| 118 | foreach (@{$ach->{chain}}) { | ||
| 119 | $rc = $_->ensureLoaded() || $rc; | ||
| 120 | } | ||
| 121 | return $rc; | ||
| 122 | } | ||
| 123 | |||
| 124 | ##------------------------------------------------------------------------ | ||
| 125 | ## Methods: Analysis: Generic | ||
| 126 | |||
| 127 | ## $bool = $ach->canAnalyze() | ||
| 128 | ## $bool = $ach->canAnalyze(\%opts) | ||
| 129 | ## + returns true if analyzer can perform its function (e.g. data is loaded & non-empty) | ||
| 130 | ## + returns true if ANY analyzers in the chain do to | ||
| 131 | sub canAnalyze { | ||
| 132 | my $ach = shift; | ||
| 133 | @{$ach->{chain}} = grep {$_ && $_->canAnalyze} @{$ach->chain(@_)}; | ||
| 134 | foreach (@{$ach->chain(@_)}) { | ||
| 135 | return 1 if ($_->canAnalyze); | ||
| 136 | } | ||
| 137 | return 1; | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | 1; ##-- be happy | ||
| 143 | |||
| 144 | __END__ |