RDF/Trine/Store/LanguagePreference.pm

filter: Statementstmt 75.6% 115 / 152 Branchbran 70.5% 24 / 34 Conditioncond 47.0% 8 / 17 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 70.3% 19 / 27 Podpod 100% 13 / 13 totaltotal 71.5% 181 / 253 CC 26 SCAR 14.7
CC
26
Cov
72%
AUTOLOAD
12.0
get_statements
8.9
qvalueForLanguage
6.0
CRAP 4.3
« RDF/Trine/Node.pm ↑ Summary Test/RDF/Trine/Store.pm »
1 =encoding utf8
2  
3 =head1 NAME
4  
5 RDF::Trine::Store::LanguagePreference - RDF Store proxy for filtering language tagged literals
6  
7 =head1 VERSION
8  
9 This document describes RDF::Trine::Store::LanguagePreference version 1.019
10  
11 =head1 SYNOPSIS
12  
13 use RDF::Trine::Store::LanguagePreference;
14  
15 =head1 DESCRIPTION
16  
17 RDF::Trine::Store::LanguagePreference provides a RDF::Trine::Store API to
18 filter the statements made available from some underlying store object based
19 on a users' language preferences (e.g. coming from an Accept-Language HTTP
20 header value).
21  
22 =cut
23  
24 package RDF::Trine::Store::LanguagePreference;
25  
26 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 68 ▶use base qw(RDF::Trine::Store);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 68 ▶use List::Util qw(reduce max);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 68 ▶use Scalar::Util qw(refaddr reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 68 ▶use RDF::Trine::Iterator qw(sgrep);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 ######################################################################
37  
38 68 my @pos_names;
39 68 our $VERSION;
40 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
41 68 $VERSION = "1.019";
42 68 my $class = __PACKAGE__;
43 68 $RDF::Trine::Store::STORE_CLASSES{ $class } = $VERSION;
44 68 @pos_names = qw(subject predicate object context);
45 }
46  
47 ######################################################################
48  
49 =head1 METHODS
50  
51 Beyond the methods documented below, this class inherits methods from the
52 L<RDF::Trine::Store> class.
53  
54 =over 4
55  
56 =item C<< new ( $store, { $lang1 => $q1, $lang2 => $q2, ... } ) >>
57  
58 Returns a new storage object that will act as a proxy for the C<< $store >> object,
59 filtering language literals based on the expressed language preferences.
60  
61 =item C<new_with_config ( $hashref )>
62  
63 Returns a new storage object configured with a hashref with certain
64 keys as arguments.
65  
66 The C<storetype> key must be C<LanguagePreference> for this backend.
67  
68 The following key must also be used:
69  
70 =over
71  
72 =item C<store>
73  
74 A configuration hash for the underlying store object.
75  
76 =item C<preferred_languages>
77  
78 A hash reference mapping language tags to quality values in the range [0, 1].
79 The referent may be changed between operations to change the set of preferred
80 languages used in statement matching.
81  
82 =back
83  
84 =cut
85  
86 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
87 3 my $class = shift;
88 3 my $store = shift;
89 3 my $pref = shift;
90 3 my $self = bless({
91 store => $store,
92 preferred_languages => $pref,
93 }, $class);
94 3 return $self;
95 }
96  
97 =item C<< new_with_config ( \%config ) >>
98  
99 Returns a new RDF::Trine::Store object based on the supplied configuration hashref.
100  
101 =cut
102  
103 0 ▶sub new_with_config {
Subroutine new_with_config: not called CC=1 SCAR=6.9
Pod documented
104 0 my $proto = shift;
105 0 my $config = shift;
106 0 $config->{storetype} = 'LanguagePreference';
107 0 return $proto->SUPER::new_with_config( $config );
108 }
109  
110 0 ▶sub _new_with_config {
Subroutine _new_with_config: not called CC=1 SCAR=6.9
111 0 my $class = shift;
112 0 my $config = shift;
113 0 return $class->new( @{ $config }{ qw(store preferred_languages) } );
114 }
115  
116 0 ▶sub _config_meta {
Subroutine _config_meta: not called CC=1 SCAR=6.9
117 return {
118 0 required_keys => [qw(store preferred_languages)],
119 fields => {
120 store => { description => 'Store config', type => 'string' },
121 preferred_languages => { description => 'Preferred languages', type => 'hash' },
122 }
123 }
124 }
125  
126  
127 =item C<< language_preferences >>
128  
129 Returns a hash of the language preference quality values.
130  
131 =cut
132  
133 0 ▶sub language_preferences {
Subroutine language_preferences: not called CC=1 SCAR=6.9
Pod documented
134 0 my $self = shift;
135 0 return %{ $self->{preferred_languages} };
136 }
137  
138 =item C<< language_preference( $lang ) >>
139  
140 Return the quality value preference for the given language.
141  
142 =cut
143  
144 0 ▶sub language_preference {
Subroutine language_preference: not called CC=1 SCAR=6.9
Pod documented
145 0 my $self = shift;
146 0 my $lang = shift;
147 0 return $self->{preferred_languages}{$lang};
148 }
149  
150 =item C<< update_language_preference( $lang => $qvalue ) >>
151  
152 Update the quality value preference for the given language.
153  
154 =cut
155  
156 0 ▶sub update_language_preference {
Subroutine update_language_preference: not called CC=2 SCAR=17.9
Pod documented
157 0 my $self = shift;
158 0 my $lang = shift;
159 0 my $q = shift;
160 0 ▶ if ($q == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q == 0) { } 0 0 0
161 0 delete $self->{preferred_languages}{$lang};
162 } else {
163 0 $self->{preferred_languages}{$lang} = $q;
164 }
165 }
166  
167 =item C<< get_statements ( $subject, $predicate, $object [, $context] ) >>
168  
169 Returns a stream object of all statements matching the specified subject,
170 predicate and objects. Any of the arguments may be undef to match any value.
171  
172 =cut
173  
174 4 ▶sub get_statements {
Subroutine get_statements: called CC=8 SCAR=21.9
Pod documented
175 4 my $self = shift;
176 4 my @nodes = @_[0..3];
177 4 my $bound = 0;
178 4 my %bound;
179
180 4 my $use_quad = 0;
181 4 ▶ if (scalar(@_) >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 4) 1 3 4
182 1 my $g = $nodes[3];
183 1 ▶ if (blessed($g) and not($g->is_variable) and not($g->is_nil)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($g) and not $g->is_variable and not $g->is_nil) 0 1 1
Condition33%2/6
builtin::blessed($g) and not $g->is_variable and not $g->is_nil
!ll&&!rl&&r
100
builtin::blessed($g) and not $g->is_variable
!ll&&!rl&&r
100
MC/DC0%0/3
builtin::blessed($g) and not $g->is_variable and not $g->is_nil
builtin::blessed($g) not $g->is_variable not $g->is_nil
Truth table25%1/4
A and not(B) and not(C)
A= builtin::blessed($g)B= not $g->is_variableC= not $g->is_nil
ABCresult
0XX0
10X0
1100
1111
184 0 $use_quad = 1;
185 0 $bound++;
186 0 $bound{ 3 } = $g;
187 }
188 }
189
190 4 my @var_map = qw(s p o g);
191 4 my %var_map = map { $var_map[$_] => $_ } (0 .. $#var_map);
192 4 my @node_map;
193 4 foreach my $i (0 .. $#nodes) {
194 16 ▶ if (not(blessed($nodes[$i])) or $nodes[$i]->is_variable) {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($nodes[$i]) or $nodes[$i]->is_variable) 13 3 16
Condition66%2/3
not builtin::blessed($nodes[$i]) or $nodes[$i]->is_variable
l!l&&r!l&&!r
1303
MC/DC50%1/2
not builtin::blessed($nodes[$i]) or $nodes[$i]->is_variable
not builtin::blessed($nodes[$i]) $nodes[$i]->is_variable
Truth table66%2/3
not(A) or B
A= not builtin::blessed($nodes[$i])B= $nodes[$i]->is_variable
ABresult
1X1
011
000
195 13 $nodes[$i] = RDF::Trine::Node::Variable->new( $var_map[ $i ] );
196 }
197 }
198
199 4 my $cache = {};
200 4 my $iter = $self->{store}->get_statements(@nodes);
201 45 ▶ return RDF::Trine::Iterator::sgrep(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
202 45 return $self->languagePreferenceAllowsStatement($_, $cache);
203 4 }, $iter);
204 }
205  
206 =item C<< count_statements ( $subject, $predicate, $object, $context ) >>
207  
208 Returns a count of all the statements matching the specified subject,
209 predicate, object, and context. Any of the arguments may be undef to match any
210 value.
211  
212 =cut
213  
214 1 ▶sub count_statements {
Subroutine count_statements: called CC=2 SCAR=6.9
Pod documented
215 1 my $self = shift;
216 1 my $iter = $self->get_statements(@_);
217 1 my $count = 0;
218 1 ▶ while ($iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->next) 2 1 3
219 2 $count++;
220 }
221 1 return $count;
222 }
223  
224 =item C<< qvalueForLanguage ( $language, \%cache ) >>
225  
226 Returns the q-value for C<< $language >> based on the current language
227 preference. C<< %cache >> is used across multiple calls to this method for
228 performance reasons.
229  
230 =cut
231  
232 484 ▶sub qvalueForLanguage {
Subroutine qvalueForLanguage: called CC=6 SCAR=17.9
Pod documented
233 484 my $self = shift;
234 484 my $lang = shift;
235 484 ▶ my $cache = shift || {};
Condition50%1/2
shift() || {}
l!l
4840
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
236 484 ▶ if (exists $cache->{$lang}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cache->{$lang}) { } 440 44 484
237 440 return $cache->{$lang};
238 } else {
239 44 my %q;
240 44 foreach my $l (keys %{ $self->{preferred_languages} }) {
241 33 ▶ if ($lang =~ /^$l/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang =~ /^$l/) 3 30 33
242 3 my $q = $self->{preferred_languages}{$l};
243 3 $q{$l} = $q;
244 }
245 }
246 44 my $q;
247 44 ▶ if (scalar(@{ [ keys %q ] })) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{[keys %q];}) { } 3 41 44
248 0 my @keys = sort { length($b) <=> length($a) } keys %q;
249 3 $q = $q{$keys[0]};
250 } else {
251 41 $q = 0.001;
252 }
253 44 $cache->{$lang} = $q;
254 44 return $q;
255 }
256 }
257  
258 =item C<< siteQValueForLanguage ( $language ) >>
259  
260 Returns an implementation-specific q-value preference for the given
261 C<< $language >>. This method may be overridden by subclasses to control the
262 default preferred language.
263  
264 =cut
265  
266 484 ▶sub siteQValueForLanguage {
Subroutine siteQValueForLanguage: called CC=2 SCAR=6.9
Pod documented
267 484 my $self = shift;
268 484 my $lang = shift;
269 484 ▶ return ($lang =~ /^en/) ? 1.0 : 0.999;
Branch: 1 branch
BranchTrueFalseTotal
$lang =~ /^en/ ? : 44 440 484
270 }
271  
272 =item C<< availableLanguagesForStatement( $statement ) >>
273  
274 Returns a list of language tags that are available in the underlying store for
275 the given statement object. For example, if C<< $statement >> represented the
276 triple:
277  
278 dbpedia:Los_Angeles rdf:label "Los Angeles"@en
279  
280 and the underlying store contains the triples:
281  
282 dbpedia:Los_Angeles rdf:label "Los Angeles"@en
283 dbpedia:Los_Angeles rdf:label "ロサンゼルス"@ja
284 dbpedia:Los_Angeles rdf:label "Лос-Анджелес"@ru
285  
286 then the return value would be C<< ('en', 'ja', 'ru') >>.
287  
288 =cut
289  
290 44 ▶sub availableLanguagesForStatement {
Subroutine availableLanguagesForStatement: called CC=4 SCAR=14.0
Pod documented
291 44 my $self = shift;
292 44 my $st = shift;
293 44 my %languages;
294 44 my @nodes = $st->nodes;
295 44 $nodes[2] = undef;
296 44 my $iter = $self->{store}->get_statements(@nodes);
297 44 ▶ while (my $q = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $q = $iter->next) 484 44 528
298 484 my $object = $q->object;
299 484 ▶ if ($object->isa('RDF::Trine::Node::Literal') and $object->has_language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->isa("RDF::Trine::Node::Literal") and $object->has_language) 484 0 484
Condition33%1/3
$object->isa("RDF::Trine::Node::Literal") and $object->has_language
!ll&&!rl&&r
00484
MC/DC0%0/2
$object->isa("RDF::Trine::Node::Literal") and $object->has_language
$object->isa("RDF::Trine::Node::Literal") $object->has_language
Truth table33%1/3
A and B
A= $object->isa("RDF::Trine::Node::Literal")B= $object->has_language
ABresult
0X0
100
111
300 484 my $language = $object->literal_value_language;
301 484 $languages{$language}++;
302 }
303 }
304 44 return keys %languages;
305 }
306  
307 =item C<< languagePreferenceAllowsStatement ( $statement, \%cache ) >>
308  
309 Returns true if the C<< $statement >> is allowed by the current language
310 preference. C<< %cache >> is used across multiple calls to this method for
311 performance reasons.
312  
313 =cut
314  
315 45 ▶sub languagePreferenceAllowsStatement {
Subroutine languagePreferenceAllowsStatement: called CC=3 SCAR=11.0
Pod documented
316 45 my $self = shift;
317 45 my $st = shift;
318 45 my $cache = shift;
319 45 my $object = $st->object;
320 45 ▶ if ($object->isa('RDF::Trine::Node::Literal') and $object->has_language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->isa('RDF::Trine::Node::Literal') and $object->has_language) { } 44 1 45
Condition66%2/3
$object->isa("RDF::Trine::Node::Literal") and $object->has_language
!ll&&!rl&&r
1044
MC/DC50%1/2
$object->isa("RDF::Trine::Node::Literal") and $object->has_language
$object->isa("RDF::Trine::Node::Literal") $object->has_language
Truth table66%2/3
A and B
A= $object->isa("RDF::Trine::Node::Literal")B= $object->has_language
ABresult
0X0
100
111
321 44 my $language = $object->literal_value_language;
322 44 my @availableLanguages = $self->availableLanguagesForStatement($st);
323 44 my %availableValues = map { $_ => $self->qvalueForLanguage($_, $cache) * $self->siteQValueForLanguage($_) } @availableLanguages;
324 44 ▶ my $prefLang = reduce { $availableValues{$a} > $availableValues{$b} ? $a : $b } keys %availableValues;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$availableValues{$a} > $availableValues{$b} ? : 202 238 440
325 44 return ($prefLang eq $language);
326 } else {
327 1 return 1;
328 }
329 }
330  
331  
332 =item C<< supports ( [ $feature ] ) >>
333  
334 If C<< $feature >> is specified, returns true if the feature is supported by the
335 store, false otherwise. If C<< $feature >> is not specified, returns a list of
336 supported features.
337  
338 =cut
339  
340 0 ▶sub supports {
Subroutine supports: not called CC=1 SCAR=6.9
Pod documented
341 0 my $self = shift;
342 0 return;
343 }
344  
345 =begin private
346  
347 =item C<< can >>
348  
349 Delegating implementation.
350  
351 =end private
352  
353 =cut
354  
355 2 ▶sub can {
Subroutine can: called CC=3 SCAR=11.4
Pod documented
356 2 my $proto = shift;
357 2 my $name = shift;
358 2 my %methods = map { $_ => 1 } qw(new new_with_config _new_with_config get_statements count_statements);
359 2 ▶ return 1 if exists $methods{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $methods{$name} 0 2 2
360 2 ▶ if (ref($proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) { } 2 0 2
361 2 return $proto->{store}->can($name);
362 } else {
363 0 return;
364 }
365 }
366  
367 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
368 0 my $self = shift;
369 0 our $AUTOLOAD;
370 0 ▶ return if ($AUTOLOAD =~ /:DESTROY$/);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Store::LanguagePreference::AUTOLOAD =~ /:DESTROY$/ 0 0 0
371 0 my ($name) = ($AUTOLOAD =~ m/^.*:(.*)$/);
372 0 my $store = $self->{store};
373 0 ▶ unless ($store->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->can($name)) 0 0 0
374 0 my $class = ref($store);
375 0 Carp::confess qq[Can't locate object method "$name" via package "$class"];
376 }
377 0 return $store->$name(@_);
378 }
379  
380 68 1;
381  
382 __END__
« RDF/Trine/Node.pm ↑ Summary Test/RDF/Trine/Store.pm »