lib/UR/BoolExpr/Template.pm

filter: Statementstmt 38.8% 123 / 317 Branchbran 23.8% 31 / 130 Conditioncond 14.6% 11 / 75 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 48.6% 18 / 37 Podpod 5.0% 1 / 20 totaltotal 29.5% 184 / 622 CC 99 SCAR 43.4
CC
99
Cov
32%
_is_subset_of_limit_offset
210.0
legacy_params_hash
179.1
is_subset_of
132.0
CRAP 76.9
« blib/lib/UR/Object/View/Lister/Text.pm ↑ Summary blib/lib/UR/Namespace/Command/Update/Doc.pm »
1  
2 =head1 NAME
3  
4 UR::BoolExpr::Template - a UR::BoolExpr minus specific values
5  
6 =head1 SYNOPSIS
7  
8 =head1 DESCRIPTION
9  
10 =cut
11  
12 package UR::BoolExpr::Template;
13  
14 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 266 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 266 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 266 ▶use UR;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 266 our @CARP_NOT = qw(UR::BoolExpr);
22  
23 # readable stringification
24 266 ▶use overload ('""' => 'id');
Subroutine BEGIN: called CC=1 SCAR=0.0
25 0 ▶use overload ('==' => sub { $_[0] . '' eq $_[1] . '' } );
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
26 0 ▶use overload ('eq' => sub { $_[0] . '' eq $_[1] . '' } );
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
27  
28 266 UR::Object::Type->define(
29 class_name => __PACKAGE__,
30 is_transactional => 0,
31 composite_id_separator => '/',
32 id_by => [
33 subject_class_name => { is => 'Text' },
34 logic_type => { is => 'Text' },
35 logic_detail => { is => 'Text' },
36 constant_value_id => { is => 'Text' }
37 ],
38 has => [
39 is_normalized => { is => 'Boolean' },
40 is_id_only => { is => 'Boolean' },
41 is_partial_id => { is => 'Boolean' }, # True if at least 1, but not all the ID props are mentioned
42 is_unique => { is => 'Boolean' },
43
44 matches_all => { is => 'Boolean' },
45 key_op_hash => { is => 'HASH' },
46 id_position => { is => 'Integer' },
47 normalized_id => { is => 'Text' },
48 normalized_positions_arrayref => { is => 'ARRAY' },
49 normalization_extender_arrayref => { is => 'ARRAY' },
50 _property_meta_hash => { is => 'HASH' },
51 _property_names_arrayref => { is => 'ARRAY' },
52 num_values => { is => 'Integer' },
53 _ambiguous_keys => { is => 'ARRAY' },
54
55 _keys => { is => 'ARRAY' },
56 _constant_values => { is => 'ARRAY' },
57 ],
58 has_optional => [
59 hints => { is => 'ARRAY' },
60 recursion_desc => { is => 'ARRAY' },
61 order_by => { is => 'ARRAY' },
62 group_by => { is => 'ARRAY' },
63 aggregate => { is => 'ARRAY' },
64 limit => { is => 'Integer' },
65 offset => { is => 'Integer' },
66 ]
67 );
68  
69 266 our $VERSION = "0.47"; # UR $VERSION;
70  
71 # Borrow from the util package.
72 # This will go away with refactoring.
73  
74 266 our $id_sep = $UR::BoolExpr::Util::id_sep;
75 266 our $record_sep = $UR::BoolExpr::Util::record_sep;
76 266 our $unit_sep = $UR::BoolExpr::Util::unit_sep;
77 266 our $null_value = $UR::BoolExpr::Util::null_value;
78 266 our $empty_string = $UR::BoolExpr::Util::empty_string;
79 266 our $empty_list = $UR::BoolExpr::Util::empty_list;
80  
81 # Names of the optional flags you can add to a rule
82 266 our @meta_param_names = qw(recursion_desc hints order_by group_by aggregate limit offset);
83  
84 # Wrappers for regular properties
85  
86 0 ▶sub _property_names {
Subroutine _property_names: not called CC=1 SCAR=6.9
87 0 return @{ $_[0]->{_property_names_arrayref} };
88 }
89  
90 # Indexability methods
91  
92 0 ▶sub _indexable_property_names {
Subroutine _indexable_property_names: not called CC=2 SCAR=17.9
93 0 ▶ $_[0]->_resolve_indexing_params unless $_[0]->{_resolve_indexing_params};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"_resolve_indexing_params"} 0 0 0
94 0 @{ $_[0]->{_indexable_property_names} }
95 }
96  
97 0 ▶sub _indexable_property_positions {
Subroutine _indexable_property_positions: not called CC=2 SCAR=17.9
98 0 ▶ $_[0]->_resolve_indexing_params unless $_[0]->{_resolve_indexing_params};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"_resolve_indexing_params"} 0 0 0
99 0 @{ $_[0]->{_indexable_property_positions} }
100 }
101  
102 0 ▶sub _is_fully_indexable {
Subroutine _is_fully_indexable: not called CC=2 SCAR=17.9
103 0 ▶ $_[0]->_resolve_indexing_params unless $_[0]->{_resolve_indexing_params};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"_resolve_indexing_params"} 0 0 0
104 0 $_[0]->{_is_fully_indexable};
105 }
106  
107 0 ▶sub _resolve_indexing_params {
Subroutine _resolve_indexing_params: not called CC=3 SCAR=24.8
108 0 my $self = $_[0];
109  
110 0 my $class_meta = UR::Object::Type->get($self->subject_class_name);
111  
112 0 my @all_names = $self->_property_names;
113  
114 0 for my $name (@all_names) {
115 0 my $m = $class_meta->property($name);
116 0 ▶ unless ($m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($m) 0 0 0
117 #$DB::single = 1;
118 0 $class_meta->property($name);
119 #$DB::single = 1;
120 0 $class_meta->property($name);
121 }
122 }
123
124 my @indexable_names =
125 sort
126 0 map { $_->property_name }
127 0 grep { $_ } #and $_->is_indexable }
128 0 map { $class_meta->property_meta_for_name($_) }
129 @all_names;
130
131 my @indexable_positions
132 0 = UR::Util::positions_of_values(\@all_names,\@indexable_names);
133
134 0 $self->{_indexable_property_names} = \@indexable_names;
135 0 $self->{_indexable_property_positions} = \@indexable_positions;
136 0 $self->{_is_fully_indexable} = (@indexable_names == @all_names);
137
138 0 return 1;
139 }
140  
141 # Return true if this rule template's parameters is a subset of the other's parameters
142 # Returns 0 if this rule specifies a parameter not in the other template
143 # Returns undef if all the properties match, but their operators do not, meaning that
144 # we do not know if an object evaluated as true under one rule's template would also be in the other
145 0 ▶sub is_subset_of {
Subroutine is_subset_of: not called CC=11 SCAR=48.8
Pod undocumented
146 0 my($self,$other_template) = @_;
147  
148 0 my $other_template_id = $other_template->id;
149 0 ▶ my $cached_subset_data = $self->{'__cache'}->{'is_subset_of'} ||= {};
Condition0%0/2
$self->{"__cache"}{"is_subset_of"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"__cache"}{"is_subset_of"} ||= {}
$self->{"__cache"}{"is_subset_of"}
Truth table0%0/2
A
A= $self->{"__cache"}{"is_subset_of"}
Aresult
11
00
150 0 ▶ if (exists $cached_subset_data->{$other_template_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cached_subset_data->{$other_template_id}) 0 0 0
151 0 return $cached_subset_data->{$other_template_id};
152 }
153  
154 0 ▶ unless (ref($other_template) and $self->isa(ref $other_template)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $other_template and $self->isa(ref $other_template)) 0 0 0
Condition0%0/3
ref $other_template and $self->isa(ref $other_template)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $other_template and $self->isa(ref $other_template)
ref $other_template $self->isa(ref $other_template)
Truth table0%0/3
A and B
A= ref $other_templateB= $self->isa(ref $other_template)
ABresult
0X0
100
111
155 0 $cached_subset_data->{$other_template_id} = 0;
156 0 return 0;
157 }
158  
159 0 my $my_class = $self->subject_class_name;
160 0 my $other_class = $other_template->subject_class_name;
161 0 ▶ unless ($my_class eq $other_class or $my_class->isa($other_class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($my_class eq $other_class or $my_class->isa($other_class)) 0 0 0
Condition0%0/3
$my_class eq $other_class or $my_class->isa($other_class)
l!l&&r!l&&!r
000
MC/DC0%0/2
$my_class eq $other_class or $my_class->isa($other_class)
$my_class eq $other_class $my_class->isa($other_class)
Truth table0%0/3
A or B
A= $my_class eq $other_classB= $my_class->isa($other_class)
ABresult
1X1
011
000
162 0 $cached_subset_data->{$other_template_id} = undef;
163 0 return;
164 }
165  
166 0 my %operators = map { $_ => $self->operator_for($_) } $self->_property_names;
167 0 my $is_subset = 1;
168 0 foreach my $prop ( $other_template->_property_names ) {
169 0 ▶ unless (exists $operators{$prop}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $operators{$prop}) 0 0 0
170 0 $is_subset = 0;
171 0 last;
172 }
173 0 ▶ $is_subset = undef if ($operators{$prop} ne $other_template->operator_for($prop));
Branch: 1 branch
BranchTrueFalseTotal
if $operators{$prop} ne $other_template->operator_for($prop) 0 0 0
174 }
175  
176 0 ▶ if ($is_subset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_subset) 0 0 0
177 0 $is_subset = $self->_is_subset_of_limit_offset($other_template);
178 }
179  
180 0 return $cached_subset_data->{$other_template_id} = $is_subset;
181 }
182  
183 0 ▶sub _is_subset_of_limit_offset {
Subroutine _is_subset_of_limit_offset: not called CC=14 SCAR=53.5
184 0 my($self, $other_template) = @_;
185  
186 0 ▶ return 1 unless ($self->offset or defined($self->limit)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->offset or defined $self->limit or $other_template->offset or defined $other_template->limit 0 0 0
Condition0%0/9
$self->offset or defined $self->limit or $other_template->offset or defined $other_template->limit
l!l&&r!l&&!r
000
$self->offset or defined $self->limit or $other_template->offset
l!l&&r!l&&!r
000
$self->offset or defined $self->limit
l!l&&r!l&&!r
000
MC/DC0%0/4
$self->offset or defined $self->limit or $other_template->offset or defined $other_template->limit
$self->offset defined $self->limit $other_template->offset defined $other_template->limit
Truth table0%0/5
A or B or C or D
A= $self->offsetB= defined $self->limitC= $other_template->offsetD= defined $other_template->limit
ABCDresult
1XXX1
01XX1
001X1
00011
00000
187 or $other_template->offset or defined($other_template->limit));
188  
189 # need to do a more comprehensive filter match. If one or both templates
190 # has -limit and/or -offset, then the filters on both templates must match
191 # exactly. Otherwise, one result set could include objects that were
192 # skipped because of the other's offset or limit
193 0 my @my_filters = map { $_ . $self->operator_for($_) } $self->_property_names;
194 0 my @other_filters = map { $_ . $other_template->operator_for($_) } $other_template->_property_names;
195 0 my($both, $only_my, $only_other) = UR::Util::intersect_lists(\@my_filters, \@other_filters);
196 0 ▶ return undef if (@$only_my or @$only_other);
Branch: 1 branch
BranchTrueFalseTotal
if @$only_my or @$only_other 0 0 0
Condition0%0/3
@$only_my or @$only_other
l!l&&r!l&&!r
000
MC/DC0%0/2
@$only_my or @$only_other
@$only_my @$only_other
Truth table0%0/3
A or B
A= @$only_myB= @$only_other
ABresult
1X1
011
000
197  
198 0 ▶ my $my_offset = $self->offset || 0;
Condition0%0/2
$self->offset || 0
l!l
00
MC/DC0%0/1
$self->offset || 0
$self->offset
Truth table0%0/2
A
A= $self->offset
Aresult
11
00
199 0 my $my_limit = $self->limit;
200 0 ▶ my $other_offset = $other_template->offset || 0;
Condition0%0/2
$other_template->offset || 0
l!l
00
MC/DC0%0/1
$other_template->offset || 0
$other_template->offset
Truth table0%0/2
A
A= $other_template->offset
Aresult
11
00
201 0 my $other_limit = $other_template->limit;
202  
203 0 my $is_subset;
204 0 ▶ if (defined($my_limit) and defined($other_limit)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $my_limit and defined $other_limit) { } 0 0 0
elsif (not defined $my_limit and defined $other_limit) { } 0 0 0
Condition0%0/3
defined $my_limit and defined $other_limit
!ll&&!rl&&r
000
MC/DC0%0/2
defined $my_limit and defined $other_limit
defined $my_limit defined $other_limit
Truth table0%0/3
A and B
A= defined $my_limitB= defined $other_limit
ABresult
0X0
100
111
205 0 my $my_last = $my_offset + $my_limit;
206 0 my $other_last = $other_offset + $other_limit;
207  
208 0 ▶ $is_subset = ($my_offset >= $other_offset) && ($my_last <= $other_last);
Condition0%0/3
$my_offset >= $other_offset && $my_last <= $other_last
!ll&&!rl&&r
000
MC/DC0%0/2
$my_offset >= $other_offset && $my_last <= $other_last
$my_offset >= $other_offset $my_last <= $other_last
Truth table0%0/3
A && B
A= $my_offset >= $other_offsetB= $my_last <= $other_last
ABresult
0X0
100
111
209  
210 ▶ } elsif (!defined($my_limit) and defined($other_limit)) {
Condition0%0/3
not defined $my_limit and defined $other_limit
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $my_limit and defined $other_limit
not defined $my_limit defined $other_limit
Truth table0%0/3
not(A) and B
A= not defined $my_limitB= defined $other_limit
ABresult
0X0
100
111
211 0 $is_subset = 0;
212  
213 } else {
214 0 $is_subset = $my_offset >= $other_offset;
215 }
216 0 return $is_subset;
217 }
218  
219  
220 # This is set lazily currently
221  
222 798 ▶sub is_unique {
Subroutine is_unique: called CC=12 SCAR=40.5
Pod undocumented
223 798 my $self = $_[0];
224 798 ▶ if (defined $self->{is_unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"is_unique"}) 532 266 798
225 return $self->{is_unique}
226 532 }
227  
228 # since this requires normalization, we don't set the value at construction time
229 266 my $normalized_self;
230 266 ▶ if ($self->is_normalized) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_normalized) { } 266 0 266
231 266 $normalized_self = $self;
232 }
233 else {
234 0 $normalized_self = $self->get_normalized_template_equivalent($self);
235 }
236  
237 266 my $op = $normalized_self->operator_for('id');
238 266 ▶ if (defined($op) and ($op eq '' or $op eq '=')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $op and $op eq '' || $op eq '=') { } 266 0 266
Condition33%2/6
defined $op and $op eq "" || $op eq "="
!ll&&!rl&&r
00266
$op eq "" or $op eq "="
l!l&&r!l&&!r
02660
MC/DC0%0/3
defined $op and $op eq "" || $op eq "="
defined $op $op eq "" $op eq "="
Truth table25%1/4
A and B or C
A= defined $opB= $op eq ""C= $op eq "="
ABCresult
0XX0
1000
11X1
1011
239 266 return $self->{is_unique} = 1;
240 }
241 else {
242 0 $self->{is_unique} = 0;
243
244 # if some combination of params can combine to
245 # satisfy at least one unique constraint,
246 # then we have uniqueness in the parameters.
247  
248 0 ▶ if (my @ps = $self->subject_class_name->__meta__->unique_property_sets) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ps) = $self->subject_class_name->__meta__->unique_property_sets) 0 0 0
249 0 my $property_meta_hash = $self->_property_meta_hash;
250 0 for my $property_set (@ps)
251 {
252 0 ▶ my $property_set = (ref($property_set) ? $property_set : [$property_set]);
Branch: 1 branch
BranchTrueFalseTotal
ref $property_set ? : 0 0 0
253 my @properties_used_from_constraint =
254 0 grep { defined($_) }
255 0 @$property_meta_hash{@$property_set};
256
257 0 ▶ if (@properties_used_from_constraint == @$property_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (@properties_used_from_constraint == @$property_set) { } 0 0 0
258 # filter imprecise operators
259 @properties_used_from_constraint =
260 grep {
261 0 $_->{operator} !~ /^(not |)like(-.|)$/i
262 and
263 0 ▶ $_->{operator} !~ /^(not |)in/i
Condition0%0/3
$_->{'operator'} =~ /^(not |)like(-.|)$/i || $_->{'operator'} =~ /^(not |)in/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{'operator'} =~ /^(not |)like(-.|)$/i || $_->{'operator'} =~ /^(not |)in/i
$_->{'operator'} =~ /^(not |)like(-.|)$/i $_->{'operator'} =~ /^(not |)in/i
Truth table0%0/3
A || B
A= $_->{'operator'} =~ /^(not |)like(-.|)$/iB= $_->{'operator'} =~ /^(not |)in/i
ABresult
1X1
011
000
264 }
265 @properties_used_from_constraint;
266
267 0 ▶ if (@properties_used_from_constraint == @$property_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (@properties_used_from_constraint == @$property_set) { } 0 0 0
268 0 $self->{is_unique} = 1;
269 0 last;
270 }
271 else {
272 ## print "some properties use bad operators: @properties_used_from_constraint\n";
273 }
274 }
275 else {
276 ## print "too few properties in @properties_used_from_constraint\n";
277 }
278 }
279 }
280  
281 0 return $self->{is_unique};
282 }
283 }
284  
285  
286 # Derivative of the ID.
287  
288 0 ▶sub rule_template_subclass_name {
Subroutine rule_template_subclass_name: not called CC=1 SCAR=6.9
Pod undocumented
289 0 return "UR::BoolExpr::Template::" . shift->logic_type;
290 }
291  
292 532 ▶sub get_normalized_template_equivalent {
Subroutine get_normalized_template_equivalent: called CC=1 SCAR=0.0
Pod undocumented
293 532 UR::BoolExpr::Template->get($_[0]->{normalized_id});
294 }
295  
296 532 ▶sub get_rule_for_values {
Subroutine get_rule_for_values: called CC=1 SCAR=0.0
Pod undocumented
297 532 my $self = shift;
298 532 my $value_id = UR::BoolExpr::Util::values_to_value_id(@_);
299 532 my $rule_id = UR::BoolExpr->__meta__->resolve_composite_id_from_ordered_values($self->id,$value_id);
300 532 my $r = UR::BoolExpr->get($rule_id);
301 #
302 # # FIXME - Don't do this part if the operator is 'in' or 'between'
303 # for (my $i = 0; $i < @_; $i++) {
304 # if (ref($_[$i]) and ! Scalar::Util::blessed($_[$i])) {
305 # $r->{'hard_refs'}->{$i} = $_[$i];
306 # }
307 # }
308 532 return $r;
309 }
310  
311 0 ▶sub get_rule_for_value_id {
Subroutine get_rule_for_value_id: not called CC=1 SCAR=6.9
Pod undocumented
312 0 my $self = shift;
313 0 my $value_id = shift;
314  
315 0 my $rule_id = UR::BoolExpr->__meta__->resolve_composite_id_from_ordered_values($self->id,$value_id);
316 0 return UR::BoolExpr->get($rule_id);
317 }
318  
319 0 ▶sub extend_params_list_for_values {
Subroutine extend_params_list_for_values: not called CC=5 SCAR=34.0
Pod undocumented
320 0 my $self = shift;
321 #my @prev = @_;
322 0 my $extenders = $self->normalization_extender_arrayref;
323 0 ▶ if (@$extenders) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$extenders) 0 0 0
324 0 my @result;
325 0 my $subject_class = $self->subject_class_name->__meta__;
326 0 for my $n (0 .. @$extenders-1) {
327 0 my $extender = $extenders->[$n];
328 0 my ($input_positions_arrayref,$subref,@more_keys) = @$extender;
329 0 my @more_values = @_[@$input_positions_arrayref];
330 0 ▶ if ($subref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subref) 0 0 0
331 ## print "calling $subref on \n\t" . join("\n\t",@more_values) . "\n";
332 0 @more_values = $subject_class->$subref(@more_values);
333 ## print "got: \n\t" . join("\n\t",@more_values) . "\n";
334 }
335 0 ▶ while (@more_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@more_keys) 0 0 0
336 0 my $k = shift @more_keys;
337 0 my $v = shift @more_values;
338 0 push @result, $k => $v;
339 }
340 }
341 0 return @result;
342 }
343 0 return ();
344 }
345  
346 532 ▶sub get_normalized_rule_for_values {
Subroutine get_normalized_rule_for_values: called CC=5 SCAR=16.5
Pod undocumented
347 532 my $self = shift;
348 532 my @unnormalized_values = @_;
349  
350 532 ▶ if ($self->is_normalized) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_normalized) 0 532 532
351 0 return $self->get_rule_for_values(@unnormalized_values);
352 }
353  
354 532 my $normalized_rule_template = $self->get_normalized_template_equivalent;
355  
356 # The normalized rule set may have more values than were actually
357 # passed-in. These 'extenders' will add to the @values array
358 # before re-ordering it.
359 532 my $extenders = $self->normalization_extender_arrayref;
360 532 ▶ if (@$extenders) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$extenders) 532 0 532
361 532 my $subject_class = $self->subject_class_name->__meta__;
362 532 for my $extender (@$extenders) {
363 532 my ($input_positions_arrayref,$subref) = @$extender;
364 532 my @more_values = @unnormalized_values[@$input_positions_arrayref];
365 532 ▶ if ($subref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subref) 0 532 532
366 ## print "calling $subref on \n\t" . join("\n\t",@more_values) . "\n";
367 0 @more_values = $subject_class->$subref(@more_values);
368 ## print "got: \n\t" . join("\n\t",@more_values) . "\n";
369 }
370 532 push @unnormalized_values, @more_values;
371 }
372 }
373
374 # Normalize the values. Since the normalized template may have added properties,
375 # and a different order we may need to re-order and expand the values list.
376 532 my $normalized_positions_arrayref = $self->normalized_positions_arrayref;
377 532 my @normalized_values = @unnormalized_values[@$normalized_positions_arrayref];
378  
379 532 my $rule = $normalized_rule_template->get_rule_for_values(@normalized_values);
380 532 return $rule;
381 }
382  
383 0 ▶sub _normalize_non_ur_values_hash {
Subroutine _normalize_non_ur_values_hash: not called CC=4 SCAR=30.0
384 0 my ($self,$unnormalized) = @_;
385 0 my %normalized;
386 0 ▶ if ($self->subject_class_name ne 'UR::Object::Property') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->subject_class_name ne "UR::Object::Property") 0 0 0
387 0 my $normalized_positions_arrayref = $self->normalized_positions_arrayref;
388 0 my @reordered_values = @$unnormalized{@$normalized_positions_arrayref};
389 0 ▶ for (my $n = 0; $n < @reordered_values; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @reordered_values) 0 0 0
390 0 my $value = $reordered_values[$n];
391 0 ▶ $normalized{$n} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
392 }
393 }
394 0 return \%normalized;
395 }
396  
397  
398 0 ▶sub value_position_for_property_name {
Subroutine value_position_for_property_name: not called CC=2 SCAR=17.9
Pod undocumented
399 0 ▶ if (exists $_[0]{_property_meta_hash}{$_[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[0]{'_property_meta_hash'}{$_[1]}) { } 0 0 0
400 0 return $_[0]{_property_meta_hash}{$_[1]}{value_position};
401 } else {
402 0 return undef;
403 }
404 }
405  
406 266 ▶sub operator_for {
Subroutine operator_for: called CC=3 SCAR=13.1
Pod undocumented
407 266 ▶ if (exists $_[0]{_property_meta_hash}{$_[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[0]{'_property_meta_hash'}{$_[1]}) { } 266 0 266
408 266 ▶ return $_[0]{_property_meta_hash}{$_[1]}{operator} || '=';
Condition50%1/2
$_[0]{'_property_meta_hash'}{$_[1]}{'operator'} || '='
l!l
0266
MC/DC0%0/1
$_[0]{'_property_meta_hash'}{$_[1]}{'operator'} || '='
$_[0]{'_property_meta_hash'}{$_[1]}{'operator'}
Truth table50%1/2
A
A= $_[0]{'_property_meta_hash'}{$_[1]}{'operator'}
Aresult
11
00
409 } else {
410 0 return undef;
411 }
412 }
413  
414 0 ▶sub operators_for_properties {
Subroutine operators_for_properties: not called CC=2 SCAR=17.9
Pod undocumented
415 0 ▶ my %properties = map { $_ => $_[0]->{'_property_meta_hash'}->{$_}->{'operator'} || '=' }
Condition0%0/2
$_[0]{'_property_meta_hash'}{$_}{'operator'} || '='
l!l
00
MC/DC0%0/1
$_[0]{'_property_meta_hash'}{$_}{'operator'} || '='
$_[0]{'_property_meta_hash'}{$_}{'operator'}
Truth table0%0/2
A
A= $_[0]{'_property_meta_hash'}{$_}{'operator'}
Aresult
11
00
416 0 @{ $_[0]->{'_property_names_arrayref'} };
417 0 return \%properties;
418 }
419  
420 0 ▶sub add_filter {
Subroutine add_filter: not called CC=3 SCAR=24.8
Pod undocumented
421 0 my $self = shift;
422 0 my $property_name = shift;
423 0 my $op = shift;
424 0 my $new_key = $property_name;
425 0 ▶ $new_key .= ' ' . $op if defined $op;
Branch: 1 branch
BranchTrueFalseTotal
if defined $op 0 0 0
426 0 my ($subject_class_name, $logic_type, $logic_detail) = split("/",$self->id);
427 0 ▶ unless ($logic_type eq 'And') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($logic_type eq "And") 0 0 0
428 0 die "Attempt to add a filter to a rule besides an 'And' rule!";
429 }
430 0 my @keys = split(',',$logic_detail);
431 0 my $new_id = join('/',$subject_class_name,$logic_type,join(',',@keys,$new_key));
432 0 return $self->class->get($new_id);
433 }
434  
435 0 ▶sub remove_filter {
Subroutine remove_filter: not called CC=1 SCAR=6.9
Pod undocumented
436 0 my $self = shift;
437 0 my $filter = shift;
438 0 my ($subject_class_name, $logic_type, $logic_detail) = split("/",$self->id);
439 0 my @keys = grep { $_ !~ /^${filter}\b/ } split(',',$logic_detail);
440 0 my $new_id = join('/',$subject_class_name,$logic_type,join(',',@keys));
441 #print "$new_id\n";
442 0 return $self->class->get($new_id);
443 }
444  
445 0 ▶sub sub_classify {
Subroutine sub_classify: not called CC=1 SCAR=6.9
Pod undocumented
446 0 my ($self,$subclass_name) = @_;
447 0 my $new_id = $self->id;
448 0 $new_id =~ s/^.*?\//$subclass_name\//;
449 0 return $self->class->get($new_id);
450 }
451  
452  
453 # flyweight constructor
454 # NOTE: this caches outside of the regular system since these are stateless objects
455 266 ▶sub get_by_subject_class_name_logic_type_and_logic_detail {
Subroutine get_by_subject_class_name_logic_type_and_logic_detail: called CC=4 SCAR=15.5
Pod undocumented
456 266 my $class = shift;
457 266 my $subject_class_name = shift;
458 266 ▶ Carp::croak("Expected a subject class name as the first arg of UR::BoolExpr::Template constructor, got "
Branch: 2 branches
BranchTrueFalseTotal
unless $subject_class_name 0 266 266
defined $subject_class_name ? : 0 0 0
459 . ( defined($subject_class_name) ? "'$subject_class_name'" : "(undef)" ) ) unless ($subject_class_name);
460 266 my $logic_type = shift;
461 266 my $logic_detail = shift;
462 266 ▶ my $constant_value_id = shift || UR::BoolExpr::Util::values_to_value_id(); # default is an empty list of values
Condition33%1/3
shift() || UR::BoolExpr::Util::values_to_value_id()
l!l&&r!l&&!r
26600
MC/DC0%0/2
shift() || UR::BoolExpr::Util::values_to_value_id()
shift() UR::BoolExpr::Util::values_to_value_id()
Truth table33%1/3
A || B
A= shift()B= UR::BoolExpr::Util::values_to_value_id()
ABresult
1X1
011
000
463  
464 266 return $class->get(join('/',$subject_class_name,$logic_type,$logic_detail,$constant_value_id));
465 }
466  
467 # The analogue of resolve in UR::BoolExpr. @params_list is a list if
468 # strings containing properties and operators separated by a space. For ex: "some_param ="
469 266 ▶sub resolve {
Subroutine resolve: called CC=3 SCAR=11.2
Pod undocumented
470 266 my($class,$subject_class_name, @params_list) = @_;
471  
472 266 my(@params, @constant_values);
473 266 ▶ for (my $i = 0; $i < @params_list; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @params_list) 532 266 798
474 532 push @params, $params_list[$i];
475 532 ▶ if (UR::BoolExpr::Util::is_meta_param($params_list[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (UR::BoolExpr::Util::is_meta_param($params_list[$i])) 0 532 532
476 0 push @constant_values, $params_list[++$i];
477 }
478 }
479  
480 266 return $class->get_by_subject_class_name_logic_type_and_logic_detail(
481 $subject_class_name,
482 "And",
483 join(',',@params),
484 UR::BoolExpr::Util::values_to_value_id(@constant_values));
485 }
486  
487 1596 ▶sub get {
Subroutine get: called CC=9 SCAR=25.1
Pod documented
488 1596 my $class = shift;
489 1596 my $id = shift;
490 1596 ▶ Carp::croak("Non-id params not supported for " . __PACKAGE__ . " yet!") if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1596 1596
491  
492 1596 my $self = $UR::Object::rule_templates->{$id};
493 1596 ▶ return $self if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 1064 532 1596
494  
495 532 my ($subject_class_name,$logic_type,$logic_detail,$constant_value_id,@extra) = split('/',$id);
496 532 ▶ if (@extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extra) 0 532 532
497 # account for a possible slash in the constant value id
498 0 $constant_value_id = join('/',$constant_value_id,@extra);
499 }
500  
501 # work on the base class or on subclasses
502 532 ▶ my $sub_class_name = (
Branch: 1 branch
BranchTrueFalseTotal
$class eq __PACKAGE__ ? : 532 0 532
503 $class eq __PACKAGE__
504 ? __PACKAGE__ . "::" . $logic_type
505 : $class
506 );
507  
508 532 ▶ unless ($logic_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($logic_type) 0 532 532
509 0 Carp::croak("Could not determine logic type from UR::BoolExpr::Template with id $id");
510 }
511  
512 532 ▶ if ($logic_type eq "And") {
Branch: 1 branch
BranchTrueFalseTotal
if ($logic_type eq 'And') { } 532 0 532
513 # TODO: move into subclass
514 532 ▶ my @keys = split(/,/,$logic_detail || '');
Condition50%1/2
$logic_detail || ''
l!l
5320
MC/DC0%0/1
$logic_detail || ''
$logic_detail
Truth table50%1/2
A
A= $logic_detail
Aresult
11
00
515 532 my @constant_values;
516 532 ▶ @constant_values = UR::BoolExpr::Util::value_id_to_values($constant_value_id) if defined $constant_value_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $constant_value_id 532 0 532
517 532 return $sub_class_name->_fast_construct(
518 $subject_class_name,
519 \@keys,
520 \@constant_values,
521 $logic_detail,
522 $constant_value_id,
523 );
524 }
525 else {
526 0 $self = bless {
527 id => $id,
528 subject_class_name => $subject_class_name,
529 logic_type => $logic_type,
530 logic_detail => $logic_detail,
531 constant_value_id => $constant_value_id,
532 normalized_id => $id,
533 }, $sub_class_name;
534 0 $UR::Object::rule_templates->{$id} = $self;
535 0 return $self;
536 }
537 }
538  
539  
540 # Return true if the template has recursion_desc, hints, order or page set
541 266 ▶sub has_meta_options {
Subroutine has_meta_options: called CC=2 SCAR=7.1
Pod undocumented
542 266 my $self = shift;
543 266 ▶ return 1 if @$self{@meta_param_names};
Branch: 1 branch
BranchTrueFalseTotal
if @$self{@UR::BoolExpr::Template::meta_param_names} 0 266 266
544 266 return 0;
545 }
546  
547  
548 # This is the basis for the hash used by the existing UR::Object system for each rule.
549 # this is created upon first request and cached in the object
550  
551 266 ▶sub legacy_params_hash {
Subroutine legacy_params_hash: called CC=26 SCAR=51.9
Pod undocumented
552 266 my $self = shift;
553 266 my $legacy_params_hash = $self->{legacy_params_hash};
554 266 ▶ return $legacy_params_hash if $legacy_params_hash;
Branch: 1 branch
BranchTrueFalseTotal
if $legacy_params_hash 0 266 266
555
556 266 $legacy_params_hash = {};
557
558 266 my $template_id = $self->id;
559 266 my $key_op_hash = $self->key_op_hash;
560 266 my $id_only = $self->is_id_only;
561
562 266 my $subject_class_name = $self->subject_class_name;
563 266 my $logic_type = $self->logic_type;
564 266 my $logic_detail = $self->logic_detail;
565 266 my @keys_sorted = $self->_underlying_keys;
566 266 my $subject_class_meta = $subject_class_name->__meta__;
567
568 266 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (@keys_sorted and not $logic_detail or $logic_detail and not @keys_sorted) 0 266 266
Condition33%3/9
@keys_sorted and not $logic_detail or $logic_detail and not @keys_sorted
l!l&&r!l&&!r
00266
@keys_sorted and not $logic_detail
!ll&&!rl&&r
02660
$logic_detail and not @keys_sorted
!ll&&!rl&&r
02660
MC/DC0%0/4
@keys_sorted and not $logic_detail or $logic_detail and not @keys_sorted
@keys_sorted not $logic_detail $logic_detail not @keys_sorted
Truth table14%1/7
A and not(B) or C and not(D)
A= @keys_sortedB= not $logic_detailC= $logic_detailD= not @keys_sorted
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
569 (@keys_sorted and not $logic_detail)
570 or
571 ($logic_detail and not @keys_sorted)
572 ) {
573 0 Carp::confess();
574 }
575
576 266 ▶ if (!$logic_detail) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $logic_detail) { } 0 266 266
577 0 %$legacy_params_hash = (_unique => 0, _none => 1);
578 }
579 else {
580 # _id_only
581 266 ▶ if ($id_only) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id_only) { } 266 0 266
582 266 $legacy_params_hash->{_id_only} = 1;
583 }
584 else {
585 0 $legacy_params_hash->{_id_only} = 0;
586 0 $legacy_params_hash->{_param_key} = undef;
587 }
588
589 # _unique
590 266 ▶ if (my $id_op = $key_op_hash->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id_op = $key_op_hash->{"id"}) 266 0 266
591 266 ▶ if ($id_op->{""} or $id_op->{"="}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id_op->{""} or $id_op->{"="}) 266 0 266
Condition33%1/3
$id_op->{""} or $id_op->{"="}
l!l&&r!l&&!r
26600
MC/DC0%0/2
$id_op->{""} or $id_op->{"="}
$id_op->{""} $id_op->{"="}
Truth table33%1/3
A or B
A= $id_op->{""}B= $id_op->{"="}
ABresult
1X1
011
000
592 266 $legacy_params_hash->{_unique} = 1;
593 266 ▶ unless ($self->is_unique) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_unique) 0 266 266
594 0 Carp::carp("The BoolExpr includes a filter on ID, but the is_unique flag is unexpectedly false for $self->{id}");
595 }
596 }
597 }
598  
599
600  
601 266 ▶ unless ($legacy_params_hash->{_unique}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($legacy_params_hash->{"_unique"}) 0 266 266
602 0 ▶ if (defined $legacy_params_hash->{id} and not ref $legacy_params_hash->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $legacy_params_hash->{'id'} and not ref $legacy_params_hash->{'id'}) { } 0 0 0
Condition0%0/3
defined $legacy_params_hash->{"id"} and not ref $legacy_params_hash->{"id"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $legacy_params_hash->{"id"} and not ref $legacy_params_hash->{"id"}
defined $legacy_params_hash->{"id"} not ref $legacy_params_hash->{"id"}
Truth table0%0/3
A and not(B)
A= defined $legacy_params_hash->{"id"}B= not ref $legacy_params_hash->{"id"}
ABresult
0X0
100
111
603 # if we have the id, then we have uniqueness
604 # NOT TRUE: we catch the truly unieq cses of having an id and an unambiguous operator above
605 #$legacy_params_hash->{_unique} = 1;
606 }
607 else {
608 # default to non-unique
609 0 $legacy_params_hash->{_unique} = 0;
610
611 # if some combination of params can combine to
612 # satisfy at least one unique constraint,
613 # then we have uniqueness in the parameters.
614
615 0 my @ps = $subject_class_meta->unique_property_sets;
616 0 for my $property_set (@ps)
617 {
618 0 ▶ my $property_set = (ref($property_set) ? $property_set : [$property_set]);
Branch: 1 branch
BranchTrueFalseTotal
ref $property_set ? : 0 0 0
619 my @properties_used_from_constraint =
620 0 ▶ grep { defined($_) }
Branch: 1 branch
BranchTrueFalseTotal
ref $property_set ? : 0 0 0
621 0 (ref($property_set) ? @$key_op_hash{@$property_set} : $key_op_hash->{$property_set});
622
623 0 ▶ if (@properties_used_from_constraint == @$property_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (@properties_used_from_constraint == @$property_set) { } 0 0 0
624 # filter imprecise operators
625 @properties_used_from_constraint =
626 grep {
627 0 not (
628 0 ▶ grep { /^(not |)like(-.|)$/i or /^\[\]/}
Condition0%0/3
/^(not |)like(-.|)$/i || /^\[\]/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^(not |)like(-.|)$/i || /^\[\]/
/^(not |)like(-.|)$/i /^\[\]/
Truth table0%0/3
A || B
A= /^(not |)like(-.|)$/iB= /^\[\]/
ABresult
1X1
011
000
629 keys %$_
630 )
631 }
632 @properties_used_from_constraint;
633
634 0 ▶ if (@properties_used_from_constraint == @$property_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (@properties_used_from_constraint == @$property_set) { } 0 0 0
635 0 $legacy_params_hash->{_unique} = 1;
636 0 last;
637 }
638 else {
639 ## print "some properties use bad operators: @properties_used_from_constraint\n";
640 }
641 }
642 else {
643 ## print "too few properties in @properties_used_from_constraint\n";
644 }
645 }
646 }
647
648 # _param_key gets re-set as long as this has a true value
649 0 ▶ $legacy_params_hash->{_param_key} = undef unless $id_only;
Branch: 1 branch
BranchTrueFalseTotal
unless $id_only 0 0 0
650 }
651 }
652  
653 266 ▶ if ($self->is_unique and not $legacy_params_hash->{_unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_unique and not $legacy_params_hash->{"_unique"}) 0 266 266
Condition33%1/3
$self->is_unique and not $legacy_params_hash->{"_unique"}
!ll&&!rl&&r
02660
MC/DC0%0/2
$self->is_unique and not $legacy_params_hash->{"_unique"}
$self->is_unique not $legacy_params_hash->{"_unique"}
Truth table33%1/3
A and not(B)
A= $self->is_uniqueB= not $legacy_params_hash->{"_unique"}
ABresult
0X0
100
111
654 0 Carp::carp "is_unique IS set but legacy params hash is NO for $self->{id}";
655 #$DB::single = 1;
656 0 $self->is_unique;
657 }
658 266 ▶ if (!$self->is_unique and $legacy_params_hash->{_unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_unique and $legacy_params_hash->{"_unique"}) 0 266 266
Condition33%1/3
not $self->is_unique and $legacy_params_hash->{"_unique"}
!ll&&!rl&&r
26600
MC/DC0%0/2
not $self->is_unique and $legacy_params_hash->{"_unique"}
not $self->is_unique $legacy_params_hash->{"_unique"}
Truth table33%1/3
not(A) and B
A= not $self->is_uniqueB= $legacy_params_hash->{"_unique"}
ABresult
0X0
100
111
659 0 Carp::carp "is_unique NOT set but legacy params hash IS for $self->{id}";
660 #$DB::single = 1;
661 0 $self->is_unique;
662 }
663  
664 266 $self->{legacy_params_hash} = $legacy_params_hash;
665 266 return $legacy_params_hash;
666 }
667  
668 0 ▶sub sorter {
Subroutine sorter: not called CC=4 SCAR=30.0
Pod undocumented
669 0 my $self = shift;
670  
671 # return a standard sorter for expressions using this template
672 # the template might contain a group_by or order_by clause which affects it...
673  
674 0 ▶ die "this method takes no paramters!" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
675  
676 0 my $class = $self->subject_class_name;
677  
678 0 my $sort_meta;
679 0 ▶ if ($self->group_by) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->group_by) { } 0 0 0
680 0 my $set_class = $class . "::Set";
681 0 $sort_meta = $set_class->__meta__;
682 }
683 else {
684 0 $sort_meta = $class->__meta__;
685 }
686  
687 0 my $sorter;
688 0 ▶ if (my $order_by = $self->order_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $order_by = $self->order_by) { } 0 0 0
689 0 $sorter = $sort_meta->sorter(@$order_by);
690 }
691 else {
692 0 $sorter = $sort_meta->sorter();
693 }
694  
695 0 return $sorter;
696 }
697  
698  
699 266 1;
700  
« blib/lib/UR/Object/View/Lister/Text.pm ↑ Summary blib/lib/UR/Namespace/Command/Update/Doc.pm »