/usr/share/perl5/UR/Object/Set.pm is in libur-perl 0.410-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | package UR::Object::Set;
use strict;
use warnings;
use UR;
use List::MoreUtils qw(any);
our $VERSION = "0.41"; # UR $VERSION;
our @CARP_NOT = qw( UR::Object::Type );
use overload ('""' => '__display_name__');
use overload ('==' => sub { $_[0] . '' eq $_[1] . '' } );
use overload ('eq' => sub { $_[0] . '' eq $_[1] . '' } );
use overload ('!=' => sub { $_[0] . '' ne $_[1] . '' } );
use overload ('ne' => sub { $_[0] . '' ne $_[1] . '' } );
class UR::Object::Set {
is => 'UR::Value',
is_abstract => 1,
has => [
rule => { is => 'UR::BoolExpr', id_by => 'id' },
rule_display => { is => 'Text', via => 'rule', to => '__display_name__'},
member_class_name => { is => 'Text', via => 'rule', to => 'subject_class_name' },
members => { is => 'UR::Object', is_many => 1, is_calculated => 1 }
],
doc => 'an unordered group of distinct UR::Objects'
};
# override the UR/system display name
# this is used in stringification overload
sub __display_name__ {
my $self = shift;
my %b = $self->rule->_params_list;
my $s = Data::Dumper->new([\%b])->Terse(1)->Indent(0)->Useqq(1)->Dump;
$s =~ s/\n/ /gs;
$s =~ s/^\s*{//;
$s =~ s/\}\s*$//;
$s =~ s/\"(\w+)\" \=\> / $1 => /g;
return '(' . ref($self) . ' ' . $s . ')';
}
# I'll neave this in here commented out for the future
# It's intended to keep 'count' for sets updated in real-time as objects are
# created/deleted/updated
#sub _load {
# my $class = shift;
# my $self = $class->SUPER::_load(@_);
#
# my $member_class_name = $rule->subject_class_name;
#
# my $rule = $self->rule
# my $rule_template = $rule->template;
#
# my @rule_properties = $rule_template->_property_names;
# my %rule_values = map { $_ => $rule->value_for($_) } @rule_properties;
#
# my %underlying_comparator_for_property = map { $_->property_name => $_ } $rule_template->get_underlying_rule_templates;
#
# my @aggregates = qw( count );
#
# $member_class_name->create_subscription(
# note => 'set monitor '.$self->id,
# priority => 0,
# callback => sub {
# # make sure the aggregate values get invalidated when objects change
# my @agg_set = @$self{aggregates};
# return unless exists(@agg_set); # returns only if none of the aggregates have values
#
# my ($changed_object, $changed_property, $old_value, $new_value) = @_;
#
# if ($changed_property eq 'create') {
# if ($rule->evaluate($changed_object)) {
# $self->{'count'}++;
# }
# } elsif ($changed_property eq 'delete') {
# if ($rule->evaluate($changed_object)) {
# $self->{'count'}--;
# }
# } elsif (exists $value_index_for_property{$changed_property}) {
#
# my $comparator = $underlying_comparator_for_property{$changed_property};
#
# # HACK!
# $changed_object->{$changed_property} = $old_value;
# my $evaled_before = $comparator->evaluate_subject_and_values($changed_object,$rule_values{$changed_property});
#
# $changed_object->{$changed_property} = $new_value;
# my $evaled_after = $comparator->evaluate_subject_and_values($changed_object,$rule_values{$changed_property});
#
# if ($evaled_before and ! $evaled_after) {
# $self->{'count'}--;
# } elsif ($evaled_after and ! $evaled_before) {
# $self->{'count'}++;
# }
# }
# }
# );
#
# return $self;
#}
sub get_with_special_parameters {
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.");
my $class = shift;
my $bx = shift;
my @params = @_;
my $member_class = $class;
$member_class =~ s/::Set$//;
my $rule = UR::BoolExpr->resolve($member_class, $bx->params_list, @params);
return $class->get($rule->id);
}
sub members {
my $self = shift;
my $rule = $self->rule;
while (@_) {
$rule = $rule->add_filter(shift, shift);
}
return $self->member_class_name->get($rule);
}
sub _members_have_changes {
my $self = shift;
return any { $self->rule->evaluate($_) && $_->__changes__ } $self->member_class_name->is_loaded;
}
sub subset {
my $self = shift;
my $member_class_name = $self->member_class_name;
my $bx = UR::BoolExpr->resolve($member_class_name,@_);
my $subset = $self->class->get($bx->id);
return $subset;
}
sub group_by {
my $self = shift;
my @group_by = @_;
my $grouping_rule = $self->rule->add_filter(-group_by => \@group_by);
my @groups = UR::Context->current->get_objects_for_class_and_rule(
$self->member_class_name,
$grouping_rule,
undef, #$load,
0, #$return_closure,
);
return $self->context_return(@groups);
}
sub __aggregate__ {
my $self = shift;
my $f = shift;
Carp::croak("$f is a group operation, and is not writable") if @_;
my $subject_class_meta = $self->rule->subject_class_name->__meta__;
my $not_ds_expressable = grep { $_->is_calculated or $_->is_transient or $_->is_constant }
map { $_->final_property_meta or $_ }
map { $subject_class_meta->property_meta_for_name($_) || () }
$self->rule->template->_property_names;
# If there are no member-class objects with changes, we can just interrogate the DB
if ($self->_members_have_changes or $not_ds_expressable) {
my $fname;
my @fargs;
if ($f =~ /^(\w+)\((.*)\)$/) {
$fname = $1;
@fargs = ($2 ? split(',',$2) : ());
}
else {
$fname = $f;
@fargs = ();
}
my $local_method = '__aggregate_' . $fname . '__';
$self->{$f} = $self->$local_method(@fargs);
}
elsif (! exists $self->{$f}) {
my $rule = $self->rule->add_filter(-aggregate => [$f])->add_filter(-group_by => []);
UR::Context->current->get_objects_for_class_and_rule(
$self->member_class_name,
$rule,
1, # load
0, # return_closure
);
}
return $self->{$f};
}
sub __aggregate_count__ {
my $self = shift;
my @members = $self->members;
return scalar(@members);
}
sub __aggregate_min__ {
my $self = shift;
my $p = shift;
my $min = undef;
no warnings;
for my $member ($self->members) {
my $v = $member->$p;
next unless defined $v;
$min = $v if not defined $min or $v < $min;
}
return $min;
}
sub __aggregate_max__ {
my $self = shift;
my $p = shift;
my $max = undef;
no warnings;
for my $member ($self->members) {
my $v = $member->$p;
next unless defined $v;
$max = $v if not defined $max or $v > $max;
}
return $max;
}
sub __aggregate_sum__ {
my $self = shift;
my $p = shift;
my $sum = undef;
no warnings;
for my $member ($self->members) {
my $v = $member->$p;
next unless defined $v;
$sum += $v;
}
return $sum;
}
sub __related_set__ {
my $self = $_[0];
my $property_name = $_[1];
my $bx1 = $self->rule;
my $bx2 = $bx1->reframe($property_name);
return $bx2->subject_class_name->define_set($bx2);
}
require Class::AutoloadCAN;
Class::AutoloadCAN->import();
sub CAN {
my ($class,$method,$self) = @_;
if ($method =~ /^__aggregate_(.*)__/) {
# prevent circularity issues since this actually calls ->can();
return;
}
my $member_class_name = $class;
$member_class_name =~ s/::Set$//g;
return unless $member_class_name;
my $is_class_method = !ref($self);
my $member_method_closure = $member_class_name->can($method);
if ($is_class_method && $member_method_closure) {
# We should only get here if the Set class has not implemented the method.
# In which case we will delegate to the member class.
return sub {
my $self = shift;
return $member_method_closure->($member_class_name, @_);
};
}
if ($member_method_closure) {
my $member_class_meta = $member_class_name->__meta__;
my $member_property_meta = $member_class_meta->property_meta_for_name($method);
# regular property access
if ($member_property_meta) {
return sub {
my $self = shift;
if (@_) {
Carp::croak("Cannot use method $method as a mutator: Set properties are not mutable");
}
my $rule = $self->rule;
if ($rule->specifies_value_for($method)) {
return $rule->value_for($method);
}
else {
my @members = $self->members;
my @values = map { $_->$method } @members;
return @values if wantarray;
return if not defined wantarray;
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;
return $values[0];
}
};
}
# set relaying with $s->foo_set->bar_set->baz_set;
if (my ($property_name) = ($method =~ /^(.*)_set$/)) {
return sub {
shift->__related_set__($property_name, @_)
}
}
# other method
return sub {
my $self = shift;
if (@_) {
Carp::croak("Cannot use method $method as a mutator: Set properties are not mutable");
}
my @members = $self->members;
my @values = map { $_->$method } @members;
return @values if wantarray;
return if not defined wantarray;
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;
return $values[0];
};
}
else {
# a possible aggregation function
# see if the method ___aggregate__ uses exists, and if so, delegate to __aggregate__
# TODO: delegate these to aggregation function modules instead of having them in this module
my $aggregator = '__aggregate_' . $method . '__';
if ($self->can($aggregator)) {
return sub {
my $self = shift;
my $f = $method;
if (@_) {
$f .= '(' . join(',',@_) . ')';
}
return $self->__aggregate__($f);
};
}
# set relaying with $s->foo_set->bar_set->baz_set;
if (my ($property_name) = ($method =~ /^(.*)_set$/)) {
return sub {
shift->__related_set__($property_name, @_)
}
}
}
return;
}
1;
|