Formula/Context.pm

filter: Statementstmt 41.1% 28 / 68 Branchbran 9.0% 4 / 44 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 44.4% 8 / 18 Podpod 91.6% 11 / 12 totaltotal 25.8% 51 / 197 CC 37 SCAR 43.7
CC
37
Cov
22%
addFormula
210.0
add
110.0
evaluate
20.0
CRAP 79.1
↑ Summary Formula.pm »
1 # Copyrights 2023 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5  
6 package Math::Formula::Context;
7 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 $VERSION = '0.10';
9  
10  
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Log::Report 'math-formula';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 my $config;
18  
19  
20 1 ▶sub new(%) { my $class = shift; (bless {}, $class)->init({@_}) }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21  
22 sub _default($$$$)
23 1 ▶{ my ($self, $name, $type, $value, $default) = @_;
Subroutine _default: called CC=5 SCAR=26.1
24 1 ▶ my $form
Branch: 4 branches
BranchTrueFalseTotal
!$value ? : 1 0 1
!builtin::blessed($value) ? : 0 0 0
$value ? : 0 0 0
$value->isa('Math::Formula') ? : 0 0 0
25 = ! $value ? $type->new(undef, $default)
26 : ! blessed $value ? ($value ? Math::Formula->new($name, $value) : undef)
27 : $value->isa('Math::Formula') ? $value
28 : error __x"unexpected value for '{name}' in #{context}", name => $name, context => $self->name;
29 }
30  
31 sub init($)
32 1 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=5 SCAR=18.9
Pod undocumented
33 1 ▶ my $name = $self->{MFC_name} = $args->{name} or error __x"context without a name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->{"MFC_name"} = $args->{"name"} or &error(&__x("context without a name")) 0 1 1
34  
35 my $config = $self->{MFC_config} = MF::FRAGMENT->new(config => $self, attributes => {
36 name => MF::NAME->new($name),
37 mf_version => MF::STRING->new(undef, $Math::Formula::VERSION),
38 version => $args->{version} ? MF::STRING->new($args->{version}) : undef,
39 1 ▶ created => $self->_default(created => 'MF::DATETIME', $args->{created}, DateTime->now),
Branch: 1 branch
BranchTrueFalseTotal
$args->{'version'} ? : 0 1 1
40 });
41  
42 1 ▶ if(my $forms = $args->{formula})
Branch: 1 branch
BranchTrueFalseTotal
if (my $forms = $args->{"formula"}) 0 1 1
43 0 ▶ { $self->add(ref $forms eq 'ARRAY' ? @$forms : $forms);
Branch: 1 branch
BranchTrueFalseTotal
ref $forms eq 'ARRAY' ? : 0 0 0
44 }
45  
46 1 $self->{MFC_claims} = { };
47 1 $self;
48 }
49  
50 #--------------
51  
52 0 ▶sub name { $_[0]->{MFC_name} }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
53 0 ▶sub config { $_[0]->{MFC_config} }
Subroutine config: not called CC=1 SCAR=6.9
Pod documented
54  
55 #--------------
56  
57 sub add(@)
58 0 ▶{ my $self = shift;
Subroutine add: not called CC=10 SCAR=47.0
Pod documented
59 0 ▶ unless(ref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 0 0 0
60 0 { my $name = shift;
61 0 ▶ return $name =~ s/^#// ? $self->addFragment($name, @_) : $self->addFormula($name, @_);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/^#// ? : 0 0 0
62 }
63  
64 0 foreach my $obj (@_)
65 0 ▶ { if(ref $obj eq 'HASH')
Branch: 3 branches
BranchTrueFalseTotal
if (ref $obj eq 'HASH') { } 0 0 0
elsif (builtin::blessed($obj) and $obj->isa('Math::Formula')) { } 0 0 0
elsif (builtin::blessed($obj) and $obj->isa('Math::Formula::Context')) { } 0 0 0
66 0 { $self->add($_, $obj->{$_}) for keys %$obj;
67 }
68 ▶ elsif(blessed $obj && $obj->isa('Math::Formula'))
Condition0%0/3
builtin::blessed($obj) and $obj->isa("Math::Formula")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Math::Formula")
builtin::blessed($obj) $obj->isa("Math::Formula")
Truth table0%0/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Math::Formula")
ABresult
0X0
100
111
69 0 { $self->{MFC_forms}{$obj->name} = $obj;
70 }
71 ▶ elsif(blessed $obj && $obj->isa('Math::Formula::Context'))
Condition0%0/3
builtin::blessed($obj) and $obj->isa("Math::Formula::Context")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Math::Formula::Context")
builtin::blessed($obj) $obj->isa("Math::Formula::Context")
Truth table0%0/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Math::Formula::Context")
ABresult
0X0
100
111
72 0 { $self->{MFC_frags}{$obj->name} = $obj;
73 }
74 else
75 0 { panic __x"formula add '{what}' not understood", what => $obj;
76 }
77 }
78  
79 0 undef;
80 }
81  
82  
83 sub addFormula(@)
84 0 ▶{ my ($self, $first) = (shift, shift);
Subroutine addFormula: not called CC=14 SCAR=53.5
Pod documented
85  
86 my ($name, $form) =
87 !@_ && blessed $first && $first->isa('Math::Formula')
88 ? ($first->name, $first)
89 : @_==1 && !ref $first && blessed $_[0] && $_[0]->isa('Math::Formula')
90 ? ($first, $_[0])
91 : @_==1 && !ref $first && ref $_[0] eq 'ARRAY'
92 0 ▶ ? ($first, Math::Formula->new($first, @{$_[0]}))
Branch: 4 branches
BranchTrueFalseTotal
!@_ && builtin::blessed($first) && $first->isa('Math::Formula') ? : 0 0 0
@_ == 1 && !ref($first) && builtin::blessed($_[0]) && $_[0]->isa('Math::Formula') ? : 0 0 0
@_ == 1 && !ref($first) && ref $_[0] eq 'ARRAY' ? : 0 0 0
@_ && !ref($first) && !ref($_[0]) ? : 0 0 0
Condition0%0/27
not @_ and builtin::blessed($first) and $first->isa("Math::Formula")
!ll&&!rl&&r
000
not @_ and builtin::blessed($first)
!ll&&!rl&&r
000
@_ == 1 and not ref $first and builtin::blessed($_[0]) and $_[0]->isa("Math::Formula")
!ll&&!rl&&r
000
@_ == 1 and not ref $first and builtin::blessed($_[0])
!ll&&!rl&&r
000
@_ == 1 and not ref $first
!ll&&!rl&&r
000
@_ == 1 and not ref $first and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
@_ == 1 and not ref $first
!ll&&!rl&&r
000
@_ and not ref $first and not ref $_[0]
!ll&&!rl&&r
000
@_ and not ref $first
!ll&&!rl&&r
000
MC/DC0%0/3
not @_ and builtin::blessed($first) and $first->isa("Math::Formula")
not @_ builtin::blessed($first) $first->isa("Math::Formula")
MC/DC0%0/4
@_ == 1 and not ref $first and builtin::blessed($_[0]) and $_[0]->isa("Math::Formula")
@_ == 1 not ref $first builtin::blessed($_[0]) $_[0]->isa("Math::Formula")
MC/DC0%0/3
@_ == 1 and not ref $first and ref $_[0] eq "ARRAY"
@_ == 1 not ref $first ref $_[0] eq "ARRAY"
MC/DC0%0/3
@_ and not ref $first and not ref $_[0]
@_ not ref $first not ref $_[0]
Truth table0%0/4
not(A) and B and C
A= not @_B= builtin::blessed($first)C= $first->isa("Math::Formula")
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/5
A and not(B) and C and D
A= @_ == 1B= not ref $firstC= builtin::blessed($_[0])D= $_[0]->isa("Math::Formula")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/4
A and not(B) and C
A= @_ == 1B= not ref $firstC= ref $_[0] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and not(C)
A= @_B= not ref $firstC= not ref $_[0]
ABCresult
0XX0
10X0
1100
1111
93 : @_ && !ref $first && !ref $_[0]
94 ? ($first, Math::Formula->new($first, @_))
95 : panic __x"formula declaration '{name}' not understood", name => $first;
96  
97 0 $self->{MFC_forms}{$name} = $form;
98 }
99  
100  
101 0 ▶sub formula($) { $_[0]->{MFC_forms}{$_[1]} }
Subroutine formula: not called CC=1 SCAR=6.9
Pod documented
102  
103  
104 sub addFragment($;$)
105 0 ▶{ my $self = shift;
Subroutine addFragment: not called CC=2 SCAR=17.9
Pod documented
106 0 ▶ my ($name, $fragment) = @_==2 ? @_ : ($_[0]->name, $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
107 0 $self->{MFC_frags}{$name} = MF::FRAGMENT->new($name, $fragment);
108 }
109  
110  
111 0 ▶sub fragment($) { $_[0]->{MFC_frags}{$_[1]} }
Subroutine fragment: not called CC=1 SCAR=6.9
Pod documented
112  
113  
114 sub evaluate($$%)
115 0 ▶{ my ($self, $name) = (shift, shift);
Subroutine evaluate: not called CC=4 SCAR=30.0
Pod documented
116  
117 # Wow, I am impressed!
118 0 ▶ length $name or return $self->config;
Branch: 1 branch
BranchTrueFalseTotal
length $name or return $self->config 0 0 0
119  
120 # silently ignore missing tags
121 0 my $form = $self->formula($name);
122 0 ▶ unless($form)
Branch: 1 branch
BranchTrueFalseTotal
unless ($form) 0 0 0
123 0 { warning __x"no formula '{name}' in {context}", name => $name, context => $self->name;
124 0 return undef;
125 }
126  
127 0 my $claims = $self->{MFC_claims};
128 0 ▶ ! $claims->{$name}++
Branch: 1 branch
BranchTrueFalseTotal
$claims->{$name}++ and &error(&__x("recursion in expression '{name}' at {context}", "name", $name, "context", $self->name)) 0 0 0
129 or error __x"recursion in expression '{name}' at {context}", name => $name, context => $self->name;
130  
131 0 my $result = $form->evaluate($self, @_);
132  
133 0 delete $claims->{$name};
134 0 $result;
135 }
136  
137  
138 sub run($%)
139 0 ▶{ my ($self, $expr, %args) = @_;
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
140 0 ▶ my $name = delete $args{name} || join '#', (caller)[1,2];
Condition0%0/3
delete $args{'name'} || join('#', (caller)[1, 2])
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $args{'name'} || join('#', (caller)[1, 2])
delete $args{'name'} join('#', (caller)[1, 2])
Truth table0%0/3
A || B
A= delete $args{'name'}B= join('#', (caller)[1, 2])
ABresult
1X1
011
000
141 0 Math::Formula->new($name, $expr)->evaluate($self, %args);
142 }
143  
144  
145 sub value($@)
146 0 ▶{ my $self = shift;
Subroutine value: not called CC=2 SCAR=17.9
Pod documented
147 0 my $result = $self->run(@_);
148 0 ▶ $result ? $result->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 0 0 0
149 }
150  
151 #--------------
152  
153 2 1;
↑ Summary Formula.pm »