File Coverage

blib/lib/Math/Expr/OpperationDB.pm
Criterion Covered Total %
statement 42 47 89.3
branch 11 12 91.6
condition 2 3 66.6
subroutine 6 8 75.0
pod 1 6 16.6
total 62 76 81.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             # OpperationDB.pm - A db of basic opperands and there properties
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::OpperationDB - A db of basic opperands properties
23              
24             =head1 SYNOPSIS
25              
26             require Math::Expr::OpperationDB;
27             $db=new Math::Expr::OpperationDB($file);
28              
29             =head1 DESCRIPTION
30              
31             This is a database containing info about the different opperations
32             (eg +, -, *, ...). Each opperation is represented by a regexp
33             mathing a type specifikation. That way Real*Real wont be the same
34             opperation as Matrix*Matrix even though the same operation character
35             is used.
36              
37             The data stored for each operation is the return type of the
38             operation and whatever this operation is kommutativ or assosiative.
39              
40             Currently the data is hardcoded into the code, but that is about to
41             chnage.
42              
43             =head1 METHODS
44              
45             =cut
46              
47             package Math::Expr::OpperationDB;
48 1     1   7 use strict;
  1         4  
  1         41  
49              
50 1     1   6 use Math::Expr;
  1         1  
  1         710  
51              
52             =head2 $db=new Math::Expr::OpperationDB;
53              
54             Creates a new db.
55              
56             =cut
57              
58             sub new {
59 1     1 0 71 my $self = bless {}, shift;
60              
61 1         7 $self->Load(shift);
62              
63 1         6 $self;
64             }
65              
66             sub InitDB {
67 1     1 0 2 my $self=shift;
68 1         3 my $a=$self->{'opps'};
69              
70 1         3 foreach (keys %{$a}) {
  1         5  
71 8 100       21 if ($a->{$_}->{'simp'}) {
72 2         7 $a->{$_}->{'simp'}=Parse($a->{$_}->{'simp'});
73             }
74 8         121 $a->{$_}{'TypeReg'}=qr/^$_$/;
75             }
76             }
77              
78             sub Load {
79 1     1 0 2 my ($self, $file) = @_;
80 1         2 my (%t, @o);
81              
82 1         44 open (F, "<$file");
83 1         19 while () {
84 52 100       272 if (/^([^:]+)\s*:\s*(.*)$/) {$t{lc($1)}=$2;}
  44         145  
85 52 100 66     353 if (/^\s*$/ || eof F) {
86 8 50       23 if ($t{'type'}) {
87 8         13 my $t=$t{'type'};
88 8         16 delete $t{'type'};
89              
90 8 100       564 if (defined $t{'prop'}) {
91 2         14 @o = split(/\s*,\s*/, $t{'prop'});
92 2         5 foreach (@o) {
93 4         15 $self->{'opps'}{$t}{lc($_)}=1;
94             }
95 2         4 delete $t{'prop'};
96             }
97              
98 8         25 foreach (keys %t) {
99 34         92 $self->{'opps'}{$t}{$_}=$t{$_};
100 34         93 delete $t{$_};
101             }
102             }
103             }
104             }
105             }
106              
107             =head2 $db->Find($t)
108              
109             Tries all the type regexps in the db on $t and if one matches that
110             post is returned.
111              
112             =cut
113              
114             sub Find {
115 585     585 1 970 my ($self, $str) = @_;
116 585         743 my $opp;
117              
118 585         661 foreach (values %{$self->{'opps'}}) {
  585         1765  
119 2956 100       18543 if ($str =~ $_->{'TypeReg'}) {$opp=$_; last;}
  455         731  
  455         608  
120             }
121              
122 585         11523 $opp;
123             }
124              
125             sub Keys {
126 0     0 0   my $self = shift;
127              
128 0           keys %{$self->{'opps'}};
  0            
129             }
130              
131             sub Get {
132 0     0 0   my ($self, $a) = @_;
133              
134 0           $self->{'opps'}{$a};
135             }
136              
137             1;