lib/UR/BoolExpr.pm

filter: Statementstmt 30.8% 189 / 612 Branchbran 15.5% 45 / 290 Conditioncond 11.2% 11 / 98 MC/DCmcdc 1.8% 1 / 55 Subroutinesub 55.1% 27 / 49 Podpod 48.0% 12 / 25 totaltotal 25.2% 285 / 1129 CC 194 SCAR 71.9
CC
194
Cov
24%
resolve
3270.0
_resolve_from_filter_array
552.0
flatten_hard_refs
135.1
CRAP 1322.4
« lib/UR/DBI/Report.pm ↑ Summary lib/UR/Context/ImportIterator.pm »
1 package UR::BoolExpr;
2 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 266 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 266 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 266 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 266 require UR;
9  
10 266 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 266 our @CARP_NOT = ('UR::Context');
12  
13 266 our $VERSION = "0.47"; # UR $VERSION;
14  
15 # readable stringification
16 266 ▶use overload ('""' => '__display_name__');
Subroutine BEGIN: called CC=1 SCAR=0.0
17 0 ▶use overload ('==' => sub { $_[0] . '' eq $_[1] . '' } );
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
18 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
19  
20 266 UR::Object::Type->define(
21 class_name => 'UR::BoolExpr',
22 composite_id_separator => $UR::BoolExpr::Util::id_sep,
23 id_by => [
24 template_id => { type => 'Blob' },
25 value_id => { type => 'Blob' },
26 ],
27 has => [
28 template => { is => 'UR::BoolExpr::Template', id_by => 'template_id' },
29 subject_class_name => { via => 'template' },
30 logic_type => { via => 'template' },
31 logic_detail => { via => 'template' },
32  
33 num_values => { via => 'template' },
34 is_normalized => { via => 'template' },
35 is_id_only => { via => 'template' },
36 has_meta_options => { via => 'template' },
37 ],
38 is_transactional => 0,
39 );
40  
41  
42 # for performance
43 532 ▶sub UR::BoolExpr::Type::resolve_composite_id_from_ordered_values {
Subroutine resolve_composite_id_from_ordered_values: called CC=1 SCAR=0.0
44 532 shift;
45 532 return join($UR::BoolExpr::Util::id_sep,@_);
46 }
47  
48 # only respect the first delimiter instead of splitting
49 266 ▶sub UR::BoolExpr::Type::resolve_ordered_values_from_composite_id {
Subroutine resolve_ordered_values_from_composite_id: called CC=1 SCAR=0.0
50 266 my ($self,$id) = @_;
51 266 my $pos = index($id,$UR::BoolExpr::Util::id_sep);
52 266 return (substr($id,0,$pos), substr($id,$pos+1));
53 }
54  
55 3458 ▶sub template {
Subroutine template: called CC=2 SCAR=7.1
Pod undocumented
56 3458 my $self = $_[0];
57 3458 ▶ return $self->{template} ||= $self->__template;
Condition66%2/3
$self->{"template"} ||= $self->__template
l!l&&r!l&&!r
29265320
MC/DC0%0/2
$self->{"template"} ||= $self->__template
$self->{"template"} $self->__template
Truth table66%2/3
A ||= B
A= $self->{"template"}B= $self->__template
ABresult
1X1
011
000
58 }
59  
60 0 ▶sub flatten {
Subroutine flatten: not called CC=3 SCAR=24.8
Pod documented
61 0 my $self = shift;
62 0 ▶ return $self->{flatten} if exists $self->{flatten};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"flatten"} 0 0 0
63 0 my $flat = $self->template->_flatten_bx($self);
64 0 $self->{flatten} = $flat;
65 0 ▶ Scalar::Util::weaken($self->{flatten}) if $self == $flat;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $flat 0 0 0
66 0 return $flat;
67 }
68  
69 0 ▶sub reframe {
Subroutine reframe: not called CC=3 SCAR=24.8
Pod documented
70 0 my $self = shift;
71 0 my $in_terms_of = shift;
72 0 ▶ return $self->{reframe}{$in_terms_of} if $self->{reframe}{$in_terms_of};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reframe"}{$in_terms_of} 0 0 0
73 0 my $reframe = $self->template->_reframe_bx($self, $in_terms_of);
74 0 $self->{reframe}{$in_terms_of} = $reframe;
75 0 ▶ Scalar::Util::weaken($self->{reframe}{$in_terms_of}) if $self == $reframe;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $reframe 0 0 0
76 0 return $reframe;
77 }
78  
79  
80 # override the UR/system display name
81 # this is used in stringification overload
82 0 ▶sub __display_name__ {
Subroutine __display_name__: not called CC=1 SCAR=6.9
83 0 my $self = shift;
84 0 my %b = $self->_params_list;
85 0 my $s = Data::Dumper->new([\%b])->Terse(1)->Indent(0)->Useqq(1)->Sortkeys(1)->Dump;
86 0 $s =~ s/\n/ /gs;
87 0 $s =~ s/^\s*{//;
88 0 $s =~ s/\}\s*$//;
89 0 $s =~ s/\"(\w+)\" \=\> / $1 => /g;
90 0 return __PACKAGE__ . '=(' . $self->subject_class_name . ':' . $s . ')';
91 }
92  
93 # The primary function: evaluate a subject object as matching the rule or not.
94 0 ▶sub evaluate {
Subroutine evaluate: not called CC=1 SCAR=6.9
Pod documented
95 0 my $self = shift;
96 0 my $subject = shift;
97 0 my $template = $self->template;
98 0 my @values = $self->values;
99 0 return $template->evaluate_subject_and_values($subject,@values);
100 }
101  
102 # Behind the id properties:
103 266 ▶sub template_and_values {
Subroutine template_and_values: called CC=1 SCAR=0.0
Pod documented
104 266 my $self = shift;
105 266 my ($template_id, $value_id) = UR::BoolExpr::Type->resolve_ordered_values_from_composite_id($self->id);
106 266 return (UR::BoolExpr::Template->get($template_id), UR::BoolExpr::Util::value_id_to_values($value_id));
107 }
108  
109 # Returns true if the rule represents a subset of the things the other
110 # rule would match. It returns undef if the answer is not known, such as
111 # when one of the values is a list and we didn't go to the trouble of
112 # searching the list for a matching value
113 0 ▶sub is_subset_of {
Subroutine is_subset_of: not called CC=12 SCAR=48.5
Pod documented
114 0 my($self, $other_rule) = @_;
115  
116 0 ▶ return 0 unless (ref($other_rule) and $self->isa(ref $other_rule));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other_rule and $self->isa(ref $other_rule) 0 0 0
Condition0%0/3
ref $other_rule and $self->isa(ref $other_rule)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $other_rule and $self->isa(ref $other_rule)
ref $other_rule $self->isa(ref $other_rule)
Truth table0%0/3
A and B
A= ref $other_ruleB= $self->isa(ref $other_rule)
ABresult
0X0
100
111
117  
118 0 my $my_template = $self->template;
119 0 my $other_template = $other_rule->template;
120  
121 0 ▶ unless ($my_template->isa("UR::BoolExpr::Template::And")
Branch: 1 branch
BranchTrueFalseTotal
unless ($my_template->isa("UR::BoolExpr::Template::And") and $other_template->isa("UR::BoolExpr::Template::And")) 0 0 0
Condition0%0/3
$my_template->isa("UR::BoolExpr::Template::And") and $other_template->isa("UR::BoolExpr::Template::And")
!ll&&!rl&&r
000
MC/DC0%0/2
$my_template->isa("UR::BoolExpr::Template::And") and $other_template->isa("UR::BoolExpr::Template::And")
$my_template->isa("UR::BoolExpr::Template::And") $other_template->isa("UR::BoolExpr::Template::And")
Truth table0%0/3
A and B
A= $my_template->isa("UR::BoolExpr::Template::And")B= $other_template->isa("UR::BoolExpr::Template::And")
ABresult
0X0
100
111
122 and $other_template->isa("UR::BoolExpr::Template::And")) {
123 0 Carp::confess("This method currently works only on ::And expressions. Update to handle ::Or, ::PropertyComparison, and templates of mismatched class!");
124 }
125 0 ▶ return unless ($my_template->is_subset_of($other_template));
Branch: 1 branch
BranchTrueFalseTotal
unless $my_template->is_subset_of($other_template) 0 0 0
126  
127 0 my $values_match = 1;
128 0 foreach my $prop ( $other_template->_property_names ) {
129 0 ▶ my $my_operator = $my_template->operator_for($prop) || '=';
Condition0%0/2
$my_template->operator_for($prop) || '='
l!l
00
MC/DC0%0/1
$my_template->operator_for($prop) || '='
$my_template->operator_for($prop)
Truth table0%0/2
A
A= $my_template->operator_for($prop)
Aresult
11
00
130 0 ▶ my $other_operator = $other_template->operator_for($prop) || '=';
Condition0%0/2
$other_template->operator_for($prop) || '='
l!l
00
MC/DC0%0/1
$other_template->operator_for($prop) || '='
$other_template->operator_for($prop)
Truth table0%0/2
A
A= $other_template->operator_for($prop)
Aresult
11
00
131  
132 0 my $my_value = $self->value_for($prop);
133 0 my $other_value = $other_rule->value_for($prop);
134  
135 # If either is a list of values, return undef
136 0 ▶ return undef if (ref($my_value) || ref($other_value));
Branch: 1 branch
BranchTrueFalseTotal
if ref $my_value or ref $other_value 0 0 0
Condition0%0/3
ref $my_value or ref $other_value
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $my_value or ref $other_value
ref $my_value ref $other_value
Truth table0%0/3
A or B
A= ref $my_valueB= ref $other_value
ABresult
1X1
011
000
137  
138 266 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
139 0 ▶ $values_match = undef if ($my_value ne $other_value);
Branch: 1 branch
BranchTrueFalseTotal
if $my_value ne $other_value 0 0 0
140 }
141  
142 0 return $values_match;
143 }
144  
145 1064 ▶sub values {
Subroutine values: called CC=6 SCAR=19.1
Pod documented
146 1064 my $self = shift;
147 1064 ▶ if ($self->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"values"}) 532 532 1064
148 532 return @{ $self->{values}}
149 }
150 532 my $value_id = $self->value_id;
151 532 ▶ return unless defined($value_id) and length($value_id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value_id and length $value_id 0 532 532
Condition33%1/3
defined $value_id and length $value_id
!ll&&!rl&&r
00532
MC/DC0%0/2
defined $value_id and length $value_id
defined $value_id length $value_id
Truth table33%1/3
A and B
A= defined $value_idB= length $value_id
ABresult
0X0
100
111
152 532 my @values;
153 532 @values = UR::BoolExpr::Util::value_id_to_values($value_id);
154 532 ▶ if (my $hard_refs = $self->{hard_refs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hard_refs = $self->{"hard_refs"}) 0 532 532
155 0 for my $n (keys %$hard_refs) {
156 0 $values[$n] = $hard_refs->{$n};
157 }
158 }
159 532 $self->{values} = \@values;
160 532 return @values;
161 }
162  
163 266 ▶sub value_for_id {
Subroutine value_for_id: called CC=2 SCAR=7.0
Pod documented
164 266 my $self = shift;
165 266 my $t = $self->template;
166 266 my $position = $t->id_position;
167 266 ▶ return unless defined $position;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $position 0 266 266
168 266 return $self->value_for_position($position);
169 }
170  
171 0 ▶sub specifies_value_for {
Subroutine specifies_value_for: not called CC=1 SCAR=6.9
Pod documented
172 0 my $self = shift;
173 0 my $rule_template = $self->template;
174 0 return $rule_template->specifies_value_for(@_);
175 }
176  
177 0 ▶sub value_for {
Subroutine value_for: not called CC=7 SCAR=40.3
Pod documented
178 0 my $self = shift;
179 0 my $property_name = shift;
180  
181 # TODO: refactor to be more efficient
182 0 my $template = $self->template;
183 0 my $h = $self->legacy_params_hash;
184 0 my $v;
185 0 ▶ if (exists $h->{$property_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h->{$property_name}) { } 0 0 0
186 # normal case
187 0 $v = $h->{$property_name};
188 0 my $tmpl_pos = $template->value_position_for_property_name($property_name);
189 0 ▶ if (exists $self->{'hard_refs'}->{$tmpl_pos}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'hard_refs'}{$tmpl_pos}) { } 0 0 0
elsif ($self->_value_is_old_style_operator_and_value($v)) { } 0 0 0
190 0 $v = $self->{'hard_refs'}->{$tmpl_pos}; # It was stored during resolve() as a hard ref
191 }
192 elsif ($self->_value_is_old_style_operator_and_value($v)) {
193 0 $v = $v->{'value'}; # It was old style operator/value hash
194 }
195 } else {
196 # No value found under that name... try decomposing the id
197 0 ▶ return if $property_name eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
if $property_name eq "id" 0 0 0
198 0 my $id_value = $self->value_for('id');
199 0 my $class_meta = $self->subject_class_name->__meta__();
200 0 my @id_property_values = $class_meta->get_composite_id_decomposer->($id_value);
201  
202 0 my @id_property_names = $class_meta->id_property_names;
203 0 ▶ for (my $i = 0; $i < @id_property_names; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @id_property_names) 0 0 0
204 0 ▶ if ($id_property_names[$i] eq $property_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id_property_names[$i] eq $property_name) 0 0 0
205 0 $v = $id_property_values[$i];
206 0 last;
207 }
208 }
209 }
210 0 return $v;
211 }
212  
213 266 ▶sub value_for_position {
Subroutine value_for_position: called CC=1 SCAR=0.0
Pod undocumented
214 266 my ($self, $pos) = @_;
215 266 return ($self->values)[$pos];
216 }
217  
218 0 ▶sub operator_for {
Subroutine operator_for: not called CC=1 SCAR=6.9
Pod documented
219 0 my $self = shift;
220 0 my $t = $self->template;
221 0 return $t->operator_for(@_);
222 }
223  
224 0 ▶sub underlying_rules {
Subroutine underlying_rules: not called CC=2 SCAR=17.9
Pod undocumented
225 0 my $self = shift;
226 0 ▶ unless (exists $self->{'_underlying_rules'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_underlying_rules"}) 0 0 0
227 0 my @values = $self->values;
228 0 $self->{'_underlying_rules'} = [ $self->template->get_underlying_rules_for_values(@values) ];
229 }
230 0 return @{ $self->{'_underlying_rules'} };
231 }
232  
233 # De-compose the rule back into its original form.
234 266 ▶sub params_list {
Subroutine params_list: called CC=1 SCAR=0.0
Pod undocumented
235 # This is the reverse of the bulk of resolve.
236 # It returns the params in list form, directly coercable into a hash if necessary.
237 # $r = UR::BoolExpr->resolve($c1,@p1);
238 # ($c2, @p2) = ($r->subject_class_name, $r->params_list);
239 266 my $self = shift;
240 266 my $template = $self->template;
241 266 my @values_sorted = $self->values;
242 266 return $template->params_list_for_values(@values_sorted);
243 }
244  
245 # TODO: replace these with the logical set operations
246 # FIXME: the name is confusing b/c it doesn't mutate the object, it returns a different object
247 0 ▶sub add_filter {
Subroutine add_filter: not called CC=1 SCAR=6.9
Pod undocumented
248 0 my $self = shift;
249 0 return __PACKAGE__->resolve($self->subject_class_name, $self->params_list, @_);
250 }
251  
252 # TODO: replace these with the logical set operations
253 # FIXME: the name is confusing b/c it doesn't mutate the object, it returns a different object
254 0 ▶sub remove_filter {
Subroutine remove_filter: not called CC=3 SCAR=24.8
Pod undocumented
255 0 my $self = shift;
256 0 my $property_name = shift;
257 0 my @params_list = $self->params_list;
258 0 my @new_params_list;
259 0 ▶ for (my $n=0; $n<=$#params_list; $n+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $#params_list) 0 0 0
260 0 my $key = $params_list[$n];
261 0 ▶ if ($key =~ /^$property_name\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^$property_name\b/) 0 0 0
262 0 next;
263 }
264 0 my $value = $params_list[$n+1];
265 0 push @new_params_list, $key, $value;
266 }
267 0 return __PACKAGE__->resolve($self->subject_class_name, @new_params_list);
268 }
269  
270 # as above, doesn't mutate, just returns a different bx
271 0 ▶sub sub_classify {
Subroutine sub_classify: not called CC=1 SCAR=6.9
Pod undocumented
272 0 my ($self,$subclass_name) = @_;
273 0 my ($t,@v) = $self->template_and_values();
274 0 return $t->sub_classify($subclass_name)->get_rule_for_values(@v);
275 }
276  
277 # flyweight constructor
278 # like regular UR::Value objects, but kept separate from the cache but kept
279 # out of the regular transaction cache so they alwasy vaporize when derefed
280 1064 ▶sub get {
Subroutine get: called CC=2 SCAR=7.0
Pod documented
281 1064 my $rule_id = pop;
282 1064 ▶ unless (exists $UR::Object::rules->{$rule_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $UR::Object::rules->{$rule_id}) 1064 0 1064
283 1064 my $pos = index($rule_id,$UR::BoolExpr::Util::id_sep);
284 1064 my ($template_id,$value_id) = (substr($rule_id,0,$pos), substr($rule_id,$pos+1));
285 1064 my $rule = { id => $rule_id, template_id => $template_id, value_id => $value_id };
286 1064 bless ($rule, "UR::BoolExpr");
287 1064 $UR::Object::rules->{$rule_id} = $rule;
288 1064 Scalar::Util::weaken($UR::Object::rules->{$rule_id});
289 1064 return $rule;
290 }
291 0 return $UR::Object::rules->{$rule_id};
292 }
293  
294 # because these are weakened
295 1064 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
296 1064 delete $UR::Object::rules->{$_[0]->{id}};
297 }
298  
299 266 ▶sub flatten_hard_refs {
Subroutine flatten_hard_refs: called CC=12 SCAR=49.1
Pod undocumented
300 266 my $self = $_[0];
301 266 ▶ return $self if not $self->{hard_refs};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"hard_refs"} 266 0 266
302  
303 0 my $subject_class_name = $self->subject_class_name;
304 0 my $meta = $subject_class_name->__meta__;
305 0 my %params = $self->_params_list;
306 0 my $changes = 0;
307 0 for my $key (keys %params) {
308 0 my $value = $params{$key};
309 0 ▶ if (ref($value) and Scalar::Util::blessed($value) and $value->isa("UR::Object")) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and builtin::blessed($value) and $value->isa("UR::Object")) 0 0 0
Condition0%0/6
ref $value and builtin::blessed($value) and $value->isa("UR::Object")
!ll&&!rl&&r
000
ref $value and builtin::blessed($value)
!ll&&!rl&&r
000
MC/DC0%0/3
ref $value and builtin::blessed($value) and $value->isa("UR::Object")
ref $value builtin::blessed($value) $value->isa("UR::Object")
Truth table0%0/4
A and B and C
A= ref $valueB= builtin::blessed($value)C= $value->isa("UR::Object")
ABCresult
0XX0
10X0
1100
1111
310 0 my ($property_name, $op) = ($key =~ /^(\S+)\s*(.*)/);
311  
312 0 my $pmeta = $meta->property($property_name);
313 0 my $final_pmeta = $pmeta->final_property_meta();
314  
315 0 my @possible_data_types = uniq grep { $_ } map { $_->data_type } ($pmeta, $final_pmeta);
316 0 ▶ unless (@possible_data_types) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@possible_data_types) 0 0 0
317 # this might not be possible at runtime
318 0 croak sprintf 'unable to determine data type for property: %s', $property_name;
319 }
320  
321 0 ▶ my $data_type = first { $value->isa($_) } @possible_data_types;
Subroutine __ANON__: not called CC=1 SCAR=6.9
322 0 ▶ unless ($data_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_type) 0 0 0
323 0 croak sprintf 'value type, %s, is incompatible with: %s', $value->class, join(', ', @possible_data_types);
324 }
325  
326 0 my $value2 = do {
327 0 local $@;
328 0 eval {
329 0 $data_type->get($value->id)
330 };
331 };
332 0 ▶ unless ($value2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value2) 0 0 0
333 0 croak sprintf 'unable to retrieve a %s by value ID: %s', $data_type, $value->id;
334 }
335  
336 0 ▶ unless ($value2 eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value2 eq $value) 0 0 0
337 0 croak sprintf 'retrieved duplicate %s with ID, %s,', $data_type, $value->id;
338 }
339  
340 # safe to re-represent as .id
341 0 my $new_key = $property_name . '.id';
342 0 ▶ $new_key .= ' ' . $op if $op;
Branch: 1 branch
BranchTrueFalseTotal
if $op 0 0 0
343 0 delete $params{$key};
344 0 $params{$new_key} = $value->id;
345 0 $changes++;
346 }
347 }
348 0 ▶ if ($changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changes) { } 0 0 0
349 0 return $self->resolve_normalized($subject_class_name, %params);
350 } else {
351 0 return $self;
352 }
353 }
354  
355 266 ▶sub resolve_normalized {
Subroutine resolve_normalized: called CC=4 SCAR=14.7
Pod undocumented
356 266 my $class = shift;
357 266 my ($unnormalized_rule, @extra) = $class->resolve(@_);
358 266 my $normalized_rule = $unnormalized_rule->normalize();
359 266 ▶ return if !defined(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 266 266
360 266 ▶ return ($normalized_rule,@extra) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 266 266
361 266 ▶ if (@extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extra) 0 266 266
362 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
363 0 my $rule_class = $normalized_rule->subject_class_name;
364 0 Carp::confess("Extra params for class $rule_class found: @extra\n");
365 }
366 266 return $normalized_rule;
367 }
368  
369 0 ▶sub resolve_for_template_id_and_values {
Subroutine resolve_for_template_id_and_values: not called CC=1 SCAR=6.9
Pod undocumented
370 0 my ($class,$template_id, @values) = @_;
371 0 my $value_id = UR::BoolExpr::Util::values_to_value_id(@values);
372 0 my $rule_id = $class->__meta__->resolve_composite_id_from_ordered_values($template_id,$value_id);
373 0 $class->get($rule_id);
374 }
375  
376  
377 # Return true if it's a hashref that specifies the old-style operator/value
378 # like property => { operator => '=', value => 1 }
379 # FYI, the new way to do this is:
380 # 'property =' => 1
381 0 ▶sub _value_is_old_style_operator_and_value {
Subroutine _value_is_old_style_operator_and_value: not called CC=6 SCAR=37.4
382 0 my($class,$value) = @_;
383  
384 return (ref($value) eq 'HASH')
385 &&
386 (exists($value->{'operator'}))
387 &&
388 (exists($value->{'value'}))
389 &&
390 ( (keys(%$value) == 2)
391 ||
392 ((keys(%$value) == 3)
393 0 ▶ && exists($value->{'escape'}))
Condition0%0/15
ref $value eq 'HASH' && exists $value->{'operator'} && exists $value->{'value'} && (keys %$value == 2 || keys %$value == 3 && exists $value->{'escape'})
!ll&&!rl&&r
000
ref $value eq "HASH" and exists $value->{"operator"} and exists $value->{"value"}
!ll&&!rl&&r
000
ref $value eq "HASH" and exists $value->{"operator"}
!ll&&!rl&&r
000
keys %$value == 2 or keys %$value == 3 and exists $value->{"escape"}
l!l&&r!l&&!r
000
keys %$value == 3 and exists $value->{"escape"}
!ll&&!rl&&r
000
MC/DC0%0/6
ref $value eq 'HASH' && exists $value->{'operator'} && exists $value->{'value'} && (keys %$value == 2 || keys %$value == 3 && exists $value->{'escape'})
ref $value eq "HASH" exists $value->{"operator"} exists $value->{"value"} keys %$value == 2 keys %$value == 3 exists $value->{"escape"}
Truth table0%0/7
A and B and C && D or E and F
A= ref $value eq "HASH"B= exists $value->{"operator"}C= exists $value->{"value"}D= keys %$value == 2E= keys %$value == 3F= exists $value->{"escape"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
11100X0
1110100
1111XX1
1110111
394 );
395 }
396  
397  
398 266 my $resolve_depth;
399 532 ▶sub resolve {
Subroutine resolve: called CC=92 SCAR=80.9
Pod undocumented
400 532 $resolve_depth++;
401 532 ▶ Carp::confess("Deep recursion in UR::BoolExpr::resolve()!") if $resolve_depth > 10;
Branch: 1 branch
BranchTrueFalseTotal
if $resolve_depth > 10 0 532 532
402  
403 # handle the case in which we've already processed the params into a boolexpr
404 532 ▶ if ( @_ == 3 and ref($_[2]) and ref($_[2])->isa("UR::BoolExpr") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3 and ref $_[2] and ref($_[2])->isa("UR::BoolExpr")) 0 532 532
Condition33%2/6
@_ == 3 and ref $_[2] and ref($_[2])->isa("UR::BoolExpr")
!ll&&!rl&&r
53200
@_ == 3 and ref $_[2]
!ll&&!rl&&r
53200
MC/DC0%0/3
@_ == 3 and ref $_[2] and ref($_[2])->isa("UR::BoolExpr")
@_ == 3 ref $_[2] ref($_[2])->isa("UR::BoolExpr")
Truth table25%1/4
A and B and C
A= @_ == 3B= ref $_[2]C= ref($_[2])->isa("UR::BoolExpr")
ABCresult
0XX0
10X0
1100
1111
405 0 $resolve_depth--;
406 0 return $_[2];
407 }
408  
409 532 my $class = shift;
410 532 my $subject_class = shift;
411 532 ▶ Carp::confess("Can't resolve BoolExpr: expected subject class as arg 2, got '$subject_class'") if not $subject_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $subject_class 0 532 532
412 # support for legacy passing of hashref instead of object or list
413 # TODO: eliminate the need for this
414 532 my @in_params;
415 532 ▶ if ($subject_class->isa('UR::Value::PerlReference') and $subject_class eq 'UR::Value::' . ref($_[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if ($subject_class->isa('UR::Value::PerlReference') and $subject_class eq 'UR::Value::' . ref($_[0])) { } 0 532 532
elsif (ref $_[0] eq 'HASH') { } 0 532 532
Condition33%1/3
$subject_class->isa("UR::Value::PerlReference") and $subject_class eq 'UR::Value::' . ref($_[0])
!ll&&!rl&&r
53200
MC/DC0%0/2
$subject_class->isa("UR::Value::PerlReference") and $subject_class eq 'UR::Value::' . ref($_[0])
$subject_class->isa("UR::Value::PerlReference") $subject_class eq 'UR::Value::' . ref($_[0])
Truth table33%1/3
A and B
A= $subject_class->isa("UR::Value::PerlReference")B= $subject_class eq 'UR::Value::' . ref($_[0])
ABresult
0X0
100
111
416 0 @in_params = @_;
417 }
418 elsif (ref($_[0]) eq "HASH") {
419 0 @in_params = %{$_[0]};
420 }
421 else {
422 532 @in_params = @_;
423 }
424  
425 532 ▶ if (defined($in_params[0]) and $in_params[0] eq '-or') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in_params[0] and $in_params[0] eq "-or") 0 532 532
Condition33%1/3
defined $in_params[0] and $in_params[0] eq "-or"
!ll&&!rl&&r
05320
MC/DC0%0/2
defined $in_params[0] and $in_params[0] eq "-or"
defined $in_params[0] $in_params[0] eq "-or"
Truth table33%1/3
A and B
A= defined $in_params[0]B= $in_params[0] eq "-or"
ABresult
0X0
100
111
426 0 shift @in_params;
427 0 my @sub_queries = @{ shift @in_params };
428  
429 0 my @meta_params;
430 0 ▶ for (my $i = 0; $i < @in_params; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @in_params) 0 0 0
431 0 ▶ if ($in_params[$i] =~ m/^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_params[$i] =~ /^-/) 0 0 0
432 0 push @meta_params, $in_params[$i], $in_params[$i+1];
433 }
434 }
435  
436 0 my $bx = UR::BoolExpr::Template::Or->_compose(
437 $subject_class,
438 \@sub_queries,
439 \@meta_params,
440 );
441  
442 0 $resolve_depth--;
443 0 return $bx;
444 }
445  
446 532 ▶ if (@in_params == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@in_params == 1) { } 0 532 532
elsif (@in_params % 2 == 1) { } 0 532 532
447 0 unshift @in_params, "id";
448 }
449 elsif (@in_params % 2 == 1) {
450 0 Carp::carp("Odd number of params while creating $class: (",join(',',@in_params),")");
451 }
452  
453 # split the params into keys and values
454 # where an operator is on the right-side, it is moved into the key
455 532 my $count = @in_params;
456 532 my (@keys,@values,@constant_values,$key,$value,$property_name,$operator,@hard_refs);
457 532 ▶ for(my $n = 0; $n < $count;) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $count) 532 532 1064
458 532 $key = $in_params[$n++];
459 532 $value = $in_params[$n++];
460  
461 532 ▶ unless (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 532 532
462 0 Carp::croak("Can't resolve BoolExpr: undef is an invalid key/property name. Args were: ".join(', ',@in_params));
463 }
464  
465 532 ▶ if (UR::BoolExpr::Util::is_meta_param($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UR::BoolExpr::Util::is_meta_param($key)) 0 532 532
466 # these are keys whose values live in the rule template
467 0 push @keys, $key;
468 0 push @constant_values, $value;
469 0 next;
470 }
471  
472 532 ▶ if ($key =~ m/^(_id_only|_param_key|_unique|__get_serial|_change_count)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(_id_only|_param_key|_unique|__get_serial|_change_count)$/) 0 532 532
473 # skip the pair: legacy/internal cruft
474 0 next;
475 }
476  
477 532 my $pos = index($key,' ');
478 532 ▶ if ($pos != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos != -1) { } 0 532 532
479 # the key is "propname op"
480 0 $property_name = substr($key,0,$pos);
481 0 $operator = substr($key,$pos+1);
482 0 ▶ if (substr($operator,0,1) eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($operator, 0, 1) eq " ") 0 0 0
483 0 $operator =~ s/^\s+//;
484 }
485 }
486 else {
487 # the key is "propname"
488 532 $property_name = $key;
489 532 $operator = '';
490 }
491  
492 532 ▶ if (my $ref = ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $value) 0 532 532
493 0 ▶ if ( (not $operator) and ($ref eq "HASH")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $operator and $ref eq "HASH") 0 0 0
Condition0%0/3
not $operator and $ref eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
not $operator and $ref eq "HASH"
not $operator $ref eq "HASH"
Truth table0%0/3
not(A) and B
A= not $operatorB= $ref eq "HASH"
ABresult
0X0
100
111
494 0 ▶ if ( $class->_value_is_old_style_operator_and_value($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_value_is_old_style_operator_and_value($value)) { } 0 0 0
495 # the key => { operator => $o, value => $v } syntax
496 # cannot be used with a value type of HASH
497 $operator = defined($value->{operator})
498 ? lc($value->{operator})
499 0 ▶ : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value->{'operator'} ? : 0 0 0
500 0 ▶ if (exists $value->{escape}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{"escape"}) 0 0 0
501 $operator .= "-" . $value->{escape}
502 0 }
503 0 $key .= " " . $operator;
504 0 $value = $value->{value};
505 0 $ref = ref($value);
506 }
507 else {
508 # the HASH is a value for the specified param
509 0 push @hard_refs, scalar(@values), $value;
510 }
511 }
512  
513 0 ▶ if ($ref eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 0 0 0
514 0 ▶ if (not $operator) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $operator) { } 0 0 0
elsif ($operator eq 'not') { } 0 0 0
515 # key => [] is the same as "key in" => []
516 0 $operator = 'in';
517 0 $key .= ' in';
518 }
519 elsif ($operator eq 'not') {
520 # "key not" => [] is the same as "key not in"
521 0 $operator .= ' in';
522 0 $key .= ' in';
523 }
524  
525 0 foreach my $val (@$value) {
526 0 ▶ if (ref($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 0 0 0
527 # when there are any refs in the arrayref
528 # we must keep the arrayerf contents
529 # to reconstruct effectively
530 0 push @hard_refs, scalar(@values), $value;
531 0 last;
532 }
533 }
534  
535 } # done handling ARRAY value
536  
537 } # done handling ref values
538  
539 532 push @keys, $key;
540 532 push @values, $value;
541 }
542  
543 # the above uses no class metadata
544  
545 # this next section uses class metadata
546 # it should be moved into the normalization layer
547  
548 532 my $subject_class_meta;
549 532 my $exception = do {
550 532 local $@;
551 532 $subject_class_meta = eval { $subject_class->__meta__ };
552 532 $@;
553 };
554 532 ▶ if ($exception) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exception) 0 532 532
555 0 Carp::croak("Can't get class metadata for $subject_class. Is it a valid class name?\nErrors were: $exception");
556 }
557 532 ▶ unless ($subject_class_meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subject_class_meta) 0 532 532
558 0 Carp::croak("No class metadata for $subject_class?!");
559 }
560  
561 my $subject_class_props =
562 $subject_class_meta->{'cache'}{'UR::BoolExpr::resolve'} ||=
563 532 ▶ { map {$_, 1} ( $subject_class_meta->all_property_type_names) };
Condition100%2/2
$subject_class_meta->{"cache"}{"UR::BoolExpr::resolve"} ||= {map({$_, 1;} $subject_class_meta->all_property_type_names)}
l!l
266266
MC/DC100%1/1
$subject_class_meta->{"cache"}{"UR::BoolExpr::resolve"} ||= {map({$_, 1;} $subject_class_meta->all_property_type_names)}
$subject_class_meta->{"cache"}{"UR::BoolExpr::resolve"}
Truth table100%2/2
A
A= $subject_class_meta->{"cache"}{"UR::BoolExpr::resolve"}
Aresult
11
00
564  
565 532 my($kn, $vn, $cn, $complex_values) = (0,0,0,0);
566 532 my ($op,@extra,@xadd_keys,@xadd_values,@xremove_keys,@xremove_values,@extra_key_pos,@extra_value_pos,
567 @swap_key_pos,@swap_key_value);
568  
569 532 for my $value (@values) {
570 532 $key = $keys[$kn++];
571 532 ▶ if (UR::BoolExpr::Util::is_meta_param($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UR::BoolExpr::Util::is_meta_param($key)) { } 0 532 532
572 0 $cn++;
573 0 redo;
574 }
575 else {
576 532 $vn++;
577 }
578  
579 532 my $pos = index($key,' ');
580 532 ▶ if ($pos != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos != -1) { } 0 532 532
581 # "propname op"
582 0 $property_name = substr($key,0,$pos);
583 0 $operator = substr($key,$pos+1);
584 0 ▶ if (substr($operator,0,1) eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($operator, 0, 1) eq " ") 0 0 0
585 0 $operator =~ s/^\s+//;
586 }
587 }
588 else {
589 # "propname"
590 532 $property_name = $key;
591 532 $operator = '';
592 }
593  
594 # account for the case where this parameter does
595 # not match an actual property
596 532 my $base_property_name = $property_name;
597 532 $base_property_name =~ s/[.-].+//;
598 532 ▶ if (!exists $subject_class_props->{$base_property_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $subject_class_props->{$base_property_name}) 0 532 532
599 0 ▶ if (substr($property_name,0,1) eq '_') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($property_name, 0, 1) eq '_') { } 0 0 0
600 0 warn "ignoring $property_name in $subject_class bx construction!"
601 }
602 else {
603 0 push @extra_key_pos, $kn-1;
604 0 push @extra_value_pos, $vn-1;
605 0 next;
606 }
607 }
608  
609 532 my $ref = ref($value);
610 532 ▶ if($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 0 532 532
611 0 $complex_values = 1;
612 0 ▶ if ($ref eq "ARRAY" and $operator ne 'between' and $operator ne 'not between') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY' and $operator ne 'between' and $operator ne 'not between') { } 0 0 0
elsif (builtin::blessed($value)) { } 0 0 0
elsif ($ref ne 'HASH') { } 0 0 0
Condition0%0/6
$ref eq "ARRAY" and $operator ne "between" and $operator ne "not between"
!ll&&!rl&&r
000
$ref eq "ARRAY" and $operator ne "between"
!ll&&!rl&&r
000
MC/DC0%0/3
$ref eq "ARRAY" and $operator ne "between" and $operator ne "not between"
$ref eq "ARRAY" $operator ne "between" $operator ne "not between"
Truth table0%0/4
A and B and C
A= $ref eq "ARRAY"B= $operator ne "between"C= $operator ne "not between"
ABCresult
0XX0
10X0
1100
1111
613 0 my $data_type;
614 my $is_many;
615 0 ▶ if ($UR::initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::initialized) { } 0 0 0
616 0 my $property_meta = $subject_class_meta->property_meta_for_name($property_name);
617 0 ▶ unless (defined $property_meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $property_meta) 0 0 0
618 0 push @extra_key_pos, $kn-1;
619 0 push @extra_value_pos, $vn-1;
620 0 next;
621 }
622 0 $data_type = $property_meta->data_type;
623 0 $is_many = $property_meta->is_many;
624 }
625 else {
626 0 ▶ if (exists $subject_class_meta->{has}{$property_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $subject_class_meta->{"has"}{$property_name}) 0 0 0
627 0 $data_type = $subject_class_meta->{has}{$property_name}{data_type};
628 0 $is_many = $subject_class_meta->{has}{$property_name}{is_many};
629 }
630 }
631 0 ▶ $data_type ||= '';
Condition0%0/2
$data_type ||= ""
l!l
00
MC/DC0%0/1
$data_type ||= ""
$data_type
Truth table0%0/2
A
A= $data_type
Aresult
11
00
632  
633 0 ▶ if ($data_type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($data_type eq 'ARRAY') { } 0 0 0
elsif (not $is_many) { } 0 0 0
634 # ensure we re-constitute the original array not a copy
635 0 push @hard_refs, $vn-1, $value;
636 0 push @swap_key_pos, $vn-1;
637 0 push @swap_key_value, $property_name;
638 }
639 elsif (not $is_many) {
640 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
641  
642 # sort and replace
643 # note that in perl5.10 and above strings like "inf*" have a numeric value
644 # causing this kind of sorting to do surprising things. Hopefully looks_like_number()
645 # does the right thing with these.
646 #
647 # undef/null sorts at the end
648 0 ▶ my $sorter = sub { if (! defined($a)) { return 1 }
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a) 0 0 0
649 0 ▶ if (! defined($b)) { return -1}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $b) 0 0 0
650 0 return $a cmp $b; };
651 0 $value = [ sort $sorter @$value ];
652  
653 # Remove duplicates from the list
654 0 my $last = $value;
655 0 ▶ for (my $i = 0; $i < @$value;) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$value) 0 0 0
656 0 ▶ if ($last eq $value->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last eq $value->[$i]) { } 0 0 0
657 0 splice(@$value, $i, 1);
658 }
659 else {
660 0 $last = $value->[$i++];
661 }
662 }
663 # push @swap_key_pos, $vn-1;
664 # push @swap_key_value, $property_name;
665 }
666 else {
667 # disable: break 47, enable: break 62
668 #push @swap_key_pos, $vn-1;
669 #push @swap_key_value, $property_name;
670 }
671 }
672 elsif (blessed($value)) {
673 0 my $property_meta = $subject_class_meta->property_meta_for_name($property_name);
674 0 ▶ unless ($property_meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($property_meta) 0 0 0
675 0 for my $class_name ($subject_class_meta->ancestry_class_names) {
676 0 my $class_object = $class_name->__meta__;
677 0 $property_meta = $subject_class_meta->property_meta_for_name($property_name);
678 0 ▶ last if $property_meta;
Branch: 1 branch
BranchTrueFalseTotal
if $property_meta 0 0 0
679 }
680 0 ▶ unless ($property_meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($property_meta) 0 0 0
681 0 Carp::croak("No property metadata for $subject_class property '$property_name'");
682 }
683 }
684  
685 0 ▶ if ($property_meta->id_by or $property_meta->reverse_as) {
Branch: 3 branches
BranchTrueFalseTotal
if ($property_meta->id_by or $property_meta->reverse_as) { } 0 0 0
elsif ($property_meta->is_valid_storage_for_value($value)) { } 0 0 0
elsif ($value->can($property_name)) { } 0 0 0
Condition0%0/3
$property_meta->id_by or $property_meta->reverse_as
l!l&&r!l&&!r
000
MC/DC0%0/2
$property_meta->id_by or $property_meta->reverse_as
$property_meta->id_by $property_meta->reverse_as
Truth table0%0/3
A or B
A= $property_meta->id_byB= $property_meta->reverse_as
ABresult
1X1
011
000
686 0 my $property_meta = $subject_class_meta->property_meta_for_name($property_name);
687 0 ▶ unless ($property_meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($property_meta) 0 0 0
688 0 Carp::croak("No property metadata for $subject_class property '$property_name'");
689 }
690  
691 0 my @joins = $property_meta->get_property_name_pairs_for_join();
692 0 for my $join (@joins) {
693 # does this really work for >1 joins?
694 0 my ($my_method, $their_method) = @$join;
695 0 push @xadd_keys, $my_method;
696 0 push @xadd_values, $value->$their_method;
697 }
698 # TODO: this may need to be moved into the above get_property_name_pairs_for_join(),
699 # but the exact syntax for expressing that this is part of the join is unclear.
700 0 ▶ if (my $id_class_by = $property_meta->id_class_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id_class_by = $property_meta->id_class_by) 0 0 0
701 0 push @xadd_keys, $id_class_by;
702 0 push @xadd_values, ref($value);
703 }
704 0 push @xremove_keys, $kn-1;
705 0 push @xremove_values, $vn-1;
706 }
707 elsif ($property_meta->is_valid_storage_for_value($value)) {
708 0 push @hard_refs, $vn-1, $value;
709 }
710 elsif ($value->can($property_name)) {
711 # TODO: stop suporting foo_id => $foo, since you can do foo=>$foo, and foo_id=>$foo->id
712 # Carp::cluck("using $property_name => \$obj to get $property_name => \$obj->$property_name is deprecated...");
713 0 $value = $value->$property_name;
714 }
715 else {
716 0 ▶ $operator = 'eq' unless $operator;
Branch: 1 branch
BranchTrueFalseTotal
unless $operator 0 0 0
717 0 $DB::single = 1;
718 0 print $value->isa($property_meta->_data_type_as_class_name),"\n";
719 0 print $value->isa($property_meta->_data_type_as_class_name),"\n";
720 0 Carp::croak("Invalid data type in rule. A value of type " . ref($value) . " cannot be used in class $subject_class property '$property_name' with operator $operator!");
721 }
722 # end of handling a value which is an arrayref
723 }
724 elsif ($ref ne 'HASH') {
725 # other reference, code, etc.
726 0 push @hard_refs, $vn-1, $value;
727 }
728 }
729 }
730 532 push @keys, @xadd_keys;
731 532 push @values, @xadd_values;
732  
733 532 ▶ if (@swap_key_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (@swap_key_pos) 0 532 532
734 0 @keys[@swap_key_pos] = @swap_key_value;
735 }
736  
737 532 ▶ if (@extra_key_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extra_key_pos) 0 532 532
738 0 push @xremove_keys, @extra_key_pos;
739 0 push @xremove_values, @extra_value_pos;
740 0 ▶ for (my $n = 0; $n < @extra_key_pos; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @extra_key_pos) 0 0 0
741 0 push @extra, $keys[$extra_key_pos[$n]], $values[$extra_value_pos[$n]];
742 }
743 }
744  
745 532 ▶ if (@xremove_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@xremove_keys) 0 532 532
746 0 my $write_key_idx = 0;
747 0 ▶ for (my($read_key_idx, $xremove_key_idx) = (0,0);
Branch: 1 branch
BranchTrueFalseTotal
if ($read_key_idx < @keys) 0 0 0
748 $read_key_idx < @keys;
749 $read_key_idx++
750 ) {
751 0 ▶ if ($xremove_key_idx < @xremove_keys
Branch: 1 branch
BranchTrueFalseTotal
if ($xremove_key_idx < @xremove_keys and $read_key_idx == $xremove_keys[$xremove_key_idx]) 0 0 0
Condition0%0/3
$xremove_key_idx < @xremove_keys and $read_key_idx == $xremove_keys[$xremove_key_idx]
!ll&&!rl&&r
000
MC/DC0%0/2
$xremove_key_idx < @xremove_keys and $read_key_idx == $xremove_keys[$xremove_key_idx]
$xremove_key_idx < @xremove_keys $read_key_idx == $xremove_keys[$xremove_key_idx]
Truth table0%0/3
A and B
A= $xremove_key_idx < @xremove_keysB= $read_key_idx == $xremove_keys[$xremove_key_idx]
ABresult
0X0
100
111
752 and
753 $read_key_idx == $xremove_keys[$xremove_key_idx]
754 ) {
755 0 $xremove_key_idx++;
756 0 next;
757 }
758 0 $keys[$write_key_idx++] = $keys[$read_key_idx];
759 }
760 0 $#keys = $write_key_idx-1;
761 }
762  
763 532 ▶ if (@xremove_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@xremove_values) 0 532 532
764 0 ▶ if (@hard_refs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hard_refs) 0 0 0
765 # shift the numbers down to account for positional removals
766 0 ▶ for (my $n = 0; $n < @hard_refs; $n+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @hard_refs) 0 0 0
767 0 my $ref_pos = $hard_refs[$n];
768 0 for my $rem_pos (@xremove_values) {
769 0 ▶ if ($rem_pos < $ref_pos) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rem_pos < $ref_pos) { } 0 0 0
elsif ($rem_pos == $ref_pos) { } 0 0 0
770 0 $hard_refs[$n] -= 1;
771 #print "$n from $ref_pos to $hard_refs[$n]\n";
772 0 $ref_pos = $hard_refs[$n];
773 }
774 elsif ($rem_pos == $ref_pos) {
775 0 $hard_refs[$n] = '';
776 0 $hard_refs[$n+1] = undef;
777 }
778 }
779 }
780 }
781  
782 0 my $write_value_idx = 0;
783 0 ▶ for(my($read_value_idx, $xremove_value_idx) = (0,0);
Branch: 1 branch
BranchTrueFalseTotal
if ($read_value_idx < @values) 0 0 0
784 $read_value_idx < @values;
785 $read_value_idx++
786 ) {
787 0 ▶ if ($xremove_value_idx < @xremove_values
Branch: 1 branch
BranchTrueFalseTotal
if ($xremove_value_idx < @xremove_values and $read_value_idx == $xremove_values[$xremove_value_idx]) 0 0 0
Condition0%0/3
$xremove_value_idx < @xremove_values and $read_value_idx == $xremove_values[$xremove_value_idx]
!ll&&!rl&&r
000
MC/DC0%0/2
$xremove_value_idx < @xremove_values and $read_value_idx == $xremove_values[$xremove_value_idx]
$xremove_value_idx < @xremove_values $read_value_idx == $xremove_values[$xremove_value_idx]
Truth table0%0/3
A and B
A= $xremove_value_idx < @xremove_valuesB= $read_value_idx == $xremove_values[$xremove_value_idx]
ABresult
0X0
100
111
788 and
789 $read_value_idx == $xremove_values[$xremove_value_idx]
790 ) {
791 0 $xremove_value_idx++;
792 0 next;
793 }
794 0 $values[$write_value_idx++] = $values[$read_value_idx];
795 }
796 0 $#values = $write_value_idx-1;
797 }
798  
799 532 my $template;
800 532 ▶ if (@constant_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@constant_values) { } 0 532 532
801 0 $template = UR::BoolExpr::Template::And->_fast_construct(
802 $subject_class,
803 \@keys,
804 \@constant_values,
805 );
806 }
807 else {
808 532 ▶ $template = $subject_class_meta->{cache}{"UR::BoolExpr::resolve"}{"template for class and keys without constant values"}{"$subject_class @keys"}
Condition66%2/3
$subject_class_meta->{"cache"}{"UR::BoolExpr::resolve"}{"template for class and keys without constant values"}{"$subject_class @keys"} ||= "UR::BoolExpr::Template::And"->_fast_construct($subject_class, \@keys, \@constant_values)
l!l&&r!l&&!r
2662660
MC/DC0%0/2
$subject_class_meta->{"cache"}{"UR::BoolExpr::resolve"}{"template for class and keys without constant values"}{"$subject_class @keys"} ||= "UR::BoolExpr::Template::And"->_fast_construct($subject_class, \@keys, \@constant_values)
$subject_class_meta->{"cache"}{"UR::BoolExpr::resolve"}{"template for class and keys without constant values"}{"$subject_class @keys"} "UR::BoolExpr::Template::And"->_fast_construct($subject_class, \@keys, \@constant_values)
Truth table66%2/3
A ||= B
A= $subject_class_meta->{"cache"}{"UR::BoolExpr::resolve"}{"template for class and keys without constant values"}{"$subject_class @keys"}B= "UR::BoolExpr::Template::And"->_fast_construct($subject_class, \@keys, \@constant_values)
ABresult
1X1
011
000
809 ||= UR::BoolExpr::Template::And->_fast_construct(
810 $subject_class,
811 \@keys,
812 \@constant_values,
813 );
814 }
815  
816 532 my $value_id = UR::BoolExpr::Util::values_to_value_id(@values);
817  
818 532 my $rule_id = join($UR::BoolExpr::Util::id_sep,$template->{id},$value_id);
819  
820 532 my $rule = __PACKAGE__->get($rule_id); # flyweight constructor
821  
822 532 $rule->{template} = $template;
823 532 $rule->{values} = \@values;
824  
825 532 $vn = 0;
826 532 $cn = 0;
827 532 my @list;
828 532 for my $key (@keys) {
829 532 push @list, $key;
830 532 ▶ if (UR::BoolExpr::Util::is_meta_param($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UR::BoolExpr::Util::is_meta_param($key)) { } 0 532 532
831 0 push @list, $constant_values[$cn++];
832 }
833 else {
834 532 push @list, $values[$vn++];
835 }
836 }
837 532 $rule->{_params_list} = \@list;
838  
839 532 ▶ if (@hard_refs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hard_refs) 0 532 532
840 0 $rule->{hard_refs} = { @hard_refs };
841 0 delete $rule->{hard_refs}{''};
842 }
843  
844 532 $resolve_depth--;
845 532 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 532 0 532
elsif (@extra and defined wantarray) { } 0 0 0
846 532 return ($rule, @extra);
847 }
848 ▶ elsif (@extra && defined wantarray) {
Condition0%0/3
@extra and defined wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
@extra and defined wantarray
@extra defined wantarray
Truth table0%0/3
A and B
A= @extraB= defined wantarray
ABresult
0X0
100
111
849 0 ▶ Carp::confess("Unknown parameters in rule for $subject_class: " . join(",", map { defined($_) ? "'$_'" : "(undef)" } @extra));
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
850 }
851 else {
852 0 return $rule;
853 }
854 }
855  
856 0 ▶sub _params_list {
Subroutine _params_list: not called CC=9 SCAR=45.0
857 0 ▶ my $list = $_[0]->{_params_list} ||= do {
Condition0%0/3
$_[0]{"_params_list"} ||= do { my $self = $_[0]; my $template = $self->template; $self->values unless $self->{"values"}; my @list; my $template_class = ref $template; if ($template_class eq "UR::BoolExpr::Template::And") { () = ($template->{"_keys"}, $self->{"values"}, $template->{"_constant_values"}); my $vn = 0; my $cn = 0; foreach my $key (@$k) { push @list, $key; if (UR::BoolExpr::Util::is_meta_param($key)) { push @list, $$c[$cn++]; } else { push @list, $$v[$vn++]; } ; } ; } elsif ($template_class eq "UR::BoolExpr::Template::Or") { my @sublist; my(@u) = $self->underlying_rules; foreach my $u (@u) { my(@p) = $u->_params_list; push @sublist, \@p; } ; @list = ("-or", \@sublist); } elsif ($template_class->isa("UR::BoolExpr::PropertyComparison")) { @list = ($template->logic_detail, [@{$self->{"values"};}]); } ; \@list }
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"_params_list"} ||= do { my $self = $_[0]; my $template = $self->template; $self->values unless $self->{"values"}; my @list; my $template_class = ref $template; if ($template_class eq "UR::BoolExpr::Template::And") { () = ($template->{"_keys"}, $self->{"values"}, $template->{"_constant_values"}); my $vn = 0; my $cn = 0; foreach my $key (@$k) { push @list, $key; if (UR::BoolExpr::Util::is_meta_param($key)) { push @list, $$c[$cn++]; } else { push @list, $$v[$vn++]; } ; } ; } elsif ($template_class eq "UR::BoolExpr::Template::Or") { my @sublist; my(@u) = $self->underlying_rules; foreach my $u (@u) { my(@p) = $u->_params_list; push @sublist, \@p; } ; @list = ("-or", \@sublist); } elsif ($template_class->isa("UR::BoolExpr::PropertyComparison")) { @list = ($template->logic_detail, [@{$self->{"values"};}]); } ; \@list }
$_[0]{"_params_list"} do { my $self = $_[0]; my $template = $self->template; $self->values unless $self->{"values"}; my @list; my $template_class = ref $template; if ($template_class eq "UR::BoolExpr::Template::And") { () = ($template->{"_keys"}, $self->{"values"}, $template->{"_constant_values"}); my $vn = 0; my $cn = 0; foreach my $key (@$k) { push @list, $key; if (UR::BoolExpr::Util::is_meta_param($key)) { push @list, $$c[$cn++]; } else { push @list, $$v[$vn++]; } ; } ; } elsif ($template_class eq "UR::BoolExpr::Template::Or") { my @sublist; my(@u) = $self->underlying_rules; foreach my $u (@u) { my(@p) = $u->_params_list; push @sublist, \@p; } ; @list = ("-or", \@sublist); } elsif ($template_class->isa("UR::BoolExpr::PropertyComparison")) { @list = ($template->logic_detail, [@{$self->{"values"};}]); } ; \@list }
Truth table0%0/3
A ||= B
A= $_[0]{"_params_list"}B= do { my $self = $_[0]; my $template = $self->template; $self->values unless $self->{"values"}; my @list; my $template_class = ref $template; if ($template_class eq "UR::BoolExpr::Template::And") { () = ($template->{"_keys"}, $self->{"values"}, $template->{"_constant_values"}); my $vn = 0; my $cn = 0; foreach my $key (@$k) { push @list, $key; if (UR::BoolExpr::Util::is_meta_param($key)) { push @list, $$c[$cn++]; } else { push @list, $$v[$vn++]; } ; } ; } elsif ($template_class eq "UR::BoolExpr::Template::Or") { my @sublist; my(@u) = $self->underlying_rules; foreach my $u (@u) { my(@p) = $u->_params_list; push @sublist, \@p; } ; @list = ("-or", \@sublist); } elsif ($template_class->isa("UR::BoolExpr::PropertyComparison")) { @list = ($template->logic_detail, [@{$self->{"values"};}]); } ; \@list }
ABresult
1X1
011
000
858 0 my $self = $_[0];
859 0 my $template = $self->template;
860 0 ▶ $self->values unless $self->{values};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"values"} 0 0 0
861 0 my @list;
862 # are method calls really too expensive here?
863 0 my $template_class = ref($template);
864 0 ▶ if ($template_class eq 'UR::BoolExpr::Template::And') {
Branch: 3 branches
BranchTrueFalseTotal
if ($template_class eq 'UR::BoolExpr::Template::And') { } 0 0 0
elsif ($template_class eq 'UR::BoolExpr::Template::Or') { } 0 0 0
elsif ($template_class->isa('UR::BoolExpr::PropertyComparison')) { } 0 0 0
865 0 my ($k,$v,$c) = ($template->{_keys}, $self->{values}, $template->{_constant_values});
866 0 my $vn = 0;
867 0 my $cn = 0;
868 0 for my $key (@$k) {
869 0 push @list, $key;
870 0 ▶ if (UR::BoolExpr::Util::is_meta_param($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UR::BoolExpr::Util::is_meta_param($key)) { } 0 0 0
871 0 push @list, $c->[$cn++];
872 }
873 else {
874 0 push @list, $v->[$vn++];
875 }
876 }
877 }
878 elsif ($template_class eq 'UR::BoolExpr::Template::Or') {
879 0 my @sublist;
880 0 my @u = $self->underlying_rules();
881 0 for my $u (@u) {
882 0 my @p = $u->_params_list;
883 0 push @sublist, \@p;
884 }
885 0 @list = (-or => \@sublist);
886 }
887 elsif ($template_class->isa("UR::BoolExpr::PropertyComparison")) {
888 0 @list = ($template->logic_detail => [@{$self->{values}}]);
889 }
890 0 \@list;
891 };
892 0 return @$list;
893 }
894  
895 1064 ▶sub normalize {
Subroutine normalize: called CC=4 SCAR=14.1
Pod documented
896 1064 my $self = shift;
897  
898 1064 my $rule_template = $self->template;
899  
900 1064 ▶ if ($rule_template->{is_normalized}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_template->{"is_normalized"}) 532 532 1064
901 532 return $self;
902 }
903 532 my @unnormalized_values = $self->values();
904  
905 532 my $normalized = $rule_template->get_normalized_rule_for_values(@unnormalized_values);
906 532 ▶ return unless defined $normalized;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $normalized 0 532 532
907  
908 532 ▶ if (my $special = $self->{hard_refs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $special = $self->{"hard_refs"}) 0 532 532
909 0 $normalized->{hard_refs} = $rule_template->_normalize_non_ur_values_hash($special);
910 }
911 532 return $normalized;
912 }
913  
914 # a handful of places still use this
915 266 ▶sub legacy_params_hash {
Subroutine legacy_params_hash: called CC=3 SCAR=11.3
Pod undocumented
916 266 my $self = shift;
917  
918 # See if we have one already.
919 266 my $params_array = $self->{legacy_params_array};
920 266 ▶ return { @$params_array } if $params_array;
Branch: 1 branch
BranchTrueFalseTotal
if $params_array 0 266 266
921  
922 # Make one by starting with the one on the rule template
923 266 my $rule_template = $self->template;
924 266 my $params = { %{$rule_template->legacy_params_hash}, $self->params_list };
925  
926 # If the template has a _param_key, fill it in.
927 266 ▶ if (exists $params->{_param_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"_param_key"}) 0 266 266
928 0 $params->{_param_key} = $self->id;
929 }
930  
931 # This was cached above and will return immediately on the next call.
932 # Note: the caller should copy this reference before making changes.
933 266 $self->{legacy_params_array} = [ %$params ];
934 266 return $params;
935 }
936  
937  
938 266 my $LOADED_BXPARSE = 0;
939 0 ▶sub resolve_for_string {
Subroutine resolve_for_string: not called CC=11 SCAR=48.8
Pod undocumented
940 0 my ($class, $subject_class_name, $filter_string, $usage_hints_string, $order_string, $page_string) = @_;
941  
942 0 ▶ unless ($LOADED_BXPARSE) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LOADED_BXPARSE) 0 0 0
943 0 my $exception = do {
944 0 local $@;
945 0 eval { require UR::BoolExpr::BxParser };
946 0 $@;
947 };
948 0 ▶ if ($exception) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exception) 0 0 0
949 0 Carp::croak("resolve_for_string() can't load UR::BoolExpr::BxParser: $exception");
950 }
951 0 $LOADED_BXPARSE=1;
952 }
953  
954 #$DB::single=1;
955 #my $tree = UR::BoolExpr::BxParser::parse($filter_string, tokdebug => 1, yydebug => 7);
956 0 my($tree, $remaining_strref) = UR::BoolExpr::BxParser::parse($filter_string);
957 0 ▶ unless ($tree) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree) 0 0 0
958 0 Carp::croak("resolve_for_string() couldn't parse string \"$filter_string\"");
959 }
960  
961 0 ▶ push @$tree, '-hints', [split(',',$usage_hints_string) ] if ($usage_hints_string);
Branch: 1 branch
BranchTrueFalseTotal
if $usage_hints_string 0 0 0
962 0 ▶ push @$tree, '-order_by', [split(',',$order_string) ] if ($order_string);
Branch: 1 branch
BranchTrueFalseTotal
if $order_string 0 0 0
963 0 ▶ push @$tree, '-page', [split(',',$page_string) ] if ($page_string);
Branch: 1 branch
BranchTrueFalseTotal
if $page_string 0 0 0
964  
965 0 my ($bx, @extra);
966 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
967 0 ($bx, @extra) = UR::BoolExpr->resolve($subject_class_name, @$tree);
968 } else {
969 0 $bx = UR::BoolExpr->resolve($subject_class_name, @$tree);
970 }
971 0 ▶ unless ($bx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bx) 0 0 0
972 0 Carp::croak("Can't create BoolExpr on $subject_class_name from params generated from string "
973 . $filter_string . " which parsed as:\n"
974 . Data::Dumper::Dumper($tree));
975 }
976 0 ▶ if ($$remaining_strref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$remaining_strref) 0 0 0
977 0 Carp::croak("Trailing input after the parsable end of the filter string: '". $$remaining_strref."'");
978 }
979 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
980 0 return ($bx, @extra);
981 } else {
982 0 return $bx;
983 }
984 }
985  
986 0 ▶sub _resolve_from_filter_array {
Subroutine _resolve_from_filter_array: not called CC=23 SCAR=63.1
987 0 my $class = shift;
988  
989 0 my $subject_class_name = shift;
990 0 my $filters = shift;
991 0 my $usage_hints = shift;
992 0 my $order = shift;
993 0 my $page = shift;
994  
995 0 my @rule_filters;
996  
997 my @keys;
998 0 my @values;
999  
1000 0 for my $fdata (@$filters) {
1001 0 my $rule_filter;
1002  
1003 # rule component
1004 0 my $key = $fdata->[0];
1005 0 my $value;
1006  
1007 # process the operator
1008 0 ▶ if ($fdata->[1] =~ /^!?(:|@|between|in)$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fdata->[1] =~ /^!?(:|@|between|in)$/i) { } 0 0 0
elsif (length $fdata->[2] == 0) { } 0 0 0
1009  
1010 0 my @list_parts;
1011 my @range_parts;
1012  
1013 0 ▶ if ($fdata->[1] eq "@") {
Branch: 1 branch
BranchTrueFalseTotal
if ($fdata->[1] eq '@') { } 0 0 0
1014 # file path
1015 0 my $fh = IO::File->new($fdata->[2]);
1016 0 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
1017 0 die "Failed to open file $fdata->[2]: $!\n";
1018 }
1019 0 @list_parts = $fh->getlines;
1020 0 chomp @list_parts;
1021 0 $fh->close;
1022 }
1023 else {
1024 0 @list_parts = split(/\//,$fdata->[2]);
1025 0 @range_parts = split(/-/,$fdata->[2]);
1026 }
1027  
1028 0 ▶ if (@list_parts > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@list_parts > 1) { } 0 0 0
elsif (@range_parts >= 2) { } 0 0 0
1029 0 ▶ my $op = ($fdata->[1] =~ /^!/ ? 'not in' : 'in');
Branch: 1 branch
BranchTrueFalseTotal
$fdata->[1] =~ /^!/ ? : 0 0 0
1030 # rule component
1031 0 ▶ if (substr($key, -3, 3) ne ' in') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($key, -3, 3) ne " in") 0 0 0
1032 0 $key = join(' ', $key, $op);
1033 }
1034 0 $value = \@list_parts;
1035 0 $rule_filter = [$fdata->[0],$op,\@list_parts];
1036 }
1037 elsif (@range_parts >= 2) {
1038 0 ▶ if (@range_parts > 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@range_parts > 2) { } 0 0 0
elsif (@range_parts == 2) { } 0 0 0
1039 0 ▶ if (@range_parts % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@range_parts % 2) { } 0 0 0
1040 0 die "The \":\" operator expects a range sparated by a single dash: @range_parts ." . "\n";
1041 }
1042 else {
1043 0 my $half = (@range_parts)/2;
1044 0 $a = join("-",@range_parts[0..($half-1)]);
1045 0 $b = join("-",@range_parts[$half..$#range_parts]);
1046 }
1047 }
1048 elsif (@range_parts == 2) {
1049 0 ($a,$b) = @range_parts;
1050 }
1051 else {
1052 0 die 'The ":" operator expects a range sparated by a dash.' . "\n";
1053 }
1054  
1055 0 $key = $fdata->[0] . " between";
1056 0 $value = [$a, $b];
1057 0 $rule_filter = [$fdata->[0], "between", [$a, $b] ];
1058 }
1059 else {
1060 0 die 'The ":" operator expects a range sparated by a dash, or a slash-separated list.' . "\n";
1061 }
1062  
1063 }
1064 # this accounts for cases where value is null
1065 elsif (length($fdata->[2])==0) {
1066 0 ▶ if ($fdata->[1] eq "=") {
Branch: 1 branch
BranchTrueFalseTotal
if ($fdata->[1] eq '=') { } 0 0 0
1067 0 $key = $fdata->[0];
1068 0 $value = undef;
1069 0 $rule_filter = [ $fdata->[0], "=", undef ];
1070 }
1071 else {
1072 0 $key = $fdata->[0] . " !=";
1073 0 $value = undef;
1074 0 $rule_filter = [ $fdata->[0], "!=", undef ];
1075 }
1076 }
1077 else {
1078 0 ▶ $key = $fdata->[0] . ($fdata->[1] and $fdata->[1] ne '='? ' ' . $fdata->[1] : '');
Branch: 1 branch
BranchTrueFalseTotal
$fdata->[1] ne '=' ? : 0 0 0
Condition0%0/3
$fdata->[1] && ($fdata->[1] ne '=' ? ' ' . $fdata->[1] : '')
!ll&&!rl&&r
000
MC/DC0%0/2
$fdata->[1] && ($fdata->[1] ne '=' ? ' ' . $fdata->[1] : '')
$fdata->[1] ($fdata->[1] ne '=' ? ' ' . $fdata->[1] : '')
Truth table0%0/3
A && B
A= $fdata->[1]B= ($fdata->[1] ne '=' ? ' ' . $fdata->[1] : '')
ABresult
0X0
100
111
1079 0 $value = $fdata->[2];
1080 0 $rule_filter = [ @$fdata ];
1081 }
1082  
1083 0 push @keys, $key;
1084 0 push @values, $value;
1085 }
1086 0 ▶ if ($usage_hints or $order or $page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usage_hints or $order or $page) { } 0 0 0
Condition0%0/6
$usage_hints or $order or $page
l!l&&r!l&&!r
000
$usage_hints or $order
l!l&&r!l&&!r
000
MC/DC0%0/3
$usage_hints or $order or $page
$usage_hints $order $page
Truth table0%0/4
A or B or C
A= $usage_hintsB= $orderC= $page
ABCresult
1XX1
01X1
0011
0000
1087 # todo: incorporate hints in a smarter way
1088 0 my %p;
1089 0 for my $key (@keys) {
1090 0 $p{$key} = shift @values;
1091 }
1092 0 ▶ return $class->resolve(
Branch: 3 branches
BranchTrueFalseTotal
$usage_hints ? : 0 0 0
$order ? : 0 0 0
$page ? : 0 0 0
1093 $subject_class_name,
1094 %p,
1095 ($usage_hints ? (-hints => $usage_hints) : () ),
1096 ($order ? (-order => $order) : () ),
1097 ($page ? (-page => $page) : () ),
1098 );
1099 }
1100 else {
1101 0 return UR::BoolExpr->_resolve_from_subject_class_name_keys_and_values(
1102 subject_class_name => $subject_class_name,
1103 keys => \@keys,
1104 values=> \@values,
1105 );
1106 }
1107  
1108 }
1109  
1110 0 ▶sub _resolve_from_subject_class_name_keys_and_values {
Subroutine _resolve_from_subject_class_name_keys_and_values: not called CC=5 SCAR=34.0
1111 0 my $class = shift;
1112  
1113 0 my %params = @_;
1114 0 my $subject_class_name = $params{subject_class_name};
1115 0 ▶ my @values = @{ $params{values} || [] };
Condition0%0/2
$params{'values'} || []
l!l
00
MC/DC0%0/1
$params{'values'} || []
$params{'values'}
Truth table0%0/2
A
A= $params{'values'}
Aresult
11
00
1116 0 ▶ my @constant_values = @{ $params{constant_values} || [] };
Condition0%0/2
$params{'constant_values'} || []
l!l
00
MC/DC0%0/1
$params{'constant_values'} || []
$params{'constant_values'}
Truth table0%0/2
A
A= $params{'constant_values'}
Aresult
11
00
1117 0 ▶ my @keys = @{ $params{keys} || [] };
Condition0%0/2
$params{'keys'} || []
l!l
00
MC/DC0%0/1
$params{'keys'} || []
$params{'keys'}
Truth table0%0/2
A
A= $params{'keys'}
Aresult
11
00
1118 0 ▶ die "unexpected params: " . Data::Dumper::Dumper(\%params) if %params;
Branch: 1 branch
BranchTrueFalseTotal
if %params 0 0 0
1119  
1120 0 my $value_id = UR::BoolExpr::Util::values_to_value_id(@values);
1121 0 my $constant_value_id = UR::BoolExpr::Util::values_to_value_id(@constant_values);
1122  
1123 0 my $template_id = $subject_class_name . '/And/' . join(",",@keys) . "/" . $constant_value_id;
1124 0 my $rule_id = join($UR::BoolExpr::Util::id_sep,$template_id,$value_id);
1125  
1126 0 my $rule = __PACKAGE__->get($rule_id);
1127  
1128 0 $rule->{values} = \@values;
1129  
1130 0 return $rule;
1131 }
1132  
1133 266 1;
1134  
1135 =pod
1136  
1137 =head1 NAME
1138  
1139 UR::BoolExpr - a "where clause" for objects
1140  
1141 =head1 SYNOPSIS
1142  
1143 my $o = Acme::Employee->create(
1144 ssn => '123-45-6789',
1145 name => 'Pat Jones',
1146 status => 'active',
1147 start_date => UR::Context->current->now,
1148 payroll_category => 'hourly',
1149 boss => $other_employee,
1150 );
1151  
1152 my $bx = Acme::Employee->define_boolexpr(
1153 'payroll_category' => 'hourly',
1154 'status' => ['active','terminated'],
1155 'name like' => '%Jones',
1156 'ssn matches' => '\d{3}-\d{2}-\d{4}',
1157 'start_date between' => ['2009-01-01','2009-02-01'],
1158 'boss.name in' => ['Cletus Titus', 'Mitzy Mayhem'],
1159 );
1160  
1161 $bx->evaluate($o); # true
1162  
1163 $bx->specifies_value_for('payroll_category') # true
1164  
1165 $bx->value_for('payroll_cagtegory') # 'hourly'
1166  
1167 $o->payroll_category('salary');
1168 $bx->evaluate($o); # false
1169  
1170 # these could take either a boolean expression, or a list of params
1171 # from which it will generate one on-the-fly
1172 my $set = Acme::Employee->define_set($bx); # same as listing all of the params
1173 my @matches = Acme::Employee->get($bx); # same as above, but returns the members
1174  
1175 my $bx2 = $bx->reframe('boss');
1176 #'employees.payroll_category' => 'hourly',
1177 #'employees.status' => ['active','terminated'],
1178 #'employees.name like' => '%Jones',
1179 #'employees.ssn matches' => '\d{3}-\d{2}-\d{4}',
1180 #'employees.start_date between' => ['2009-01-01','2009-02-01'],
1181 #'name in' => ['Cletus Titus', 'Mitzy Mayhem'],
1182  
1183 my $bx3 = $bx->flatten();
1184 # any indirection in the params takes the form a.b.c at the lowest level
1185 # also 'payroll_category' might become 'pay_history.category', and 'pay_history.is_current' => 1 is added to the list
1186 # if this parameter has that as a custom filter
1187  
1188  
1189 =head1 DESCRIPTION
1190  
1191 A UR::BoolExpr object captures a set of match criteria for some class of object.
1192  
1193 Calls to get(), create(), and define_set() all use this internally to objectify
1194 their parameters. If given a boolean expression object directly they will use it.
1195 Otherwise they will construct one from the parameters given.
1196  
1197 They have a 1:1 correspondence within the WHERE clause in an SQL statement where
1198 RDBMS persistence is used. They also imply the FROM clause in these cases,
1199 since the query properties control which joins must be included to return
1200 the matching object set.
1201  
1202 =head1 REFLECTION
1203  
1204 The data used to create the boolean expression can be re-extracted:
1205  
1206 my $c = $r->subject_class_name;
1207 # $c eq "GSC::Clone"
1208  
1209 my @p = $r->params_list;
1210 # @p = four items
1211  
1212 my %p = $r->params_list;
1213 # %p = two key value pairs
1214  
1215 =head1 TEMPLATE SUBCLASSES
1216  
1217 The template behind the expression can be of type ::Or, ::And or ::PropertyComparison.
1218 These classes handle all of the operating logic for the expressions.
1219  
1220 Each of those classes incapsulates 0..n of the next type in the list. All templates
1221 simplify to this level. See L<UR::BoolExpr::Template> for details.
1222  
1223 =head1 CONSTRUCTOR
1224  
1225 =over 4
1226  
1227 my $bx = UR::BoolExpr->resolve('Some::Class', property_1 => 'value_1', ... property_n => 'value_n');
1228 my $bx1 = Some::Class->define_boolexpr(property_1 => value_1, ... property_n => 'value_n');
1229 my $bx2 = Some::Class->define_boolexpr('property_1 >' => 12345);
1230 my $bx3 = UR::BoolExpr->resolve_for_string(
1231 'Some::Class',
1232 'property_1 = value_1 and ( property_2 < value_2 or property_3 = value_3 )',
1233 );
1234  
1235 Returns a UR::BoolExpr object that can be used to perform tests on the given class and
1236 properties. The default comparison for each property is equality. The third example shows
1237 using greater-than operator for property_1. The last example shows constructing a
1238 UR::BoolExpr from a string containing properties, operators and values joined with
1239 'and' and 'or', with parentheses indicating precedence.
1240  
1241 =back
1242  
1243 C<resolve_for_string()> can parse simple and complicated expressions. A simple expression
1244 is a property name followed by an operator followed by a value. The property name can be
1245 a series of properties joined by dots (.) to indicate traversal of multiple layers of
1246 indirect properties. Values that include spaces, characters that look like operators,
1247 commas, or other special characters should be enclosed in quotes.
1248  
1249 The parser understands all the same operators the underlying C<resolve()> method understands:
1250 =, <, >, <=, >=, "like", "between" and "in". Operators may be prefixed by a bang (!) or the
1251 word "not" to negate the operator. The "like" operator understands the SQL wildcards % and _.
1252 Values for the "between" operator should be separated by a minus (-). Values for the "in"
1253 operator should begin with a left bracket, end with a right bracket, and have commas between
1254 them. For example:
1255 name_property in [Bob,Fred,Joe]
1256  
1257 Simple expressions may be joined together with the words "and" and "or" to form a more
1258 complicated expression. "and" has higher precedence than "or", and parentheses can
1259 surround sub-expressions to indicate the requested precedence. For example:
1260 ((prop1 = foo or prop2 = 1) and (prop2 > 10 or prop3 like 'Yo%')) or prop4 in [1,2,3]
1261  
1262 In general, whitespace is insignificant. The strings "prop1 = 1" is parsed the same as
1263 "prop1=1". Spaces inside quoted value strings are preserved. For backward compatibility
1264 with the deprecated string parser, bare words that appear after the operators =,<,>,<=
1265 and >= which are separated by one or more spaces is treated as if it had quotes around
1266 the list of words starting with the first character of the first word and ending with
1267 the last character of the last word, meaning that spaces at the start and end of the
1268 list are trimmed.
1269  
1270 Specific ordering may be requested by putting an "order by" clause at the end, and is the
1271 same as using a -order argument to resolve():
1272 score > 10 order by name,score.
1273  
1274 Likewise, grouping and Set construction is indicated with a "group by" clause:
1275 score > 10 group by color
1276  
1277 =head1 METHODS
1278  
1279 =over 4
1280  
1281 =item evaluate
1282  
1283 $bx->evaluate($object)
1284  
1285 Returns true if the given object satisfies the BoolExpr
1286  
1287  
1288 =item template_and_values
1289  
1290 ($template, @values) = $bx->template_and_values();
1291  
1292 Returns the UR::BoolExpr::Template and list of the values for the given BoolExpr
1293  
1294 =item is_subset_of
1295  
1296 $bx->is_subset_of($other_bx)
1297  
1298 Returns true if the set of objects that matches this BoolExpr is a subset of
1299 the set of objects that matches $other_bx. In practice this means:
1300  
1301 * The subject class of $bx isa the subject class of $other_bx
1302 * all the properties from $bx also appear in $other_bx
1303 * the operators and values for $bx's properties match $other_bx
1304  
1305 =item values
1306  
1307 @values = $bx->values
1308  
1309 Return a list of the values from $bx. The values will be in the same order
1310 the BoolExpr was created from
1311  
1312 =item value_for_id
1313  
1314 $id = $bx->value_for_id
1315  
1316 If $bx's properties include all the ID properties of its subject class,
1317 C<value_for_id> returns that value. Otherwise, it returns the empty list.
1318 If the subject class has more than one ID property, this returns the value
1319 of the composite ID.
1320  
1321 =item specifies_value_for
1322  
1323 $bx->specifies_value_for('property_name');
1324  
1325 Returns true if the filter list of $bx includes the given property name
1326  
1327 =item value_for
1328  
1329 my $value = $bx->value_for('property_name');
1330  
1331 Return the value for the given property
1332  
1333 =item operator_for
1334  
1335 my $operator = $bx->operator_for('property_name');
1336  
1337 Return a string for the operator of the given property. A value of '' (the
1338 empty string) means equality ("="). Other possible values include '<', '>',
1339 '<=', '>=', 'between', 'true', 'false', 'in', 'not <', 'not >', etc.
1340  
1341 =item normalize
1342  
1343 $bx2 = $bx->normalize;
1344  
1345 A boolean expression can be changed in incidental ways and still be equivalent.
1346 This method converts the expression into a normalized form so that it can be
1347 compared to other normalized expressions without incidental differences
1348 affecting the comparison.
1349  
1350 =item flatten
1351  
1352 $bx2 = $bx->flatten();
1353  
1354 Transforms a boolean expression into a functional equivalent where
1355 indirect properties are turned into property chains.
1356  
1357 For instance, in a class with
1358  
1359 a => { is => "A", id_by => "a_id" },
1360 b => { via => "a", to => "bb" },
1361 c => { via => "b", to => "cc" },
1362  
1363 An expression of:
1364  
1365 c => 1234
1366  
1367 Becomes:
1368  
1369 a.bb.cc => 1234
1370  
1371 In cases where one of the indirect properties includes a "where" clause,
1372 the flattened expression would have an additional value for each element:
1373  
1374 a => { is => "A", id_by => "a_id" },
1375 b => { via => "a", to => "bb" },
1376 c => { via => "b", where ["xx" => 5678], to => "cc" },
1377  
1378 An expression of:
1379  
1380 c => 1234
1381  
1382 Becomes:
1383  
1384 a.bb.cc => 1234
1385 a.bb.xx => 5678
1386  
1387  
1388  
1389 =item reframe
1390  
1391 $bx = Acme::Order->define_boolexpr(status => 'active');
1392 $bx2 = $bx->reframe('customer');
1393  
1394 The above will turn a query for orders which are active into a query for
1395 customers with active orders, presuming an Acme::Order has a property called
1396 "customer" with a defined relationship to another class.
1397  
1398 =back
1399  
1400 =head1 INTERNAL STRUCTURE
1401  
1402 A boolean expression (or "rule") has an "id", which completely describes the rule in stringified form,
1403 and a method called evaluate($o) which tests the rule on a given object.
1404  
1405 The id is composed of two parts:
1406 - A template_id.
1407 - A value_id.
1408  
1409 Nearly all real work delegates to the template to avoid duplication of cached details.
1410  
1411 The template_id embeds several other properties, for which the rule delegates to it:
1412 - subject_class_name, objects of which the rule can be applied-to
1413 - subclass_name, the subclass of rule (property comparison, and, or "or")
1414 - the body of the rule either key-op-val, or a list of other rules
1415  
1416 For example, the rule GSC::Clone name=x,chromosome>y:
1417 - the template_id embeds:
1418 subject_class_name = GSC::Clone
1419 subclass_name = UR::BoolExpr::And
1420 and the key-op pairs in sorted order: "chromosome>,name="
1421 - the value_id embeds the x,y values in a special format
1422  
1423 =head1 EXAMPLES
1424  
1425  
1426 my $bool = $x->evaluate($obj);
1427  
1428 my $t = GSC::Clone->template_for_params(
1429 "status =",
1430 "chromosome []",
1431 "clone_name like",
1432 "clone_size between"
1433 );
1434  
1435 my @results = $t->get_matching_objects(
1436 "active",
1437 [2,4,7],
1438 "Foo%",
1439 [100000,200000]
1440 );
1441  
1442 my $r = $t->get_rule($v1,$v2,$v3);
1443  
1444 my $t = $r->template;
1445  
1446 my @results = $t->get_matching_objects($v1,$v2,$v3);
1447 my @results = $r->get_matching_objects();
1448  
1449 @r = $r->underlying_rules();
1450 for (@r) {
1451 print $r->evaluate($c1);
1452 }
1453  
1454 my $rt = $r->template();
1455 my @rt = $rt->get_underlying_rule_templates();
1456  
1457 $r = $rt->get_rule_for_values(@v);
1458  
1459 $r = UR::BoolExpr->resolve_for_string(
1460 'My::Class',
1461 'name=Bob and (score=10 or score < 5)',
1462 );
1463  
1464 =head1 SEE ALSO
1465  
1466 UR(3), UR::Object(3), UR::Object::Set(3), UR::BoolExpr::Template(3)
1467  
1468 =cut
« lib/UR/DBI/Report.pm ↑ Summary lib/UR/Context/ImportIterator.pm »