blib/lib/Astro/XSPEC/Model/Parse.pm

filter: Statementstmt 97.8% 91 / 93 Branchbran 65.0% 26 / 40 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 87.9% 139 / 158 CC 24 SCAR 20.1
CC
24
Cov
88%
_parse_stanza
13.6
parse_file
8.1
_handle_model
3.1
CRAP 7.5
↑ Summary
1 # --8<--8<--8<--8<--
2 #
3 # Copyright (C) 2010 Smithsonian Astrophysical Observatory
4 #
5 # This file is part of Astro::XSPEC::Model::Parse
6 #
7 # Astro::XSPEC::Model::Parse is free software: you can redistribute it
8 # and/or modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation, either version 3 of
10 # the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # -->8-->8-->8-->8--
21  
22 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 package Astro::XSPEC::Model::Parse;
25 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 $Astro::XSPEC::Model::Parse::VERSION = '0.01';
27 }
28  
29 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use Params::Validate qw[ :all ];
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 2 my %model_handler = (
36 start => { type => CODEREF, optional => 1 },
37 end => { type => CODEREF, optional => 1 },
38 );
39  
40 sub new
41 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
42  
43 1 my $class = shift;
44  
45 my %par = validate( @_,
46 {
47 model => { type => HASHREF,
48 callbacks => { 'handler' =>
49 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 1 pop @_;
51 1 validate( @_, \%model_handler )
52 }
53 },
54 1 default => {},
55 },
56 par => { type => CODEREF, optional => 1 },
57 args => { type => HASHREF, optional => 1 },
58 norm => { type => SCALAR, default => 0 }, }
59 );
60  
61 1 return bless {@_}, $class;
62 }
63  
64 288 ▶sub _handle_model {
Subroutine _handle_model: called CC=3 SCAR=11.2
65  
66 288 my ( $self, $event, $args ) = @_;
67  
68 288 ▶ return 1 unless defined $self->{model}->{$event};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"model"}{$event} 0 288 288
69  
70 288 my $ret = eval { $self->{model}->{$event}->( $event, $args, $self->{args} ) };
71  
72 288 ▶ die( "error in model handler for event $event: $@\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 0 288 288
73 unless defined $ret;
74  
75 288 return $ret;
76 }
77  
78 1401 ▶sub _handle_par {
Subroutine _handle_par: called CC=3 SCAR=11.2
79  
80 1401 my ( $self, $args ) = @_;
81  
82 1401 ▶ return 1 unless defined $self->{par};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"par"} 0 1401 1401
83  
84 1401 my $ret = eval { $self->{par}->( $args, $self->{args} ) };
85  
86 1401 ▶ die( "error in parameter handler: $@\n" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 0 1401 1401
87 unless defined $ret;
88  
89 1401 return $ret;
90 }
91  
92 3 ▶sub parse_file {
Subroutine parse_file: called CC=8 SCAR=20.9
Pod documented
93  
94 3 my ( $self, $file ) = @_;
95  
96 3 ▶ my $fh = IO::File->new( $file )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file) or croak("${file}: error opening file\n") 3 0 3
97 or croak( "$file: error opening file\n" );
98  
99 3 my @stanza;
100  
101 3 ▶ while (my $rec = $fh->getline)
Branch: 1 branch
BranchTrueFalseTotal
if (my $rec = $fh->getline) 1686 3 1689
102 {
103 1686 chomp $rec;
104  
105 1686 my $blank = $rec =~ /^\s*$/;
106  
107 1686 ▶ if ( $blank )
Branch: 1 branch
BranchTrueFalseTotal
if ($blank) { } 141 1545 1686
108 {
109 # ignore blank lines between stanzas
110 141 ▶ next if 0 == @stanza;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == @stanza 0 141 141
111 }
112 else
113 {
114 1545 push @stanza, [ $fh->input_line_number, $rec ];
115 }
116  
117 # if we hit a blank line, or this is the last record,
118 # we're done with the current stanza; parse it.
119 1686 ▶ if ( $blank || $fh->eof )
Branch: 1 branch
BranchTrueFalseTotal
if ($blank or $fh->eof) 144 1542 1686
Condition100%3/3
$blank or $fh->eof
l!l&&r!l&&!r
14131542
MC/DC100%2/2
$blank or $fh->eof
$blank $fh->eof
Truth table100%3/3
A or B
A= $blankB= $fh->eof
ABresult
1X1
011
000
120 {
121 144 ▶ eval { $self->_parse_stanza( \@stanza ) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_parse_stanza(\@stanza) } } or croak("${file}: $@\n") 144 0 144
122 or croak( "$file: $@\n" );
123  
124 144 @stanza = ();
125 }
126  
127 }
128  
129 3 return $self;
130 }
131  
132 144 ▶sub _parse_stanza {
Subroutine _parse_stanza: called CC=13 SCAR=26.1
133  
134 144 my ( $self, $records ) = @_;
135  
136 # first line is the model spec
137 144 my ( $lineno, $record ) = @{ shift @{ $records } };
138 144 my @fields = split( ' ', $record );
139  
140 144 ▶ die( "$lineno: syntax error in first line of stanza: $record")
Branch: 1 branch
BranchTrueFalseTotal
unless @fields >= 6 0 144 144
141 unless @fields >= 6;
142  
143 144 my %model;
144  
145 144 @model{ qw[ name npars elo ehi subname type calcvar forcecalc ] } = @fields;
146  
147 # delete undefined parameters
148 144 delete @model{ grep { ! defined $model{$_} } keys %model };
149  
150 144 ▶ $self->_handle_model( 'start', \%model ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_handle_model("start", \%model) or return 144 0 144
151  
152 # now grab the individual parameters
153 144 my @pars;
154 144 for my $par ( 1..$model{npars} )
155 {
156 1401 my %par;
157 1401 my $rec = shift @{ $records };
158  
159 1401 my ( $lineno, $record ) = @$rec;
160  
161 1401 my @fields = Text::ParseWords::parse_line( qr/\s+/, 0, $record );
162  
163 1401 my $name = shift @fields;
164 1401 ▶ if ( $name =~ /^\*(.*)/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /^\*(.*)/) { } 5 1396 1401
elsif ($name =~ /^\$(.*)/) { } 13 1383 1396
165 {
166 5 $par{type} = 'scale';
167 5 $par{name} = $1;
168 5 $par{units} = shift @fields;
169 5 $par{value} = shift @fields;
170 }
171  
172 elsif( $name =~ /^\$(.*)/ )
173 {
174 13 $par{type} = 'switch';
175 13 $par{name} = $1;
176 $par{units} = shift @fields
177 13 ▶ if @fields == 7;
Branch: 1 branch
BranchTrueFalseTotal
if @fields == 7 3 10 13
178  
179 13 $par{value} = shift @fields;
180  
181 13 ▶ @par{ qw( hard_min soft_min soft_max hard_max delta ) } = @fields
Branch: 1 branch
BranchTrueFalseTotal
if @fields == 5 7 6 13
182 if @fields == 5;
183 }
184  
185 else
186 {
187 1383 $par{type} = 'variable';
188 1383 $par{name} = $name;
189 1383 @par{ qw( units value hard_min soft_min soft_max hard_max delta periodic ) }
190 = @fields;
191 }
192  
193 # make sure we delete any undefined ones
194 1401 delete @par{ grep { ! defined $par{$_} } keys %par };
195  
196 1401 $self->_handle_par( \%par );
197 }
198  
199 144 ▶ if ( @$records )
Branch: 1 branch
BranchTrueFalseTotal
if (@$records) 0 144 144
200 {
201 0 die( "$records->[0][0]: extra records in stanza\n" )
202 }
203  
204 144 ▶ if ( $model{type} eq 'add' && $self->{norm} )
Branch: 1 branch
BranchTrueFalseTotal
if ($model{"type"} eq "add" and $self->{"norm"}) 0 144 144
Condition66%2/3
$model{"type"} eq "add" and $self->{"norm"}
!ll&&!rl&&r
51930
MC/DC0%0/2
$model{"type"} eq "add" and $self->{"norm"}
$model{"type"} eq "add" $self->{"norm"}
Truth table66%2/3
A and B
A= $model{"type"} eq "add"B= $self->{"norm"}
ABresult
0X0
100
111
205 {
206 0 ▶ $self->_handle_par(
Branch: 1 branch
BranchTrueFalseTotal
$self->_handle_par({"type", "variable", "name", "norm", "value", 1, "delta", "0.01", "hard_min", 0, "soft_min", 0, "soft_max", "1e+24", "hard_max", "1e+24"}) or return 0 0 0
207 {
208 type => 'variable',
209 name => 'norm',
210 value => 1,
211 delta => 0.01,
212 hard_min => 0.0,
213 soft_min => 0.0,
214 soft_max => 1e+24,
215 hard_max => 1e+24,
216 }) or return;
217 }
218  
219 144 ▶ $self->_handle_model( 'end', \%model ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_handle_model("end", \%model) or return 144 0 144
220  
221 144 return 1;
222 }
223  
224  
225 2 1;
226  
227 __END__
↑ Summary