blib/lib/UR/BoolExpr/Template.pm untested

filter: Statementstmt 0.0% 0 / 328 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 75 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 589 SCAR 83.0
CC
63
Cov
0%
legacy_params_hash
306.0
is_unique
72.0
get
72.0
CRAP 4032.0
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 use warnings;
15 use strict;
16  
17 use Scalar::Util qw(blessed);
18 use Data::Dumper;
19 use UR;
20  
21 our @CARP_NOT = qw(UR::BoolExpr);
22  
23 # readable stringification
24 use overload ('""' => 'id');
25 use overload ('==' => sub { $_[0] . '' eq $_[1] . '' } );
26 use overload ('eq' => sub { $_[0] . '' eq $_[1] . '' } );
27  
28 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 our $VERSION = "0.46"; # UR $VERSION;;
70  
71 # Borrow from the util package.
72 # This will go away with refactoring.
73  
74 our $id_sep = $UR::BoolExpr::Util::id_sep;
75 our $record_sep = $UR::BoolExpr::Util::record_sep;
76 our $unit_sep = $UR::BoolExpr::Util::unit_sep;
77 our $null_value = $UR::BoolExpr::Util::null_value;
78 our $empty_string = $UR::BoolExpr::Util::empty_string;
79 our $empty_list = $UR::BoolExpr::Util::empty_list;
80  
81 # Names of the optional flags you can add to a rule
82 our @meta_param_names = qw(recursion_desc hints order_by group_by aggregate limit offset);
83  
84 # Wrappers for regular properties
85  
86 sub _property_names {
87 return @{ $_[0]->{_property_names_arrayref} };
88 }
89  
90 # Indexability methods
91  
92 sub _indexable_property_names {
93 $_[0]->_resolve_indexing_params unless $_[0]->{_resolve_indexing_params};
94 @{ $_[0]->{_indexable_property_names} }
95 }
96  
97 sub _indexable_property_positions {
98 $_[0]->_resolve_indexing_params unless $_[0]->{_resolve_indexing_params};
99 @{ $_[0]->{_indexable_property_positions} }
100 }
101  
102 sub _is_fully_indexable {
103 $_[0]->_resolve_indexing_params unless $_[0]->{_resolve_indexing_params};
104 $_[0]->{_is_fully_indexable};
105 }
106  
107 sub _resolve_indexing_params {
108 my $self = $_[0];
109  
110 my $class_meta = UR::Object::Type->get($self->subject_class_name);
111  
112 my @all_names = $self->_property_names;
113  
114 for my $name (@all_names) {
115 my $m = $class_meta->property($name);
116 unless ($m) {
117 #$DB::single = 1;
118 $class_meta->property($name);
119 #$DB::single = 1;
120 $class_meta->property($name);
121 }
122 }
123
124 my @indexable_names =
125 sort
126 map { $_->property_name }
127 grep { $_ } #and $_->is_indexable }
128 map { $class_meta->property_meta_for_name($_) }
129 @all_names;
130
131 my @indexable_positions
132 = UR::Util::positions_of_values(\@all_names,\@indexable_names);
133
134 $self->{_indexable_property_names} = \@indexable_names;
135 $self->{_indexable_property_positions} = \@indexable_positions;
136 $self->{_is_fully_indexable} = (@indexable_names == @all_names);
137
138 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 sub is_subset_of {
146 my($self,$other_template) = @_;
147  
148 my $other_template_id = $other_template->id;
149 my $cached_subset_data = $self->{'__cache'}->{'is_subset_of'} ||= {};
150 if (exists $cached_subset_data->{$other_template_id}) {
151 return $cached_subset_data->{$other_template_id};
152 }
153  
154 unless (ref($other_template) and $self->isa(ref $other_template)) {
155 $cached_subset_data->{$other_template_id} = 0;
156 return 0;
157 }
158  
159 my $my_class = $self->subject_class_name;
160 my $other_class = $other_template->subject_class_name;
161 unless ($my_class eq $other_class or $my_class->isa($other_class)) {
162 $cached_subset_data->{$other_template_id} = undef;
163 return;
164 }
165  
166 my %operators = map { $_ => $self->operator_for($_) } $self->_property_names;
167 my $is_subset = 1;
168 foreach my $prop ( $other_template->_property_names ) {
169 unless (exists $operators{$prop}) {
170 $is_subset = 0;
171 last;
172 }
173 $is_subset = undef if ($operators{$prop} ne $other_template->operator_for($prop));
174 }
175  
176 if ($is_subset) {
177 $is_subset = $self->_is_subset_of_limit_offset($other_template);
178 }
179  
180 return $cached_subset_data->{$other_template_id} = $is_subset;
181 }
182  
183 sub _is_subset_of_limit_offset {
184 my($self, $other_template) = @_;
185  
186 return 1 unless ($self->offset or defined($self->limit)
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 my @my_filters = map { $_ . $self->operator_for($_) } $self->_property_names;
194 my @other_filters = map { $_ . $other_template->operator_for($_) } $other_template->_property_names;
195 my($both, $only_my, $only_other) = UR::Util::intersect_lists(\@my_filters, \@other_filters);
196 return undef if (@$only_my or @$only_other);
197  
198 my $my_offset = $self->offset || 0;
199 my $my_limit = $self->limit;
200 my $other_offset = $other_template->offset || 0;
201 my $other_limit = $other_template->limit;
202  
203 my $is_subset;
204 if (defined($my_limit) and defined($other_limit)) {
205 my $my_last = $my_offset + $my_limit;
206 my $other_last = $other_offset + $other_limit;
207  
208 $is_subset = ($my_offset >= $other_offset) && ($my_last <= $other_last);
209  
210 } elsif (!defined($my_limit) and defined($other_limit)) {
211 $is_subset = 0;
212  
213 } else {
214 $is_subset = $my_offset >= $other_offset;
215 }
216 return $is_subset;
217 }
218  
219  
220 # This is set lazily currently
221  
222 sub is_unique {
223 my $self = $_[0];
224 if (defined $self->{is_unique}) {
225 return $self->{is_unique}
226 }
227  
228 # since this requires normalization, we don't set the value at construction time
229 my $normalized_self;
230 if ($self->is_normalized) {
231 $normalized_self = $self;
232 }
233 else {
234 $normalized_self = $self->get_normalized_template_equivalent($self);
235 }
236  
237 my $op = $normalized_self->operator_for('id');
238 if (defined($op) and ($op eq '' or $op eq '=')) {
239 return $self->{is_unique} = 1;
240 }
241 else {
242 $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 if (my @ps = $self->subject_class_name->__meta__->unique_property_sets) {
249 my $property_meta_hash = $self->_property_meta_hash;
250 for my $property_set (@ps)
251 {
252 my $property_set = (ref($property_set) ? $property_set : [$property_set]);
253 my @properties_used_from_constraint =
254 grep { defined($_) }
255 @$property_meta_hash{@$property_set};
256
257 if (@properties_used_from_constraint == @$property_set) {
258 # filter imprecise operators
259 @properties_used_from_constraint =
260 grep {
261 $_->{operator} !~ /^(not |)like(-.|)$/i
262 and
263 $_->{operator} !~ /^(not |)in/i
264 }
265 @properties_used_from_constraint;
266
267 if (@properties_used_from_constraint == @$property_set) {
268 $self->{is_unique} = 1;
269 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 return $self->{is_unique};
282 }
283 }
284  
285  
286 # Derivative of the ID.
287  
288 sub rule_template_subclass_name {
289 return "UR::BoolExpr::Template::" . shift->logic_type;
290 }
291  
292 sub get_normalized_template_equivalent {
293 UR::BoolExpr::Template->get($_[0]->{normalized_id});
294 }
295  
296 sub get_rule_for_values {
297 my $self = shift;
298 my $value_id = UR::BoolExpr::Util::values_to_value_id(@_);
299 my $rule_id = UR::BoolExpr->__meta__->resolve_composite_id_from_ordered_values($self->id,$value_id);
300 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 return $r;
309 }
310  
311 sub get_rule_for_value_id {
312 my $self = shift;
313 my $value_id = shift;
314  
315 my $rule_id = UR::BoolExpr->__meta__->resolve_composite_id_from_ordered_values($self->id,$value_id);
316 return UR::BoolExpr->get($rule_id);
317 }
318  
319 sub extend_params_list_for_values {
320 my $self = shift;
321 #my @prev = @_;
322 my $extenders = $self->normalization_extender_arrayref;
323 if (@$extenders) {
324 my @result;
325 my $subject_class = $self->subject_class_name->__meta__;
326 for my $n (0 .. @$extenders-1) {
327 my $extender = $extenders->[$n];
328 my ($input_positions_arrayref,$subref,@more_keys) = @$extender;
329 my @more_values = @_[@$input_positions_arrayref];
330 if ($subref) {
331 ## print "calling $subref on \n\t" . join("\n\t",@more_values) . "\n";
332 @more_values = $subject_class->$subref(@more_values);
333 ## print "got: \n\t" . join("\n\t",@more_values) . "\n";
334 }
335 while (@more_keys) {
336 my $k = shift @more_keys;
337 my $v = shift @more_values;
338 push @result, $k => $v;
339 }
340 }
341 return @result;
342 }
343 return ();
344 }
345  
346 sub get_normalized_rule_for_values {
347 my $self = shift;
348 my @unnormalized_values = @_;
349  
350 if ($self->is_normalized) {
351 return $self->get_rule_for_values(@unnormalized_values);
352 }
353  
354 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 my $extenders = $self->normalization_extender_arrayref;
360 if (@$extenders) {
361 my $subject_class = $self->subject_class_name->__meta__;
362 for my $extender (@$extenders) {
363 my ($input_positions_arrayref,$subref) = @$extender;
364 my @more_values = @unnormalized_values[@$input_positions_arrayref];
365 if ($subref) {
366 ## print "calling $subref on \n\t" . join("\n\t",@more_values) . "\n";
367 @more_values = $subject_class->$subref(@more_values);
368 ## print "got: \n\t" . join("\n\t",@more_values) . "\n";
369 }
370 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 my $normalized_positions_arrayref = $self->normalized_positions_arrayref;
377 my @normalized_values = @unnormalized_values[@$normalized_positions_arrayref];
378  
379 my $rule = $normalized_rule_template->get_rule_for_values(@normalized_values);
380 return $rule;
381 }
382  
383 sub _normalize_non_ur_values_hash {
384 my ($self,$unnormalized) = @_;
385 my %normalized;
386 if ($self->subject_class_name ne 'UR::Object::Property') {
387 my $normalized_positions_arrayref = $self->normalized_positions_arrayref;
388 my @reordered_values = @$unnormalized{@$normalized_positions_arrayref};
389 for (my $n = 0; $n < @reordered_values; $n++) {
390 my $value = $reordered_values[$n];
391 $normalized{$n} = $value if defined $value;
392 }
393 }
394 return \%normalized;
395 }
396  
397  
398 sub value_position_for_property_name {
399 if (exists $_[0]{_property_meta_hash}{$_[1]}) {
400 return $_[0]{_property_meta_hash}{$_[1]}{value_position};
401 } else {
402 return undef;
403 }
404 }
405  
406 sub operator_for {
407 if (exists $_[0]{_property_meta_hash}{$_[1]}) {
408 return $_[0]{_property_meta_hash}{$_[1]}{operator} || '=';
409 } else {
410 return undef;
411 }
412 }
413  
414 sub operators_for_properties {
415 my %properties = map { $_ => $_[0]->{'_property_meta_hash'}->{$_}->{'operator'} || '=' }
416 @{ $_[0]->{'_property_names_arrayref'} };
417 return \%properties;
418 }
419  
420 sub add_filter {
421 my $self = shift;
422 my $property_name = shift;
423 my $op = shift;
424 my $new_key = $property_name;
425 $new_key .= ' ' . $op if defined $op;
426 my ($subject_class_name, $logic_type, $logic_detail) = split("/",$self->id);
427 unless ($logic_type eq 'And') {
428 die "Attempt to add a filter to a rule besides an 'And' rule!";
429 }
430 my @keys = split(',',$logic_detail);
431 my $new_id = join('/',$subject_class_name,$logic_type,join(',',@keys,$new_key));
432 return $self->class->get($new_id);
433 }
434  
435 sub remove_filter {
436 my $self = shift;
437 my $filter = shift;
438 my ($subject_class_name, $logic_type, $logic_detail) = split("/",$self->id);
439 my @keys = grep { $_ !~ /^${filter}\b/ } split(',',$logic_detail);
440 my $new_id = join('/',$subject_class_name,$logic_type,join(',',@keys));
441 #print "$new_id\n";
442 return $self->class->get($new_id);
443 }
444  
445 sub sub_classify {
446 my ($self,$subclass_name) = @_;
447 my $new_id = $self->id;
448 $new_id =~ s/^.*?\//$subclass_name\//;
449 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 sub get_by_subject_class_name_logic_type_and_logic_detail {
456 my $class = shift;
457 my $subject_class_name = shift;
458 Carp::croak("Expected a subject class name as the first arg of UR::BoolExpr::Template constructor, got "
459 . ( defined($subject_class_name) ? "'$subject_class_name'" : "(undef)" ) ) unless ($subject_class_name);
460 my $logic_type = shift;
461 my $logic_detail = shift;
462 my $constant_value_id = shift || UR::BoolExpr::Util::values_to_value_id(); # default is an empty list of values
463  
464 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 sub resolve {
470 my($class,$subject_class_name, @params_list) = @_;
471  
472 my(@params, @constant_values);
473 for (my $i = 0; $i < @params_list; $i++) {
474 push @params, $params_list[$i];
475 if (UR::BoolExpr::Util::is_meta_param($params_list[$i])) {
476 push @constant_values, $params_list[++$i];
477 }
478 }
479  
480 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 sub get {
488 my $class = shift;
489 my $id = shift;
490 Carp::croak("Non-id params not supported for " . __PACKAGE__ . " yet!") if @_;
491  
492 my $self = $UR::Object::rule_templates->{$id};
493 return $self if $self;
494  
495 my ($subject_class_name,$logic_type,$logic_detail,$constant_value_id,@extra) = split('/',$id);
496 if (@extra) {
497 # account for a possible slash in the constant value id
498 $constant_value_id = join('/',$constant_value_id,@extra);
499 }
500  
501 # work on the base class or on subclasses
502 my $sub_class_name = (
503 $class eq __PACKAGE__
504 ? __PACKAGE__ . "::" . $logic_type
505 : $class
506 );
507  
508 unless ($logic_type) {
509 Carp::croak("Could not determine logic type from UR::BoolExpr::Template with id $id");
510 }
511  
512 if ($logic_type eq "And") {
513 # TODO: move into subclass
514 my @keys = split(/,/,$logic_detail || '');
515 my @constant_values;
516 @constant_values = UR::BoolExpr::Util::value_id_to_values($constant_value_id) if defined $constant_value_id;
517 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 $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 $UR::Object::rule_templates->{$id} = $self;
535 return $self;
536 }
537 }
538  
539  
540 # Return true if the template has recursion_desc, hints, order or page set
541 sub has_meta_options {
542 my $self = shift;
543 return 1 if @$self{@meta_param_names};
544 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 sub legacy_params_hash {
552 my $self = shift;
553 my $legacy_params_hash = $self->{legacy_params_hash};
554 return $legacy_params_hash if $legacy_params_hash;
555
556 $legacy_params_hash = {};
557
558 my $template_id = $self->id;
559 my $key_op_hash = $self->key_op_hash;
560 my $id_only = $self->is_id_only;
561
562 my $subject_class_name = $self->subject_class_name;
563 my $logic_type = $self->logic_type;
564 my $logic_detail = $self->logic_detail;
565 my @keys_sorted = $self->_underlying_keys;
566 my $subject_class_meta = $subject_class_name->__meta__;
567
568 if (
569 (@keys_sorted and not $logic_detail)
570 or
571 ($logic_detail and not @keys_sorted)
572 ) {
573 Carp::confess();
574 }
575
576 if (!$logic_detail) {
577 %$legacy_params_hash = (_unique => 0, _none => 1);
578 }
579 else {
580 # _id_only
581 if ($id_only) {
582 $legacy_params_hash->{_id_only} = 1;
583 }
584 else {
585 $legacy_params_hash->{_id_only} = 0;
586 $legacy_params_hash->{_param_key} = undef;
587 }
588
589 # _unique
590 if (my $id_op = $key_op_hash->{id}) {
591 if ($id_op->{""} or $id_op->{"="}) {
592 $legacy_params_hash->{_unique} = 1;
593 unless ($self->is_unique) {
594 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 unless ($legacy_params_hash->{_unique}) {
602 if (defined $legacy_params_hash->{id} and not ref $legacy_params_hash->{id}) {
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 $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 my @ps = $subject_class_meta->unique_property_sets;
616 for my $property_set (@ps)
617 {
618 my $property_set = (ref($property_set) ? $property_set : [$property_set]);
619 my @properties_used_from_constraint =
620 grep { defined($_) }
621 (ref($property_set) ? @$key_op_hash{@$property_set} : $key_op_hash->{$property_set});
622
623 if (@properties_used_from_constraint == @$property_set) {
624 # filter imprecise operators
625 @properties_used_from_constraint =
626 grep {
627 not (
628 grep { /^(not |)like(-.|)$/i or /^\[\]/}
629 keys %$_
630 )
631 }
632 @properties_used_from_constraint;
633
634 if (@properties_used_from_constraint == @$property_set) {
635 $legacy_params_hash->{_unique} = 1;
636 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 $legacy_params_hash->{_param_key} = undef unless $id_only;
650 }
651 }
652  
653 if ($self->is_unique and not $legacy_params_hash->{_unique}) {
654 Carp::carp "is_unique IS set but legacy params hash is NO for $self->{id}";
655 #$DB::single = 1;
656 $self->is_unique;
657 }
658 if (!$self->is_unique and $legacy_params_hash->{_unique}) {
659 Carp::carp "is_unique NOT set but legacy params hash IS for $self->{id}";
660 #$DB::single = 1;
661 $self->is_unique;
662 }
663  
664 $self->{legacy_params_hash} = $legacy_params_hash;
665 return $legacy_params_hash;
666 }
667  
668 sub sorter {
669 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 die "this method takes no paramters!" if @_;
675  
676 my $class = $self->subject_class_name;
677  
678 my $sort_meta;
679 if ($self->group_by) {
680 my $set_class = $class . "::Set";
681 $sort_meta = $set_class->__meta__;
682 }
683 else {
684 $sort_meta = $class->__meta__;
685 }
686  
687 my $sorter;
688 if (my $order_by = $self->order_by) {
689 $sorter = $sort_meta->sorter(@$order_by);
690 }
691 else {
692 $sorter = $sort_meta->sorter();
693 }
694  
695 return $sorter;
696 }
697  
698  
699 1;
700