Transformator.pm

filter: Statementstmt 72.2% 263 / 364 Branchbran 60.0% 84 / 140 Conditioncond 50.0% 8 / 16 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 82.8% 29 / 35 Podpod 0.0% 0 / 3 totaltotal 68.1% 387 / 568 CC 81 SCAR 26.7
CC
81
Cov
68%
_apply_user_transformations
49.2
_apply_identity_transformation
31.7
_transform_any
25.5
CRAP 14.4
« Merger.pm ↑ Summary Comparator.pm »
1 #!/usr/bin/perl -w
2 #!/usr/bin/perl -d:ptkdb -w
3 #
4 # This is module is based on a module with the same name, implemented
5 # when working for Newtec Cy, located in Belgium,
6 # http://www.newtec.be/.
7 #
8  
9 =head1 NAME
10  
11 Data::Transformator - transform nested Perl data structures.
12  
13 =head1 SYNOPSIS
14  
15 From one of the test cases:
16  
17 use Data::Transformator;
18  
19 my $tree
20 = {
21 e => [
22 {
23 e1 => {
24 },
25 },
26 {
27 e2 => {
28 },
29 },
30 {
31 e3 => {
32 },
33 },
34 ],
35 };
36  
37 my $expected_data
38 = {
39 e => [
40 {
41 e1 => {
42 },
43 },
44 ],
45 };
46  
47 my $transformation
48 = Data::Transformator->new
49 (
50 name => 'test_transform3',
51 contents => $tree,
52 apply_identity_transformation => {
53 e => [
54 1,
55 ],
56 },
57 );
58  
59 my $transformed_data = $transformation->transform();
60  
61 use Data::Comparator qw(data_comparator);
62  
63 my $differences = data_comparator($transformed_data, $expected_data);
64  
65 if ($differences->is_empty())
66 {
67 print "$0: 3: success\n";
68  
69 ok(1, '3: success');
70 }
71 else
72 {
73 print "$0: 3: failed\n";
74  
75 ok(0, '3: failed');
76 }
77  
78 =head1 DESCRIPTION
79  
80 Data::Transformator allows to transform a nested perl data structure
81 -- the source data -- in a new nested perl data structure -- the
82 result data. The source nested data structure can contain perl hashes,
83 arrays and scalars, but should not be self-referential (if I remember
84 well, the existing protection against self-referential data structures
85 in the transformator engine is currently broken).
86  
87 =head1 USE
88  
89 Running a transformation can be done in two essentially different
90 ways:
91  
92 =over 2
93  
94 =item
95  
96 extract data from an existing source : the transformation applies an
97 identity transformation (meaning copy the source structure to the
98 result), and during the process it uses selectors to select what data
99 from the source to copy to the result. The result automatically
100 inherits the structure from the source. This is called a selective
101 transformation. Use the key 'apply_identity_transformation' to enable
102 this mode.
103  
104 =item
105  
106 copy data from source to result : the transformation must be told what
107 the exact structure of the result is, before the result data can be
108 inserted into the result. This is called a constructive
109 transformation.
110  
111 =back
112  
113 It is possible to combine the two above in a single run, but that is
114 currently not tested enough to be sure it works alright, so be very
115 careful with that.
116  
117 To use Data::Transformator, you have to
118  
119 =over 2
120  
121 =item 1
122  
123 Construct the transformation with appropriate options:
124  
125 =over 2
126  
127 =item
128  
129 Give the transformation a name, the name describes the purpose and/or
130 activities for the transformation.
131  
132 =item
133  
134 Tell the transformation if you want it to run as a selective
135 transformation or not (option name
136 'apply_identity_transformation'). Transformations can always be used
137 as constructive transformations.
138  
139 =item
140  
141 Tell the transformation how to find the data source.
142  
143 =over 2
144  
145 =item
146  
147 Or the data source is literal content (option name 'contents').
148  
149 =item
150  
151 Or the data source is an object that implements a '->generate()'
152 method (option name 'source'). Transformations implement themselves a
153 '->generate()' method such that transformations can be cascaded
154 easily.
155  
156 =back
157  
158 =item
159  
160 Tell the transformation what to transform (selection) and how to
161 transform (generate result). Data::Transformator uses code references
162 to generate results, of alternatively simpler things as explained
163 below. Whenever a code reference is used, it is called with the
164 arguments (self, context, current_content). Here, self is the
165 Data::Transformator object, context is an object that describes the
166 current context in the source data structure, current_content is the
167 generated result so far. $context->{path} contains a string with the
168 path to the current element. Compononents of the path are '/'
169 separated (unless overwritten with the constructor key 'separator').
170 This can be used for regular matching, and is especially handy using
171 'simple_transformators', see below.
172  
173 Following keys are available to the constructor of
174 Data::Transformator:
175  
176 =over 2
177  
178 =item ->{simple_transformators}
179  
180 Is an array of simple_transformators. Each simple_transformator is a
181 hash with a 'matcher' key that contains a regular expression that is
182 matched with the path of the currently selected element. If there is
183 a match, the selected subtree is put in the end result, under the
184 value of the 'key' element of the simple_transformator (creating a
185 hash in the result if necessary). If there is no 'key' element in the
186 simple_transformator, a 'code' element is looked for, which is a code
187 reference. The code is called to insert an appropriate result.
188  
189 =item ->{transformators}
190  
191 Is an array of transformators. Each transformator is code reference
192 that gets called as usual.
193  
194 =item ->{apply_identity_transformation}
195  
196 Contains a nested perl data structure that reflects the structure of
197 the source data. All data of the source that is selected by scalars
198 that evaluate to true in the content of apply_identity_transformation,
199 is inserted in the result. This key is very handy for selecting a set
200 of small portions of data, if the structure of the source is known
201 beforehand.
202  
203 =back
204  
205 Take a look at existing examples, e.g. in the unit tests of the
206 transformation engine.
207  
208 =back
209  
210 =item 2
211  
212 Call the '->transform()' method on the transformator.
213  
214 =item 3
215  
216 Use the result data that is returned by the '->transform()' method.
217  
218 =back
219  
220  
221 =head1 The transformation library
222  
223 There is a small transformation library embedded in the
224 Data::Transformator. This library currently allows to
225  
226 =over 2
227  
228 =item
229  
230 transform an array to be found somewhere in the data source to a hash
231 in the result set.
232  
233 =item
234  
235 transform a hash to be found somewhere in the data source to an array
236 in the result set.
237  
238 =back
239  
240 The library generates closures that work on the source data.
241  
242  
243 =head1 BACKGROUND
244  
245 For the interested reader, please follow these reasoning steps:
246  
247 =over 2
248  
249 =item 1
250  
251 A database query of a relational database (using tables and nested
252 tabled) can always be written out in one of the XML query dialects.
253  
254 =item 2
255  
256 Following from point 1: a database query can be expressed as a
257 structured query.
258  
259 =item 3
260  
261 A structured query can be summarized as
262  
263 =over 2
264  
265 =item 1
266  
267 a selection of data from a preexisting data source.
268  
269 =item 2
270  
271 a structural simplification of the selection.
272  
273 =back
274  
275 =item 4
276  
277 Combining the above: a query can be defined as applying (1) a
278 selective transformation and (2) a constructive transformation in
279 sequence to a preexisting data source. This is called cascaded
280 transformations.
281  
282 =back
283  
284 =head1 BUGS
285  
286 Does only work with scalars, hashes and arrays. Support for
287 self-referential structures seems broken at the moment.
288  
289 =head1 AUTHOR
290  
291 Hugo Cornelis, hugo.cornelis@gmail.com
292  
293 Copyright 2007 Hugo Cornelis.
294  
295 This module is free software; you can redistribute it and/or
296 modify it under the same terms as Perl itself.
297  
298 =head1 SEE ALSO
299  
300 Data::Merger(3), Data::Comparator(3), Clone(3)
301  
302 =cut
303  
304  
305 package Data::Transformator;
306  
307  
308 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
309 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
310  
311  
312 5 my $debug_enabled = '0';
313  
314 5 my $debug_context = '0';
315  
316 5 my $debug_identity_transform = '0';
317  
318 5 my $debug_identity_transform2 = '0';
319  
320 5 my $debug_identity_transform3 = '0';
321  
322 5 my $separator = "/";
323  
324  
325 sub _apply_identity_transformation
326 264 ▶{
Subroutine _apply_identity_transformation: called CC=12 SCAR=34.6
327 264 my $self = shift;
328  
329 264 my $context = shift;
330  
331 264 my $current = _context_get_current($context);
332  
333 264 my $result_current = _context_get_current_result($context);
334  
335 264 my $previous = _context_get_previous($context);
336  
337 264 my $result_previous = _context_get_previous_result($context);
338  
339 264 my $previous_type = $previous->{type};
340  
341 #! Do not change the order of the tests, it does harm, though it should
342 #! not. I do not know what is happening.
343  
344 # fill in constants
345  
346 264 ▶ if ($current->{type} eq 'SCALAR')
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{"type"} eq "SCALAR") 100 164 264
347 {
348 100 my $content = _context_get_current_content($context);
349  
350 # fill in the constant
351  
352 100 $result_current->{content} = $content;
353  
354 100 ▶ if ($debug_identity_transform)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_identity_transform) 0 100 100
355 {
356 0 print STDERR "_apply_identity_transformation() for constant $content\n";
357  
358 0 ▶ if ($debug_identity_transform2)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_identity_transform2) 0 0 0
359 {
360 0 print STDERR Data::Dumper::Dumper($result_current);
361 }
362 }
363  
364 }
365  
366 # link items on stack by looking at the type of one level up
367  
368 # if the previous result is the root
369  
370 264 ▶ if ($previous_type eq 'ROOT')
Branch: 3 branches
BranchTrueFalseTotal
if ($previous_type eq 'ROOT') { } 21 243 264
elsif ($previous_type eq 'HASH') { } 190 53 243
elsif ($previous_type eq 'ARRAY') { } 53 0 53
371 {
372 # we do not have a result yet, get one by assigning (initialization)
373  
374 21 $result_previous->{content} = $result_current->{content};
375 }
376 elsif ($previous_type eq 'HASH')
377 {
378 190 my $previous_component_key = $previous->{component_key};
379  
380 $result_previous->{content}->{$previous_component_key}
381 190 = $result_current->{content};
382  
383 190 ▶ if ($debug_identity_transform2)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_identity_transform2) 0 190 190
384 {
385 0 my $component_key = $previous->{component_key};
386  
387 0 my $content = $result_current->{content};
388  
389 0 print STDERR "Default Transform : added to previous hash for key $component_key\n";
390 0 print STDERR Data::Dumper::Dumper($content);
391 0 print STDERR "Default Transform : results in\n";
392 0 print STDERR Data::Dumper::Dumper($result_previous);
393 0 print STDERR "Default Transform : end\n";
394 }
395 }
396 elsif ($previous_type eq 'ARRAY')
397 {
398 53 push @{$result_previous->{content}}, $result_current->{content};
399  
400 53 ▶ if ($debug_identity_transform2)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_identity_transform2) 0 53 53
401 {
402 0 my $component_key = $previous->{component_key};
403  
404 0 my $content = $result_current->{content};
405  
406 0 print STDERR "Default Transform : added to previous array, item $component_key\n";
407 0 print STDERR Data::Dumper::Dumper($content);
408 0 print STDERR "Default Transform : results in\n";
409 0 print STDERR Data::Dumper::Dumper($result_previous);
410 0 print STDERR "Default Transform : end\n";
411 }
412 }
413 else
414 {
415 0 ▶ if ($debug_identity_transform)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_identity_transform) 0 0 0
416 {
417 0 print STDERR "_apply_identity_transformation(): Illegal context type $previous_type\n";
418  
419 0 ▶ if ($debug_identity_transform2)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_identity_transform2) 0 0 0
420 {
421 0 print STDERR Data::Dumper::Dumper($context, $previous, $current);
422 }
423 }
424 }
425  
426 264 ▶ if ($debug_identity_transform3)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_identity_transform3) 0 264 264
427 {
428 0 print STDERR "_apply_identity_transformation() main result is now\n", Dumper($context->{result});
429 }
430 }
431  
432  
433 sub _apply_transformations
434 555 ▶{
Subroutine _apply_transformations: called CC=2 SCAR=6.9
435 555 my $self = shift;
436  
437 555 my $context = shift;
438  
439 555 my $component_key = shift;
440  
441 555 my $component = shift;
442  
443 # # if the default transformation is enabled (i.e. straight copy)
444  
445 # if ($self->{apply_identity_transformation})
446 # {
447 # $self->_apply_identity_transformation($context);
448 # }
449  
450 # if the component_key is defined
451  
452 #! note that component_keys are always defined for hashes and arrays, but
453 #! not for scalars. This is currently a bug.
454  
455 555 ▶ if (defined $component_key)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $component_key) 533 22 555
456 {
457 # apply user transformations
458  
459 533 $self->_apply_user_transformations($context, $component_key, $component, );
460 }
461 }
462  
463  
464 sub _apply_user_transformations
465 533 ▶{
Subroutine _apply_user_transformations: called CC=11 SCAR=39.0
466 533 my $self = shift;
467  
468 533 my $context = shift;
469  
470 533 my $component_key = shift;
471  
472 533 my $component = shift;
473  
474 533 my $array = $context->{array};
475  
476 # simple regex transforms (based on sems_system module)
477  
478 533 ▶ if (exists $self->{simple_transformators})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"simple_transformators"}) 0 533 533
479 {
480  
481 SIMPLE_TRANSFORM:
482 0 foreach my $transformator (@{$self->{simple_transformators}})
483 {
484 0 my $matcher = $transformator->{matcher};
485  
486 0 ▶ if ($context->{path} =~ m|$matcher|)
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{'path'} =~ /$matcher/) { } 0 0 0
487 {
488 # print STDERR "Calling transformator : $transformator->{name}\n";
489  
490 # a simple one-to-one mapping
491  
492 0 ▶ if (exists $transformator->{key})
Branch: 2 branches
BranchTrueFalseTotal
if (exists $transformator->{'key'}) { } 0 0 0
elsif (exists $transformator->{'code'}) { } 0 0 0
493 {
494 0 my $result_key = $transformator->{key};
495  
496 #t why do I fetch the main result here ?
497 #t I should be fetching the current result ?
498  
499 #t Perhaps I could use $result_key in an eval string
500 #t to allow nested results for simple transformators.
501  
502 0 my $result = _context_get_main_result($context);
503  
504 0 $result->{content}->{$result_key}
505 = _context_get_current_content($context);
506 }
507  
508 # possibly a one-to-many mapping
509  
510 elsif (exists $transformator->{code})
511 {
512 0 my $code = $transformator->{code};
513  
514 0 my $result = _context_get_main_result($context);
515  
516 0 my $current_content
517 = _context_get_current_content($context);
518  
519 0 my $transformation_result
520 = &$code($self, $context, $current_content, );
521  
522 0 ▶ if ($transformation_result)
Branch: 1 branch
BranchTrueFalseTotal
if ($transformation_result) 0 0 0
523 {
524 #! expensive copy, can be replaced with a loop
525  
526 0 ▶ if (exists $result->{content}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{'content'} and defined $result->{'content'}) { } 0 0 0
Condition0%0/3
exists $result->{"content"} and defined $result->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $result->{"content"} and defined $result->{"content"}
exists $result->{"content"} defined $result->{"content"}
Truth table0%0/3
A and B
A= exists $result->{"content"}B= defined $result->{"content"}
ABresult
0X0
100
111
527 && defined $result->{content})
528 {
529 $result->{content}
530 = {
531 0 %{$result->{content}},
532 %$transformation_result,
533 };
534 }
535 else
536 {
537 $result->{content}
538 = {
539 0 %$transformation_result,
540 };
541 }
542 }
543 }
544  
545 0 last SIMPLE_TRANSFORM;
546 }
547 else
548 {
549 # print STDERR "$context->{path} does not match with $matcher\n";
550 }
551 }
552 }
553  
554 # general transformators
555  
556 533 ▶ if (exists $self->{transformators})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"transformators"}) 193 340 533
557 {
558 193 my $count = 0;
559  
560 193 foreach my $transformator (@{$self->{transformators}})
561 {
562 273 $count++;
563  
564 # print STDERR "Calling transformator $count\n"; #$transformator->{name}\n";
565  
566 273 &$transformator($array->[$#$array], $context, $component);
567 }
568 }
569 }
570  
571  
572 sub _result_create
573 434 ▶{
Subroutine _result_create: called CC=1 SCAR=0.0
574 434 my $default_result = shift;
575  
576 return {
577 434 content => $default_result,
578 };
579 }
580  
581  
582 sub _context_create
583 30 ▶{
Subroutine _context_create: called CC=2 SCAR=7.0
584 30 my $base_path = shift;
585  
586 30 my $base_result = shift;
587  
588 30 ▶ my $separator = shift || "/";
Condition50%1/2
shift() || '/'
l!l
030
MC/DC0%0/1
shift() || '/'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
589  
590 #t the root contains the complete result, the other entries in the
591 #t array are sub results, which are to be covered by the root
592 #t result.
593  
594 30 my $context
595 = {
596 array => [
597 {
598 result => _result_create($base_result),
599 type => 'ROOT',
600 }
601 ],
602 path => $base_path,
603 separator => $separator,
604 };
605  
606 30 $context->{result} = $context->{array}->[0]->{result};
607  
608 30 return $context;
609 }
610  
611  
612 #
613 # Obtain a ref. containing info of the top element of the current context.
614 #
615  
616 sub _context_get_current
617 1075 ▶{
Subroutine _context_get_current: called CC=1 SCAR=0.0
618 1075 my $context = shift;
619  
620 1075 my $array = $context->{array};
621  
622 1075 my $current = $array->[$#$array];
623  
624 1075 return $current;
625 }
626  
627  
628 #
629 # Obtain the original content of the top element of the current context. The
630 # original content is taken from the original element. Must be considered
631 # read-only, and for informational purposes only. Do not embed it into the
632 # result, since if so, you are mixing the content of the original data
633 # structure with the resulting data structure, which is not the intent of this
634 # module. Perhaps in the future this can be changed, to allow creation of
635 # simplified views on a complicated data structure.
636 #
637  
638 sub _context_get_current_content
639 110 ▶{
Subroutine _context_get_current_content: called CC=1 SCAR=0.0
640 110 return _context_get_current($_[0])->{content};
641 }
642  
643  
644 #
645 # Obtain information of the resulting content of the top element of the
646 # current context. The actual content of the result can be found ->{content}.
647 #
648  
649 sub _context_get_current_result
650 671 ▶{
Subroutine _context_get_current_result: called CC=1 SCAR=0.0
651 671 return _context_get_current($_[0])->{result};
652 }
653  
654  
655 #
656 # Obtain information of the main content of the current context. The actual
657 # content of the result can be found ->{content}.
658 #
659  
660 sub _context_get_main_result
661 52 ▶{
Subroutine _context_get_main_result: called CC=1 SCAR=0.0
662 52 return $_[0]->{result};
663 }
664  
665  
666 #
667 # Obtain a ref. containing info of the next-to-top element of the current
668 # context.
669 #
670  
671 sub _context_get_previous
672 528 ▶{
Subroutine _context_get_previous: called CC=2 SCAR=7.1
673 528 my $context = shift;
674  
675 528 my $array = $context->{array};
676  
677 528 my $top_index = $#$array;
678  
679 528 ▶ if ($top_index > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($top_index > 0) { } 528 0 528
680 {
681 528 my $current = $array->[$#$array - 1];
682  
683 528 return $current;
684 }
685 else
686 {
687 0 return undef;
688 }
689 }
690  
691  
692 #
693 # Obtain information of the next-to-top result of the context. The actual
694 # content of the result can be found ->{content}.
695 #
696 # returns undef if there is no such item.
697 #
698  
699 sub _context_get_previous_result
700 264 ▶{
Subroutine _context_get_previous_result: called CC=2 SCAR=7.6
701 264 my $previous = _context_get_previous($_[0]);
702  
703 264 ▶ if ($previous)
Branch: 1 branch
BranchTrueFalseTotal
if ($previous) { } 264 0 264
704 {
705 264 return $previous->{result};
706 }
707 else
708 {
709 0 return undef;
710 }
711 }
712  
713  
714 sub _context_get_seen_info
715 0 ▶{
Subroutine _context_get_seen_info: not called CC=1 SCAR=6.9
716 0 return($_[0]->{seen}->{$_[1]});
717 }
718  
719  
720 sub _context_has_seen
721 407 ▶{
Subroutine _context_has_seen: called CC=1 SCAR=0.0
722 407 return(exists $_[0]->{seen}->{$_[1]});
723 }
724  
725  
726 sub _context_matches
727 0 ▶{
Subroutine _context_matches: not called CC=1 SCAR=6.9
728 #t we could do fancy things, e.g. if $_[1] is a 'context
729 #t describing' hash, convert hash to string before comparing.
730  
731 0 return $_[0]->{path} =~ /$_[1]/;
732 }
733  
734  
735 sub _context_pop
736 404 ▶{
Subroutine _context_pop: called CC=2 SCAR=7.1
737 404 my $context = shift;
738  
739 404 my $separator = $context->{separator};
740  
741 404 pop @{$context->{array}};
742  
743 404 $context->{path} =~ s/^(.*[^\\])$separator.*/$1/;
744  
745 404 ▶ if ($debug_context)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_context) 0 404 404
746 {
747 0 print STDERR "($context->{path}) _context_pop()\n";
748 }
749 }
750  
751  
752 sub _context_push
753 404 ▶{
Subroutine _context_push: called CC=2 SCAR=7.1
754 404 my $context = shift;
755  
756 404 my $new = shift;
757  
758 404 my $separator = $context->{separator};
759  
760 404 $new->{result} = _result_create($new->{default_result});
761  
762 404 push @{$context->{array}}, $new;
763  
764 404 $context->{path} .= "${separator}__NONE__";
765  
766 404 ▶ if ($debug_context)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_context) 0 404 404
767 {
768 0 print STDERR "($context->{path}) _context_push(), default_result $new->{default_result}\n";
769 }
770 }
771  
772  
773 sub _context_register_current
774 574 ▶{
Subroutine _context_register_current: called CC=5 SCAR=16.3
775 574 my $context = shift;
776  
777 574 my $transform = shift;
778  
779 574 my $component_key = shift;
780  
781 574 my $component = shift;
782  
783 574 my $count = shift;
784  
785 574 my $separator = $context->{separator};
786  
787 574 my $array = $context->{array};
788  
789 #t actually I think I need an _context_unregister_current() sub
790 #t that resets ->{string}, ->{display}, and possibly others.
791  
792 # print STDERR "($separator), $component, $component_key\n";
793  
794 574 ▶ if ($component_key)
Branch: 1 branch
BranchTrueFalseTotal
if ($component_key) 558 16 574
795 {
796 558 $component_key =~ s|$separator|\\${separator}|g;
797 }
798  
799 574 $array->[$#$array]->{current} = $count;
800 $array->[$#$array]->{component_key}
801 574 ▶ = defined $component_key ? $component_key : '__UNDEF__';
Branch: 1 branch
BranchTrueFalseTotal
defined $component_key ? : 568 6 574
802 # $array->[$#$array]->{string} = undef;
803 # $array->[$#$array]->{type} = ref $component || 'SCALAR';
804 574 $array->[$#$array]->{content} = $component;
805  
806 574 ▶ if ($debug_enabled)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_enabled) 0 574 574
807 {
808 0 print
809 STDERR
810 " " x (2 * $#$array)
811 . "$array->[$#$array]->{type} : $array->[$#$array]->{component_key}\n";
812 }
813  
814 $context->{path}
815 574 =~ s|(.*[^\\]$separator).*|$1$array->[$#$array]->{component_key}|;
816  
817 574 ▶ if ($debug_context)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_context) 0 574 574
818 {
819 0 print STDERR "($context->{path}) [_context_register_current($array->[$#$array]->{component_key})]\n";
820 }
821 }
822  
823  
824 sub _context_set_seen_info
825 814 ▶{
Subroutine _context_set_seen_info: called CC=1 SCAR=0.0
826 814 $_[0]->{seen}->{$_[1]} = $_[2];
827 }
828  
829  
830 sub _transform_any
831 407 ▶{
Subroutine _transform_any: called CC=16 SCAR=32.4
832 407 my $self = shift;
833  
834 407 my $context = shift;
835  
836 407 my $contents = shift;
837  
838 407 my $result;
839  
840 407 ▶ my $contents_output
Branch: 1 branch
BranchTrueFalseTotal
defined $contents ? : 401 6 407
841 = defined $contents ? $contents : '__UNDEF__';
842  
843 #t The problem is as follows : the current is not pushed on the context,
844 #t so we consult the wrong entry to compute the result.
845 #t Second this result may only be computed if the default transform
846 #t is enabled.
847  
848 407 ▶ if (_context_has_seen($context, $contents_output) && 0)
Branch: 1 branch
BranchTrueFalseTotal
if (_context_has_seen($context, $contents_output) and 0) 0 407 407
Condition100%2/2
_context_has_seen($context, $contents_output) and 0
!ll
38423
MC/DC100%1/1
_context_has_seen($context, $contents_output) and 0
_context_has_seen($context, $contents_output)
Truth table100%2/2
A
A= _context_has_seen($context, $contents_output)
Aresult
00
11
849 {
850 0 my $seen_info = _context_get_seen_info($context, $contents_output);
851  
852 0 ▶ if ($debug_context)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_context) 0 0 0
853 {
854 0 print STDERR "For context $context->{path}, we have seen $contents_output, $seen_info\n";
855 }
856  
857 0 $result = $seen_info;
858  
859 0 return $result;
860 }
861  
862 407 _context_set_seen_info($context, $contents_output, $contents_output);
863  
864 407 local $_ = ref($contents);
865  
866 407 ▶ if (!$_)
Branch: 1 branch
BranchTrueFalseTotal
if (not $_) { } 162 245 407
867 {
868 # a constant.
869  
870 162 ▶ if ($debug_enabled)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_enabled) 0 162 162
871 {
872 0 print STDERR "$self->{name} : Constant ($contents_output)\n";
873 }
874  
875 162 $result = $self->_transform_constant($context, $contents);
876 }
877 else
878 {
879 BASE_TYPE:
880 {
881 # an array.
882  
883 245 /^ARRAY$/ and do
884 245 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
/^ARRAY$/ and do { if ($debug_enabled) { print STDERR qq[$self->{"name"} : Array ($contents_output)\n]; } ; $result = $self->_transform_array($context, $contents); last BASE_TYPE } 26 219 245
885 26 ▶ if ($debug_enabled)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_enabled) 0 26 26
886 {
887 0 print STDERR "$self->{name} : Array ($contents_output)\n";
888 }
889  
890 26 $result = $self->_transform_array($context, $contents);
891  
892 26 last BASE_TYPE;
893 };
894  
895 # a hash
896  
897 /^HASH$/ and do
898 219 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
/^HASH$/ and do { if ($debug_enabled) { print STDERR qq[$self->{"name"} : Hash ($contents_output)\n]; } ; $result = $self->_transform_hash($context, $contents); last BASE_TYPE } 208 11 219
899 208 ▶ if ($debug_enabled)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_enabled) 0 208 208
900 {
901 0 print STDERR "$self->{name} : Hash ($contents_output)\n";
902 }
903  
904 208 $result = $self->_transform_hash($context, $contents);
905  
906 208 last BASE_TYPE;
907 };
908  
909 # an object.
910  
911 11 ▶ if ($debug_enabled)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_enabled) 0 11 11
912 {
913 0 print STDERR "$self->{name} : Object ($_)\n";
914 }
915  
916 11 local $_ = $contents_output;
917  
918 OBJECT_TYPE:
919 {
920 11 /=HASH/ and do
921 11 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
/=HASH/ and do { if ($debug_enabled) { print STDERR qq[$self->{"name"} : Object hash ($contents_output)\n]; } ; $result = $self->_transform_hash($context, $contents); last OBJECT_TYPE } 8 3 11
922 8 ▶ if ($debug_enabled)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_enabled) 0 8 8
923 {
924 0 print STDERR "$self->{name} : Object hash ($contents_output)\n";
925 }
926  
927 8 $result = $self->_transform_hash($context, $contents);
928  
929 8 last OBJECT_TYPE;
930 };
931  
932 /=ARRAY/ and do
933 3 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
/=ARRAY/ and do { if ($debug_enabled) { print STDERR qq[$self->{"name"} : Object Array ($contents_output)\n]; } ; $result = $self->_transform_array($context, $contents); last OBJECT_TYPE } 0 3 3
934 0 ▶ if ($debug_enabled)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_enabled) 0 0 0
935 {
936 0 print STDERR "$self->{name} : Object Array ($contents_output)\n";
937 }
938  
939 0 $result = $self->_transform_array($context, $contents);
940  
941 0 last OBJECT_TYPE;
942 };
943  
944 #t implement.
945 #t
946 #t This is meant for easy extensibility.
947 #t Probably it is the most convenient if we force the object to
948 #t implement an agreed upon interface.
949 #t
950 #t use UNIVERSAL::isa() and perhaps UNIVERSAL::can() to check if
951 #t the interface is implemented by the object.
952 #t
953 #t perform a default action for hashes and arrays if the object
954 #t does not implement a suitable interface, allow the user to
955 #t configure or change the default action.
956 #t
957  
958 #$str .= $self->_formalize_object($context, $contents);
959 }
960 }
961 }
962  
963 407 my $current_result = _context_get_current_result($context);
964  
965 407 _context_set_seen_info($context, $contents_output, $current_result->{content});
966  
967 407 return($result);
968 }
969  
970  
971 sub _transform_array
972 26 ▶{
Subroutine _transform_array: called CC=7 SCAR=19.5
973 26 my $self = shift;
974  
975 26 my $context = shift;
976  
977 26 my $contents = shift;
978  
979 26 my $array = $context->{array};
980  
981 26 my $count = 0;
982  
983 26 my $result = [];
984  
985 26 _context_push(
986 $context,
987 {
988 # contents => $contents,
989 current => 0,
990 default_result => [], #$result,
991 component_key => '__NONE__',
992 size => scalar @$contents,
993 # recurse => 1,
994 # string => undef,
995 type => 'ARRAY',
996 },
997 );
998  
999 # if the default transformation is enabled (i.e. straight copy)
1000  
1001 26 ▶ if ($self->{apply_identity_transformation})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"apply_identity_transformation"}) 20 6 26
1002 {
1003 20 $self->_apply_identity_transformation($context);
1004 }
1005  
1006 # the sort is quite useless in this form : has no clue of nesting
1007 # of $contents.
1008  
1009 #t given an array of (sort(), <regex>) tuples, match <regex> with
1010 #t current component, if matches, apply associated sorting
1011 #t function.
1012 #t
1013 #t this is quite close to transformations too.
1014  
1015 #! note that this assumes that perl sort is order preserving. The man page
1016 #! of perl sort says that it is 'stable' i.e. order preserving.
1017  
1018 26 foreach my $component (sort
1019 {
1020 defined $self->{sort}
1021 0 ? &{$self->{sort}}
1022 (
1023 $a,
1024 $b,
1025 $contents->{$a},
1026 64 ▶ $contents->{$b},
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'sort'} ? : 0 64 64
1027 $context,
1028 )
1029 : 0;
1030 }
1031 @$contents)
1032 {
1033 # compute the component key
1034  
1035 69 my $component_key = '[' . $count . ']';
1036  
1037 # register the name and count of current column
1038  
1039 69 _context_register_current
1040 ($context,
1041 $self,
1042 $component_key,
1043 $component,
1044 $count);
1045  
1046 # increment count (before applying filters)
1047  
1048 69 $count++;
1049  
1050 #
1051 # array_filter return values :
1052 #
1053 # 0 : do not recurse.
1054 # 1 : do recurse.
1055 #
1056  
1057 69 my $filter_data = 1;
1058  
1059 69 ▶ if (exists $self->{array_filter})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"array_filter"}) 6 63 69
1060 {
1061 6 $filter_data = &{$self->{array_filter}}($context, $component);
1062 }
1063  
1064 69 ▶ next if $filter_data eq 0;
Branch: 1 branch
BranchTrueFalseTotal
if $filter_data eq 0 0 69 69
1065  
1066 # apply transformations
1067  
1068 69 $self->_apply_transformations($context, $component_key, $component);
1069  
1070 # transform content of array
1071  
1072 69 push @$result, $self->_transform_any($context, $component);
1073 }
1074  
1075 26 ▶ if (scalar @$contents eq 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$contents eq 0) 2 24 26
1076 {
1077 # apply transformations
1078  
1079 2 $self->_apply_transformations($context, undef, undef,);
1080 }
1081  
1082 # remove this column
1083  
1084 26 _context_pop($context);
1085  
1086 26 return($result);
1087 }
1088  
1089  
1090 sub _transform_constant
1091 162 ▶{
Subroutine _transform_constant: called CC=4 SCAR=13.9
1092 162 my $self = shift;
1093  
1094 162 my $context = shift;
1095  
1096 162 my $contents = shift;
1097  
1098 162 my $result = '';
1099  
1100 162 _context_push(
1101 $context,
1102 {
1103 # contents => $contents,
1104 current => 0,
1105 default_result => '', #$result,
1106 component_key => '__NONE__',
1107 size => 1,
1108 # recurse => 1,
1109 # string => undef,
1110 type => 'SCALAR',
1111 },
1112 );
1113  
1114 162 ▶ if ($debug_enabled)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_enabled) 0 162 162
1115 {
1116 0 print STDERR " # register the name and count of current column\n";
1117 }
1118  
1119 # register the name and count of current column
1120  
1121 162 _context_register_current($context, $self, $contents, $contents, 0);
1122  
1123 # if the default transformation is enabled (i.e. straight copy)
1124  
1125 162 ▶ if ($self->{apply_identity_transformation})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"apply_identity_transformation"}) 100 62 162
1126 {
1127 100 $self->_apply_identity_transformation($context);
1128 }
1129  
1130 162 ▶ if (defined $contents)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $contents) { } 156 6 162
1131 {
1132 156 $result .= $contents;
1133 }
1134 else
1135 {
1136 6 $result .= '__UNDEF__';
1137 }
1138  
1139 # apply transformations
1140  
1141 162 $self->_apply_transformations($context, $contents, $contents, );
1142  
1143 # remove this column
1144  
1145 162 _context_pop($context);
1146  
1147 162 return($result);
1148 }
1149  
1150  
1151 sub _transform_hash
1152 216 ▶{
Subroutine _transform_hash: called CC=7 SCAR=19.5
1153 216 my $self = shift;
1154  
1155 216 my $context = shift;
1156  
1157 216 my $contents = shift;
1158  
1159 216 my $count = 0;
1160  
1161 216 my $result = {};
1162  
1163 216 _context_push(
1164 $context,
1165 {
1166 # contents => $contents,
1167 current => 0,
1168 default_result => {}, #$result,
1169 component_key => '__NONE__',
1170 size => scalar keys %$contents,
1171 # recurse => 1,
1172 # string => undef,
1173 type => 'HASH',
1174 },
1175 );
1176  
1177 # if the default transformation is enabled (i.e. straight copy)
1178  
1179 216 ▶ if ($self->{apply_identity_transformation})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"apply_identity_transformation"}) 144 72 216
1180 {
1181 144 $self->_apply_identity_transformation($context);
1182 }
1183  
1184 # the sort is quite useless in this form : has no clue of nesting
1185 # of $contents.
1186  
1187 #t given an array of (sort(), <regex>) tuples, match <regex> with
1188 #t current component, if matches, apply associated sorting
1189 #t function.
1190 #t
1191 #t this is quite close to transformations too.
1192  
1193 216 foreach my $component_key (sort
1194 {
1195 defined $self->{sort}
1196 14 ? &{$self->{sort}}
1197 (
1198 $a,
1199 $b,
1200 $contents->{$a},
1201 214 ▶ $contents->{$b},
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'sort'} ? : 14 200 214
1202 $context,
1203 )
1204 : $a cmp $b;
1205 }
1206 keys %$contents)
1207 {
1208 343 my $component = $contents->{$component_key};
1209  
1210 # register name and count of current column
1211  
1212 343 _context_register_current($context, $self, $component_key, $component, $count);
1213  
1214 # increment count (before applying filters)
1215  
1216 343 $count++;
1217  
1218 #
1219 # hash_filter return values :
1220 #
1221 # 0 : do not recurse.
1222 # 1 : do recurse.
1223 #
1224  
1225 343 my $filter_data = 1;
1226  
1227 343 ▶ if (exists $self->{hash_filter})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"hash_filter"}) 103 240 343
1228 {
1229 $filter_data
1230 103 = &{$self->{hash_filter}}($context, $component_key, $component);
1231 }
1232  
1233 343 ▶ next if $filter_data eq 0;
Branch: 1 branch
BranchTrueFalseTotal
if $filter_data eq 0 35 308 343
1234  
1235 # apply transformations
1236  
1237 308 $self->_apply_transformations($context, $component_key, $component);
1238  
1239 # transform component
1240  
1241 308 $result->{$component_key} = $self->_transform_any($context, $component);
1242 }
1243  
1244 216 ▶ if (scalar keys %$contents eq 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$contents eq 0) 14 202 216
1245 {
1246 # apply transformations
1247  
1248 14 $self->_apply_transformations($context, undef, undef, );
1249 }
1250  
1251 # remove this column
1252  
1253 216 _context_pop($context);
1254  
1255 216 return($result);
1256 }
1257  
1258  
1259 #
1260 # generate()
1261 #
1262 # Generate data resulting from this transformation. This is particularly
1263 # useful when cascading transformations.
1264 #
1265  
1266 sub generate
1267 2 ▶{
Subroutine generate: called CC=1 SCAR=0.0
Pod undocumented
1268 2 my $self = shift;
1269  
1270 # we are being used in a transformation cascade of some sort
1271  
1272 2 return $self->transform();
1273 }
1274  
1275  
1276 sub new
1277 28 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
1278 28 my $proto = shift;
1279 28 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0280
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
1280  
1281 28 my $self = { @_ };
1282  
1283 28 bless ($self, $class);
1284  
1285 28 return $self;
1286 }
1287  
1288  
1289 sub transform
1290 30 ▶{
Subroutine transform: called CC=4 SCAR=13.9
Pod undocumented
1291 30 my $self = shift;
1292  
1293 30 my $contents = shift;
1294  
1295 # construct data to be transformed
1296  
1297 30 ▶ my $data = $contents || $self->{contents};
Condition66%2/3
$contents || $self->{'contents'}
l!l&&r!l&&!r
0282
MC/DC50%1/2
$contents || $self->{'contents'}
$contents $self->{'contents'}
Truth table66%2/3
A || B
A= $contentsB= $self->{'contents'}
ABresult
1X1
011
000
1298  
1299 # if there is a cascade from a related object
1300  
1301 30 ▶ if (!$data
Branch: 1 branch
BranchTrueFalseTotal
if (not $data and $self->{"source"}) 2 28 30
Condition66%2/3
not $data and $self->{"source"}
!ll&&!rl&&r
2802
MC/DC50%1/2
not $data and $self->{"source"}
not $data $self->{"source"}
Truth table66%2/3
not(A) and B
A= not $dataB= $self->{"source"}
ABresult
0X0
100
111
1302 && $self->{source})
1303 {
1304 # construct the data from the source
1305  
1306 2 my $source = $self->{source};
1307  
1308 2 $data = $source->generate();
1309 }
1310  
1311 # initialize the working context of the transformation
1312  
1313 30 my $context = _context_create($self->{name}, $self->{context_separator}, );
1314  
1315 # $self->{result} is a literal copy of the original, the
1316 # construction of this copy will probably be removed later on for
1317 # reasons of efficiency.
1318  
1319 30 $self->{result} = $self->_transform_any($context, $data);
1320  
1321 30 return $context->{result}->{content};
1322 }
1323  
1324  
1325 # small convenience library : common transforms of hashes and arrays.
1326 #
1327 # note that transformators simply copy the root of a sub-tree (with
1328 # the sub-tree beneath), such that the filters applied to the sub-tree
1329 # do not interfere anymore with the result. This is currently a
1330 # deliberate choice for (1) ease of implementation, (2) performance..
1331  
1332 # transform an array with given name to a hash with hash_keys as the array
1333 # indices. Use an additional prefix to nest the result.
1334  
1335 sub _lib_transform_array_to_hash
1336 3 ▶{
Subroutine _lib_transform_array_to_hash: called CC=2 SCAR=7.0
1337 3 my $array_name = shift;
1338  
1339 3 my $prefix = shift;
1340  
1341 3 ▶ if (!$prefix)
Branch: 1 branch
BranchTrueFalseTotal
unless ($prefix) 0 3 3
1342 {
1343 0 $prefix = '';
1344 }
1345  
1346 3 my $array_name_quoted = quotemeta $array_name;
1347  
1348 return
1349 sub
1350 87 ▶ {
Subroutine __ANON__: called CC=6 SCAR=18.0
1351 87 my ($transform_data, $context, $contents) = @_;
1352  
1353 # print STDERR $context->{path}, "\n";
1354  
1355 # initialize library sub private variables
1356  
1357 87 ▶ if ($context->{path} =~ m|[^/]/$array_name_quoted$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"path"} =~ m[[^/]/$array_name_quoted$]) 4 83 87
1358 {
1359 4 my $result = _context_get_main_result($context);
1360  
1361 4 return;
1362 }
1363  
1364 83 ▶ if ($context->{path} =~ m|[^/]/$array_name_quoted/\[([0-9])*\]$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"path"} =~ m([^/]/$array_name_quoted/\[([0-9])*\]$)) 8 75 83
1365 {
1366 # print STDERR "Setting result for $context->{path}\n";
1367  
1368 8 my $component = $1;
1369  
1370 8 my $result = _context_get_main_result($context);
1371  
1372 8 my $eval = 1;
1373  
1374 8 ▶ if ($eval)
Branch: 1 branch
BranchTrueFalseTotal
if ($eval) { } 8 0 8
1375 {
1376 # $prefix is interpolated during compilation if this code,
1377 # the other variables are interpolated during the eval.
1378 #
1379 # note the use of single and double quotes.
1380  
1381 8 my $command
1382 # = '$result->{content}${prefix}->{$component} = undef;';
1383 = '#print STDERR Dumper($result, $component);
1384 $result->{content}' . "$prefix" . '->{$component} = undef;';
1385  
1386 8 eval $command;
1387 }
1388 else
1389 {
1390 # print STDERR Dumper($result); print STDERR Dumper($component);
1391 0 $result->{content}->{$component} = undef;
1392 }
1393  
1394 8 return;
1395 }
1396  
1397 75 ▶ if ($context->{path} =~ m|[^/]/$array_name_quoted/\[([0-9])*\]/[^/]+$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"path"} =~ m([^/]/$array_name_quoted/\[([0-9])*\]/[^/]+$)) 9 66 75
1398 {
1399 9 my $component = $1;
1400  
1401 9 my $result = _context_get_main_result($context);
1402  
1403 9 my $eval = 1;
1404  
1405 9 ▶ if ($eval)
Branch: 1 branch
BranchTrueFalseTotal
if ($eval) { } 9 0 9
1406 {
1407 # $prefix is interpolated during execution if this code,
1408 # the other variables are interpolated during the eval.
1409 #
1410 # note the use of single and double quotes.
1411  
1412 9 my $command
1413 # = '$result->{content}${prefix}->{$component} = _context_get_current_content($context);';
1414 = '#print STDERR Dumper($result, $component);
1415 $result->{content}' . "$prefix" . '->{$component} = _context_get_current_content($context);';
1416  
1417 9 eval $command;
1418 }
1419 else
1420 {
1421 # print STDERR Dumper($result); print STDERR Dumper($component);
1422 0 $result->{content}->{$component} = _context_get_current_content($context);
1423 }
1424  
1425 9 return;
1426 }
1427 3 };
1428 }
1429  
1430  
1431 # transform the entries of a hash with given name to an array in the
1432 # result. The result is nested with a given prefix.
1433  
1434 sub _lib_transform_hash_to_array
1435 6 ▶{
Subroutine _lib_transform_hash_to_array: called CC=2 SCAR=6.9
1436 6 my $hash_name = shift;
1437  
1438 6 my $prefix = shift;
1439  
1440 6 ▶ if (!$prefix)
Branch: 1 branch
BranchTrueFalseTotal
unless ($prefix) 4 2 6
1441 {
1442 4 $prefix = '';
1443 }
1444  
1445 6 my $hash_name_quoted = quotemeta $hash_name;
1446  
1447 return
1448 sub
1449 144 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
1450 144 my ($transform_data, $context, $contents) = @_;
1451  
1452 # print STDERR $context->{path}, "\n";
1453  
1454 # initialize library sub private variables
1455  
1456 144 ▶ if ($context->{path} =~ m|[^/]/$hash_name_quoted$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"path"} =~ m[[^/]/$hash_name_quoted$]) 6 138 144
1457 {
1458 6 my $result = _context_get_main_result($context);
1459  
1460 6 $result->{library}->{"_lib_transform_hash_to_array"} = 0;
1461  
1462 6 return;
1463 }
1464  
1465 138 ▶ if ($context->{path} =~ m|[^/]/$hash_name_quoted/([^/])*$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"path"} =~ m[[^/]/$hash_name_quoted/([^/])*$]) 24 114 138
1466 {
1467 # print STDERR "Setting result for $context->{path}\n";
1468  
1469 24 my $component_key = $1;
1470  
1471 24 my $result = _context_get_main_result($context);
1472  
1473 24 my $eval = 1;
1474  
1475 24 ▶ if ($eval)
Branch: 1 branch
BranchTrueFalseTotal
if ($eval) { } 24 0 24
1476 {
1477 # $prefix is interpolated during execution of this code,
1478 # the other variables are interpolated during the eval.
1479 #
1480 # note the use of single and double quotes.
1481  
1482 24 my $command
1483 # = '$result->{content}${prefix}->{$component_key} = _context_get_current_content($context);';
1484 = '#print STDERR Dumper($result, $component_key);
1485 $result->{content}' . "${prefix}->[$result->{library}->{_lib_transform_hash_to_array}]" . ' = $contents;';
1486  
1487 24 eval $command;
1488  
1489 # print STDERR Dumper($result, $component_key);
1490 }
1491 else
1492 {
1493 # print STDERR Dumper($result);
1494 0 $result->{content}->[$component_key] = $contents;
1495 }
1496  
1497 24 $result->{library}->{"_lib_transform_hash_to_array"} += 1;
1498  
1499 24 return;
1500 }
1501 6 };
1502 }
1503  
1504  
1505 # test sub : test the functionality of Data::Transformator.
1506  
1507 sub _main
1508 0 ▶{
Subroutine _main: not called CC=2 SCAR=17.9
1509 0 my $tree;
1510 my $tree1;
1511  
1512 0 $Data::Dumper::Sortkeys = 1;
1513  
1514 0 $tree
1515 = {
1516 a => {
1517 a1 => '-a1',
1518 a2 => '-a2',
1519 },
1520 b => [
1521 '-b1',
1522 '-b2',
1523 '-b3',
1524 ],
1525 c => {
1526 c1 => {
1527 c11 => '-c11',
1528 },
1529 c2 => {
1530 c21 => '-c21',
1531 },
1532 },
1533 d => {
1534 d1 => {
1535 d11 => {
1536 d111 => '-d111',
1537 },
1538 },
1539 },
1540 e => [
1541 {
1542 e1 => {
1543 e11 => {
1544 e111 => '-e111',
1545 },
1546 },
1547 },
1548 {
1549 e2 => {
1550 e21 => {
1551 e211 => '-e211',
1552 },
1553 },
1554 },
1555 {
1556 e3 => {
1557 e31 => {
1558 e311 => '-e311',
1559 },
1560 },
1561 },
1562 ],
1563 };
1564  
1565 # my $config = do '/var/sems/sems.config';
1566  
1567 0 my $config = {};
1568  
1569 0 my $devices;
1570  
1571 $devices->{ANT_CTRL} =
1572 {
1573 0 type => 'UserDefined',
1574 bus => 'dummy',
1575 addr => 0,
1576 equipm_url => 'USS_MON+main',
1577 ok_function => { "USS_MON.ant_ctrl.ntcSeEqSxSwitchControl" => 0, },
1578 };
1579  
1580 # $devices = $config->{devices};
1581  
1582 my $transformation1
1583 = new Data::Transformator
1584 (
1585 name => 'tree-tester',
1586 contents => $tree->{devices} ? $tree->{devices} : $tree,
1587 array_filter =>
1588 sub
1589 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1590 # my ($context, $component) = @_;
1591  
1592 0 ▶ $_[0]->{path} =~ m|/b2$| ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'path'} =~ m[/b2$] ? : 0 0 0
1593 },
1594 hash_filter1 =>
1595 sub
1596 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1597 # my ($context, $hash_key, $hash) = @_;
1598  
1599 0 ▶ $_[0]->{path} =~ m|/c2| ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'path'} =~ m[/c2] ? : 0 0 0
1600 },
1601 0 ▶ transformators =>
Branch: 1 branch
BranchTrueFalseTotal
$tree->{'devices'} ? : 0 0 0
1602 [
1603 _lib_transform_array_to_hash('b', '->{hash_from_array}'),
1604 _lib_transform_hash_to_array('c', '->{array_from_hash}'),
1605 ],
1606 );
1607  
1608 0 my $result1 = $transformation1->transform();
1609  
1610 0 print Dumper($result1);
1611  
1612 0 my $b_entries_source = scalar @{$tree->{b}};
1613 0 my $b_entries_result = scalar keys %{$result1->{hash_from_array}};
1614  
1615 0 my $c_entries_source = scalar keys %{$tree->{c}};
1616 0 my $c_entries_result = scalar @{$result1->{array_from_hash}};
1617  
1618 # print "b entries source $b_entries_source =? b entries result $b_entries_result\n";
1619 # print "c entries source $c_entries_source =? c entries result $c_entries_result\n";
1620  
1621 my $transformation2
1622 = new Data::Transformator
1623 (
1624 name => 'devices',
1625 contents => $devices,
1626 transformators =>
1627 [
1628 # _lib_transform_array_to_hash('b', '->{hash_from_array}'),
1629 # _lib_transform_hash_to_array('c', '->{array_from_hash}'),
1630 sub
1631 0 ▶ {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1632 0 my ($transform_data, $context, $contents) = @_;
1633  
1634 # print STDERR $context->{path}, "\n";
1635  
1636 # retain functions and ok_function.
1637  
1638 0 ▶ if ($context->{path} =~ m|^[^/]*/([^/]*)/([^/]*?function[^/]*?)$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"path"} =~ m[^[^/]*/([^/]*)/([^/]*?function[^/]*?)$]) 0 0 0
1639 {
1640 0 my $device = $1;
1641 0 my $function = $2;
1642  
1643 0 my $result = _context_get_main_result($context);
1644  
1645 0 $result->{content}->{$device}->{$function}
1646 = _context_get_current_content($context);
1647  
1648 0 return;
1649 }
1650  
1651 0 ▶ if ($context->{path} =~ m|^[^/]*/([^/]*)/([^/]*led[^/]*)$|)
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"path"} =~ m[^[^/]*/([^/]*)/([^/]*led[^/]*)$]) 0 0 0
1652 {
1653 0 my $device = $1;
1654 0 my $led = $2;
1655  
1656 0 my $result = _context_get_main_result($context);
1657  
1658 0 $result->{content}->{$device}->{$led}
1659 = _context_get_current_content($context);
1660  
1661 0 return;
1662 }
1663  
1664 },
1665 0 ],
1666 );
1667  
1668 # my $result2 = $transformation2->transform();
1669  
1670 # print Dumper($result2);
1671  
1672 }
1673  
1674  
1675 5 1;
1676  
1677  
« Merger.pm ↑ Summary Comparator.pm »