lib/UR/BoolExpr/Util.pm

filter: Statementstmt 42.2% 49 / 116 Branchbran 16.6% 9 / 54 Conditioncond 7.6% 2 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 55.5% 10 / 18 Podpod 0.0% 0 / 5 totaltotal 30.0% 70 / 233 CC 44 SCAR 41.8
CC
44
Cov
31%
_fixup_ur_objects_from_thawed_data
156.0
values_to_value_id
94.1
value_id_to_values
35.0
CRAP 65.4
« blib/lib/UR/Namespace/Command/Update/Doc.pm ↑ Summary blib/lib/UR/DataSource.pm »
1  
2 package UR::BoolExpr::Util;
3  
4 # Non-OO Utility methods for the rule modules.
5  
6 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 266 require UR;
9 266 our $VERSION = "0.47"; # UR $VERSION;
10  
11 266 ▶use Scalar::Util qw(blessed reftype refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 266 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 266 ▶use FreezeThaw;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Because the id is actually a full data structure we need some separators.
16 # Note that these are used for the common case, where FreezeThaw is for arbitrarily complicated rule identifiers.
17  
18 266 our $id_sep = chr(29); # spearetes id property values instead of the old \t
19 266 our $record_sep = chr(30); # within a value_id, delimits a distinct values
20 266 our $unit_sep = chr(31); # seperates items within a single value
21  
22 266 our $null_value = chr(21); # used for undef/null
23 266 our $empty_string = chr(28); # used for ""
24 266 our $empty_list = chr(20); # used for []
25  
26 # These are used when there is any sort of complicated data in the rule.
27  
28 0 ▶sub values_to_value_id_frozen {
Subroutine values_to_value_id_frozen: not called CC=1 SCAR=6.9
Pod undocumented
29 0 my $frozen = FreezeThaw::safeFreeze(@_);
30 0 return "F:" . $frozen;
31 }
32  
33 0 ▶sub value_id_to_values_frozen {
Subroutine value_id_to_values_frozen: not called CC=1 SCAR=1.2
Pod undocumented
34 0 my $value_id = shift;
35 266 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 0 local *FreezeThaw::copyContents = \&_FreezeThaw__copyContents;
37 0 return _fixup_ur_objects_from_thawed_data(FreezeThaw::thaw($value_id));
38 }
39  
40 # FreezeThaw::thaw calls copyContents when thawing out a data structure it's seen before to
41 # copy the contents from an already thawed version into a to-be-thawed container.
42 # But this doesn't work for code references, since it can't reconstitute it. Luckily, if
43 # it's been frozen with safeFreeze, then $first and $second both point to the correct
44 # coderef and there's nothing to do.
45 266 my $original_FreezeThaw__copyContents = \&FreezeThaw::copyContents;
46 0 ▶sub _FreezeThaw__copyContents {
Subroutine _FreezeThaw__copyContents: not called CC=5 SCAR=34.0
47 0 my($first, $second) = @_;
48  
49 0 ▶ goto &$original_FreezeThaw__copyContents if (reftype($first) ne 'CODE' or reftype($second) ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($first) ne "CODE" or builtin::reftype($second) ne "CODE" 0 0 0
Condition0%0/3
builtin::reftype($first) ne "CODE" or builtin::reftype($second) ne "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
builtin::reftype($first) ne "CODE" or builtin::reftype($second) ne "CODE"
builtin::reftype($first) ne "CODE" builtin::reftype($second) ne "CODE"
Truth table0%0/3
A or B
A= builtin::reftype($first) ne "CODE"B= builtin::reftype($second) ne "CODE"
ABresult
1X1
011
000
50  
51 0 ▶ if (refaddr($first) != refaddr($second)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($first) != builtin::refaddr($second)) 0 0 0
52 0 my $ref = reftype($second);
53 0 Carp::croak("Don't know how to copyContents of type `$ref'");
54 }
55 0 ▶ if (ref($second) ne ref($first)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $second ne ref $first) 0 0 0
56 0 bless $_[0], ref $second; # Rebless
57 }
58 0 return $first;
59 }
60  
61 0 ▶sub _fixup_ur_objects_from_thawed_data {
Subroutine _fixup_ur_objects_from_thawed_data: not called CC=12 SCAR=50.5
62 0 my @values = @_;
63  
64 0 our $seen;
65 0 local $seen = $seen;
66 0 ▶ $seen ||= {};
Condition0%0/2
$UR::BoolExpr::Util::seen ||= {}
l!l
00
MC/DC0%0/1
$UR::BoolExpr::Util::seen ||= {}
$UR::BoolExpr::Util::seen
Truth table0%0/2
A
A= $UR::BoolExpr::Util::seen
Aresult
11
00
67  
68 # For things that are UR::Objects (or used to be UR objects), swap the
69 # thawed/cloned one with one from the object cache
70 #
71 # This sub is localized inside _fixup_ur_objects_from_thawed_data so it's not called
72 # externally, and uses $_ as the thing to process, which is set in the foreach loop
73 # below - both as a performance speedup of# not having to prepare an argument list while
74 # processing a possibly deep data structure, and clarity of avoiding double dereferencing
75 # as this sub needs to mutate the item it's processing
76 0 ▶ my $process_it = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
77 0 ▶ if (blessed($_)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_) and $_->isa("UR::Object") || $_->isa("UR::BoolExpr::Util::clonedThing")) 0 0 0
Condition0%0/6
builtin::blessed($_) and $_->isa("UR::Object") || $_->isa("UR::BoolExpr::Util::clonedThing")
!ll&&!rl&&r
000
$_->isa("UR::Object") or $_->isa("UR::BoolExpr::Util::clonedThing")
l!l&&r!l&&!r
000
MC/DC0%0/3
builtin::blessed($_) and $_->isa("UR::Object") || $_->isa("UR::BoolExpr::Util::clonedThing")
builtin::blessed($_) $_->isa("UR::Object") $_->isa("UR::BoolExpr::Util::clonedThing")
Truth table0%0/4
A and B or C
A= builtin::blessed($_)B= $_->isa("UR::Object")C= $_->isa("UR::BoolExpr::Util::clonedThing")
ABCresult
0XX0
1000
11X1
1011
78 and (
79 $_->isa('UR::Object')
80 or
81 $_->isa('UR::BoolExpr::Util::clonedThing')
82 )
83 ) {
84 0 my($class, $id) = ($_->class, $_->id);
85 0 ▶ if (refaddr($_) != refaddr($UR::Context::all_objects_loaded->{$class}->{$id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($_) != builtin::refaddr($UR::Context::all_objects_loaded->{$class}{$id})) 0 0 0
86 # bless the original thing to a non-UR::Object class so UR::Object::DESTROY
87 # doesn't run on it
88 0 my $cloned_thing = UR::BoolExpr::Util::clonedThing->bless($_);
89 # Swap in the object from the object cache
90 0 $_ = $UR::Context::all_objects_loaded->{$class}->{$id};
91 }
92  
93 }
94 0 _fixup_ur_objects_from_thawed_data($_);
95 0 };
96  
97 0 foreach my $data ( @values ) {
98 0 ▶ next unless ref $data; # Don't need to recursively inspect normal scalar data
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 0 0
99 0 ▶ next if $seen->{$data}++;
Branch: 1 branch
BranchTrueFalseTotal
if $UR::BoolExpr::Util::seen->{$data}++ 0 0 0
100  
101 0 ▶ if (ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) 0 0 0
102 0 my $reftype = reftype($data);
103 0 my $iter;
104 0 ▶ if ($reftype eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 0 0 0
elsif ($reftype eq 'HASH') { } 0 0 0
elsif ($reftype eq 'SCALAR' or $reftype eq 'REF') { } 0 0 0
105 0 foreach (@$data) {
106 0 &$process_it;
107 }
108 } elsif ($reftype eq 'HASH') {
109 0 foreach (values %$data) {
110 0 &$process_it;
111 }
112  
113 ▶ } elsif ($reftype eq 'SCALAR' or $reftype eq 'REF') {
Condition0%0/3
$reftype eq "SCALAR" or $reftype eq "REF"
l!l&&r!l&&!r
000
MC/DC0%0/2
$reftype eq "SCALAR" or $reftype eq "REF"
$reftype eq "SCALAR" $reftype eq "REF"
Truth table0%0/3
A or B
A= $reftype eq "SCALAR"B= $reftype eq "REF"
ABresult
1X1
011
000
114 0 local $_ = $$data;
115 0 &$process_it;
116 }
117 }
118 }
119 0 return @values;
120 }
121  
122 # These are used for the simple common-case rules.
123  
124 1596 ▶sub values_to_value_id {
Subroutine values_to_value_id: called CC=15 SCAR=45.4
Pod undocumented
125 1596 my $value_id = "O:";
126  
127 1596 for my $value (@_) {
128  
129 266 ▶ no warnings;# 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
130 1596 ▶ if (length($value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $value) { } 1596 0 1596
elsif (not defined $value) { } 0 0 0
131 1596 ▶ if (ref($value) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 1596 1596
132 0 ▶ if (@$value == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$value == 0) { } 0 0 0
133 0 $value_id .= $empty_list;
134 }
135 else {
136 0 for my $value2 (@$value) {
137 0 ▶ if (not defined $value2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value2) { } 0 0 0
elsif ($value2 eq '') { } 0 0 0
138 0 $value_id .= $null_value . $unit_sep;
139 }
140 elsif ($value2 eq "") {
141 0 $value_id .= $empty_string . $unit_sep;
142 }
143 else {
144 0 ▶ if (ref($value2) or index($value2, $unit_sep) >= 0 or index($value2, $record_sep) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value2 or index($value2, $UR::BoolExpr::Util::unit_sep) != -1 or index($value2, $UR::BoolExpr::Util::record_sep) != -1) 0 0 0
Condition0%0/6
ref $value2 or index($value2, $UR::BoolExpr::Util::unit_sep) != -1 or index($value2, $UR::BoolExpr::Util::record_sep) != -1
l!l&&r!l&&!r
000
ref $value2 or index($value2, $UR::BoolExpr::Util::unit_sep) != -1
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $value2 or index($value2, $UR::BoolExpr::Util::unit_sep) != -1 or index($value2, $UR::BoolExpr::Util::record_sep) != -1
ref $value2 index($value2, $UR::BoolExpr::Util::unit_sep) != -1 index($value2, $UR::BoolExpr::Util::record_sep) != -1
Truth table0%0/4
A or B or C
A= ref $value2B= index($value2, $UR::BoolExpr::Util::unit_sep) != -1C= index($value2, $UR::BoolExpr::Util::record_sep) != -1
ABCresult
1XX1
01X1
0011
0000
145 0 return values_to_value_id_frozen(@_);
146 }
147 0 $value_id .= $value2 . $unit_sep;
148 }
149 }
150 }
151 0 $value_id .= $record_sep;
152 }
153 else {
154 1596 ▶ if (ref($value) or index($value,$unit_sep) >= 0 or index($value,$record_sep) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value or index($value, $UR::BoolExpr::Util::unit_sep) != -1 or index($value, $UR::BoolExpr::Util::record_sep) != -1) 0 1596 1596
Condition33%2/6
ref $value or index($value, $UR::BoolExpr::Util::unit_sep) != -1 or index($value, $UR::BoolExpr::Util::record_sep) != -1
l!l&&r!l&&!r
001596
ref $value or index($value, $UR::BoolExpr::Util::unit_sep) != -1
l!l&&r!l&&!r
001596
MC/DC0%0/3
ref $value or index($value, $UR::BoolExpr::Util::unit_sep) != -1 or index($value, $UR::BoolExpr::Util::record_sep) != -1
ref $value index($value, $UR::BoolExpr::Util::unit_sep) != -1 index($value, $UR::BoolExpr::Util::record_sep) != -1
Truth table25%1/4
A or B or C
A= ref $valueB= index($value, $UR::BoolExpr::Util::unit_sep) != -1C= index($value, $UR::BoolExpr::Util::record_sep) != -1
ABCresult
1XX1
01X1
0011
0000
155 0 return values_to_value_id_frozen(@_);
156 }
157 1596 $value_id .= $value . $record_sep;
158 }
159 } elsif (not defined $value ) {
160 0 $value_id .= $null_value . $record_sep;
161 }
162 else {# ($value eq "") {
163 0 $value_id .= $empty_string . $record_sep;
164 }
165 }
166 1596 return $value_id;
167 }
168  
169 1330 ▶sub value_id_to_values {
Subroutine value_id_to_values: called CC=11 SCAR=35.6
Pod undocumented
170 1330 my $value_id = shift;
171  
172 1330 ▶ unless (defined $value_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value_id) 0 1330 1330
173 0 Carp::confess('No value_id passed in to value_id_to_values()!?');
174 }
175  
176 1330 my $method_identifier = substr($value_id,0,2);
177 1330 $value_id = substr($value_id, 2, length($value_id)-2);
178 1330 ▶ if ($method_identifier eq "F:") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_identifier eq "F:") 0 1330 1330
179 0 return value_id_to_values_frozen($value_id);
180 }
181  
182 1330 my @values = ($value_id =~ /(.*?)$record_sep/gs);
183 1330 for (@values) {
184 1596 ▶ if (substr($_,-1) eq $unit_sep) {
Branch: 4 branches
BranchTrueFalseTotal
if (substr($_, -1) eq $UR::BoolExpr::Util::unit_sep) { } 0 1596 1596
elsif ($_ eq $UR::BoolExpr::Util::null_value) { } 0 1596 1596
elsif ($_ eq $UR::BoolExpr::Util::empty_string) { } 0 1596 1596
elsif ($_ eq $UR::BoolExpr::Util::empty_list) { } 0 1596 1596
185 #$_ = [split($unit_sep,$_)]
186 0 my @values2 = /(.*?)$unit_sep/gs;
187 0 $_ = \@values2;
188 0 for (@values2) {
189 0 ▶ if ($_ eq $null_value) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq $UR::BoolExpr::Util::null_value) { } 0 0 0
elsif ($_ eq $UR::BoolExpr::Util::empty_string) { } 0 0 0
190 0 $_ = undef;
191 }
192 elsif ($_ eq $empty_string) {
193 0 $_ = "";
194 }
195 }
196 }
197 elsif ($_ eq $null_value) {
198 0 $_ = undef;
199 }
200 elsif ($_ eq $empty_string) {
201 0 $_ = "";
202 }
203 elsif ($_ eq $empty_list) {
204 0 $_ = [];
205 }
206 }
207 1330 return @values;
208 }
209  
210 13832 ▶sub is_meta_param {
Subroutine is_meta_param: called CC=1 SCAR=0.0
Pod undocumented
211 13832 my $param_name = shift;
212 13832 return substr($param_name, 0, 1) eq '-';
213 }
214  
215 package UR::BoolExpr::Util::clonedThing;
216  
217 0 ▶sub bless {
Subroutine bless: not called CC=1 SCAR=6.9
218 0 my($class, $thing) = @_;
219 # return $thing if ($thing->isa(__PACKAGE__));
220  
221 0 $thing->{__original_class} = $thing->class;
222 0 bless $thing, $class;
223 }
224  
225 0 ▶sub id {
Subroutine id: not called CC=1 SCAR=6.9
226 0 return shift->{id};
227 }
228  
229 0 ▶sub class {
Subroutine class: not called CC=1 SCAR=6.9
230 0 return shift->{__original_class};
231 }
232  
233 266 1;
234  
235 =pod
236  
237 =head1 NAME
238  
239 UR::BoolExpr::Util - non-OO module to collect utility functions used by the BoolExpr modules
240  
241 =cut
242  
« blib/lib/UR/Namespace/Command/Update/Doc.pm ↑ Summary blib/lib/UR/DataSource.pm »