DiaColloDB/Relation.pm

filter: Statementstmt 10.3% 20 / 193 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 30.0% 6 / 20 Podpod 78.5% 11 / 14 totaltotal 9.0% 37 / 410 CC 78 SCAR 62.4
CC
78
Cov
6%
profile
1190.0
compare
380.0
qinfoData
90.0
CRAP 510.4
« DiaColloDB/Upgrade/v0_10_x2t.pm ↑ Summary DiaColloDB/Corpus/Compiled.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::Relation.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: collocation db, relation API (abstract & utilities)
5  
6 package DiaColloDB::Relation;
7 1 ▶use DiaColloDB::Persistent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DiaColloDB::Profile;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DiaColloDB::Profile::Multi;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DiaColloDB::Utils qw(:si :pack :math);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Algorithm::BinarySearch::Vec qw(:api);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 ##==============================================================================
15 ## Globals & Constants
16  
17 1 our @ISA = qw(DiaColloDB::Persistent);
18  
19 ##==============================================================================
20 ## Constructors etc.
21  
22 ## $rel = CLASS_OR_OBJECT->new(%args)
23 ## + %args, object structure: see subclases
24 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
25 0 my ($that,%args) = @_;
26 0 ▶ return bless({ %args }, ref($that)||$that);
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
27 }
28  
29 ##==============================================================================
30 ## Relation API: create
31  
32 ## $rel = $CLASS_OR_OBJECT->create($coldb, $tokdat_file, %opts)
33 ## + populates current database from $tokdat_file,
34 ## a tt-style text file containing with lines of the form:
35 ## TID DATE ##-- single token
36 ## "\n" ##-- blank line --> EOS
37 ## + %opts: clobber %$rel
38 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
39 0 my ($rel,$coldb,$datfile,%opts) = @_;
40 0 $rel->logconfess($coldb->{error}="create(): abstract method called");
41 }
42  
43 ##==============================================================================
44 ## Relation API: union
45  
46 ## $rel = $CLASS_OR_OBJECT->union($coldb, \@pairs, %opts)
47 ## + merge multiple co-frequency indices into new object
48 ## + @pairs : array of pairs ([$argrel,\@ti2u],...)
49 ## of relation-objects $argrel and tuple-id maps \@ti2u for $rel
50 ## + %opts: clobber %$rel
51 ## + implicitly flushes the new index
52 0 ▶sub union {
Subroutine union: not called CC=1 SCAR=6.9
Pod documented
53 0 my ($rel,$coldb, $pairs,%opts) = @_;
54 0 $rel->logconfess($coldb->{error}="union(): abstract method called");
55 }
56  
57 ##==============================================================================
58 ## Relation API: info
59  
60 ## \%info = $rel->dbinfo($coldb)
61 ## + embedded info-hash for $coldb->dbinfo()
62 0 ▶sub dbinfo {
Subroutine dbinfo: not called CC=2 SCAR=17.9
Pod undocumented
63 0 my $rel = shift;
64 0 my $info = { class=>ref($rel) };
65 0 ▶ if ($rel->can('du')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->can("du")) 0 0 0
66 0 $info->{du_b} = $rel->du();
67 0 $info->{du_h} = si_str($info->{du_b});
68 }
69 0 return $info;
70 }
71  
72  
73 ##==============================================================================
74 ## Relation API: profiling & comparison: top-level
75  
76 ##--------------------------------------------------------------
77 ## Relation API: profile
78  
79 ## $mprf = $rel->profile($coldb, %opts)
80 ## + get a relation profile for selected items as a DiaColloDB::Profile::Multi object
81 ## + %opts:
82 ## (
83 ## ##-- selection parameters
84 ## query => $query, ##-- target request ATTR:REQ...
85 ## date => $date1, ##-- string or array or range "MIN-MAX" (inclusive) : default=all
86 ## ##
87 ## ##-- aggregation parameters
88 ## slice => $slice, ##-- date slice (default=1, 0 for global profile)
89 ## groupby => $groupby, ##-- string or array "ATTR1[:HAVING1] ...": default=$coldb->attrs; see groupby() method
90 ## ##
91 ## ##-- scoring and trimming parameters
92 ## eps => $eps, ##-- smoothing constant (default=0)
93 ## score => $func, ##-- scoring function ("f"|"lf"|"fm"|"lfm"|"mi"|"ld"|"ll") : default="f"
94 ## kbest => $k, ##-- return only $k best collocates per date (slice) : default=-1:all
95 ## cutoff => $cutoff, ##-- minimum score
96 ## global => $bool, ##-- trim profiles globally (vs. locally for each date-slice?) (default=0)
97 ## extend => \%label2gkeys, ##-- maps slice-labels to selected (packed) group-keys, for extend() method
98 ## ##
99 ## ##-- profiling and debugging parameters
100 ## strings => $bool, ##-- do/don't stringify item keys (default=do)
101 ## packed => $bool, ##-- leave item keys packed (default=don't)
102 ## fill => $bool, ##-- if true, returned multi-profile will have null profiles inserted for missing slices
103 ## onepass => $bool, ##-- if true, use fast but incorrect 1-pass method (Cofreqs subclass only, >= v0.09.001)
104 ## )
105 ## + default implementation
106 ## - parses request and extracts target tuple-ids
107 ## - calls $rel->subprofile1() to compute slice-wise joint frequency profiles (f12)
108 ## - calls $rel->subprofile2() to compute independent collocate frequencies (f2), and finally
109 ## - collects the result in a DiaColloDB::Profile::Multi object
110 ## + default values for %opts should be set by higher-level call, e.g. DiaColloDB::profile()
111 0 ▶sub profile {
Subroutine profile: not called CC=34 SCAR=70.8
Pod documented
112 0 my ($reldb,$coldb,%opts) = @_;
113  
114 ##-- common variables
115 0 $opts{coldb} = $coldb; ##-- pass-down to subprofile() methods
116 0 my $logProfile = $coldb->{logProfile};
117  
118 ##-- variables: by attribute
119 0 my $groupby= $opts{groupby} = $coldb->groupby($opts{groupby});
120 0 my $attrs = $coldb->attrs();
121 0 my $adata = $coldb->attrData($attrs);
122 0 my $a2data = $opts{a2data} = {map {($_->{a}=>$_)} @$adata};
123 0 my $areqs = $coldb->parseRequest($opts{query}, logas=>'query', default=>$attrs->[0], qref=>\$opts{qobj});
124 0 foreach (@$areqs) {
125 0 $a2data->{$_->[0]}{req} = $_->[1];
126 }
127  
128 ##-- sanity check(s)
129 0 ▶ if (!@$areqs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$areqs) 0 0 0
130 0 $reldb->logwarn($coldb->{error}="profile(): no target attributes specified (supported attributes: ".join(' ',@{$coldb->attrs}).")");
131 0 return undef;
132 }
133 0 ▶ if (!@{$groupby->{attrs}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$groupby->{"attrs"};}) 0 0 0
134 0 $reldb->logconfess($coldb->{error}="profile(): cannot profile with empty groupby clause");
135 0 return undef;
136 }
137  
138 ##-- prepare: get target IDs (by attribute)
139 0 my ($ac);
140 0 ▶ foreach $ac (grep {($_->{req}//'') ne ''} @$adata) {
Condition0%0/2
$_->{"req"} // ""
l!l
00
MC/DC0%0/1
$_->{"req"} // ""
$_->{"req"}
Truth table0%0/2
A
A= $_->{"req"}
Aresult
11
00
141 0 $ac->{reqids} = $coldb->enumIds($ac->{enum},$ac->{req},logLevel=>$logProfile,logPrefix=>"profile(): get target $ac->{a}-values");
142 0 ▶ if (!@{$ac->{reqids}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$ac->{"reqids"};}) 0 0 0
143 0 $reldb->logwarn($coldb->{error}="profile(): no $ac->{a}-attribute values match user query '$ac->{req}'");
144 0 return undef;
145 }
146 }
147  
148 ##-- prepare: get tuple-ids (by attribute)
149 0 $reldb->vlog($logProfile, "profile(): get target tuple IDs");
150 0 my $tivec = undef;
151 0 my $nbits = undef;
152 0 my $pack_tv = undef;
153 0 my $test_tv = undef; ##-- test value via vec()
154 0 foreach $ac (grep {$_->{reqids}} @$adata) {
155 ##-- sanity checks
156 0 ▶ $nbits //= $ac->{a2t}{len_i}*8;
Condition0%0/2
$nbits //= $ac->{"a2t"}{"len_i"} * 8
l!l
00
MC/DC0%0/1
$nbits //= $ac->{"a2t"}{"len_i"} * 8
$nbits
Truth table0%0/2
A
A= $nbits
Aresult
11
00
157 0 ▶ $pack_tv //= "$ac->{a2t}{pack_i}*";
Condition0%0/2
$pack_tv //= qq[$ac->{"a2t"}{"pack_i"}*]
l!l
00
MC/DC0%0/1
$pack_tv //= qq[$ac->{"a2t"}{"pack_i"}*]
$pack_tv
Truth table0%0/2
A
A= $pack_tv
Aresult
11
00
158 0 ▶ vec($test_tv='',0,$nbits) = 0x12345678 if (!defined($test_tv));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $test_tv 0 0 0
159 $reldb->logconfess($coldb->{error}="profile(): multimap pack-size mismatch: nbits($ac->{a2t}{base}.*) != $nbits")
160 0 ▶ if ($ac->{a2t}{len_i} != $nbits/8);
Branch: 1 branch
BranchTrueFalseTotal
if $ac->{"a2t"}{"len_i"} != $nbits / 8 0 0 0
161 $reldb->logconfess($coldb->{error}="profile(): multimap pack-template '$ac->{a2t}{pack_i}' for $ac->{a2t}{base}.* is not big-endian")
162 0 ▶ if (pack($ac->{a2t}{pack_i},0x12345678) ne $test_tv);
Branch: 1 branch
BranchTrueFalseTotal
if pack($ac->{"a2t"}{"pack_i"}, 305419896) ne $test_tv 0 0 0
163  
164 ##-- target set construction
165 0 my $atiset = '';
166 0 $atiset = vunion($atiset, $ac->{a2t}->fetchraw($_), $nbits) foreach (@{$ac->{reqids}});
167 0 ▶ $tivec = defined($tivec) ? vintersect($tivec, $atiset, $nbits) : $atiset;
Branch: 1 branch
BranchTrueFalseTotal
defined $tivec ? : 0 0 0
168 }
169  
170 ##-- check maxExpand
171 0 ▶ $nbits //= packsize($coldb->{pack_id});
Condition0%0/3
$nbits //= packsize($coldb->{"pack_id"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$nbits //= packsize($coldb->{"pack_id"})
$nbits packsize($coldb->{"pack_id"})
Truth table0%0/3
A //= B
A= $nbitsB= packsize($coldb->{"pack_id"})
ABresult
1X1
011
000
172 0 ▶ my $ntis = $tivec ? length($tivec)/($nbits/8) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tivec ? : 0 0 0
173 0 ▶ if ($coldb->{maxExpand}>0 && $ntis > $coldb->{maxExpand}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coldb->{"maxExpand"} > 0 and $ntis > $coldb->{"maxExpand"}) 0 0 0
Condition0%0/3
$coldb->{"maxExpand"} > 0 and $ntis > $coldb->{"maxExpand"}
!ll&&!rl&&r
000
MC/DC0%0/2
$coldb->{"maxExpand"} > 0 and $ntis > $coldb->{"maxExpand"}
$coldb->{"maxExpand"} > 0 $ntis > $coldb->{"maxExpand"}
Truth table0%0/3
A and B
A= $coldb->{"maxExpand"} > 0B= $ntis > $coldb->{"maxExpand"}
ABresult
0X0
100
111
174 0 $reldb->logwarn("profile(): Warning: target set exceeds max expansion size ($ntis > $coldb->{maxExpand}): truncating");
175 0 substr($tivec, -($ntis - $coldb->{maxExpand})*($nbits/8)) = '';
176 }
177 0 ▶ my $tis = [$tivec ? unpack($pack_tv, $tivec) : qw()];
Branch: 1 branch
BranchTrueFalseTotal
$tivec ? : 0 0 0
178  
179 ##-- parse date request (no filtering here)
180 0 $reldb->vlog($logProfile, "profile(): parse date request (date=$opts{date}, slice=$opts{slice}, fill=$opts{fill})");
181 0 my $dreq = $opts{dreq} = $coldb->parseDateRequest(@opts{qw(date slice fill)});
182  
183 ##-- profile: get relation profiles (by date-slice, pass 1: f12)
184 0 ▶ my $onepass = $opts{onepass} || ($reldb->can('subprofile2') eq \&subprofile2);
Condition0%0/3
$opts{'onepass'} || $reldb->can('subprofile2') eq \&DiaColloDB::Relation::subprofile2
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'onepass'} || $reldb->can('subprofile2') eq \&DiaColloDB::Relation::subprofile2
$opts{'onepass'} $reldb->can('subprofile2') eq \&DiaColloDB::Relation::subprofile2
Truth table0%0/3
A || B
A= $opts{'onepass'}B= $reldb->can('subprofile2') eq \&DiaColloDB::Relation::subprofile2
ABresult
1X1
011
000
185 0 ▶ $reldb->vlog($logProfile, "profile(): get frequency profile(s): ".($onepass ? 'single-pass' : 'pass-1'));
Branch: 1 branch
BranchTrueFalseTotal
$onepass ? : 0 0 0
186 0 my $s2prf = $reldb->subprofile1($tis, \%opts);
187 0 foreach (keys %$s2prf) {
188 0 @{$s2prf->{$_}}{qw(label titles)} = ($_,$groupby->{titles});
189 }
190  
191 ##-- profile/extend: insert extension keys
192 0 my $extend = $opts{extend};
193 0 ▶ if ($extend) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extend) 0 0 0
194 0 my ($slice,$prf,$sxkeys);
195 0 ▶ while (($slice,$prf) = each %$s2prf) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$s2prf) 0 0 0
196 0 ▶ $sxkeys = $extend->{$slice}//{};
Condition0%0/2
$extend->{$slice} // {}
l!l
00
MC/DC0%0/1
$extend->{$slice} // {}
$extend->{$slice}
Truth table0%0/2
A
A= $extend->{$slice}
Aresult
11
00
197 0 ▶ $prf->{f12}{$_} //= 0 foreach (keys %$sxkeys);
Condition0%0/2
$prf->{"f12"}{$_} //= 0
l!l
00
MC/DC0%0/1
$prf->{"f12"}{$_} //= 0
$prf->{"f12"}{$_}
Truth table0%0/2
A
A= $prf->{"f12"}{$_}
Aresult
11
00
198 }
199 }
200  
201 ##-- profile: complete slice-wise profiles (pass 2: f2)
202 0 ▶ if (!$onepass || !$opts{onepass}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($onepass and $opts{"onepass"}) 0 0 0
Condition0%0/3
$onepass and $opts{"onepass"}
!ll&&!rl&&r
000
MC/DC0%0/2
$onepass and $opts{"onepass"}
$onepass $opts{"onepass"}
Truth table0%0/3
A and B
A= $onepassB= $opts{"onepass"}
ABresult
0X0
100
111
203 0 $reldb->vlog($logProfile, "profile(): get frequency profile(s): pass-2");
204 0 $reldb->subprofile2($s2prf, \%opts);
205 }
206  
207 ##-- compile & collect: multi-profile
208 0 foreach (values %$s2prf) {
209 0 $_->compile($opts{score}, eps=>$opts{eps});
210 }
211 0 my $mp = DiaColloDB::Profile::Multi->new(profiles=>[@$s2prf{sort {$a<=>$b} keys %$s2prf}],
212 titles=>$groupby->{titles},
213 0 qinfo =>$reldb->qinfo($coldb, %opts, qreqs=>$areqs, gbreq=>$groupby),
214 );
215  
216 ##-- trim and stringify
217 0 $reldb->vlog($logProfile, "profile(): trim and stringify");
218 0 $mp->trim(%opts, empty=>!$opts{fill});
219 0 ▶ if (!$opts{packed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"packed"}) 0 0 0
220 0 ▶ if ($opts{strings}//1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'strings'} // 1) { } 0 0 0
Condition0%0/2
$opts{"strings"} // 1
l!l
00
MC/DC0%0/1
$opts{"strings"} // 1
$opts{"strings"}
Truth table0%0/2
A
A= $opts{"strings"}
Aresult
11
00
221 0 $mp->stringify($groupby->{g2s});
222 } else {
223 0 $mp->stringify($groupby->{g2txt});
224 }
225 }
226  
227 ##-- return
228 0 return $mp;
229 }
230  
231 ##--------------------------------------------------------------
232 ## Relation API: extend (pass-2 for multi-clients)
233  
234 ## $mprf = $rel->extend($coldb, %opts)
235 ## + extend f12 and f2 frequencies for \%slice2keys = $opts{slice2keys}
236 ## + calls $rel->profile($coldb, %opts,extend=>\%slice2keys_packed)
237 ## + returns a DiaColloDB::Profile::Multi containing the appropriate f12 and f2 entries
238 0 ▶sub extend {
Subroutine extend: not called CC=8 SCAR=42.8
Pod documented
239 0 my ($reldb,$coldb,%opts) = @_;
240  
241 ##-- common variables
242 0 $opts{coldb} = $coldb; ##-- pass-down to subprofile() methods
243 0 my $logProfile = $coldb->{logProfile};
244  
245 ##-- sanity check(s)
246 0 ▶ my $slice2keys = $opts{slice2keys} || $opts{extend};
Condition0%0/3
$opts{'slice2keys'} || $opts{'extend'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'slice2keys'} || $opts{'extend'}
$opts{'slice2keys'} $opts{'extend'}
Truth table0%0/3
A || B
A= $opts{'slice2keys'}B= $opts{'extend'}
ABresult
1X1
011
000
247 0 ▶ if (!$slice2keys) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $slice2keys) { } 0 0 0
elsif (not UNIVERSAL::isa($slice2keys, 'HASH')) { } 0 0 0
248 0 $reldb->logwarn($coldb->{error}="extend(): no 'slice2keys' or 'extend' parameter specified!");
249 0 return undef;
250 }
251 elsif (!UNIVERSAL::isa($slice2keys,'HASH')) {
252 0 $reldb->logwarn($coldb->{error}="extend(): failed to parse 'slice2keys' or 'extend' parameter");
253 0 return undef;
254 }
255 0 delete $opts{slice2keys};
256  
257 ##-- get packed group-keys (avoid temporary dummy-profiles: they can't handle unknown group-components)
258 0 my $groupby = $opts{groupby} = $coldb->groupby($opts{groupby});
259 0 my $s2gx = $groupby->{s2gx};
260 0 my ($xslice,$xkeys, $xgkeys,$xkey,$xg, %extend);
261 0 ▶ while (($xslice,$xkeys) = each %$slice2keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$slice2keys) 0 0 0
262 0 $xgkeys = $extend{$xslice} = {};
263 0 ▶ foreach $xkey (UNIVERSAL::isa($xkeys,'HASH') ? keys(%$xkeys) : @$xkeys) {
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($xkeys, 'HASH') ? : 0 0 0
264 0 ▶ next if (!defined($xg = $s2gx->($xkey)));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($xg = &$s2gx($xkey)) 0 0 0
265 0 $xgkeys->{$xg} = undef;
266 }
267 }
268  
269 ##-- guts: dispatch to profile()
270 0 my $mp = $reldb->profile($coldb, %opts, kbest=>0,kbesta=>0,cutoff=>undef,global=>0,fill=>1, extend=>\%extend);
271  
272 0 return $mp;
273 }
274  
275 ##--------------------------------------------------------------
276 ## Relation API: comparison (diff)
277  
278 ## $mpdiff = $rel->compare($coldb, %opts)
279 ## + get a relation comparison profile for selected items as a DiaColloDB::Profile::MultiDiff object
280 ## + %opts:
281 ## (
282 ## ##-- selection parameters
283 ## (a|b)?query => $query, ##-- target query as for parseRequest()
284 ## (a|b)?date => $date1, ##-- string or array or range "MIN-MAX" (inclusive) : default=all
285 ## ##
286 ## ##-- aggregation parameters
287 ## groupby => $groupby, ##-- string or array "ATTR1[:HAVING1] ...": default=$coldb->attrs; see groupby() method
288 ## (a|b)?slice => $slice, ##-- date slice (default=1, 0 for global profile)
289 ## ##
290 ## ##-- scoring and trimming parameters
291 ## eps => $eps, ##-- smoothing constant (default=0)
292 ## score => $func, ##-- scoring function ("f"|"lf"|"fm"|"lfm"|"mi"|"ld"|"ll") : default="f"
293 ## kbest => $k, ##-- return only $k best collocates per date (slice) : default=-1:all
294 ## cutoff => $cutoff, ##-- minimum score
295 ## global => $bool, ##-- trim profiles globally (vs. locally for each date-slice?) (default=0)
296 ## diff => $diff, ##-- low-level score-diff operation (adiff|diff|sum|min|max|avg|havg); default='adiff'
297 ## ##
298 ## ##-- profiling and debugging parameters
299 ## strings => $bool, ##-- do/don't stringify item keys (default=do)
300 ## packed => $bool, ##-- leave item keys packed (override stringification; default=don't)
301 ## ##
302 ## ##-- sublcass abstraction parameters
303 ## _gbparse => $bool, ##-- if true (default), 'groupby' clause will be parsed only once, using $coldb->groupby() method
304 ## _abkeys => \@abkeys, ##-- additional key-suffixes KEY s.t. (KEY=>VAL) gets passed to profile() calls if e.g. (aKEY=>VAL) is in %opts
305 ## )
306 ## + default implementation wraps profile() method
307 ## + default values for %opts should be set by higher-level call, e.g. DiaColloDB::compare()
308 0 ▶sub compare {
Subroutine compare: not called CC=19 SCAR=59.4
Pod documented
309 0 my ($reldb,$coldb,%opts) = @_;
310  
311 ##-- common variables
312 0 my $logProfile = $coldb->{logProfile};
313 0 ▶ my $groupby = $opts{groupby} || [@{$coldb->attrs}];
Condition0%0/2
$opts{'groupby'} || [@{$coldb->attrs;}]
l!l
00
MC/DC0%0/1
$opts{'groupby'} || [@{$coldb->attrs;}]
$opts{'groupby'}
Truth table0%0/2
A
A= $opts{'groupby'}
Aresult
11
00
314 0 ▶ $groupby = $coldb->groupby($groupby) if ($opts{_gbparse}//1);
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"_gbparse"} // 1 0 0 0
Condition0%0/2
$opts{"_gbparse"} // 1
l!l
00
MC/DC0%0/1
$opts{"_gbparse"} // 1
$opts{"_gbparse"}
Truth table0%0/2
A
A= $opts{"_gbparse"}
Aresult
11
00
315 0 ▶ my %aopts = map {exists($opts{"a$_"}) ? ($_=>$opts{"a$_"}) : qw()} (qw(query date slice), @{$opts{_abkeys}//[]});
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{"a$_"} ? : 0 0 0
Condition0%0/2
$opts{"_abkeys"} // []
l!l
00
MC/DC0%0/1
$opts{"_abkeys"} // []
$opts{"_abkeys"}
Truth table0%0/2
A
A= $opts{"_abkeys"}
Aresult
11
00
316 0 ▶ my %bopts = map {exists($opts{"b$_"}) ? ($_=>$opts{"b$_"}) : qw()} (qw(query date slice), @{$opts{_abkeys}//[]});
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{"b$_"} ? : 0 0 0
Condition0%0/2
$opts{"_abkeys"} // []
l!l
00
MC/DC0%0/1
$opts{"_abkeys"} // []
$opts{"_abkeys"}
Truth table0%0/2
A
A= $opts{"_abkeys"}
Aresult
11
00
317 0 my %popts = (kbest=>-1,cutoff=>'',global=>0,strings=>0,packed=>1,fill=>1, groupby=>$groupby);
318  
319 ##-- get profiles to compare
320 0 ▶ my $mpa = $reldb->profile($coldb,%opts, %aopts,%popts) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $mpa = $reldb->profile($coldb, %opts, %aopts, %popts) or return undef 0 0 0
321 0 ▶ my $mpb = $reldb->profile($coldb,%opts, %bopts,%popts) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $mpb = $reldb->profile($coldb, %opts, %bopts, %popts) or return undef 0 0 0
322  
323 ##-- alignment and trimming
324 0 ▶ $reldb->vlog($logProfile, "compare(): align and trim (".($opts{global} ? 'global' : 'local').")");
Branch: 1 branch
BranchTrueFalseTotal
$opts{'global'} ? : 0 0 0
325 0 my $ppairs = DiaColloDB::Profile::MultiDiff->align($mpa,$mpb);
326 0 DiaColloDB::Profile::MultiDiff->trimPairs($ppairs, %opts);
327 0 my $diff = DiaColloDB::Profile::MultiDiff->new($mpa,$mpb, titles=>$mpa->{titles}, diff=>$opts{diff});
328 0 ▶ $diff->trim( DiaColloDB::Profile::Diff->diffkbest($opts{diff})=>$opts{kbest} ) if (!$opts{global});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"global"} 0 0 0
329  
330 ##-- finalize: stringify
331 0 ▶ if (!$opts{packed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"packed"}) 0 0 0
332 0 ▶ if ($opts{strings}//1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'strings'} // 1) { } 0 0 0
Condition0%0/2
$opts{"strings"} // 1
l!l
00
MC/DC0%0/1
$opts{"strings"} // 1
$opts{"strings"}
Truth table0%0/2
A
A= $opts{"strings"}
Aresult
11
00
333 $diff->stringify($groupby->{g2s}) if (ref($groupby) && $groupby->{g2s})
334 0 ▶ } else {
Branch: 1 branch
BranchTrueFalseTotal
if ref $groupby and $groupby->{"g2s"} 0 0 0
Condition0%0/3
ref $groupby and $groupby->{"g2s"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $groupby and $groupby->{"g2s"}
ref $groupby $groupby->{"g2s"}
Truth table0%0/3
A and B
A= ref $groupbyB= $groupby->{"g2s"}
ABresult
0X0
100
111
335 0 ▶ $diff->stringify($groupby->{g2txt}) if (ref($groupby) && $groupby->{g2txt});
Branch: 1 branch
BranchTrueFalseTotal
if ref $groupby and $groupby->{"g2txt"} 0 0 0
Condition0%0/3
ref $groupby and $groupby->{"g2txt"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $groupby and $groupby->{"g2txt"}
ref $groupby $groupby->{"g2txt"}
Truth table0%0/3
A and B
A= ref $groupbyB= $groupby->{"g2txt"}
ABresult
0X0
100
111
336 }
337 }
338  
339 0 return $diff;
340 }
341  
342 ## $mpdiff = $rel->diff($coldb, %opts)
343 ## + alias for compare()
344 0 ▶sub diff {
Subroutine diff: not called CC=1 SCAR=6.9
Pod documented
345 0 my $rel = shift;
346 0 return $rel->compare(@_);
347 }
348  
349  
350 ##==============================================================================
351 ## Relation API: default
352  
353 ##--------------------------------------------------------------
354 ## Relation API: default: sliceN
355  
356 ## $N = $rel->sliceN($sliceBy, $dateLo)
357 ## + get total slice-wise co-occurrence count for a slice of size $sliceBy starting at $dateLo
358 ## + not called by any default methods, but useful for sub-classes
359 ## + default implementation is really only appropriate for Cofreqs and Unigrams relations;
360 ## uses $rel properties 'N', 'sizeN', 'ymin', 'rN'
361 0 ▶sub sliceN {
Subroutine sliceN: not called CC=6 SCAR=37.4
Pod undocumented
362 0 my ($rel,$slice,$dlo) = @_;
363 0 ▶ return $rel->{N} if ($slice==0 || !UNIVERSAL::can($rel->{rN},'fetch'));
Branch: 1 branch
BranchTrueFalseTotal
if $slice == 0 or not UNIVERSAL::can($rel->{"rN"}, "fetch") 0 0 0
Condition0%0/3
$slice == 0 or not UNIVERSAL::can($rel->{"rN"}, "fetch")
l!l&&r!l&&!r
000
MC/DC0%0/2
$slice == 0 or not UNIVERSAL::can($rel->{"rN"}, "fetch")
$slice == 0 not UNIVERSAL::can($rel->{"rN"}, "fetch")
Truth table0%0/3
A or not(B)
A= $slice == 0B= not UNIVERSAL::can($rel->{"rN"}, "fetch")
ABresult
1X1
011
000
364 0 ▶ my $ymin = ($rel->{ymin}//0);
Condition0%0/2
$rel->{"ymin"} // 0
l!l
00
MC/DC0%0/1
$rel->{"ymin"} // 0
$rel->{"ymin"}
Truth table0%0/2
A
A= $rel->{"ymin"}
Aresult
11
00
365 0 my $rN = $rel->{rN};
366 0 ▶ my $ihi = min2( $dlo-$ymin+$slice, $rel->{sizeN}//$rN->size );
Condition0%0/3
$rel->{"sizeN"} // $rN->size
l!l&&r!l&&!r
000
MC/DC0%0/2
$rel->{"sizeN"} // $rN->size
$rel->{"sizeN"} $rN->size
Truth table0%0/3
A // B
A= $rel->{"sizeN"}B= $rN->size
ABresult
1X1
011
000
367 0 my $ilo = max2( $dlo-$ymin, 0);
368 0 my $N = 0;
369 0 ▶ for (my $i=$ilo; $i < $ihi; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ihi) 0 0 0
370 0 $N += $rN->fetch($i);
371 }
372 0 return $N;
373 }
374  
375 ##--------------------------------------------------------------
376 ## Relation API: default: subprofile1
377  
378 ## \%slice2prf = $rel->subprofile1(\@tids,\%opts)
379 ## + get slice-wise joint frequency profile(s) for \@tids (db must be opened)
380 ## + %opts: as for profile(), also:
381 ## coldb => $coldb, ##-- parent DiaColloDB object (for shared data, debugging)
382 ## a2data => \%a2data, ##-- maps indexed attributes to associated data structures
383 ## dreq => \%dreq, ##-- parsed date request
384 0 ▶sub subprofile1 {
Subroutine subprofile1: not called CC=1 SCAR=6.9
Pod documented
385 0 my ($rel,$tids,$opts) = @_;
386 0 $rel->logconfess($opts->{coldb}{error}="subprofile(): abstract method called");
387 }
388  
389 ##--------------------------------------------------------------
390 ## Relation API: default: subprofile2
391  
392 ## \%slice2prf = $rel->subprofile2(\%slice2prf,\%opts)
393 ## + populate f2 frequencies for profiles in \%slice2prf
394 ## + %opts: as for subprofile1()
395 ## + default implementation just returns \%slice2prf
396 0 ▶sub subprofile2 {
Subroutine subprofile2: not called CC=1 SCAR=6.9
Pod documented
397 #my ($rel,$slice2prf,$opts) = @_;
398 0 return $_[1];
399 }
400  
401 ##--------------------------------------------------------------
402 ## Relation API: default: qinfo
403  
404 ## \%qinfo = $rel->qinfo($coldb, %opts)
405 ## + get query-info hash for profile administrivia (ddc hit links)
406 ## + %opts: as for profile(), additionally:
407 ## (
408 ## qreqs => \@areqs, ##-- as returned by $coldb->parseRequest($opts{query})
409 ## gbreq => \%groupby, ##-- as returned by $coldb->groupby($opts{groupby})
410 ## )
411 ## + returned hash \%qinfo should have keys:
412 ## (
413 ## fcoef => $fcoef, ##-- frequency coefficient (2*$coldb->{dmax} for CoFreqs)
414 ## qtemplate => $qtemplate, ##-- query template with __W1.I1__ rsp __W2.I2__ replacing groupby fields
415 ## qcanon => $qcanon, ##-- canonical query string (after parsing)
416 ## )
417 0 ▶sub qinfo {
Subroutine qinfo: not called CC=1 SCAR=6.9
Pod documented
418 0 my ($rel,$coldb,%opts) = @_;
419 0 $rel->logconfess("qinfo(): abstract method called");
420 }
421  
422 ## (\@q1strs,\@q2strs,\@qxstrs,\@fstrs) = $rel->qinfoData($coldb,%opts)
423 ## + parses @opts{qw(qreqs gbreq)} into conditions on w1, w2 and metadata filters (for ddc linkup)
424 ## + call this from subclass qinfo() methods
425 0 ▶sub qinfoData {
Subroutine qinfoData: not called CC=9 SCAR=45.0
Pod documented
426 0 my ($rel,$coldb,%opts) = @_;
427 0 my (@q1strs,@q2strs,@qxstrs,@fstrs,$q,$q2);
428  
429 ##-- query clause
430 0 foreach (@{$opts{qreqs}}) {
431 0 $q = $coldb->attrQuery(@$_);
432 0 ▶ if (UNIVERSAL::isa($q,'DDC::Any::CQFilter')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($q, 'DDC::Any::CQFilter')) { } 0 0 0
elsif (defined $q and not UNIVERSAL::isa($q, 'DDC::Any::CQTokAny')) { } 0 0 0
433 0 push(@fstrs, $q->toString);
434 }
435 ▶ elsif (defined($q) && !UNIVERSAL::isa($q,'DDC::Any::CQTokAny')) {
Condition0%0/3
defined $q and not UNIVERSAL::isa($q, "DDC::Any::CQTokAny")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $q and not UNIVERSAL::isa($q, "DDC::Any::CQTokAny")
defined $q not UNIVERSAL::isa($q, "DDC::Any::CQTokAny")
Truth table0%0/3
A and not(B)
A= defined $qB= not UNIVERSAL::isa($q, "DDC::Any::CQTokAny")
ABresult
0X0
100
111
436 0 push(@q1strs, $q->toString);
437 }
438 }
439  
440 ##-- groupby clause
441 0 my $xi=1;
442 0 foreach (@{$opts{gbreq}{areqs}}) {
443 0 ▶ if ($_->[0] =~ /^doc\.(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->[0] =~ /^doc\.(.*)/) { } 0 0 0
444 0 push(@fstrs, DDC::Any::CQFHasField->new("$1","__W2.${xi}__")->toString);
445 }
446 else {
447 0 push(@q2strs, DDC::Any::CQTokExact->new($_->[0],"__W2.${xi}__")->toString);
448 }
449 0 ++$xi;
450 }
451  
452 ##-- common restrictions (trunk/2015-10-28: these are too expensive for large corpora (timeouts): ignore 'em
453 #push(@qxstrs, qq(\$p=/$coldb->{pgood}/)) if ($coldb->{pgood});
454 #push(@qxstrs, qq(\$=!/$coldb->{pbad}/)) if ($coldb->{pbad});
455  
456 ##-- utf8
457 0 foreach (@q1strs,@q2strs,@qxstrs,@fstrs) {
458 0 ▶ utf8::decode($_) if (!utf8::is_utf8($_));
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($_) 0 0 0
459 }
460  
461 0 return (\@q1strs,\@q2strs,\@qxstrs,\@fstrs);
462 }
463  
464 ## $qstr_or_undef = $rel->qcanon($coldb,%opts)
465 ## + returns "canonical" query string for %opts
466 ## + default implementation uses:
467 ## - $opts{qcanon} if defined
468 ## - $opts{qobj}->toStringFull() if available
469 ## - undef
470 0 ▶sub qcanon {
Subroutine qcanon: not called CC=5 SCAR=34.0
Pod undocumented
471 0 my ($rel,$coldb,%opts) = @_;
472 0 ▶ my $q = $opts{qcanon} // $opts{qobj};
Condition0%0/3
$opts{"qcanon"} // $opts{"qobj"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"qcanon"} // $opts{"qobj"}
$opts{"qcanon"} $opts{"qobj"}
Truth table0%0/3
A // B
A= $opts{"qcanon"}B= $opts{"qobj"}
ABresult
1X1
011
000
473 0 ▶ $q = $q->toStringFull if (ref($q) && UNIVERSAL::can($q,'toStringFull'));
Branch: 1 branch
BranchTrueFalseTotal
if ref $q and UNIVERSAL::can($q, "toStringFull") 0 0 0
Condition0%0/3
ref $q and UNIVERSAL::can($q, "toStringFull")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $q and UNIVERSAL::can($q, "toStringFull")
ref $q UNIVERSAL::can($q, "toStringFull")
Truth table0%0/3
A and B
A= ref $qB= UNIVERSAL::can($q, "toStringFull")
ABresult
0X0
100
111
474 0 ▶ utf8::decode($q) if (!utf8::is_utf8($q));
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($q) 0 0 0
475 0 return $q;
476 }
477  
478  
479 ##==============================================================================
480 ## Footer
481 1 1;
482  
483 __END__
« DiaColloDB/Upgrade/v0_10_x2t.pm ↑ Summary DiaColloDB/Corpus/Compiled.pm »