blib/lib/VS/RuleEngine/Writer/XML.pm

filter: Statementstmt 95.5% 106 / 111 Branchbran 75.0% 12 / 16 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 90.7% 138 / 152 CC 23 SCAR 22.1
CC
23
Cov
92%
_engine_to_doc
16.0
_args_to_element
7.1
_decl_to_element
3.0
CRAP 9.1
↑ Summary
1 package VS::RuleEngine::Writer::XML;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use VS::RuleEngine::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use VS::RuleEngine::Util qw(is_existing_package);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 our $VERSION = "0.03";
14  
15 0 ▶sub _new {
Subroutine _new: not called CC=1 SCAR=6.9
16 0 my ($pkg) = @_;
17 0 my $self = bless {
18 }, $pkg;
19 0 return $self;
20 }
21  
22 1 ▶sub to_file {
Subroutine to_file: called CC=1 SCAR=0.0
Pod documented
23 1 my ($self, $engine, $path) = @_;
24 1 my $xml = $self->_engine_to_doc($engine);
25 1 $xml->toFile($path);
26 }
27  
28 11 our $XML_FORMAT = 1;
29 12 ▶sub as_xml {
Subroutine as_xml: called CC=1 SCAR=0.0
Pod documented
30 12 my ($self, $engine) = @_;
31 12 my $doc = $self->_engine_to_doc($engine);
32 10 return $doc->serialize($XML_FORMAT);
33 }
34  
35 13 ▶sub _engine_to_doc {
Subroutine _engine_to_doc: called CC=16 SCAR=27.7
36 13 my ($self, $engine) = @_;
37
38 13 ▶ croak "Engine is undefined" if !defined $engine;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $engine 1 12 13
39 12 ▶ croak "Not a VS::RuleEngine::Engine instance" if !(blessed $engine && $engine->isa("VS::RuleEngine::Engine"));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($engine) and $engine->isa("VS::RuleEngine::Engine") 1 11 12
Condition66%2/3
builtin::blessed($engine) and $engine->isa("VS::RuleEngine::Engine")
!ll&&!rl&&r
1011
MC/DC50%1/2
builtin::blessed($engine) and $engine->isa("VS::RuleEngine::Engine")
builtin::blessed($engine) $engine->isa("VS::RuleEngine::Engine")
Truth table66%2/3
A and B
A= builtin::blessed($engine)B= $engine->isa("VS::RuleEngine::Engine")
ABresult
0X0
100
111
40
41 11 my $doc = XML::LibXML::Document->new();
42 11 my $root = $doc->createElement("engine");
43 11 $doc->setDocumentElement($root);
44  
45 11 ▶ if (ref $engine ne "VS::RuleEngine::Engine") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $engine ne "VS::RuleEngine::Engine") 1 10 11
46 1 my $engine_class = ref $engine;
47 1 $root->setAttribute(instanceOf => $engine_class);
48 }
49 # Defaults
50 11 for my $defaults_name (sort $engine->defaults) {
51 2 my $values = $engine->get_defaults($defaults_name);
52 2 my $defaults = $doc->createElement("defaults");
53 2 $defaults->setAttribute(name => $defaults_name);
54 2 for my $key (sort keys %$values) {
55 3 $defaults->appendTextChild($key, $values->{$key});
56 }
57 2 $root->addChild($defaults)
58 }
59
60 # Actions
61 11 for my $action (sort $engine->actions) {
62 6 my $decl = $engine->_get_action($action);
63 6 my $entity = _decl_to_element("action", $action, $doc, $decl);
64 6 $root->addChild($entity);
65 }
66  
67 # Inputs
68 11 for my $input (sort $engine->inputs) {
69 3 my $decl = $engine->_get_input($input);
70 3 my $entity = _decl_to_element("input", $input, $doc, $decl);
71 3 $root->addChild($entity);
72 }
73  
74 # Prehooks
75 11 for my $hook (@{$engine->_pre_hooks}) {
76 2 my $decl = $engine->_get_hook($hook);
77 2 my $entity = _decl_to_element("prehook", $hook, $doc, $decl);
78 2 $root->addChild($entity);
79 }
80
81 11 my %actionmap;
82  
83 # Rule comes here
84 11 for my $rule (@{$engine->_rule_order}) {
85 7 my $decl = $engine->_get_rule($rule);
86 7 my $entity = _decl_to_element("rule", $rule, $doc, $decl);
87 7 $root->addChild($entity);
88
89 7 for my $action (@{$engine->_get_rule_actions($rule)}) {
90 5 push @{$actionmap{$action}}, $rule;
91 }
92
93 }
94
95 # Rule -> Action mapping
96 11 for my $action (sort keys %actionmap) {
97 2 my $run = $doc->createElement("run");
98 2 $run->setAttribute("action" => $action);
99 2 for my $rule (@{$actionmap{$action}}) {
100 5 my $rule_elem = $doc->createElement("rule");
101 5 $rule_elem->appendText($rule);
102 5 $run->addChild($rule_elem);
103 }
104 2 $root->addChild($run);
105 }
106
107 # Posthooks
108 11 for my $hook (@{$engine->_post_hooks}) {
109 2 my $decl = $engine->_get_hook($hook);
110 2 my $entity = _decl_to_element("posthook", $hook, $doc, $decl);
111 2 $root->addChild($entity);
112 }
113
114 # Outputs
115 11 for my $output (sort $engine->outputs) {
116 2 my $decl = $engine->_get_output($output);
117 2 my $entity = _decl_to_element("output", $output, $doc, $decl);
118 2 $root->addChild($entity);
119 }
120  
121 11 return $doc;
122 }
123  
124 22 ▶sub _decl_to_element {
Subroutine _decl_to_element: called CC=3 SCAR=11.0
125 22 my ($type, $name, $doc, $decl) = @_;
126
127 22 my $entity = $doc->createElement($type);
128  
129 22 $entity->setAttribute("name" => $name);
130 22 $entity->setAttribute("instanceOf" => $decl->_pkg);
131
132 22 my $defaults = $decl->_defaults;
133 22 ▶ if ($defaults && @$defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaults and @$defaults) 2 20 22
Condition66%2/3
$defaults and @$defaults
!ll&&!rl&&r
0202
MC/DC50%1/2
$defaults and @$defaults
$defaults @$defaults
Truth table66%2/3
A and B
A= $defaultsB= @$defaults
ABresult
0X0
100
111
134 2 $entity->setAttribute("defaults" => join(", ", @$defaults));
135 }
136
137 22 _args_to_element($doc, $entity, $decl->_pkg, $decl->_args);
138
139 22 return $entity;
140 }
141  
142 22 ▶sub _args_to_element {
Subroutine _args_to_element: called CC=6 SCAR=19.6
143 22 my ($doc, $parent, $pkg, $args) = @_;
144
145 22 ▶ if (!is_existing_package($pkg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_existing_package($pkg)) 0 22 22
146 0 eval "require ${pkg};";
147 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
148 }
149
150 22 ▶ if ($pkg->can("process_xml_writer_args")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg->can("process_xml_writer_args")) 1 21 22
151 1 $pkg->process_xml_writer_args($doc, $parent, @$args);
152 1 return;
153 }
154
155 21 ▶ if (!(@$args & 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$args & 1) 21 0 21
156 21 my %args = @$args;
157 21 for my $key (sort keys %args) {
158 8 $parent->appendTextChild($key, $args{$key});
159 }
160 }
161 }
162  
163 11 1;
164 __END__
↑ Summary