| 1 | |||
| 2 | package UR::BoolExpr::Template::PropertyComparison; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | our $VERSION = "0.46"; # UR $VERSION;; | ||
| 7 | |||
| 8 | # Define the class metadata. | ||
| 9 | |||
| 10 | require UR; | ||
| 11 | |||
| 12 | UR::Object::Type->define( | ||
| 13 | class_name => __PACKAGE__, | ||
| 14 | is => ['UR::BoolExpr::Template'], | ||
| 15 | #has => [qw/ | ||
| 16 | # rule_type | ||
| 17 | # subject_class_name | ||
| 18 | # property_name | ||
| 19 | # comparison_operator | ||
| 20 | # value | ||
| 21 | # resolution_code_perl | ||
| 22 | # resolution_code_sql | ||
| 23 | #/], | ||
| 24 | #id_by => ['subject_class_name','logic_string'] | ||
| 25 | ); | ||
| 26 | |||
| 27 | use UR::BoolExpr::Template::PropertyComparison::Equals; | ||
| 28 | use UR::BoolExpr::Template::PropertyComparison::LessThan; | ||
| 29 | use UR::BoolExpr::Template::PropertyComparison::In; | ||
| 30 | use UR::BoolExpr::Template::PropertyComparison::Like; | ||
| 31 | |||
| 32 | sub property_name { | ||
| 33 | (split(' ',$_[0]->logic_detail))[0] | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | sub comparison_operator { | ||
| 38 | (split(' ',$_[0]->logic_detail))[1] | ||
| 39 | } | ||
| 40 | |||
| 41 | sub sub_group { | ||
| 42 | my $self = shift; | ||
| 43 | my $spec = $self->property_name; | ||
| 44 | if ($spec =~ /-/) { | ||
| 45 | #$DB::single = 1; | ||
| 46 | } | ||
| 47 | if ($spec =~ /^(.*)+\-(\w+)(\?|)(\..+|)/) { | ||
| 48 | return $2 . $3; | ||
| 49 | } | ||
| 50 | else { | ||
| 51 | return ''; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | sub get_underlying_rules_for_values { | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub num_values { | ||
| 60 | # Not strictly correct... | ||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub evaluate_subject_and_values { | ||
| 65 | my ($self,$subject,$comparison_value) = @_; | ||
| 66 | my @property_values = $subject->__get_attr__($self->property_name); | ||
| 67 | return $self->_compare($comparison_value, @property_values); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub resolve_subclass_for_comparison_operator { | ||
| 71 | my $class = shift; | ||
| 72 | my $comparison_operator = shift; | ||
| 73 | |||
| 74 | # Remove any escape sequence that may have been put in at UR::BoolExpr::resolve() | ||
| 75 | $comparison_operator =~ s/-.+$// if $comparison_operator; | ||
| 76 | |||
| 77 | my $suffix = UR::Util::class_suffix_for_operator($comparison_operator); | ||
| 78 | |||
| 79 | my $subclass_name = join('::', $class, $suffix); | ||
| 80 | |||
| 81 | my $subclass_meta = UR::Object::Type->get($subclass_name); | ||
| 82 | unless ($subclass_meta) { | ||
| 83 | Carp::confess("Unknown operator '$comparison_operator'"); | ||
| 84 | } | ||
| 85 | return $subclass_name; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _get_for_subject_class_name_and_logic_detail { | ||
| 89 | my $class = shift; | ||
| 90 | my $subject_class_name = shift; | ||
| 91 | my $logic_detail = shift; | ||
| 92 | |||
| 93 | my ($property_name, $comparison_operator) = split(' ',$logic_detail, 2); | ||
| 94 | my $subclass_name = $class->resolve_subclass_for_comparison_operator($comparison_operator); | ||
| 95 | my $id = $subclass_name->__meta__->resolve_composite_id_from_ordered_values($subject_class_name, 'PropertyComparison', $logic_detail); | ||
| 96 | |||
| 97 | return $subclass_name->get($id); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub comparison_value_and_escape_character_to_regex { | ||
| 101 | my ($class, $value, $escape) = @_; | ||
| 102 | |||
| 103 | return '' unless defined($value); | ||
| 104 | |||
| 105 | # anyone who uses the % as an escape character deserves to suffer | ||
| 106 | if ($value eq '%') { | ||
| 107 | return '^.+$'; | ||
| 108 | } | ||
| 109 | |||
| 110 | my $regex = $value; | ||
| 111 | |||
| 112 | # Escape all special characters in the regex. | ||
| 113 | $regex =~ s/([\(\)\[\]\{\}\+\*\.\?\|\^\$\-])/\\$1/g; | ||
| 114 | |||
| 115 | # Handle the escape sequence | ||
| 116 | if (defined $escape) | ||
| 117 | { | ||
| 118 | $escape =~ s/\\/\\\\/g; # replace \ with \\ | ||
| 119 | $regex =~ s/(?<!${escape})\%/\.\*/g; | ||
| 120 | $regex =~ s/(?<!${escape})\_/./g; | ||
| 121 | #LSF: Take away the escape characters. | ||
| 122 | $regex =~ s/$escape\%/\%/g; | ||
| 123 | $regex =~ s/$escape\_/\_/g; | ||
| 124 | } | ||
| 125 | else | ||
| 126 | { | ||
| 127 | $regex =~ s/\%/\.\*/g; | ||
| 128 | $regex =~ s/\_/\./g; | ||
| 129 | } | ||
| 130 | |||
| 131 | # Wrap the regex in delimiters. | ||
| 132 | $regex = "^${regex}\$"; | ||
| 133 | |||
| 134 | my $exception = do { | ||
| 135 | local $@; | ||
| 136 | $regex = eval { qr($regex) }; | ||
| 137 | $@; | ||
| 138 | }; | ||
| 139 | if ($exception) { | ||
| 140 | Carp::confess($exception); | ||
| 141 | } | ||
| 142 | |||
| 143 | return $regex; | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | =head1 NAME | ||
| 149 | |||
| 150 | UR::BoolExpr::Template::PropertyComparison - implements logic for rules with a logic_type of "PropertyComparison" | ||
| 151 | |||
| 152 | =head1 SEE ALSO | ||
| 153 | |||
| 154 | UR::Object(3), UR::BoolExpr::Temmplate(3), UR::BoolExpr(3), UR::BoolExpr::Template::PropertyComparison::* | ||
| 155 | |||
| 156 | =cut | ||
| 157 |