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

filter: Statementstmt 0.0% 0 / 550 Branchbran 0.0% 0 / 222 Conditioncond 0.0% 0 / 151 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 953 SCAR 94.5
CC
112
Cov
0%
_fast_construct
4422.0
_reframe
306.0
_flatten
110.0
CRAP 12656.0
1 package UR::BoolExpr::Template::And;
2 use warnings;
3 use strict;
4 require UR;
5 our $VERSION = "0.46"; # UR $VERSION;;
6  
7 UR::Object::Type->define(
8 class_name => __PACKAGE__,
9 is => ['UR::BoolExpr::Template::Composite'],
10 );
11  
12 sub _flatten_bx {
13 my ($class, $bx) = @_;
14 my $template = $bx->template;
15 my ($flattened_template, @extra_values) = $template->_flatten(@_);
16 my $flattened_bx;
17 if (not @extra_values) {
18 # optimized
19 my $flattened_bx_id = $flattened_template->id . $UR::BoolExpr::Util::id_sep . $bx->value_id;
20 $flattened_bx = UR::BoolExpr->get($flattened_bx_id);
21 $flattened_bx->{'values'} = $bx->{'values'} unless $flattened_bx->{'values'};
22 }
23 else {
24 $flattened_bx = $flattened_template->get_rule_for_values($bx->values, @extra_values);
25 }
26 return $flattened_bx;
27 }
28  
29 sub _reframe_bx {
30 my ($class, $bx, $in_terms_of_property_name) = @_;
31 my $template = $bx->template;
32 my ($reframed_template, @extra_values) = $template->_reframe($in_terms_of_property_name);
33 my $reframed_bx;
34 if (@extra_values == 0) {
35 my $reframed_bx_id = $reframed_template->id . $UR::BoolExpr::Util::id_sep . $bx->value_id;
36 $reframed_bx = UR::BoolExpr->get($reframed_bx_id);
37 $reframed_bx->{'values'} = $bx->{'values'} unless $reframed_bx->{'values'};
38 }
39 else {
40 my @values = ($bx->values, @extra_values);
41 $reframed_bx = $reframed_template->get_rule_for_values(@values);
42 }
43 return $reframed_bx;
44 }
45  
46 sub _flatten {
47 my $self = $_[0];
48
49 if ($self->{flatten}) {
50 return @{ $self->{flatten} }
51 }
52  
53 my @old_keys = @{ $self->_keys };
54 my $old_property_meta_hash = $self->_property_meta_hash;
55  
56 my $class_meta = $self->subject_class_name->__meta__;
57
58 my @new_keys;
59 my @extra_keys;
60 my @extra_values;
61
62 my $old_constant_values;
63 my @new_constant_values;
64  
65 my $found_unflattened_params = 0;
66 while (my $key = shift @old_keys) {
67 my $name = $key;
68 $name =~ s/ .*//;
69 if (! UR::BoolExpr::Util::is_meta_param($name)) {
70 my $mdata = $old_property_meta_hash->{$name};
71 my ($value_position, $operator) = @$mdata{'value_position','operator'};
72
73 my ($flat, $add_keys, $add_values) = $class_meta->_flatten_property_name($name);
74 $found_unflattened_params = 1 if $flat ne $name or @$add_keys or @$add_values;
75  
76 $flat .= ' ' . $operator if $operator and $operator ne '=';
77 push @new_keys, $flat;
78
79 push @extra_keys, @$add_keys;
80 push @extra_values, @$add_values;
81 }
82 else {
83 push @new_keys, $key;
84 $old_constant_values ||= [ @{ $self->_constant_values } ];
85 my $old_value = shift @$old_constant_values;
86 my $new_value = [];
87 for my $part (@$old_value) {
88 my ($flat, $add_keys, $add_values) = $class_meta->_flatten_property_name($part);
89 $found_unflattened_params = 1 if $flat ne $name or @$add_keys or @$add_values;
90 push @$new_value, $flat;
91  
92 push @extra_keys, @$add_keys;
93 push @extra_values, @$add_values;
94 }
95 push @new_constant_values, $new_value;
96 }
97 }
98  
99 my $constant_values;
100 if ($old_constant_values) {
101 # some -* keys were found above, and we flattened the value internals
102 $constant_values = \@new_constant_values;
103 }
104 else {
105 # no -* keys, just re-use the empty arrayref
106 $constant_values = $self->_constant_values;
107 }
108  
109 if ($found_unflattened_params or @extra_keys) {
110 if (@extra_keys) {
111 # there may be duplication between these and the primary joins
112 # or each other
113 my %keys_seen = map { $_ => 1 } @new_keys;
114 my @nodup_extra_keys;
115 my @nodup_extra_values;
116 while (my $extra_key = shift @extra_keys) {
117 my $extra_value = shift @extra_values;
118 unless ($keys_seen{$extra_key}) {
119 push @nodup_extra_keys, $extra_key;
120 push @nodup_extra_values, $extra_value;
121 $keys_seen{$extra_key} = 1;
122 }
123 }
124 push @new_keys, @nodup_extra_keys;
125 @extra_values = @nodup_extra_values
126 }
127 my $flat = UR::BoolExpr::Template::And->_fast_construct(
128 $self->subject_class_name,
129 \@new_keys,
130 $constant_values,
131 );
132 $self->{flatten} = [$flat,@extra_values];
133 return ($flat, @extra_values);
134 }
135 else {
136 # everything was already flat, just remember this so you DRY
137 $self->{flatten} = [$self];
138 Scalar::Util::weaken($self->{flatten}[0]);
139 return $self
140 }
141 }
142  
143 sub _reframe {
144 my $self = shift;
145 my $in_terms_of_property_name = shift;
146
147 # determine the from_class, to_class, and path_back
148 my $from_class = $self->subject_class_name;
149 my $cmeta = $self->subject_class_name->__meta__;
150 my @pmeta = $cmeta->property_meta_for_name($in_terms_of_property_name);
151 unless (@pmeta) {
152 Carp::confess("Failed to find property $in_terms_of_property_name on $from_class. Cannot reframe $self!");
153 }
154 my @reframe_path_forward = map { $_->_resolve_join_chain($in_terms_of_property_name) } @pmeta;
155 my $to_class = $reframe_path_forward[-1]{foreign_class};
156  
157 # translate all of the old properties to use the path back to the original class
158 my ($flat,@extra_values) = $self->_flatten;
159 my @old_keys = @{ $flat->_keys };
160 my $old_property_meta_hash = $flat->_property_meta_hash;
161  
162 my %sub_group_label_used;
163 my $reframer = sub {
164 my $old_name = $_[0];
165 # uses: @reframe_path_forward from above in this closure
166  
167 # get back to the original object
168 my @reframe_path_back = reverse @reframe_path_forward;
169  
170 # then forward to the property related to it
171 my @filter_path_forward = split('\.',$old_name);
172  
173 # if the end of the path back matches the beginning of the path
174 # to the property in the expression unneeded steps (beyond 1)
175 my $new_key;
176 while (1) {
177 unless (@reframe_path_back) {
178 last;
179 }
180 unless (@filter_path_forward) {
181 last;
182 }
183 my $last_name_back = $reframe_path_back[-1]{source_name_for_foreign};
184 my $first_name_forward = $filter_path_forward[0];
185  
186
187 my $turnaround_match = 0;
188 if ($last_name_back eq $first_name_forward) {
189 # complete overlap
190 $turnaround_match = 1; # safe
191 }
192 else {
193 # see if stripping off any labels makes them match
194 my $last_name_back_base = $last_name_back;
195 $last_name_back_base =~ s/-.*//;
196  
197 my $first_name_forward_base = $first_name_forward;
198 $first_name_forward_base =~ s/-.*//;
199  
200 if ($last_name_back_base eq $first_name_forward_base) {
201 # removing the grouping label causes a match
202 # possible overlap
203 for my $pair (
204 [$first_name_forward_base, $last_name_back],
205 [$last_name_back_base, $first_name_forward],
206 ) {
207 my ($partial, $full) = @$pair;
208 if (index($full, $partial) == 0) {
209 #print "$partial is part of $full\n";
210 if (my $prev_full = $sub_group_label_used{$partial}) {
211 # we've tracked back through this $partially specified relationship once
212 # see if we did it the same way
213 if ($prev_full eq $full) {
214 $turnaround_match = 1;
215 }
216 else {
217 #print "previously used $prev_full for $partial: cannot use $full\n";
218 next;
219 }
220 }
221 else {
222 # this relationship has not been seen
223 #print "using $full for $partial\n";
224 $sub_group_label_used{$partial} = $full;
225 $turnaround_match = 1;
226 }
227 }
228 }
229 }
230 }
231  
232 if ($turnaround_match == 0) {
233 # found a difference: no shortcut
234 # we have to trek all the way back to the original subject before
235 # moving forward to this property
236 last;
237 }
238 else {
239 # the last step back matches the first step to the property
240 if (@reframe_path_back == 1 and @filter_path_forward == 1) {
241 # just keep one of the identical pair
242 shift @filter_path_forward;
243 }
244 else {
245 # remove both (if one is empty this is no problem)
246 pop @reframe_path_back;
247 shift @filter_path_forward;
248 }
249 }
250 }
251
252 $new_key = join('.', map { $_->{foreign_name_for_source} } @reframe_path_back);
253 $new_key = join('.', ($new_key ? $new_key : ()), @filter_path_forward);
254  
255 return $new_key;
256 };
257  
258
259 # this is only set below if we find any -* keys
260 my $old_constant_values;
261  
262 my @new_keys;
263 my @new_constant_values;
264  
265 while (@old_keys) {
266 my $old_key = shift @old_keys;
267  
268 if (! UR::BoolExpr::Util::is_meta_param($old_key)) {
269 # a regular property
270 my $old_name = $old_key;
271 $old_name =~ s/ .*//;
272
273 my $mdata = $old_property_meta_hash->{$old_name};
274 my ($value_position, $operator) = @$mdata{'value_position','operator'};
275
276 my $new_key = $reframer->($old_name);
277  
278 $new_key .= ' ' . $operator if $operator and $operator ne '=';
279 push @new_keys, $new_key;
280 }
281 else {
282 # this key is not a property, it's a special key like -order_by or -group_by
283 unless ($old_key eq '-order_by'
284 or $old_key eq '-group_by'
285 or $old_key eq '-hints'
286 or $old_key eq '-recurse'
287 ) {
288 Carp::confess("no support yet for $old_key in bx reframe()!");
289 }
290  
291 push @new_keys, $old_key;
292  
293 unless ($old_constant_values) {
294 $old_constant_values = [ @{ $flat->_constant_values } ];
295 }
296  
297 my $old_value = shift @$old_constant_values;
298 my $new_value = [];
299 for my $part (@$old_value) {
300 my $reframed_part = $reframer->($part);
301 push @$new_value, $reframed_part;
302 }
303 push @new_constant_values, $new_value;
304 }
305 }
306  
307  
308 my $constant_values;
309 if (@new_constant_values) {
310 $constant_values = \@new_constant_values;
311 }
312 else {
313 $constant_values = $flat->_constant_values; # re-use empty immutable arrayref
314 }
315  
316 my $reframed = UR::BoolExpr::Template::And->_fast_construct(
317 $to_class,
318 \@new_keys,
319 $constant_values,
320 );
321  
322 return $reframed, @extra_values;
323 }
324  
325 sub _template_for_grouped_subsets {
326 my $self = shift;
327 my $group_by = $self->group_by;
328 die "rule template $self->{id} has no -group_by!?!?" unless $group_by;
329  
330 my @base_property_names = $self->_property_names;
331 for (my $i = 0; $i < @base_property_names; $i++) {
332 my $operator = $self->operator_for($base_property_names[$i]);
333 if ($operator ne '=') {
334 $base_property_names[$i] .= " $operator";
335 }
336 }
337  
338 my $template = UR::BoolExpr::Template->get_by_subject_class_name_logic_type_and_logic_detail(
339 $self->subject_class_name,
340 'And',
341 join(",", @base_property_names, @$group_by),
342 );
343  
344 return $template;
345 }
346  
347 sub _variable_value_count {
348 my $self = shift;
349 my $k = $self->_underlying_keys;
350 my $v = $self->_constant_values;
351 if ($v) {
352 $v = scalar(@$v);
353 }
354 else {
355 $v = 0;
356 }
357 return $k-$v;
358 }
359  
360 sub _underlying_keys {
361 my $self = shift;
362 my $logic_detail = $self->logic_detail;
363 return unless $logic_detail;
364 my @underlying_keys = split(",",$logic_detail);
365 return @underlying_keys;
366 }
367  
368 sub get_underlying_rule_templates {
369 my $self = shift;
370 my @underlying_keys = grep { UR::BoolExpr::Util::is_meta_param($_) ? () : ($_) } $self->_underlying_keys();
371 my $subject_class_name = $self->subject_class_name;
372 return map {
373 UR::BoolExpr::Template::PropertyComparison
374 ->_get_for_subject_class_name_and_logic_detail(
375 $subject_class_name,
376 $_
377 );
378 } @underlying_keys;
379 }
380  
381 sub specifies_value_for {
382 my ($self, $property_name) = @_;
383 Carp::confess('Missing required parameter property_name for specifies_value_for()') if not defined $property_name;
384 my @underlying_templates = $self->get_underlying_rule_templates();
385 foreach ( @underlying_templates ) {
386 return 1 if $property_name eq $_->property_name;
387 }
388 return;
389 }
390  
391 sub _filter_breakdown {
392 my $self = $_[0];
393 my $filter_breakdown = $self->{_filter_breakdown} ||= do {
394 my @underlying = $self->get_underlying_rule_templates;
395 my @primary;
396 my %sub_group_filters;
397 my %sub_group_sub_filters;
398 for (my $n = 0; $n < @underlying; $n++) {
399 my $underlying = $underlying[$n];
400 my $sub_group = $underlying->sub_group;
401 if ($sub_group) {
402 if (substr($sub_group,-1) ne '?') {
403 # control restruct the subject based on the sub-group properties
404 my $list = $sub_group_filters{$sub_group} ||= [];
405 push @$list, $underlying, $n;
406 }
407 else {
408 # control what is IN a sub-group (effectively define it with these)
409 chop($sub_group);
410 my $list = $sub_group_sub_filters{$sub_group} ||= [];
411 push @$list, $underlying, $n;
412 }
413 }
414 else {
415 push @primary, $underlying, $n;
416 }
417 }
418  
419 {
420 primary => \@primary,
421 sub_group_filters => \%sub_group_filters,
422 sub_group_sub_filters => \%sub_group_sub_filters,
423 };
424 };
425 return $filter_breakdown;
426 }
427  
428 sub evaluate_subject_and_values {
429 my $self = shift;
430 my $subject = shift;
431  
432 return unless (ref($subject) && $subject->isa($self->subject_class_name));
433  
434 my $filter_breakdown = $self->_filter_breakdown;
435 my ($primary,$sub_group_filters,$sub_group_sub_filters)
436 = @$filter_breakdown{"primary","sub_group_filters","sub_group_sub_filters"};
437
438 # flattening expresions now requires that we re-group them :(
439 # these effectively are subqueries where they occur
440  
441 # check the ungrouped comparisons first since they are simpler
442 for (my $n = 0; $n < @$primary; $n+=2) {
443 my $underlying = $primary->[$n];
444 my $pos = $primary->[$n+1];
445 my $value = $_[$pos];
446 unless ($underlying->evaluate_subject_and_values($subject, $value)) {
447 return;
448 }
449 }
450  
451 # only check the complicated rules if none of the above failed
452 if (%$sub_group_filters) {
453 #$DB::single = 1;
454 for my $sub_group (keys %$sub_group_filters) {
455 my $filters = $sub_group_filters->{$sub_group};
456 my $sub_filters = $sub_group_sub_filters->{$sub_group};
457 print "FILTERING $sub_group: " . Data::Dumper::Dumper($filters, $sub_filters);
458 }
459 }
460
461 return 1;
462 }
463  
464 sub params_list_for_values {
465 # This is the reverse of the bulk of resolve.
466 # It returns the params in list form, directly coercable into a hash if necessary.
467 # $r = UR::BoolExpr->resolve($c1,@p1);
468 # ($c2, @p2) = ($r->subject_class_name, $r->params_list);
469
470 my $rule_template = shift;
471 my @values_sorted = @_;
472
473 my @keys_sorted = $rule_template->_underlying_keys;
474 my $constant_values = $rule_template->_constant_values;
475
476 my @params;
477 my ($v,$c) = (0,0);
478 for (my $k=0; $k<@keys_sorted; $k++) {
479 my $key = $keys_sorted[$k];
480 #if (substr($key,0,1) eq "_") {
481 # next;
482 #}
483 if (UR::BoolExpr::Util::is_meta_param($key)) {
484 my $value = $constant_values->[$c];
485 push @params, $key, $value;
486 $c++;
487 }
488 else {
489 my ($property, $op) = ($key =~ /^(\-*[\w\.]+)\s*(.*)$/);
490 unless ($property) {
491 $DB::single = 1;
492 Carp::confess("bad key '$key' in ",join(', ', @keys_sorted));
493 }
494 my $value = $values_sorted[$v];
495 if ($op) {
496 if ($op ne "in") {
497 if ($op =~ /^(.+)-(.+)$/) {
498 $value = { operator => $1, value => $value, escape => $2 };
499 }
500 else {
501 $value = { operator => $op, value => $value };
502 }
503 }
504 }
505 push @params, $property, $value;
506 $v++;
507 }
508 }
509  
510 return @params;
511 }
512  
513 sub _fast_construct {
514 my ($class,
515 $subject_class_name, # produces subject class meta
516 $keys, # produces logic detail
517 $constant_values, # produces constant value id
518
519 $logic_detail, # optional, passed by get
520 $constant_value_id, # optional, passed by get
521 $subject_class_meta, # optional, passed by bx
522 ) = @_;
523  
524 my $logic_type = 'And';
525 $logic_detail ||= join(",",@$keys);
526 $constant_value_id ||= UR::BoolExpr::Util::values_to_value_id(@$constant_values);
527
528 my $id = join('/',$subject_class_name,$logic_type,$logic_detail,$constant_value_id);
529 my $self = $UR::Object::rule_templates->{$id};
530 return $self if $self;
531  
532 $subject_class_meta ||= $subject_class_name->__meta__;
533  
534 # See what properties are id-related for the class
535 my $cache = $subject_class_meta->{cache}{'UR::BoolExpr::Template::get'} ||= do {
536 my $id_related = {};
537 my $id_translations = [];
538 my $id_pos = {};
539 my $id_prop_is_real; # true if there's a property called 'id' that's a real property, not from UR::Object
540 for my $iclass ($subject_class_name, $subject_class_meta->ancestry_class_names) {
541 last if $iclass eq "UR::Object";
542 next unless $iclass->isa("UR::Object");
543 my $iclass_meta = $iclass->__meta__;
544 my @id_props = $iclass_meta->id_property_names;
545 next unless @id_props;
546 $id_prop_is_real = 1 if (grep { $_ eq 'id'} @id_props);
547 next if @id_props == 1 and $id_props[0] eq "id" and !$id_prop_is_real;
548 @$id_related{@id_props} = @id_props;
549 push @$id_translations, \@id_props;
550 @$id_pos{@id_props} = (0..$#id_props) unless @id_props == 1 and $id_props[0] eq 'id';
551 }
552 [$id_related,$id_translations,$id_pos];
553 };
554 my ($id_related,$id_translations,$id_pos) = @$cache;
555 my @keys = @$keys;
556 my @constant_values = @$constant_values;
557  
558 # Make a hash to quick-validate the params for duplication
559 no warnings;
560 my %check_for_duplicate_rules;
561 for (my $n=0; $n < @keys; $n++) {
562 next if UR::BoolExpr::Util::is_meta_param($keys[$n]);
563 my $pos = index($keys[$n],' ');
564 if ($pos != -1) {
565 my $property = substr($keys[$n],0,$pos);
566 $check_for_duplicate_rules{$property}++;
567 }
568 else {
569 $check_for_duplicate_rules{$keys[$n]}++;
570 }
571 }
572  
573 # each item in this list mutates the initial set of key-value pairs
574 my $extenders = [];
575
576 # add new @$extenders for class-specific characteristics
577 # add new @keys at the same time
578 # flag keys as removed also at the same time
579  
580 # note the positions for each key in the "original" rule
581 # by original, we mean the original plus the extensions from above
582 #
583 my $id_position = undef;
584 my $var_pos = 0;
585 my $const_pos = 0;
586 my $property_meta_hash = {};
587 my $property_names = [];
588 for my $key (@keys) {
589 if (UR::BoolExpr::Util::is_meta_param($key)) {
590 $property_meta_hash->{$key} = {
591 name => $key,
592 value_position => $const_pos
593 };
594 $const_pos++;
595 }
596 else {
597 my ($name, $op) = ($key =~ /^(.+?)\s+(.*)$/);
598 $name ||= $key;
599 if ($name eq 'id') {
600 $id_position = $var_pos;
601 }
602 $property_meta_hash->{$name} = {
603 name => $name,
604 operator => $op,
605 value_position => $var_pos
606 };
607 $var_pos++;
608 push @$property_names, $name;
609 }
610 }
611  
612  
613 # Note whether there are properties not involved in the ID
614 # Add value extenders for any cases of id-related properties,
615 # or aliases.
616 my $original_key_count = @keys;
617 my $id_only = 1;
618 my $partial_id = 0;
619 my $key_op_hash = {};
620 if (@$id_translations and @{$id_translations->[0]} == 1) {
621 # single-property ID
622 ## use Data::Dumper;
623 ## print "single property id\n". Dumper($id_translations);
624 my ($property, $op);
625  
626 # Presume we are only getting id properties until another is found.
627 # If a multi-property is partially specified, we'll zero this out too.
628
629 my $values_index = -1; # -1 so we can bump it at start of loop
630 for (my $key_pos = 0; $key_pos < $original_key_count; $key_pos++) {
631 my $key = $keys[$key_pos];
632 if (UR::BoolExpr::Util::is_meta_param($key)) {
633 # -* are constant value keys and do not need to be changed
634 next;
635 } else {
636 $values_index++;
637 }
638  
639 my ($property, $op) = ($key =~ /^(.+?)\s+(.*)$/);
640 $property ||= $key;
641 $op ||= "";
642 $op =~ s/\s+//;
643 $key_op_hash->{$property} ||= {};
644 $key_op_hash->{$property}{$op}++;
645
646 if ($property eq "id" or $id_related->{$property}) {
647 # Put an id key into the key list.
648 for my $alias (["id"], @$id_translations) {
649 next if $alias->[0] eq $property;
650 next if $check_for_duplicate_rules{$alias->[0]};
651 $op ||= "";
652 push @keys, $alias->[0] . ($op ? " $op" : "");
653 push @$extenders, [ [$values_index], undef, $keys[-1] ];
654 $key_op_hash->{$alias->[0]} ||= {};
655 $key_op_hash->{$alias->[0]}{$op}++;
656 ## print ">> extend for @$alias with op $op.\n";
657 }
658 unless ($op =~ m/^(=|eq|in|\[\]|)$/) {
659 $id_only = 0;
660 }
661 }
662 elsif (! UR::BoolExpr::Util::is_meta_param($key)) {
663 $id_only = 0;
664 ## print "non id single property $property on $subject_class\n";
665 }
666 }
667 }
668 else {
669 # multi-property ID
670 ## print "multi property id\n". Dumper($id_translations);
671 my ($property, $op);
672 my %id_parts;
673 my $values_index = -1; # -1 so we can bump it at start of loop
674 my $id_op;
675 for (my $key_pos = 0; $key_pos < $original_key_count; $key_pos++) {
676 my $key = $keys[$key_pos];
677 if (UR::BoolExpr::Util::is_meta_param($key)) {
678 # -* are constant value keys and do not need to be changed
679 next;
680 } else {
681 $values_index++;
682 }
683 next if UR::BoolExpr::Util::is_meta_param($key);
684  
685 my ($property, $op) = ($key =~ /^(.+?)\s+(.*)$/);
686 $property ||= $key;
687 $op ||= '';
688 $op =~ s/^\s+// if $op;
689 $key_op_hash->{$property} ||= {};
690 $key_op_hash->{$property}{$op}++;
691
692 if ($property eq "id") {
693 $id_op = $op;
694 $key_op_hash->{id} ||= {};
695 $key_op_hash->{id}{$op}++;
696 # Put an id-breakdown key into the key list.
697 for my $alias (@$id_translations) {
698 my @new_keys = map { $_ . ($op ? " $op" : "") } @$alias;
699 if (grep { $check_for_duplicate_rules{$_} } @$alias) {
700 #print "up @new_keys with @$alias\n";
701 }
702 else {
703 push @keys, @new_keys;
704 push @$extenders, [ [$values_index], "resolve_ordered_values_from_composite_id", @new_keys ];
705 for (@$alias) {
706 $key_op_hash->{$_} ||= {};
707 $key_op_hash->{$_}{$op}++;
708 }
709 # print ">> extend for @$alias with op $op.\n";
710 }
711 }
712 }
713 elsif ($id_related->{$property}) {
714 $id_op ||= $op;
715 if ($op eq "" or $op eq "eq" or $op eq "=" or $op eq 'in') {
716 $id_parts{$id_pos->{$property}} = $values_index;
717 }
718 else {
719 # We're doing some sort of gray-area comparison on an ID
720 # field, and though we could possibly resolve an ID
721 # from things like an 'in' op, it's more than we've done
722 # before.
723 $id_only = 0;
724 }
725 }
726 else {
727 ## print "non id multi property $property on class $subject_class\n";
728 $id_only = 0;
729 }
730 }
731
732 if (my $parts = (scalar(keys(%id_parts)))) {
733 # some parts are id-related
734 if ($parts == @{$id_translations->[0]}) {
735 # all parts are of the id are there
736 if (@$id_translations) {
737 if (grep { $_ eq 'id' } @keys) {
738 #print "found id already\n";
739 }
740 else {
741 #print "no id\n";
742 # we have translations of that ID into underlying properties
743 #print "ADDING ID for " . join(",",keys %id_parts) . "\n";
744 my @id_pos = sort { $a <=> $b } keys %id_parts;
745 push @$extenders, [ [@id_parts{@id_pos}], "resolve_composite_id_from_ordered_values", 'id' ]; #TODO was this correct?
746 $key_op_hash->{id} ||= {};
747 $key_op_hash->{id}{$id_op}++;
748 push @keys, "id";
749 }
750 }
751 }
752 else {
753 # not all parts of the id are there
754 ## print "partial id property $property on class $subject_class\n";
755 $id_only = 0;
756 $partial_id = 1;
757 }
758 } else {
759 $id_only = 0;
760 $partial_id = 0;
761 }
762 }
763
764 # Determine the positions of each key in the parameter list.
765 # In actuality, the position of the key's value in the @values or @constant_values array,
766 # depending on whether it is a -* key or not.
767 my %key_positions;
768 my $vpos = 0;
769 my $cpos = 0;
770 for my $key (@keys) {
771 $key_positions{$key} ||= [];
772 if (UR::BoolExpr::Util::is_meta_param($key)) {
773 push @{ $key_positions{$key} }, $cpos++;
774 }
775 else {
776 push @{ $key_positions{$key} }, $vpos++;
777 }
778 }
779  
780 # Sort the keys, and make an arrayref which will
781 # re-order the values to match.
782 my $last_key = '';
783 my @keys_sorted = map { $_ eq $last_key ? () : ($last_key = $_) } sort @keys;
784  
785  
786 my $normalized_positions_arrayref = [];
787 my $constant_value_normalized_positions = [];
788 my $recursion_desc = undef;
789 my $hints = undef;
790 my $order_by = undef;
791 my $group_by = undef;
792 my $page = undef;
793 my $limit = undef;
794 my $offset = undef;
795 my $aggregate = undef;
796 my @constant_values_sorted;
797  
798 for my $key (@keys_sorted) {
799 my $pos_list = $key_positions{$key};
800 my $pos = pop @$pos_list;
801 if (UR::BoolExpr::Util::is_meta_param($key)) {
802 push @$constant_value_normalized_positions, $pos;
803 my $constant_value = $constant_values[$pos];
804  
805 if ($key eq '-recurse') {
806 $constant_value = [$constant_value] if (!ref $constant_value);
807 $recursion_desc = $constant_value;
808 }
809 elsif ($key eq '-hints' or $key eq '-hint') {
810 $constant_value = [$constant_value] if (!ref $constant_value);
811 $hints = $constant_value;
812 }
813 elsif ($key eq '-order_by' or $key eq '-order') {
814 $constant_value = [$constant_value] if (!ref $constant_value);
815 $order_by = $constant_value;
816 }
817 elsif ($key eq '-group_by' or $key eq '-group') {
818 $constant_value = [$constant_value] if (!ref $constant_value);
819 $group_by = $constant_value;
820 }
821 elsif ($key eq '-page') {
822 $constant_value = [$constant_value] if (!ref $constant_value);
823 $page = $constant_value;
824 }
825 elsif ($key eq '-limit') {
826 $limit = $constant_value;
827 }
828 elsif ($key eq '-offset') {
829 $offset = $constant_value;
830 }
831 elsif ($key eq '-aggregate') {
832 $constant_value = [$constant_value] if (!ref $constant_value);
833 $aggregate = $constant_value;
834 }
835 else {
836 Carp::croak("Unknown special param '$key'. Expected one of: @UR::BoolExpr::Template::meta_param_names");
837 }
838 push @constant_values_sorted, $constant_value;
839 }
840 else {
841 push @$normalized_positions_arrayref, $pos;
842 }
843 }
844  
845 if ($page) {
846 if (defined($limit) || defined($offset)) {
847 Carp::croak("-page and -limit/-offset are mutually exclusive when defining a BoolExpr");
848 }
849 if (ref($page) and ref($page) eq 'ARRAY') {
850 if (@$page == 2) {
851 $limit = $page->[1];
852 $offset = ($page->[0] - 1) * $limit;
853 } elsif (@$page) {
854 Carp::croak('-page must be an arrayref of two integers: -page => [$page_number, $page_size]');
855 }
856 } else {
857 Carp::croak('-page must be an arrayref of two integers: -page => [$page_number, $page_size]');
858 }
859 }
860  
861 if (defined($hints) and ref($hints) ne 'ARRAY') {
862 if (! ref($hints)) {
863 $hints = [$hints]; # convert it to a list of one item
864 } else {
865 Carp::croak('-hints of a rule must be an arrayref of property names');
866 }
867 }
868  
869 my $matches_all;
870 do {
871 my $is_no_filters = (scalar(@keys_sorted) == scalar(@constant_values));
872 $id_only = 0 if ($is_no_filters);
873 $matches_all = ($is_no_filters && ! (defined($limit) or $offset));
874 };
875  
876 # these are used to rapidly turn a bx used for querying into one
877 # suitable for object construction
878 my @ambiguous_keys;
879 my @ambiguous_property_names;
880 for (my $n=0; $n < @keys; $n++) {
881 next if UR::BoolExpr::Util::is_meta_param($keys[$n]);
882 my ($property, $op) = ($keys[$n] =~ /^(.+?)\s+(.*)$/);
883 $property ||= $keys[$n];
884 $op ||= '';
885 $op =~ s/^\s+// if $op;
886 if ($op and $op ne 'eq' and $op ne '==' and $op ne '=') {
887 push @ambiguous_keys, $keys[$n];
888 push @ambiguous_property_names, $property;
889 }
890 }
891  
892 # Determine the rule template's ID.
893 # The normalizer will store this. Below, we'll
894 # find or create the template for this ID.
895 my $normalized_constant_value_id = (scalar(@constant_values_sorted) ? UR::BoolExpr::Util::values_to_value_id(@constant_values_sorted) : $constant_value_id);
896  
897 my @keys_unaliased = $UR::Object::Type::bootstrapping
898 ? @keys_sorted
899 : map { $_->[0] = UR::BoolExpr::Util::is_meta_param($_->[0]) ? $_->[0] : $subject_class_meta->resolve_property_aliases($_->[0]);
900 join(' ',@$_); }
901 map { [ split(' ') ] }
902 @keys_sorted;
903 my $normalized_id = UR::BoolExpr::Template->__meta__->resolve_composite_id_from_ordered_values($subject_class_name, "And", join(",",@keys_unaliased), $normalized_constant_value_id);
904  
905 $self = bless {
906 id => $id,
907 subject_class_name => $subject_class_name,
908 logic_type => $logic_type,
909 logic_detail => $logic_detail,
910 constant_value_id => $constant_value_id,
911 normalized_id => $normalized_id,
912
913 # subclass specific
914 id_position => $id_position,
915 is_id_only => $id_only,
916 is_partial_id => $partial_id,
917 is_unique => undef, # assigned on first use
918 matches_all => $matches_all,
919
920 key_op_hash => $key_op_hash,
921 _property_names_arrayref => $property_names,
922 _property_meta_hash => $property_meta_hash,
923
924 recursion_desc => $recursion_desc,
925 hints => $hints,
926 order_by => $order_by,
927 group_by => $group_by,
928 limit => $limit,
929 offset => $offset,
930 aggregate => $aggregate,
931
932 is_normalized => ($id eq $normalized_id ? 1 : 0),
933 normalized_positions_arrayref => $normalized_positions_arrayref,
934 constant_value_normalized_positions_arrayref => $constant_value_normalized_positions,
935 normalization_extender_arrayref => $extenders,
936
937 num_values => scalar(@$keys),
938
939 _keys => \@keys,
940 _constant_values => $constant_values,
941  
942 _ambiguous_keys => (@ambiguous_keys ? \@ambiguous_keys : undef),
943 _ambiguous_property_names => (@ambiguous_property_names ? \@ambiguous_property_names : undef),
944  
945 }, 'UR::BoolExpr::Template::And';
946  
947 $UR::Object::rule_templates->{$id} = $self;
948 return $self;
949 }
950  
951  
952 1;
953  
954 =pod
955  
956 =head1 NAME
957  
958 UR::BoolExpr::And - a rule which is true if ALL the underlying conditions are true
959  
960 =head1 SEE ALSO
961  
962 UR::BoolExpr;(3)
963  
964 =cut