| 1 | package UR::Object::Set; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use UR; | ||
| 6 | use List::MoreUtils qw(any); | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | |||
| 9 | our @CARP_NOT = qw( UR::Object::Type ); | ||
| 10 | |||
| 11 | use overload ('""' => '__display_name__'); | ||
| 12 | use overload ('==' => sub { $_[0] . '' eq $_[1] . '' } ); | ||
| 13 | use overload ('eq' => sub { $_[0] . '' eq $_[1] . '' } ); | ||
| 14 | use overload ('!=' => sub { $_[0] . '' ne $_[1] . '' } ); | ||
| 15 | use overload ('ne' => sub { $_[0] . '' ne $_[1] . '' } ); | ||
| 16 | use overload ('cmp' => sub { $_[0]->id cmp $_[1]->id } ); | ||
| 17 | |||
| 18 | class UR::Object::Set { | ||
| 19 | is => 'UR::Value', | ||
| 20 | is_abstract => 1, | ||
| 21 | has => [ | ||
| 22 | rule => { is => 'UR::BoolExpr', id_by => 'id' }, | ||
| 23 | rule_display => { is => 'Text', via => 'rule', to => '__display_name__'}, | ||
| 24 | member_class_name => { is => 'Text', via => 'rule', to => 'subject_class_name' }, | ||
| 25 | members => { is => 'UR::Object', is_many => 1, is_calculated => 1 } | ||
| 26 | ], | ||
| 27 | doc => 'an unordered group of distinct UR::Objects' | ||
| 28 | }; | ||
| 29 | |||
| 30 | # override the UR/system display name | ||
| 31 | # this is used in stringification overload | ||
| 32 | sub __display_name__ { | ||
| 33 | my $self = shift; | ||
| 34 | my %b = $self->rule->_params_list; | ||
| 35 | my $s = Data::Dumper->new([\%b])->Terse(1)->Indent(0)->Useqq(1)->Dump; | ||
| 36 | $s =~ s/\n/ /gs; | ||
| 37 | $s =~ s/^\s*{//; | ||
| 38 | $s =~ s/\}\s*$//; | ||
| 39 | $s =~ s/\"(\w+)\" \=\> / $1 => /g; | ||
| 40 | return '(' . ref($self) . ' ' . $s . ')'; | ||
| 41 | } | ||
| 42 | |||
| 43 | # When a set comes into existance, set up a subscription to monitor changes | ||
| 44 | # to the set's members | ||
| 45 | UR::Object::Set->create_subscription( | ||
| 46 | method => 'load', | ||
| 47 | note => 'set creation monitor', | ||
| 48 | callback => sub { | ||
| 49 | my $set = shift; | ||
| 50 | my $rule = $set->rule; | ||
| 51 | my %set_defining_attributes = map { $_ => 1 } $rule->template->_property_names(); | ||
| 52 | my $deps = $set->{__aggregate_deps} ||= {}; | ||
| 53 | |||
| 54 | $set->member_class_name->create_subscription( | ||
| 55 | note => 'set monitor '.$set->id, | ||
| 56 | priority => 0, | ||
| 57 | callback => sub { | ||
| 58 | return unless exists($set->{__aggregates}); # nothing cached yet | ||
| 59 | |||
| 60 | my ($member, $attr_name, $before, $after) = @_; | ||
| 61 | # load/unload won't affect aggregate values | ||
| 62 | return if ($attr_name eq 'load' or $attr_name eq 'unload'); | ||
| 63 | |||
| 64 | # If a set-defining attribute changes, or an object matching | ||
| 65 | # the set is created or deleted, then the set membership has | ||
| 66 | # possibly changed. Invalidate the whole aggregate cache. | ||
| 67 | if (exists($set_defining_attributes{$attr_name}) | ||
| 68 | || | ||
| 69 | ( ($attr_name eq 'create' or $attr_name eq 'delete') | ||
| 70 | && | ||
| 71 | $rule->evaluate($member) | ||
| 72 | ) | ||
| 73 | ) { | ||
| 74 | $set->__invalidate_cache__; | ||
| 75 | # A later call to _members_have_changes() would miss the case | ||
| 76 | # where a member becomes deleted or a member-defining attribute | ||
| 77 | # changes | ||
| 78 | $set->{__members_have_changes} = 1; | ||
| 79 | |||
| 80 | } | ||
| 81 | # if the changed attribute is a dependancy for a cached aggregation | ||
| 82 | # value, and it's a set member... | ||
| 83 | elsif ((my $dependant_aggregates = $deps->{$attr_name}) | ||
| 84 | && | ||
| 85 | $rule->evaluate($member) | ||
| 86 | ) { | ||
| 87 | # remove the cached aggregates that depend on this attribute | ||
| 88 | delete @{$set->{__aggregates}}{@$dependant_aggregates}; | ||
| 89 | # remove the dependancy records | ||
| 90 | delete @$deps{@$dependant_aggregates}; | ||
| 91 | delete $deps->{$attr_name} | ||
| 92 | } | ||
| 93 | } | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | ); | ||
| 97 | |||
| 98 | # When a transaction rolls back, it doesn't trigger subscriptions for the | ||
| 99 | # member objects as they get changed back to their original values. | ||
| 100 | # The safe thing is to set wipe out all Sets' aggregate caches :( | ||
| 101 | # It would be helpful if sets had a db_committed like other objects | ||
| 102 | # and we could just revert their values back to their db_committed values | ||
| 103 | UR::Context::Transaction->create_subscription( | ||
| 104 | method => 'rollback', | ||
| 105 | note => 'rollback set cache invalidator', | ||
| 106 | callback => sub { | ||
| 107 | delete(@$_{'__aggregates','__aggregate_deps','__members_have_changes'}) foreach UR::Object::Set->is_loaded(); | ||
| 108 | } | ||
| 109 | ); | ||
| 110 | |||
| 111 | UR::Context->create_subscription( | ||
| 112 | method => 'commit', | ||
| 113 | callback => sub { | ||
| 114 | my $worked = shift; | ||
| 115 | return unless $worked; # skip if the commit failed | ||
| 116 | delete $_->{__members_have_changes} foreach UR::Object::Set->is_loaded(); | ||
| 117 | } | ||
| 118 | ); | ||
| 119 | |||
| 120 | |||
| 121 | sub get_with_special_parameters { | ||
| 122 | Carp::cluck("Getting sets by directly properties of their members method will be removed shortly because of ambiguity on the meaning of 'id'. Please update the code which calls this."); | ||
| 123 | my $class = shift; | ||
| 124 | my $bx = shift; | ||
| 125 | my @params = @_; | ||
| 126 | my $member_class = $class; | ||
| 127 | $member_class =~ s/::Set$//; | ||
| 128 | return $member_class->define_set($bx->params_list, @params); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub members { | ||
| 132 | my $self = shift; | ||
| 133 | my $rule = $self->rule; | ||
| 134 | while (@_) { | ||
| 135 | $rule = $rule->add_filter(shift, shift); | ||
| 136 | } | ||
| 137 | return $self->member_class_name->get($rule); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub member_iterator { | ||
| 141 | my $self = shift; | ||
| 142 | my $rule = $self->rule; | ||
| 143 | while (@_) { | ||
| 144 | $rule = $rule->add_filter(shift, shift); | ||
| 145 | } | ||
| 146 | return $self->member_class_name->create_iterator($rule); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _members_have_changes { | ||
| 150 | my $self = shift; | ||
| 151 | return 1 if $self->{__members_have_changes}; | ||
| 152 | |||
| 153 | my @property_names = @_; | ||
| 154 | my $rule = $self->rule; | ||
| 155 | return any { $rule->evaluate($_) && $_->__changes__(@property_names) } $self->member_class_name->is_loaded; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub subset { | ||
| 159 | my $self = shift; | ||
| 160 | my $member_class_name = $self->member_class_name; | ||
| 161 | my $bx = UR::BoolExpr->resolve($member_class_name,@_); | ||
| 162 | my $subset = $self->class->get($bx->id); | ||
| 163 | return $subset; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub group_by { | ||
| 167 | my $self = shift; | ||
| 168 | my @group_by = @_; | ||
| 169 | my $grouping_rule = $self->rule->add_filter(-group_by => \@group_by); | ||
| 170 | my @groups = UR::Context->current->get_objects_for_class_and_rule( | ||
| 171 | $self->member_class_name, | ||
| 172 | $grouping_rule, | ||
| 173 | undef, #$load, | ||
| 174 | 0, #$return_closure, | ||
| 175 | ); | ||
| 176 | return $self->context_return(@groups); | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | sub __invalidate_cache__ { | ||
| 181 | my $self = shift; | ||
| 182 | if (@_) { | ||
| 183 | my $aggregate = shift; | ||
| 184 | delete $self->{__aggregates}->{$aggregate}; | ||
| 185 | } else { | ||
| 186 | delete @$self{'__aggregates','__aggregate_deps'}; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | sub __aggregate__ { | ||
| 191 | my $self = shift; | ||
| 192 | my $aggr = shift; | ||
| 193 | |||
| 194 | my $f = $aggr->{f}; | ||
| 195 | my $aggr_properties = $aggr->{properties}; | ||
| 196 | |||
| 197 | Carp::croak("$f is a group operation, and is not writable") if @_; | ||
| 198 | |||
| 199 | my $subject_class_meta = $self->rule->subject_class_name->__meta__; | ||
| 200 | |||
| 201 | my $not_ds_expressable = grep { $_->is_calculated or $_->is_transient or $_->is_constant } | ||
| 202 | map { $_->final_property_meta or $_ } | ||
| 203 | map { $subject_class_meta->property_meta_for_name($_) || () } | ||
| 204 | $self->rule->template->_property_names; | ||
| 205 | |||
| 206 | my($cache, $deps) = @$self{'__aggregates','__aggregate_deps'}; | ||
| 207 | |||
| 208 | # If there are no member-class objects with changes, we can just interrogate the DB | ||
| 209 | if (! exists($cache->{$f})) { | ||
| 210 | if ($not_ds_expressable or $self->_members_have_changes(@$aggr_properties)) { | ||
| 211 | my $fname; | ||
| 212 | my @fargs; | ||
| 213 | if ($f =~ /^(\w+)\((.*)\)$/) { | ||
| 214 | $fname = $1; | ||
| 215 | @fargs = ($2 ? split(',',$2) : ()); | ||
| 216 | } | ||
| 217 | else { | ||
| 218 | $fname = $f; | ||
| 219 | @fargs = (); | ||
| 220 | } | ||
| 221 | my $local_method = '__aggregate_' . $fname . '__'; | ||
| 222 | $self->{__aggregates}->{$f} = $self->$local_method(@fargs); | ||
| 223 | |||
| 224 | } else { | ||
| 225 | my $rule = $self->rule->add_filter(-aggregate => [$f])->add_filter(-group_by => []); | ||
| 226 | UR::Context->current->get_objects_for_class_and_rule( | ||
| 227 | $self->member_class_name, | ||
| 228 | $rule, | ||
| 229 | 1, # load | ||
| 230 | 0, # return_closure | ||
| 231 | ); | ||
| 232 | |||
| 233 | } | ||
| 234 | # keep 2-way mapping of dependances... | ||
| 235 | # First, keep a list of properties this aggregate cached value depends on | ||
| 236 | $deps->{$f} = $aggr_properties; | ||
| 237 | # And add this aggregate to the lists these properties are dependancies for | ||
| 238 | foreach ( @$aggr_properties ) { | ||
| 239 | $deps->{$_} ||= []; | ||
| 240 | push @{$deps->{$_}}, $f; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | return $self->{__aggregates}->{$f}; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub __aggregate_count__ { | ||
| 247 | my $self = shift; | ||
| 248 | my @members = $self->members; | ||
| 249 | return scalar(@members); | ||
| 250 | } | ||
| 251 | |||
| 252 | sub __aggregate_min__ { | ||
| 253 | my $self = shift; | ||
| 254 | my $p = shift; | ||
| 255 | my $min = undef; | ||
| 256 | no warnings; | ||
| 257 | for my $member ($self->members) { | ||
| 258 | my $v = $member->$p; | ||
| 259 | next unless defined $v; | ||
| 260 | $min = $v if (!defined($min) || ($v < $min) || ($v lt $min)); | ||
| 261 | } | ||
| 262 | return $min; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub __aggregate_max__ { | ||
| 266 | my $self = shift; | ||
| 267 | my $p = shift; | ||
| 268 | my $max = undef; | ||
| 269 | no warnings; | ||
| 270 | for my $member ($self->members) { | ||
| 271 | my $v = $member->$p; | ||
| 272 | next unless defined $v; | ||
| 273 | $max = $v if (!defined($max) || ($v > $max) || ($v gt $max)); | ||
| 274 | } | ||
| 275 | return $max; | ||
| 276 | } | ||
| 277 | |||
| 278 | sub __aggregate_sum__ { | ||
| 279 | my $self = shift; | ||
| 280 | my $p = shift; | ||
| 281 | my $sum = undef; | ||
| 282 | no warnings; | ||
| 283 | for my $member ($self->members) { | ||
| 284 | my $v = $member->$p; | ||
| 285 | next unless defined $v; | ||
| 286 | $sum += $v; | ||
| 287 | } | ||
| 288 | return $sum; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub __related_set__ { | ||
| 292 | my $self = $_[0]; | ||
| 293 | my $property_name = $_[1]; | ||
| 294 | my $bx1 = $self->rule; | ||
| 295 | my $bx2 = $bx1->reframe($property_name); | ||
| 296 | return $bx2->subject_class_name->define_set($bx2); | ||
| 297 | } | ||
| 298 | |||
| 299 | require Class::AutoloadCAN; | ||
| 300 | Class::AutoloadCAN->import(); | ||
| 301 | |||
| 302 | sub CAN { | ||
| 303 | my ($class,$method,$self) = @_; | ||
| 304 | |||
| 305 | if ($method =~ /^__aggregate_(.*)__/) { | ||
| 306 | # prevent circularity issues since this actually calls ->can(); | ||
| 307 | return; | ||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 311 | my $member_class_name = $class; | ||
| 312 | $member_class_name =~ s/::Set$//g; | ||
| 313 | return unless $member_class_name; | ||
| 314 | |||
| 315 | my $is_class_method = !ref($self); | ||
| 316 | my $member_method_closure = $member_class_name->can($method); | ||
| 317 | if ($is_class_method && $member_method_closure) { | ||
| 318 | # We should only get here if the Set class has not implemented the method. | ||
| 319 | # In which case we will delegate to the member class. | ||
| 320 | return sub { | ||
| 321 | my $self = shift; | ||
| 322 | return $member_method_closure->($member_class_name, @_); | ||
| 323 | }; | ||
| 324 | } | ||
| 325 | |||
| 326 | if ($member_method_closure) { | ||
| 327 | my $member_class_meta = $member_class_name->__meta__; | ||
| 328 | my $member_property_meta = $member_class_meta->property_meta_for_name($method); | ||
| 329 | |||
| 330 | # regular property access | ||
| 331 | if ($member_property_meta) { | ||
| 332 | return sub { | ||
| 333 | my $self = shift; | ||
| 334 | if (@_) { | ||
| 335 | Carp::croak("Cannot use method $method as a mutator: Set properties are not mutable"); | ||
| 336 | } | ||
| 337 | my $rule = $self->rule; | ||
| 338 | if ($rule->specifies_value_for($method)) { | ||
| 339 | return $rule->value_for($method); | ||
| 340 | } | ||
| 341 | else { | ||
| 342 | my @members = $self->members; | ||
| 343 | my @values = map { $_->$method } @members; | ||
| 344 | return @values if wantarray; | ||
| 345 | return if not defined wantarray; | ||
| 346 | Carp::confess("Multiple matches for $class method '$method' called in scalar context. The set has ".scalar(@values)." values to return") if @values > 1 and not wantarray; | ||
| 347 | return $values[0]; | ||
| 348 | } | ||
| 349 | }; | ||
| 350 | } | ||
| 351 | |||
| 352 | # set relaying with $s->foo_set->bar_set->baz_set; | ||
| 353 | if (my ($property_name) = ($method =~ /^(.*)_set$/)) { | ||
| 354 | return sub { | ||
| 355 | shift->__related_set__($property_name, @_) | ||
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | # other method | ||
| 360 | return sub { | ||
| 361 | my $self = shift; | ||
| 362 | if (@_) { | ||
| 363 | Carp::croak("Cannot use method $method as a mutator: Set properties are not mutable"); | ||
| 364 | } | ||
| 365 | my @members = $self->members; | ||
| 366 | my @values = map { $_->$method } @members; | ||
| 367 | return @values if wantarray; | ||
| 368 | return if not defined wantarray; | ||
| 369 | Carp::confess("Multiple matches for $class method '$method' called in scalar context. The set has ".scalar(@values)." values to return") if @values > 1 and not wantarray; | ||
| 370 | return $values[0]; | ||
| 371 | }; | ||
| 372 | |||
| 373 | } | ||
| 374 | else { | ||
| 375 | # a possible aggregation function | ||
| 376 | # see if the method ___aggregate__ uses exists, and if so, delegate to __aggregate__ | ||
| 377 | # TODO: delegate these to aggregation function modules instead of having them in this module | ||
| 378 | my $aggregator = '__aggregate_' . $method . '__'; | ||
| 379 | if ($self->can($aggregator)) { | ||
| 380 | return sub { | ||
| 381 | my $self = shift; | ||
| 382 | my $f = $method; | ||
| 383 | my @aggr_properties = @_; | ||
| 384 | if (@aggr_properties) { | ||
| 385 | $f .= '(' . join(',',@aggr_properties) . ')'; | ||
| 386 | } | ||
| 387 | return $self->__aggregate__({ f => $f, properties => \@aggr_properties }); | ||
| 388 | }; | ||
| 389 | } | ||
| 390 | |||
| 391 | # set relaying with $s->foo_set->bar_set->baz_set; | ||
| 392 | if (my ($property_name) = ($method =~ /^(.*)_set$/)) { | ||
| 393 | return sub { | ||
| 394 | shift->__related_set__($property_name, @_) | ||
| 395 | } | ||
| 396 | } | ||
| 397 | } | ||
| 398 | return; | ||
| 399 | } | ||
| 400 | |||
| 401 | 1; | ||
| 402 |