Expr/Rule.pm

filter: Statementstmt 76.1% 32 / 42 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 3 / 5 Podpod 0.0% 0 / 4 totaltotal 66.1% 39 / 59 CC 7 SCAR 17.8
CC
7
Cov
72%
ApplyAt
12.0
Apply
5.0
GetId
2.0
CRAP 5.9
« Expr.pm ↑ Summary Expr/OpperationDB.pm »
1 #!/usr/bin/perl
2  
3 # Rule.pm - Represents a agebraic rule
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::Var - Represents a agebraic rule
23  
24 =head1 SYNOPSIS
25  
26 reuire Math::Expr::Var;
27 reuire Math::Expr;
28
29 $r=new Math::Expr::Var($from, $to);
30 @res=$r->Apply($expr)
31  
32 =head1 DESCRIPTION
33  
34 This will create a rule that converts the expression $from to $to,
35 and then apply that rule to $expr. $from, $to, $expr are all
36 Math::Expr::Opp structures that should be Simplified to work ok.
37  
38 The result is a array @res of Math::Expr::Opp objects which should
39 contain the results of applying the rule once on $expr in all
40 possible ways. They are all simplified, and duplicaions are removed.
41  
42 =head1 METHODS
43  
44 =cut
45  
46 package Math::Expr::Rule;
47 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 =head2 $r=new Math::Expr::Var($from, $to);
50  
51 Makes a rule converting the Math::Expr::Opp structur $from into $to.
52  
53 =cut
54  
55 11 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
56 11 my($class, $from, $to) = @_;
57 11 my $self = bless { }, $class;
58  
59 11 $self->{'From'}=$from;
60 11 $self->{'To'}=$to;
61  
62 11 $self;
63 }
64  
65 11 ▶sub Apply {
Subroutine Apply: called CC=5 SCAR=16.1
Pod undocumented
66 11 my ($self, $expr, $pre)=@_;
67 11 my $e=$expr;
68 11 my $ms=$e->Match($self->{'From'}, "", $pre);
69 11 my ($n,$nh);
70 11 my @res;
71 11 my ($ok, $t);
72 11 my $id=[];
73  
74 11 foreach ($ms->Keys) {
75 35 $n=$e->Copy;
76 35 $nh=$self->{'To'}->Subs($ms->Get($_));
77 35 $n=$n->Set($_,$nh);
78 35 $n->Simplify;
79  
80 35 $ok=1;
81 35 foreach $t (@res) {
82 75 ▶ if ($t->tostr eq $n->tostr) {$ok=0; last;}
Branch: 1 branch
BranchTrueFalseTotal
if ($t->tostr eq $n->tostr) 13 62 75
83 }
84 35 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 22 13 35
85 22 push @res,$n;
86 22 push @{$id}, $_;
87
88 }
89 }
90 11 $self->{'Id'}=$id;
91 11 @res;
92 }
93  
94 0 ▶sub ApplyAt {
Subroutine ApplyAt: not called CC=3 SCAR=24.8
Pod undocumented
95 0 my ($self, $e, $pos, $pre) = @_;
96 0 my $i;
97 0 my @r=$self->Apply($e, $pre);
98  
99 0 ▶ for ($i=0; $i<=$#r; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#r) 0 0 0
100 0 ▶ if ($self->{'Id'}[$i] eq $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Id"}[$i] eq $pos) 0 0 0
101 0 return $r[$i];
102 }
103 }
104 0 warn "Unable to apply at that position";
105 0 return 0;
106 }
107  
108 0 ▶sub GetId {
Subroutine GetId: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $self=shift;
110 0 $self->{'Id'};
111 }
112  
113 1 1;
114  
« Expr.pm ↑ Summary Expr/OpperationDB.pm »