Expr/Opp.pm

filter: Statementstmt 88.8% 272 / 306 Branchbran 76.7% 129 / 168 Conditioncond 75.7% 25 / 33 MC/DCmcdc 54.5% 12 / 22 Subroutinesub 88.0% 22 / 25 Podpod 50.0% 7 / 14 totaltotal 82.2% 467 / 568 CC 98 SCAR 24.2
CC
98
Cov
84%
SubOpp
30.0
_SubMatch
22.8
SubExpr
12.3
CRAP 11.3
« Expr/FormulaDB.pm ↑ Summary Expr/TypeDB.pm »
1 #!/usr/bin/perl
2  
3 # Opp.pm - A perl representation mathematicall expressions.
4 # (c) Copyright 1998 Hakan Ardo <hakan@debian.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  
20 =head1 NAME
21  
22 Math::Expr::Opp - Represents one operation in the parsed expression
23 tree
24  
25 =head1 SYNOPSIS
26  
27 require Math::Expr::Opp;
28 require Math::Expr::Var;
29 require Math::Expr::Num;
30
31 # To represent the expression "x+7":
32 $n=new Math::Expr::Opp("+");
33 $n->SetOpp(0,new Math::Expr::Var("x"));
34 $n->SetOpp(1,new Math::Expr::Num(7));
35 print $n->tostr . "\n";
36  
37 =head1 DESCRIPTION
38  
39 Used by the Math::Expr to represent algebraic expressions. This class
40 represents one operation or function with a set of operands, which
41 in turn can be other Math::Expr::Opp objects. And in that way we are
42 able to represent entire expression.
43  
44 Operations like a+b and functions like sin(a) or f(a,b) are all
45 represented by this kind of objects with "+", "sin" and "f" as the
46 operation- or function names and Math::Expr::Var(a) and
47 Math::Expr::Var(b) as operands (only a in the sin example).
48  
49 =head1 METHODS
50  
51 =cut
52  
53 package Math::Expr::Opp;
54 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 1 ▶use Math::Expr qw ($Pri $OppDB);
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 require Math::Expr::MatchSet;
58 1 require Math::Expr::Node;
59 1 require Math::Expr::VarSet;
60 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 1 ▶use Math::Expr::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 @ISA = qw(Math::Expr::Node);
64  
65 =head2 $e=new Math::Expr::Opp($name,$db)
66  
67 Creates a new operation object with the operation- or function-name
68 $name. Using the operations defined in $db. See
69 L<Math::Expr::OpperationDB> for more info.
70  
71 =cut
72  
73 469 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod undocumented
74 469 my($class, $val) = @_;
75 469 my $self = bless { }, $class;
76  
77 469 ▶ if (!ref $OppDB || !ref $Pri) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $Math::Expr::Opp::OppDB and ref $Math::Expr::Opp::Pri) 0 469 469
Condition33%1/3
ref $Math::Expr::Opp::OppDB and ref $Math::Expr::Opp::Pri
!ll&&!rl&&r
00469
MC/DC0%0/2
ref $Math::Expr::Opp::OppDB and ref $Math::Expr::Opp::Pri
ref $Math::Expr::Opp::OppDB ref $Math::Expr::Opp::Pri
Truth table33%1/3
A and B
A= ref $Math::Expr::Opp::OppDBB= ref $Math::Expr::Opp::Pri
ABresult
0X0
100
111
78 0 warn "OppDB not initiated, please set it using SetOppDB(...)";
79 }
80  
81 469 $self->{'Val'}=$val;
82 469 $self->Breakable(0);
83  
84 469 $self;
85  
86 }
87  
88 =head2 $e->SetOpp($i, $v)
89  
90 Sets operand number $i to $v.
91  
92 =cut
93  
94 222 ▶sub SetOpp {
Subroutine SetOpp: called CC=4 SCAR=14.5
Pod documented
95 222 my ($self, $i, $val) = @_;
96  
97 # Sanity checks
98 222 ▶ defined $i || warn "Bad param i.";
Branch: 1 branch
BranchTrueFalseTotal
defined $i or warn "Bad param i." 222 0 222
99 222 ▶ $val->isa("Math::Expr::Node") || warn "Bad param val: $val";
Branch: 1 branch
BranchTrueFalseTotal
$val->isa("Math::Expr::Node") or warn "Bad param val: $val" 222 0 222
100 222 ▶ !$self->InTable || warn "Can't edit items in the table";
Branch: 1 branch
BranchTrueFalseTotal
$self->InTable and warn "Can't edit items in the table" 0 222 222
101  
102 222 delete $self->{'Op'};
103  
104 222 $self->{'Opps'}[$i]=$val;
105 }
106  
107 =head2 $e->Opp($i)
108  
109 Returns operand to number $i.
110  
111 =cut
112  
113 88 ▶sub Opp {
Subroutine Opp: called CC=2 SCAR=7.1
Pod documented
114 88 my ($self, $i) = @_;
115  
116 # Sanity checks
117 88 ▶ defined $i || warn "Bad param i.";
Branch: 1 branch
BranchTrueFalseTotal
defined $i or warn "Bad param i." 88 0 88
118  
119 88 $self->{'Opps'}[$i];
120 }
121  
122 =head2 $e->tostr
123  
124 Returns a string representation of the entire expression to be
125 used for debugging.
126  
127 =cut
128  
129 1733 ▶sub tostr {
Subroutine tostr: called CC=4 SCAR=13.9
Pod documented
130 1733 my $self = shift;
131 1733 my $str=$self->{'Val'}."(";
132 1733 my $i;
133  
134 1733 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 2710 1733 4443
135 2710 ▶ if (ref $self->{'Opps'}[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'Opps'}[$i]) { } 2710 0 2710
136 2710 $str .= $self->{'Opps'}[$i]->tostr;
137 } else {
138 0 $str .= "?";
139 }
140 2710 ▶ if ($i+1<=$#{$self->{'Opps'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 <= $#{$$self{"Opps"};}) 977 1733 2710
141 977 $str .= ",";
142 }
143 }
144 1733 "$str)";
145 }
146  
147 =head2 $e->strtype
148  
149 Returns a string representation of this expressions entire type,
150 without simplifying it. In the same notation as the tostr method.
151  
152 =cut
153  
154 0 ▶sub strtype {
Subroutine strtype: not called CC=3 SCAR=24.8
Pod documented
155 0 my $self = shift;
156 0 my $str=$self->{'Val'}."(";
157 0 my $i;
158  
159 0 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 0 0 0
160 0 $str .= $self->{'Opps'}[$i]->strtype;
161 0 ▶ if ($i+1<=$#{$self->{'Opps'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 <= $#{$$self{"Opps"};}) 0 0 0
162 0 $str .= ",";
163 }
164 }
165 0 "$str)";
166 }
167  
168 =head2 $n->Simplify
169  
170 Simplifys the expression to some normal from.
171  
172 =cut
173  
174 1372 ▶sub op {
Subroutine op: called CC=3 SCAR=11.0
Pod undocumented
175 1372 my ($self, $force)=@_;
176 1372 ▶ if ($force || !$self->{'Op'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force or not $self->{"Op"}) 585 787 1372
Condition100%3/3
$force or not $self->{"Op"}
l!l&&r!l&&!r
212373787
MC/DC100%2/2
$force or not $self->{"Op"}
$force not $self->{"Op"}
Truth table100%3/3
A or not(B)
A= $forceB= not $self->{"Op"}
ABresult
1X1
011
000
177 585 $self->{'Op'}=$OppDB->Find($self->DBType);
178 }
179 1372 return $self->{'Op'};
180 }
181  
182 207 ▶sub Simplify {
Subroutine Simplify: called CC=10 SCAR=23.0
Pod documented
183 207 my ($self)=@_;
184 207 my $i;
185 my $op;
186  
187 207 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 343 207 550
188 343 $self->{'Opps'}[$i]=$self->{'Opps'}[$i]->Simplify;
189 }
190  
191 207 $op=$self->op(1);
192  
193 # Type specific simplification rules
194 207 ▶ if ($op->{'simp'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{"simp"}) 5 202 207
195 5 my $vs=new Math::Expr::VarSet;
196  
197 5 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 10 5 15
198 10 $vs->Set(chr(97+$i), $self->{'Opps'}[$i]);
199 }
200 # print $vs->tostr . "\n";
201  
202 5 my $e=$op->{'simp'}->Copy;
203 5 $e=$e->Subs($vs);
204  
205 # print $e->tostr . "\n";
206  
207 5 foreach (keys %{$e}) {
208 15 $self->{$_}=$e->{$_};
209 }
210 5 $op=$self->op(1);
211 }
212  
213 # (a+b)+c => a+b+c
214 207 ▶ if ($op->{'ass'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{"ass"}) 107 100 207
215 107 my @nopp;
216 107 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 215 107 322
217 215 ▶ if ($self->{'Val'} eq $self->{'Opps'}[$i]{'Val'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Val'} eq $self->{'Opps'}[$i]{'Val'}) { } 14 201 215
218 14 foreach (@{$self->{'Opps'}[$i]{'Opps'}}) {
219 28 push(@nopp, $_);
220 }
221 } else {
222 201 push (@nopp, $self->{'Opps'}[$i]);
223 }
224 }
225 107 $self->{'Opps'}=\@nopp;
226 }
227  
228 # a+c+b => a+b+c
229 207 ▶ if ($op->{'com'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{"com"}) 107 100 207
230 107 my @nopp = sort {$a->tostr cmp $b->tostr} @{$self->{'Opps'}};
231 107 $self->{'Opps'}=\@nopp;
232 }
233 207 delete $self->{'Op'};
234 207 return $self->IntoTable;
235 }
236  
237  
238 =head2 $n->BaseType
239  
240 Returns a string type of this expression simplifyed as much as
241 possible.
242  
243 =cut
244  
245 792 ▶sub BaseType {
Subroutine BaseType: called CC=2 SCAR=6.9
Pod documented
246 792 my ($self)=@_;
247 792 my $op;
248 792 my $str=$self->DBType;
249  
250 792 $op= $self->op;
251 792 ▶ if ($op) {$str=$op->{'out'}}
Branch: 1 branch
BranchTrueFalseTotal
if ($op) 678 114 792
252  
253 792 $str;
254 }
255  
256 1377 ▶sub DBType {
Subroutine DBType: called CC=3 SCAR=11.0
Pod undocumented
257 1377 my ($self)=@_;
258 1377 my $str=$self->{'Val'}."(";
259 1377 my $i;
260  
261 1377 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 2143 1377 3520
262 2143 $str .= $self->{'Opps'}[$i]->BaseType;
263 2143 ▶ if ($i+1<=$#{$self->{'Opps'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 <= $#{$$self{"Opps"};}) 766 1377 2143
264 766 $str .= ",";
265 }
266 }
267 1377 "$str)";
268 }
269  
270 12 ▶sub power {
Subroutine power: called CC=2 SCAR=6.9
Pod undocumented
271 12 my ($a, $b) = @_;
272 12 my $i;
273 12 my $sum=1;
274
275 12 ▶ for ($i=0; $i<$b; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $b) 26 12 38
276 26 $sum=$sum*$a
277 }
278 12 $sum;
279 }
280  
281 =head2 $n->SubMatch($rules,$match)
282  
283 Tries to match $rules to this expretions and adds the substitutions
284 needed to $match.Returns 1 if the match excists and the substitutions
285 needed can coexcist with those already in $match otherwise 0.
286  
287 =cut
288  
289 75 ▶sub _SubMatch {
Subroutine _SubMatch: called CC=22 SCAR=31.3
290 75 my ($self, $rule, $mset) = @_;
291 75 my $op=$self->op;
292  
293 75 ▶ $self->InTable || warn "self not in table!";
Branch: 1 branch
BranchTrueFalseTotal
$self->InTable or warn "self not in table!" 75 0 75
294 75 ▶ $rule->InTable || warn "rule not in table!";
Branch: 1 branch
BranchTrueFalseTotal
$rule->InTable or warn "rule not in table!" 75 0 75
295  
296 75 ▶ if ($rule->isa('Math::Expr::Var') &&
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->isa('Math::Expr::Var') and $rule->BaseType eq $self->BaseType) { } 27 48 75
elsif ($rule->isa('Math::Expr::Opp') and $rule->{'Val'} eq $self->{'Val'}) { } 24 24 48
Condition66%2/3
$rule->isa("Math::Expr::Var") and $rule->BaseType eq $self->BaseType
!ll&&!rl&&r
48027
MC/DC50%1/2
$rule->isa("Math::Expr::Var") and $rule->BaseType eq $self->BaseType
$rule->isa("Math::Expr::Var") $rule->BaseType eq $self->BaseType
Truth table66%2/3
A and B
A= $rule->isa("Math::Expr::Var")B= $rule->BaseType eq $self->BaseType
ABresult
0X0
100
111
297 $rule->BaseType eq $self->BaseType
298 ) {
299 27 return $mset->SetAll($rule->{'Val'},$self);
300 }
301 ▶ elsif ($rule->isa('Math::Expr::Opp') &&
Condition100%3/3
$rule->isa("Math::Expr::Opp") and $rule->{"Val"} eq $self->{"Val"}
!ll&&!rl&&r
22224
MC/DC100%2/2
$rule->isa("Math::Expr::Opp") and $rule->{"Val"} eq $self->{"Val"}
$rule->isa("Math::Expr::Opp") $rule->{"Val"} eq $self->{"Val"}
Truth table100%3/3
A and B
A= $rule->isa("Math::Expr::Opp")B= $rule->{"Val"} eq $self->{"Val"}
ABresult
0X0
100
111
302 $rule->{'Val'} eq $self->{'Val'}) {
303 24 ▶ if ($op->{'ass'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->{'ass'}) { } 12 12 24
elsif ($#{$$self{'Opps'};} eq $#{$$rule{'Opps'};}) { } 12 0 12
304 12 ▶ if ($op->{'com'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{'com'}) { } 12 0 12
305 12 my @part;
306 my @pcnt;
307 12 my ($i,$j,$cnt);
308 12 my $p=$#{$rule->{'Opps'}} + 1;
309 12 my $s=$#{$self->{'Opps'}} + 1;
310 12 my $ps=power($p,$s) - 1;
311 12 my $resset = new Math::Expr::MatchSet;
312 12 my $m;
313 my $t;
314 12 my $a;
315 12 my $ok;
316  
317 12 ▶ for ($i=1; $i<$ps; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ps) 32 12 44
318 32 ▶ for ($j=0; $j<$p; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $p) 64 32 96
319 64 $part[$j]=new Math::Expr::Opp($self->{'Val'});
320 64 $pcnt[$j]=0;
321 }
322 32 $cnt=0;
323  
324 32 $t=$i;
325 32 ▶ for ($j=0; $j<$s; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $s) 76 32 108
326 76 $a= $t % $p;
327 76 $part[$a]->{'Opps'}[$pcnt[$a]]=$self->{'Opps'}[$cnt];
328 76 $pcnt[$a]++;
329 76 $cnt++;
330 76 $t=int($t/$p);
331 }
332  
333 32 $a=1;
334 32 ▶ for ($j=0; $j<$p; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $p) 64 32 96
335 # print $part[$j]->tostr . "\t";
336 0 ▶ if (!defined $part[$j]->{'Opps'}[0]) {$a=0; last;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $part[$j]{"Opps"}[0]) 0 64 64
337 64 ▶ if (!defined $part[$j]->{'Opps'}[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $part[$j]{"Opps"}[1]) 52 12 64
338 52 $part[$j]=$part[$j]->{'Opps'}[0];
339 }
340 64 $part[$j]=$part[$j]->IntoTable;
341 }
342 # print "\n";
343  
344 32 ▶ if ($a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a) 32 0 32
345 32 $m=$mset->Copy;
346 32 $m->AddPos("($i)");
347 # print "m:\n" . $m->tostr . "\n";
348 32 $ok=1;
349 32 ▶ for ($j=0; $j<$p; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $p) 64 32 96
350 64 my $t=$part[$j]->SubMatch($rule->{'Opps'}[$j],$m);
351 64 ▶ if (!$t) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 14 50 64
352 14 $ok=0;
353 }
354 }
355 32 ▶ if ($ok) {$resset->Insert($m);}
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 20 12 32
356 }
357 }
358  
359 # print "res:\n" . $resset->tostr . "\n";
360
361 12 $mset->Clear;
362 12 $mset->Insert($resset);
363 12 return 1;
364 } else {
365 #FIXME: Handle ass only objs...
366 }
367 }
368 12 elsif ($#{$self->{'Opps'}} eq $#{$rule->{'Opps'}}) {
369 12 my $ok=1;
370 12 my $i;
371
372 12 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 12 12 24
373 12 ▶ if (!$self->{'Opps'}[$i]->SubMatch($rule->{'Opps'}[$i],$mset)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Opps"}[$i]->SubMatch($rule->{"Opps"}[$i], $mset)) 0 12 12
374 0 $ok=0;
375 0 last;
376 }
377 }
378 12 return $ok;
379 } else {
380 0 return 0;
381 }
382 } else {
383 24 return 0;
384 }
385 }
386  
387 =head2 $n->Match($rules)
388  
389 Tries to match $rules to this expretions and to all its subexpretions.
390 Returns a MatchSet object specifying where the matches ocored and what
391 substitutions they represent.
392  
393 =cut
394  
395 37 ▶sub _Match {
Subroutine _Match: called CC=8 SCAR=20.9
396 37 my ($self, $rule, $pos, $pre) = @_;
397 37 my $i;
398 37 my $mset = new Math::Expr::MatchSet;
399 37 my $op=$self->op;
400  
401 37 ▶ $self->InTable || warn "self not in table!";
Branch: 1 branch
BranchTrueFalseTotal
$self->InTable or warn "self not in table!" 37 0 37
402 37 ▶ $rule->InTable || warn "rule not in table!";
Branch: 1 branch
BranchTrueFalseTotal
$rule->InTable or warn "rule not in table!" 37 0 37
403  
404 0 ▶ if (!defined $pos) {$pos="";}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pos) 0 37 37
405 37 ▶ if (!defined $pre) {$pre=new Math::Expr::VarSet}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pre) 8 29 37
406  
407 37 $mset->Set($pos, $pre->Copy);
408 37 ▶ if (!$self->SubMatch($rule, $mset)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->SubMatch($rule, $mset)) 18 19 37
409 18 $mset->del($pos);
410 }
411  
412 37 ▶ if ($pos ne "") {$pos .=","}
Branch: 1 branch
BranchTrueFalseTotal
if ($pos ne "") 27 10 37
413  
414 37 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 64 37 101
415 64 my $m=$self->SubExpr($i)->IntoTable->Match($rule, "$pos$i", $pre->Copy);
416 64 $mset->Insert($m);
417 }
418  
419 37 $mset;
420 }
421  
422 0 ▶sub SubOpp {
Subroutine SubOpp: not called CC=5 SCAR=34.0
Pod undocumented
423 0 my ($self, $a,$b) = @_;
424 0 my $i;
425 0 my $o= new Math::Expr::Opp($self->{'Val'});
426  
427 # Sanity checks
428 0 ▶ defined $a|| warn("Bad param a.");
Branch: 1 branch
BranchTrueFalseTotal
defined $a or warn "Bad param a." 0 0 0
429 0 ▶ defined $b|| warn("Bad param b.");
Branch: 1 branch
BranchTrueFalseTotal
defined $b or warn "Bad param b." 0 0 0
430  
431 0 ▶ if ($a==$b) {return $self->{'Opps'}[$a]}
Branch: 1 branch
BranchTrueFalseTotal
if ($a == $b) 0 0 0
432  
433 0 ▶ for ($i=$a; $i<=$b; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $b) 0 0 0
434 0 $o->SetOpp($i-$a,$self->{'Opps'}[$i]);
435 }
436 0 return $o->IntoTable;
437 }
438  
439 =head2 $n->Subs($vars)
440  
441 Substitues all variables in the expretion with there vaules in $vars.
442  
443 =cut
444  
445 82 ▶sub _Subs {
Subroutine _Subs: called CC=2 SCAR=6.9
446 82 my ($self, $vars) = @_;
447 82 my $i;
448 82 my $n = new Math::Expr::Opp($self->{'Val'});
449  
450 82 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 138 82 220
451 138 $n->{'Opps'}[$i]=$self->{'Opps'}[$i]->Subs($vars);
452 }
453 82 $n;
454 }
455  
456 =head2 $n->Copy
457  
458 Returns a copy of this object.
459  
460 =cut
461  
462 103 ▶sub _Copy {
Subroutine _Copy: called CC=2 SCAR=6.9
463 103 my $self = shift;
464 103 my $n = new Math::Expr::Opp($self->{'Val'});
465 103 my $i;
466  
467 103 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 177 103 280
468 177 $n->{'Opps'}[$i]=$self->{'Opps'}[$i]->Copy;
469 }
470 103 $n;
471 }
472  
473 =head2 $n->Breakable
474  
475 Used by the parser to indikate if this object was created using
476 parantesis or if he should break it up to preserve the rules of order
477 between the diffrent opperations.
478  
479 =cut
480  
481 639 ▶sub _Breakable {
Subroutine _Breakable: called CC=2 SCAR=6.9
482 639 my $self=shift;
483 639 my $val=shift;
484  
485 639 ▶ if (defined $val) {$self->{'Breakable'}=$val}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 588 51 639
486 639 $self->{'Breakable'}
487 }
488  
489 =head2 $n->Find($pos)
490  
491 Returns an object pointer to the subexpression represented by the
492 string $pos.
493  
494 =cut
495  
496 0 ▶sub Find {
Subroutine Find: not called CC=3 SCAR=24.8
Pod documented
497 0 my ($self, $pos) = @_;
498  
499 # Sanity checks
500 0 ▶ defined $pos || warn "Bad param pos.";
Branch: 1 branch
BranchTrueFalseTotal
defined $pos or warn "Bad param pos." 0 0 0
501  
502 0 ▶ if ($pos =~ s/^(\d+),?//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos =~ s/^(\d+),?//) { } 0 0 0
503 0 return $self->SubExpr($1)->Find($pos);
504 } else {
505 0 return $self;
506 }
507 }
508  
509 93 ▶sub SubExpr {
Subroutine SubExpr: called CC=12 SCAR=25.1
Pod undocumented
510 93 my ($self, $pos, $rest) = @_;
511 93 my $op=$self->op;
512  
513 # Sanity checks
514 93 ▶ defined $pos || warn "Bad param pos.";
Branch: 1 branch
BranchTrueFalseTotal
defined $pos or warn "Bad param pos." 93 0 93
515 93 ▶ if (ref $rest) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rest) { } 29 64 93
elsif (defined $rest) { } 0 64 64
516 29 ▶ $rest->isa("Math::Expr::Opp") || warn "Bad param rest: $rest";
Branch: 1 branch
BranchTrueFalseTotal
$rest->isa("Math::Expr::Opp") or warn "Bad param rest: $rest" 29 0 29
517 29 ▶ !$rest->InTable || warn "Can't edit items in the table";
Branch: 1 branch
BranchTrueFalseTotal
$rest->InTable and warn "Can't edit items in the table" 0 29 29
518 }
519 elsif(defined $rest) {
520 0 warn "Bad param rest: $rest";
521 }
522  
523 93 ▶ if ($op->{'ass'} && $op->{'com'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{'ass'} and $op->{'com'}) { } 80 13 93
Condition66%2/3
$op->{"ass"} and $op->{"com"}
!ll&&!rl&&r
13080
MC/DC50%1/2
$op->{"ass"} and $op->{"com"}
$op->{"ass"} $op->{"com"}
Truth table66%2/3
A and B
A= $op->{"ass"}B= $op->{"com"}
ABresult
0X0
100
111
524 80 my ($part, $j);
525 80 my $cnt=0;
526 80 my $rcnt=0;
527
528 80 $part=new Math::Expr::Opp($self->{'Val'});
529  
530 80 ▶ for($j=0; $j<=$#{$self->{'Opps'}}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $#{$$self{"Opps"};}) 184 80 264
531 184 ▶ if ($j!=$pos) {
Branch: 2 branches
BranchTrueFalseTotal
if ($j != $pos) { } 104 80 184
elsif (ref $rest) { } 29 51 80
532 104 $part->{'Opps'}[$cnt]=$self->{'Opps'}[$j];
533 104 $cnt++;
534 }
535 elsif(ref $rest) {
536 29 $rest->{'Opps'}[$rcnt]=$self->{'Opps'}[$j];
537 29 $rcnt++;
538 }
539 }
540  
541 80 ▶ if (!defined $part->{'Opps'}[1]) {$part=$part->{'Opps'}[0];}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $part->{"Opps"}[1]) 56 24 80
542 80 return $part; #->IntoTable;
543 } else {
544 13 return $self->{'Opps'}[$pos];
545 }
546 }
547  
548 =head2 $n->Set($pos, $val)
549  
550 Replaces the subexpression at position $pos with $val.
551  
552 =cut
553  
554 58 ▶sub _Set {
Subroutine _Set: called CC=5 SCAR=16.1
555 58 my ($self, $pos, $val) = @_;
556 58 my $op=$self->op;
557  
558 58 $pos =~ s/\(\d+\)//g;
559  
560 58 ▶ if ($pos eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos eq '') { } 25 33 58
561 25 return $val;
562 } else {
563 33 $pos =~ s/^(\d+),?//;
564 33 my $i=$1;
565  
566 33 ▶ if ($op->{'ass'} && $op->{'com'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{'ass'} and $op->{'com'}) { } 29 4 33
Condition66%2/3
$op->{"ass"} and $op->{"com"}
!ll&&!rl&&r
4029
MC/DC50%1/2
$op->{"ass"} and $op->{"com"}
$op->{"ass"} $op->{"com"}
Truth table66%2/3
A and B
A= $op->{"ass"}B= $op->{"com"}
ABresult
0X0
100
111
567 29 my $rest=new Math::Expr::Opp($self->{'Val'});
568 29 my $part=$self->SubExpr($i, $rest)->Set($pos,$val);
569 29 my $n=new Math::Expr::Opp($self->{'Val'});
570  
571 29 ▶ if (!defined $rest->{'Opps'}[1]) {$rest=$rest->{'Opps'}[0];}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rest->{"Opps"}[1]) 29 0 29
572  
573 29 $n->{'Opps'}[0]=$rest;
574 29 $n->{'Opps'}[1]=$part;
575 29 return $n;
576 } else {
577 4 $self->{'Opps'}[$i]=$self->{'Opps'}[$i]->Set($pos,$val);
578 }
579 4 return $self;
580 }
581 }
582  
583 27 ▶sub _toMathML {
Subroutine _toMathML: called CC=12 SCAR=24.9
584 27 my $self = shift;
585 27 my @p;
586 my $i;
587 27 my $op = $self->op;
588  
589 27 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 53 27 80
590 53 $p[$i]=$self->{'Opps'}[$i]->toMathML;
591 53 ▶ if (!defined $op->{'noparammathml'} || !eval($op->{'noparammathml'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $op->{"noparammathml"} and eval $op->{"noparammathml"}) 46 7 53
Condition100%3/3
defined $op->{"noparammathml"} and eval $op->{"noparammathml"}
!ll&&!rl&&r
4337
MC/DC100%2/2
defined $op->{"noparammathml"} and eval $op->{"noparammathml"}
defined $op->{"noparammathml"} eval $op->{"noparammathml"}
Truth table100%3/3
A and B
A= defined $op->{"noparammathml"}B= eval $op->{"noparammathml"}
ABresult
0X0
100
111
592 46 ▶ if ($self->{'Opps'}[$i]->isa('Math::Expr::Opp')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Opps"}[$i]->isa("Math::Expr::Opp")) 19 27 46
593 19 ▶ if (!$op->{'ass'} || $self->{'Opps'}[$i]{'Val'} ne $self->{'Val'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $op->{"ass"} or $self->{"Opps"}[$i]{"Val"} ne $self->{"Val"}) 19 0 19
Condition66%2/3
not $op->{"ass"} or $self->{"Opps"}[$i]{"Val"} ne $self->{"Val"}
l!l&&r!l&&!r
1180
MC/DC0%0/2
not $op->{"ass"} or $self->{"Opps"}[$i]{"Val"} ne $self->{"Val"}
not $op->{"ass"} $self->{"Opps"}[$i]{"Val"} ne $self->{"Val"}
Truth table66%2/3
not(A) or B
A= not $op->{"ass"}B= $self->{"Opps"}[$i]{"Val"} ne $self->{"Val"}
ABresult
1X1
011
000
594 19 ▶ if (defined $Pri->{$self->{'Val'}} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$Math::Expr::Opp::Pri{$self->{"Val"}} and defined $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}) 18 1 19
Condition66%2/3
defined $$Math::Expr::Opp::Pri{$self->{"Val"}} and defined $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}
!ll&&!rl&&r
0118
MC/DC50%1/2
defined $$Math::Expr::Opp::Pri{$self->{"Val"}} and defined $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}
defined $$Math::Expr::Opp::Pri{$self->{"Val"}} defined $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}
Truth table66%2/3
A and B
A= defined $$Math::Expr::Opp::Pri{$self->{"Val"}}B= defined $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}
ABresult
0X0
100
111
595 defined $Pri->{$self->{'Opps'}[$i]{'Val'}}) {
596 18 ▶ if ($Pri->{$self->{'Val'}} >=
Branch: 1 branch
BranchTrueFalseTotal
if ($$Math::Expr::Opp::Pri{$self->{"Val"}} >= $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}) 3 15 18
597 $Pri->{$self->{'Opps'}[$i]{'Val'}}) {
598 3 $p[$i]='<mrow><mo fence="true">(</mo>'.$p[$i].
599 '<mo fence="true">)</mo></mrow>';
600 }
601 }
602 }
603 }
604 }
605 }
606  
607 27 ▶ if (defined $op->{'prmathml'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $op->{'prmathml'}) { } 19 8 27
608 19 eval($op->{'prmathml'});
609 } else {
610 8 ▶ if ($self->{'Val'} =~ /^[^a-zA-Z0-9\(\)\,\.\:]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Val'} =~ /^[^a-zA-Z0-9\(\)\,\.\:]+$/) { } 7 1 8
611 7 "<mrow>".join ("<mo>".$self->{'Val'}."</mo>", @p)."</mrow>";
612 } else {
613 1 '<mrow><mi fontstyle="normal">'.$self->{'Val'}.'</mi>'.
614 '<mo fence="true">(</mo>'.join (", ", @p) . "".
615 '<mo fence="true">)</mo></mrow>'
616 }
617 }
618 }
619  
620 78 ▶sub toText {
Subroutine toText: called CC=9 SCAR=22.0
Pod undocumented
621 78 my $self = shift;
622 78 my @p;
623 my $i;
624 78 my $op = $self->op;
625  
626 78 ▶ for ($i=0; $i<=$#{$self->{'Opps'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"Opps"};}) 134 78 212
627 134 $p[$i]=$self->{'Opps'}[$i]->toText;
628 134 ▶ if ($self->{'Opps'}[$i]->isa('Math::Expr::Opp')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Opps"}[$i]->isa("Math::Expr::Opp")) 61 73 134
629 61 ▶ if (!$op->{'ass'} || $self->{'Opps'}[$i]{'Val'} ne $self->{'Val'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $op->{"ass"} or $self->{"Opps"}[$i]{"Val"} ne $self->{"Val"}) 61 0 61
Condition66%2/3
not $op->{"ass"} or $self->{"Opps"}[$i]{"Val"} ne $self->{"Val"}
l!l&&r!l&&!r
29320
MC/DC0%0/2
not $op->{"ass"} or $self->{"Opps"}[$i]{"Val"} ne $self->{"Val"}
not $op->{"ass"} $self->{"Opps"}[$i]{"Val"} ne $self->{"Val"}
Truth table66%2/3
not(A) or B
A= not $op->{"ass"}B= $self->{"Opps"}[$i]{"Val"} ne $self->{"Val"}
ABresult
1X1
011
000
630 61 ▶ if (defined $Pri->{$self->{'Val'}} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$Math::Expr::Opp::Pri{$self->{"Val"}} and defined $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}) 24 37 61
Condition100%3/3
defined $$Math::Expr::Opp::Pri{$self->{"Val"}} and defined $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}
!ll&&!rl&&r
112624
MC/DC100%2/2
defined $$Math::Expr::Opp::Pri{$self->{"Val"}} and defined $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}
defined $$Math::Expr::Opp::Pri{$self->{"Val"}} defined $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}
Truth table100%3/3
A and B
A= defined $$Math::Expr::Opp::Pri{$self->{"Val"}}B= defined $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}
ABresult
0X0
100
111
631 defined $Pri->{$self->{'Opps'}[$i]{'Val'}}) {
632 24 ▶ if ($Pri->{$self->{'Val'}} >=
Branch: 1 branch
BranchTrueFalseTotal
if ($$Math::Expr::Opp::Pri{$self->{"Val"}} >= $$Math::Expr::Opp::Pri{$self->{"Opps"}[$i]{"Val"}}) 7 17 24
633 $Pri->{$self->{'Opps'}[$i]{'Val'}}) {
634 7 $p[$i]='('.$p[$i].')';
635 }
636 }
637 }
638 }
639 }
640  
641 78 ▶ if ($self->{'Val'} =~ /^[^a-zA-Z0-9\(\)\,\.\:]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Val'} =~ /^[^a-zA-Z0-9\(\)\,\.\:]+$/) { } 49 29 78
642 49 join ($self->{'Val'}, @p);
643 } else {
644 29 $self->{'Val'}.'('.join (", ", @p).')'
645 }
646 }
647  
648 =head1 AUTHOR
649  
650 Hakan Ardo <hakan@debian.org>
651  
652 =head1 SEE ALSO
653  
654 L<Math::Expr>
655  
656 =cut
657  
658 1 1;
« Expr/FormulaDB.pm ↑ Summary Expr/TypeDB.pm »