| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # FormulaDB.pm - A db of formulas and there properties | ||||||||||||||||||||||||||||
| 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::FormulaDB - A db of formulas and there properties | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | require Math::Expr::FormulaDB; | ||||||||||||||||||||||||||||
| 27 | $db=new Math::Expr::FormulaDB($file); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | package Math::Expr::FormulaDB; | ||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use Math::Expr; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | require Math::Expr::Rule; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 0 | my $self = bless {}, shift; | |||||||||||||||||||||||||||
| 39 | 0 | $self->Load(shift); | |||||||||||||||||||||||||||
| 40 | 0 | my $db; | |||||||||||||||||||||||||||
| 41 | 0 | my ($vl, $hl); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | foreach ($self->Keys) { | |||||||||||||||||||||||||||
| 44 | 0 | $db=$self->Get($_); | |||||||||||||||||||||||||||
| 45 | 0 | $vl=Parse($db->{'vl'}); $vl=$vl->Simplify; | |||||||||||||||||||||||||||
| 46 | 0 | $hl=Parse($db->{'hl'}); $hl=$hl->Simplify; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | $db->{'for'}= new Math::Expr::Rule($vl, $hl); | |||||||||||||||||||||||||||
| 49 | 0 | $db->{'back'}= new Math::Expr::Rule($hl, $vl); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | $self; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub Load { | ||||||||||||||||||||||||||
|
Subroutine
Load: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 0 | my ($self, $file) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (-f $file) {$self->LoadFile($file);} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if (-d $file) {$self->LoadDir($file);} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub LoadFile { | ||||||||||||||||||||||||||
|
Subroutine
LoadFile: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 0 | my ($self, $file) = @_; | |||||||||||||||||||||||||||
| 63 | 0 | my %t; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | open (F, "<$file"); | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | while (<F>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (/^([^:]+)\s*:\s*(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | my $a=lc($1); | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if (defined $t{$a}) {$t{$a}.="\n$2";} else {$t{$a}=$2;} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (/^\s*$/ || eof F) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
/^\s*$/ or eof F
MC/DC0%0/2
/^\s*$/ or eof F
/^\s*$/
eof F
Truth table0%0/3
A or B
A= /^\s*$/B= eof F
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($t{'name'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | my $t=$t{'name'}; | |||||||||||||||||||||||||||
| 74 | 0 | delete $t{'name'}; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | foreach (keys %t) { | |||||||||||||||||||||||||||
| 77 | 0 | $self->{'opps'}{$t}{$_}=$t{$_}; | |||||||||||||||||||||||||||
| 78 | 0 | delete $t{$_}; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub LoadDir { | ||||||||||||||||||||||||||
|
Subroutine
LoadDir: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 0 | my ($self, $dir) = @_; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | foreach (split(/\n/s, `find $dir -type f`)) { | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | next if (/~$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | $self->LoadFile($_); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub Keys { | ||||||||||||||||||||||||||
|
Subroutine
Keys: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | keys %{$self->{'opps'}}; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub Get { | ||||||||||||||||||||||||||
|
Subroutine
Get: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | 0 | my ($self, $a) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | $self->{'opps'}{$a}; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub Find { | ||||||||||||||||||||||||||
|
Subroutine
Find: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 0 | my ($self, $e, $t) = @_; | |||||||||||||||||||||||||||
| 108 | 0 | my $db; | |||||||||||||||||||||||||||
| 109 | 0 | my ($n, $d, $i); | |||||||||||||||||||||||||||
| 110 | 0 | my (@res, $id); | |||||||||||||||||||||||||||
| 111 | 0 | my $r; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if (!defined $t) {$t=0;} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | foreach $n ($self->Keys) { | |||||||||||||||||||||||||||
| 116 | 0 | $db=$self->Get($n); | |||||||||||||||||||||||||||
| 117 | 0 | foreach $d ("for", "back") { | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | if (!$t || !$db->{'triv'.$d}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$t and $$db{'triv' . $d}
MC/DC0%0/2
$t and $$db{'triv' . $d}
$t
$$db{'triv' . $d}
Truth table0%0/3
A and B
A= $tB= $$db{'triv' . $d}
| |||||||||||||||||||||||||||||
| 119 | 0 | @res=$db->{$d}->Apply($e); | |||||||||||||||||||||||||||
| 120 | 0 | $id=$db->{$d}->GetId; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | for ($i=0; $i<=$#res; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $r->{"$n-$d-".$id->[$i]}=$res[$i]; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | $r; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub ApplyProof { | ||||||||||||||||||||||||||
|
Subroutine
ApplyProof: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 131 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 132 | 0 | my %vars; | |||||||||||||||||||||||||||
| 133 | 0 | my $db=$self->Get($key); | |||||||||||||||||||||||||||
| 134 | 0 | my $res=""; | |||||||||||||||||||||||||||
| 135 | 0 | my $prev=""; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if (defined $db->{'b'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | $vars{'vl'}=Parse($db->{'vl'}); | |||||||||||||||||||||||||||
| 139 | 0 | $vars{'hl'}=Parse($db->{'hl'}); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | $res.=$vars{'hl'}->toText; | |||||||||||||||||||||||||||
| 142 | 0 | $vars{'hl'}->Simplify; | |||||||||||||||||||||||||||
| 143 | 0 | $res.=" <=> ".$vars{'hl'}->toText."\n"; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | $res.=$vars{'vl'}->toText; | |||||||||||||||||||||||||||
| 146 | 0 | $vars{'vl'}->Simplify; | |||||||||||||||||||||||||||
| 147 | 0 | $res.=" <=> " . $vars{'vl'}->toText; | |||||||||||||||||||||||||||
| 148 | 0 | $prev="vl"; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | foreach (split(/\n/s, $db->{'b'})) { | |||||||||||||||||||||||||||
| 151 | 0 | ▶ | if (/^\s*\$([^\s=]+)\s*=\s*([^\'\s]*)\s*(\'[^\']+\')?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | my $var=$1; my $rule=$2; my $pre=$3; | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | if (defined $pre && $pre=~/^\s*\'\s*([^=\s]+)\s*=\s*([^\']+)\'\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $pre and $pre =~ /^\s*\'\s*([^=\s]+)\s*=\s*([^\']+)\'\s*$/
MC/DC0%0/2
defined $pre and $pre =~ /^\s*\'\s*([^=\s]+)\s*=\s*([^\']+)\'\s*$/
defined $pre
$pre =~ /^\s*\'\s*([^=\s]+)\s*=\s*([^\']+)\'\s*$/
Truth table0%0/3
A and B
A= defined $preB= $pre =~ /^\s*\'\s*([^=\s]+)\s*=\s*([^\']+)\'\s*$/
| |||||||||||||||||||||||||||||
| 154 | 0 | my $a=$1; my $b=$2; | |||||||||||||||||||||||||||
| 155 | 0 | print "Pre: $a<=>$b\n"; | |||||||||||||||||||||||||||
| 156 | 0 | $pre=new Math::Expr::VarSet; | |||||||||||||||||||||||||||
| 157 | 0 | $pre->Set($a, Parse($b)); | |||||||||||||||||||||||||||
| 158 | } else { | ||||||||||||||||||||||||||||
| 159 | 0 | $pre=undef; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | if ($prev ne $var) {$res.="\n".$vars{$var}->toText;} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | $vars{$var}=$self->ApplyAt($vars{$var},$rule,$pre); | |||||||||||||||||||||||||||
| 164 | 0 | $res.=" <=> " . $vars{$var}->toText; | |||||||||||||||||||||||||||
| 165 | 0 | $prev=$var; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 0 | $res."\n"; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub ApplyAt { | ||||||||||||||||||||||||||
|
Subroutine
ApplyAt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 173 | 0 | my ($self, $e, $r,$pre) = @_; | |||||||||||||||||||||||||||
| 174 | 0 | my ($rule, $dir, $pos) = split (/-/, $r); | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | $self->Get($rule)->{$dir}->ApplyAt($e,$pos,$pre); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 1 | 1; | |||||||||||||||||||||||||||