File Coverage

blib/lib/Math/Expr/Rule.pm
Criterion Covered Total %
statement 29 41 70.7
branch 4 6 66.6
condition n/a
subroutine 3 5 60.0
pod 0 4 0.0
total 36 56 64.2


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             # Rule.pm - Represents a agebraic rule
4             # (c) Copyright 1998 Hakan Ardo
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     1   624 use strict;
  1         2  
  1         366  
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             sub new {
56 11     11 0 84 my($class, $from, $to) = @_;
57 11         46 my $self = bless { }, $class;
58              
59 11         45 $self->{'From'}=$from;
60 11         31 $self->{'To'}=$to;
61              
62 11         28 $self;
63             }
64              
65             sub Apply {
66 11     11 0 113 my ($self, $expr, $pre)=@_;
67 11         19 my $e=$expr;
68 11         59 my $ms=$e->Match($self->{'From'}, "", $pre);
69 11         20 my ($n,$nh);
70 0         0 my @res;
71 0         0 my ($ok, $t);
72 11         25 my $id=[];
73              
74 11         49 foreach ($ms->Keys) {
75 35         136 $n=$e->Copy;
76 35         190 $nh=$self->{'To'}->Subs($ms->Get($_));
77 35         162 $n=$n->Set($_,$nh);
78 35         272 $n->Simplify;
79              
80 35         55 $ok=1;
81 35         81 foreach $t (@res) {
82 73 100       202 if ($t->tostr eq $n->tostr) {$ok=0; last;}
  13         20  
  13         24  
83             }
84 35 100       184 if ($ok) {
85 22         38 push @res,$n;
86 22         36 push @{$id}, $_;
  22         64  
87            
88             }
89             }
90 11         39 $self->{'Id'}=$id;
91 11         69 @res;
92             }
93              
94             sub ApplyAt {
95 0     0 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++) {
100 0 0         if ($self->{'Id'}[$i] eq $pos) {
101 0           return $r[$i];
102             }
103             }
104 0           warn "Unable to apply at that position";
105 0           return 0;
106             }
107              
108             sub GetId {
109 0     0 0   my $self=shift;
110 0           $self->{'Id'};
111             }
112              
113             1;
114