Formula/Context.pm

filter: Statementstmt 94.7% 91 / 96 Branchbran 71.2% 47 / 66 Conditioncond 55.2% 21 / 38 MC/DCmcdc 34.7% 8 / 23 Subroutinesub 100% 22 / 22 Podpod 93.3% 14 / 15 totaltotal 78.0% 203 / 260 CC 49 SCAR 23.6
CC
49
Cov
80%
addFormula
21.2
add
16.8
_default
8.6
CRAP 10.6
« Formula.pm ↑ Summary Formula/Type.pm »
1 # This code is part of Perl distribution Math-Formula version 0.18.
2 # The POD got stripped from this file by OODoc version 3.03.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2023-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11 #oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
12 #oodist: This file contains OODoc-style documentation which will get stripped
13 #oodist: during its release in the distribution. You can use this file for
14 #oodist: testing, however the code of this development version may be broken!
15  
16 package Math::Formula::Context;{
17 15 our $VERSION = '0.18';
18 }
19  
20  
21 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 15 ▶use Log::Report 'math-formula';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 15 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #--------------------
28  
29 16 ▶sub new(%) { my $class = shift; (bless {}, $class)->init({@_}) }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
30  
31 sub _default($$$$)
32 64 ▶{ my ($self, $name, $type, $value, $default) = @_;
Subroutine _default: called CC=6 SCAR=21.5
33 64 ▶ my $form
Branch: 5 branches
BranchTrueFalseTotal
!$value ? : 60 4 64
!builtin::blessed($value) ? : 0 4 4
$value ? : 0 0 0
$value->isa('Math::Formula') ? : 0 4 4
$value->isa('Math::Formula::Type') ? : 4 0 4
34 = ! $value ? $type->new(undef, $default)
35 : ! blessed $value ? ($value ? Math::Formula->new($name, $value) : undef)
36 : $value->isa('Math::Formula') ? $value
37 : $value->isa('Math::Formula::Type') ? $value
38 : error __x"unexpected value for '{name}' in #{context}", name => $name, context => $self->name;
39 }
40  
41 sub init($)
42 16 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=7 SCAR=19.6
Pod undocumented
43 16 ▶ my $name = $args->{name} or error __x"context requires a name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = $args->{"name"} or &error(&__x("context requires a name")) 16 0 16
44 16 ▶ my $node = blessed $name ? $name : MF::STRING->new(undef, $name);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($name) ? : 1 15 16
45 16 $self->{MFC_name} = $node->value;
46  
47 16 my $now;
48 $self->{MFC_attrs} = {
49 ctx_name => $node,
50 ctx_version => $self->_default(version => 'MF::STRING', $args->{version}, "1.00"),
51 ctx_created => $self->_default(created => 'MF::DATETIME', $args->{created}, $now = DateTime->now),
52 ctx_updated => $self->_default(updated => 'MF::DATETIME', $args->{updated}, $now //= DateTime->now),
53 16 ▶ ctx_mf_version => $self->_default(mf_version => 'MF::STRING', $args->{mf_version}, $Math::Formula::VERSION),
Condition33%1/3
$now //= "DateTime"->now
l!l&&r!l&&!r
1600
MC/DC0%0/2
$now //= "DateTime"->now
$now "DateTime"->now
Truth table33%1/3
A //= B
A= $nowB= "DateTime"->now
ABresult
1X1
011
000
54 };
55  
56 16 ▶ $self->{MFC_lead} = $args->{lead_expressions} // '';
Condition100%2/2
$args->{"lead_expressions"} // ""
l!l
115
MC/DC100%1/1
$args->{"lead_expressions"} // ""
$args->{"lead_expressions"}
Truth table100%2/2
A
A= $args->{"lead_expressions"}
Aresult
11
00
57 16 $self->{MFC_forms} = { };
58 16 $self->{MFC_frags} = { };
59 16 ▶ if(my $forms = $args->{formulas})
Branch: 1 branch
BranchTrueFalseTotal
if (my $forms = $args->{"formulas"}) 3 13 16
60 3 ▶ { $self->add(ref $forms eq 'ARRAY' ? @$forms : $forms);
Branch: 1 branch
BranchTrueFalseTotal
ref $forms eq 'ARRAY' ? : 1 2 3
61 }
62  
63 16 $self->{MFC_claims} = { };
64 16 $self->{MFC_capts} = [ ];
65 16 $self;
66 }
67  
68 # For save()
69 sub _index()
70 2 ▶{ my $self = shift;
Subroutine _index: called CC=1 SCAR=0.0
71 +{ attributes => $self->{MFC_attrs},
72 formulas => $self->{MFC_forms},
73 fragments => $self->{MFC_frags},
74 2 };
75 }
76  
77 #--------------------
78  
79 13 ▶sub name { $_[0]->{MFC_name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
80 20 ▶sub lead_expressions { $_[0]->{MFC_lead} }
Subroutine lead_expressions: called CC=1 SCAR=0.0
Pod documented
81  
82 #--------------------
83  
84 sub attribute($)
85 4 ▶{ my ($self, $name) = @_;
Subroutine attribute: called CC=2 SCAR=7.1
Pod documented
86 4 ▶ my $def = $self->{MFC_attrs}{$name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $def = $self->{"MFC_attrs"}{$name} or return 4 0 4
87 4 Math::Formula->new($name => $def);
88 }
89  
90 #--------------------
91 #XXX example with fragment
92  
93 sub add(@)
94 41 ▶{ my $self = shift;
Subroutine add: called CC=10 SCAR=28.2
Pod documented
95 41 ▶ unless(ref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 34 7 41
96 34 { my $name = shift;
97 34 ▶ return $name =~ s/^#// ? $self->addFragment($name, @_) : $self->addFormula($name, @_);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/^#// ? : 0 34 34
98 }
99  
100 7 foreach my $obj (@_)
101 9 ▶ { if(ref $obj eq 'HASH')
Branch: 3 branches
BranchTrueFalseTotal
if (ref $obj eq 'HASH') { } 7 2 9
elsif (builtin::blessed($obj) and $obj->isa('Math::Formula')) { } 2 0 2
elsif (builtin::blessed($obj) and $obj->isa('Math::Formula::Context')) { } 0 0 0
102 7 { $self->add($_, $obj->{$_}) for keys %$obj;
103 }
104 ▶ elsif(blessed $obj && $obj->isa('Math::Formula'))
Condition33%1/3
builtin::blessed($obj) and $obj->isa("Math::Formula")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($obj) and $obj->isa("Math::Formula")
builtin::blessed($obj) $obj->isa("Math::Formula")
Truth table33%1/3
A and B
A= builtin::blessed($obj)B= $obj->isa("Math::Formula")
ABresult
0X0
100
111
105 2 { $self->{MFC_forms}{$obj->name} = $obj;
106 }
107 ▶ 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
108 0 { $self->{MFC_frags}{$obj->name} = $obj;
109 }
110 else
111 0 { panic __x"formula add '{what}' not understood", what => $obj;
112 }
113 }
114  
115 7 undef;
116 }
117  
118  
119 sub addFormula(@)
120 45 ▶{ my ($self, $name) = (shift, shift);
Subroutine addFormula: called CC=19 SCAR=30.6
Pod documented
121 45 my $next = $_[0];
122 45 my $forms = $self->{MFC_forms};
123  
124 45 ▶ if(ref $name)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $name) { } 1 44 45
elsif (not ref $name and @_) { } 44 0 44
125 1 ▶ { return $forms->{$name->name} = $name
Branch: 1 branch
BranchTrueFalseTotal
if not @_ and builtin::blessed($name) and $name->isa("Math::Formula") 1 0 1
Condition33%2/6
not @_ and builtin::blessed($name) and $name->isa("Math::Formula")
!ll&&!rl&&r
001
not @_ and builtin::blessed($name)
!ll&&!rl&&r
001
MC/DC0%0/3
not @_ and builtin::blessed($name) and $name->isa("Math::Formula")
not @_ builtin::blessed($name) $name->isa("Math::Formula")
Truth table25%1/4
not(A) and B and C
A= not @_B= builtin::blessed($name)C= $name->isa("Math::Formula")
ABCresult
0XX0
10X0
1100
1111
126 if !@_ && blessed $name && $name->isa('Math::Formula');
127 }
128 ▶ elsif(! ref $name && @_)
Condition33%1/3
not ref $name and @_
!ll&&!rl&&r
0044
MC/DC0%0/2
not ref $name and @_
not ref $name @_
Truth table33%1/3
not(A) and B
A= not ref $nameB= @_
ABresult
0X0
100
111
129 44 ▶ { return $forms->{$name} = $next
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and builtin::blessed($next) and $next->isa("Math::Formula") 5 39 44
Condition100%6/6
@_ == 1 and builtin::blessed($next) and $next->isa("Math::Formula")
!ll&&!rl&&r
26135
@_ == 1 and builtin::blessed($next)
!ll&&!rl&&r
12518
MC/DC100%3/3
@_ == 1 and builtin::blessed($next) and $next->isa("Math::Formula")
@_ == 1 builtin::blessed($next) $next->isa("Math::Formula")
Truth table100%4/4
A and B and C
A= @_ == 1B= builtin::blessed($next)C= $next->isa("Math::Formula")
ABCresult
0XX0
10X0
1100
1111
130 if @_==1 && blessed $next && $next->isa('Math::Formula');
131  
132 39 ▶ return $forms->{$name} = Math::Formula->new($name, @_)
Branch: 1 branch
BranchTrueFalseTotal
if ref $next eq "CODE" 1 38 39
133 if ref $next eq 'CODE';
134  
135 38 ▶ return $forms->{$name} = Math::Formula->new($name, @_)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($next) and $next->isa("Math::Formula::Type") 13 25 38
Condition66%2/3
builtin::blessed($next) and $next->isa("Math::Formula::Type")
!ll&&!rl&&r
25013
MC/DC50%1/2
builtin::blessed($next) and $next->isa("Math::Formula::Type")
builtin::blessed($next) $next->isa("Math::Formula::Type")
Truth table66%2/3
A and B
A= builtin::blessed($next)B= $next->isa("Math::Formula::Type")
ABresult
0X0
100
111
136 if blessed $next && $next->isa('Math::Formula::Type');
137  
138 25 ▶ my ($data, %attrs) = @_==1 && ref $next eq 'ARRAY' ? @$next : $next;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $next eq 'ARRAY' ? : 5 20 25
Condition100%3/3
@_ == 1 and ref $next eq "ARRAY"
!ll&&!rl&&r
1195
MC/DC100%2/2
@_ == 1 and ref $next eq "ARRAY"
@_ == 1 ref $next eq "ARRAY"
Truth table100%3/3
A and B
A= @_ == 1B= ref $next eq "ARRAY"
ABresult
0X0
100
111
139 25 ▶ if(my $r = $attrs{returns})
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $attrs{"returns"}) 5 20 25
140 5 ▶ { my $typed = $r->isa('MF::STRING') ? $r->new(undef, $data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
$r->isa('MF::STRING') ? : 0 5 5
141 5 return $forms->{$name} = Math::Formula->new($name, $typed, %attrs);
142 }
143  
144 20 ▶ if(length(my $leader = $self->lead_expressions))
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $leader = $self->lead_expressions)) 3 17 20
145 3 ▶ { my $typed = $data =~ s/^\Q$leader// ? $data : \$data;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ s/^\Q$leader\E// ? : 2 1 3
146 3 return $forms->{$name} = Math::Formula->new($name, $typed, %attrs);
147 }
148  
149 17 return $forms->{$name} = Math::Formula->new($name, $data, %attrs);
150 }
151  
152 0 error __x"formula declaration '{name}' not understood", name => $name;
153 }
154  
155  
156 55 ▶sub formula($) { $_[0]->{MFC_forms}{$_[1]} }
Subroutine formula: called CC=1 SCAR=0.0
Pod documented
157  
158  
159 sub addFragment($;$)
160 1 ▶{ my $self = shift;
Subroutine addFragment: called CC=2 SCAR=7.1
Pod documented
161 1 ▶ my ($name, $fragment) = @_==2 ? @_ : ($_[0]->name, $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 1 1
162 1 $self->{MFC_frags}{$name} = MF::FRAGMENT->new($name, $fragment);
163 }
164  
165  
166 7 ▶sub fragment($) { $_[0]->{MFC_frags}{$_[1]} }
Subroutine fragment: called CC=1 SCAR=0.0
Pod documented
167  
168 #--------------------
169  
170 sub evaluate($$%)
171 32 ▶{ my ($self, $name) = (shift, shift);
Subroutine evaluate: called CC=5 SCAR=17.0
Pod documented
172  
173 # Wow, I am impressed! Caused by prefix(#,.) -> infix
174 32 ▶ length $name or return $self;
Branch: 1 branch
BranchTrueFalseTotal
length $name or return $self 32 0 32
175  
176 32 ▶ my $form = $name =~ /^ctx_/ ? $self->attribute($name) : $self->formula($name);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^ctx_/ ? : 3 29 32
177 32 ▶ unless($form)
Branch: 1 branch
BranchTrueFalseTotal
unless ($form) 0 32 32
178 0 { warning __x"no formula '{name}' in {context}", name => $name, context => $self->name;
179 0 return undef;
180 }
181  
182 32 my $claims = $self->{MFC_claims};
183 32 ▶ ! $claims->{$name}++
Branch: 1 branch
BranchTrueFalseTotal
$claims->{$name}++ and &error(&__x("recursion in expression '{name}' at {context}", "name", $name, "context", $self->name)) 0 32 32
184 or error __x"recursion in expression '{name}' at {context}", name => $name, context => $self->name;
185  
186 32 my $result = $form->evaluate($self, @_);
187  
188 32 delete $claims->{$name};
189 32 $result;
190 }
191  
192  
193 sub run($%)
194 21 ▶{ my ($self, $expr, %args) = @_;
Subroutine run: called CC=4 SCAR=14.2
Pod documented
195 21 ▶ my $name = delete $args{name} || join '#', (caller)[1,2];
Condition33%1/3
delete $args{'name'} || join('#', (caller)[1, 2])
l!l&&r!l&&!r
0210
MC/DC0%0/2
delete $args{'name'} || join('#', (caller)[1, 2])
delete $args{'name'} join('#', (caller)[1, 2])
Truth table33%1/3
A || B
A= delete $args{'name'}B= join('#', (caller)[1, 2])
ABresult
1X1
011
000
196 21 my $result = Math::Formula->new($name, $expr)->evaluate($self, %args);
197  
198 21 ▶ while($result && $result->isa('MF::NAME'))
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $result->isa("MF::NAME")) 6 21 27
Condition66%2/3
$result and $result->isa("MF::NAME")
!ll&&!rl&&r
0216
MC/DC50%1/2
$result and $result->isa("MF::NAME")
$result $result->isa("MF::NAME")
Truth table66%2/3
A and B
A= $resultB= $result->isa("MF::NAME")
ABresult
0X0
100
111
199 6 { $result = $self->evaluate($result->token, %args);
200 }
201  
202 21 $result;
203 }
204  
205  
206 sub value($@)
207 12 ▶{ my $self = shift;
Subroutine value: called CC=2 SCAR=7.1
Pod documented
208 12 my $result = $self->run(@_);
209 12 ▶ $result ? $result->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 12 0 12
210 }
211  
212  
213 10 ▶sub setCaptures($) { $_[0]{MFC_capts} = $_[1] }
Subroutine setCaptures: called CC=1 SCAR=0.0
Pod documented
214 7 ▶sub _captures() { $_[0]{MFC_capts} }
Subroutine _captures: called CC=1 SCAR=0.0
215  
216  
217 4 ▶sub capture($) { $_[0]->_captures->[$_[1]] }
Subroutine capture: called CC=1 SCAR=0.0
Pod documented
218  
219 #--------------------
220  
221 15 1;
« Formula.pm ↑ Summary Formula/Type.pm »