Nasmod.pm

filter: Statementstmt 91.7% 134 / 146 Branchbran 80.0% 48 / 60 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 93.3% 14 / 15 Podpod 72.7% 8 / 11 totaltotal 87.2% 219 / 251 CC 40 SCAR 23.6
CC
40
Cov
88%
parse
20.0
print
10.8
getCol
6.0
CRAP 10.6
« Nasmod/Entity.pm ↑ Summary
1 package CAE::Nastran::Nasmod;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use CAE::Nastran::Nasmod::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use vars qw($VERSION $ABSTRACT $DATE);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 $VERSION = '0.26';
9 4 $DATE = 'Fri Apr 25 13:17:31 2014';
10 4 $ABSTRACT = 'basic access to nastran models';
11  
12 sub new
13 32 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod documented
14 32 my $this = shift;
15 32 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0320
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
16 32 my $self={};
17  
18 32 $self =
19 {
20 "bulk" => [],
21 "tmp" => [],
22 };
23  
24 32 bless ($self, $class);
25 32 return $self;
26 }
27  
28 #---------------------
29 # prints the whole model to STDOUT or a file
30 # print()
31 # return: 0 | 1
32 #---------------------
33 sub print
34 1 ▶{
Subroutine print: called CC=9 SCAR=23.8
Pod documented
35 1 my $self = shift;
36  
37 1 my $outfile = undef;
38 1 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
39 {
40 1 $outfile = shift(@_);
41 1 ▶ if (stat $outfile)
Branch: 1 branch
BranchTrueFalseTotal
if (stat $outfile) 0 1 1
42 {
43 0 print("error: file does already exist. " . $outfile . "\n");
44 0 return 0;
45 }
46 }
47  
48 # if an outfile has been defined, redirect STDOUT to this file
49 1 ▶ if($outfile)
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) 1 0 1
50 {
51 1 ▶ open (SAVE, ">&STDOUT") or die "can't save STDOUT $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open SAVE, ">&STDOUT" or die "can't save STDOUT $!\n" 1 0 1
52 1 ▶ open (STDOUT, '>', $outfile) or die "can't redirect STDOUT to " . $outfile . ": $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $outfile or die q[can't redirect STDOUT to ] . $outfile . ": $!" 1 0 1
53 }
54  
55 # print "WOOOOOPP\n";
56 # print "anzahl der entities: " . scalar(@{$self->{'bulk'}}) . "\n";
57  
58 # print each entity
59 1 foreach my $entity (@{$self->{'bulk'}})
60 {
61 6 $entity->print();
62 }
63
64 # remove redirection of STDOUT
65 1 ▶ if($outfile)
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) 1 0 1
66 {
67 1 close STDOUT;
68 1 ▶ open (STDOUT, ">&SAVE") or die "can't restore STDOUT $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&SAVE" or die "can't restore STDOUT $!\n" 1 0 1
69 1 close SAVE;
70 }
71
72 1 return 1;
73 }
74 #---------------------
75  
76 #---------------------
77 # imports data from a nastran file
78 # optional filtering possible
79 #---------------------
80 sub importBulk
81 21 ▶{
Subroutine importBulk: called CC=4 SCAR=13.9
Pod undocumented
82 21 my $self = shift;
83 21 my $path = shift;
84 21 my $refh_options;
85  
86 21 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 12 21
87 {
88 9 $refh_options = shift;
89 }
90  
91 0 ▶ if (!open (MODEL, "<$path")) {die "cannot read $path"}
Branch: 1 branch
BranchTrueFalseTotal
unless (open MODEL, "<$path") 0 21 21
92  
93 21 my @model = <MODEL>;
94 21 chomp @model;
95 21 close MODEL;
96  
97 21 $self->{'tmp'} = \@model;
98  
99 21 ▶ if ($refh_options)
Branch: 1 branch
BranchTrueFalseTotal
if ($refh_options) { } 9 12 21
100 {
101 9 $self->parse($refh_options);
102 }
103 else
104 {
105 12 $self->parse();
106 }
107 }
108  
109 #---------------------
110 # parse bulkdata and store entity-objects in show
111 #---------------------
112 sub parse
113 21 ▶{
Subroutine parse: called CC=20 SCAR=30.0
Pod undocumented
114 21 my $self = shift;
115  
116 21 my $maxoccur;
117 21 my $occur = 0;
118  
119 21 my $cards;
120 my $refa_filter;
121  
122 21 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 12 21
123 {
124 9 my $refh_options = shift;
125 9 my %OPTIONS = %$refh_options;
126 9 ▶ if (defined $OPTIONS{'cards'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $OPTIONS{"cards"}) 6 3 9
127 {
128 6 $cards = join("|", @{$OPTIONS{'cards'}});
129 }
130 9 ▶ if (defined $OPTIONS{'filter'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $OPTIONS{"filter"}) 7 2 9
131 {
132 7 $refa_filter = $OPTIONS{'filter'};
133 }
134 9 ▶ if (defined $OPTIONS{'maxoccur'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $OPTIONS{"maxoccur"}) 1 8 9
135 {
136 1 $maxoccur = $OPTIONS{'maxoccur'};
137 }
138 }
139  
140 21 my $entity;
141 my @comment;
142  
143 21 my $just_skipped = 0;
144  
145 21 my $folgezeile = 0;
146  
147 # each line of bulk
148 21 foreach my $line (@{$self->{tmp}})
149 {
150 # if its a comment
151 184 ▶ if ($line =~ m/^\$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\$/) { } 41 143 184
152 {
153 41 push @comment, $line;
154 # print "-----\n";
155 # print "COMMENT: $line\n";
156 }
157
158 # if its an entity
159 else
160 {
161  
162 # sofort ueberpruefen ob die karte ueberhaupt eingelesen werden soll
163 143 ▶ if (($cards) && ($line =~ m/^\w+/) && ($line !~ m/^$cards/))
Branch: 1 branch
BranchTrueFalseTotal
if ($cards and $line =~ /^\w+/ and not $line =~ /^$cards/) 9 134 143
Condition100%6/6
$cards and $line =~ /^\w+/ and not $line =~ /^$cards/
!ll&&!rl&&r
110249
$cards and $line =~ /^\w+/
!ll&&!rl&&r
105533
MC/DC100%3/3
$cards and $line =~ /^\w+/ and not $line =~ /^$cards/
$cards $line =~ /^\w+/ not $line =~ /^$cards/
Truth table100%4/4
A and B and not(C)
A= $cardsB= $line =~ /^\w+/C= not $line =~ /^$cards/
ABCresult
0XX0
10X0
1100
1111
164 {
165 9 $just_skipped = 1;
166 9 undef @comment;
167 9 next;
168 }
169
170 # zeile zerteilen
171 134 my @line = &split8($line);
172
173 # handelt es sich um die erste Zeile einer Karte?
174 134 ▶ if ($line =~ m/^\w+/)
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\w+/) { } 114 20 134
elsif (not $just_skipped) { } 20 0 20
175 {
176 114 $just_skipped = 0;
177 114 $folgezeile = 0;
178
179 # first store previous entity-object if available and if matches the filter
180 114 ▶ if ($entity)
Branch: 1 branch
BranchTrueFalseTotal
if ($entity) 93 21 114
181 {
182 # greift der filter? dann ablegen | ist $maxoccur erreicht? dann abbrechen
183 93 ▶ if($entity->match($refa_filter))
Branch: 1 branch
BranchTrueFalseTotal
if ($entity->match($refa_filter)) 74 19 93
184 {
185 # print "FILTER GREIFT fuer Zeile: $line\n";
186 74 $self->addEntity($entity);
187 74 $occur++;
188 74 ▶ if( ($maxoccur) && ($maxoccur <= $occur) )
Branch: 1 branch
BranchTrueFalseTotal
if ($maxoccur and $maxoccur <= $occur) 1 73 74
Condition100%3/3
$maxoccur and $maxoccur <= $occur
!ll&&!rl&&r
7211
MC/DC100%2/2
$maxoccur and $maxoccur <= $occur
$maxoccur $maxoccur <= $occur
Truth table100%3/3
A and B
A= $maxoccurB= $maxoccur <= $occur
ABresult
0X0
100
111
189 {
190 1 return;
191 }
192 }
193 }
194
195 # ein neues entity anlegen
196 113 $entity = CAE::Nastran::Nasmod::Entity->new();
197 113 $entity->setComment(@comment);
198 113 undef(@comment);
199
200 # die zerhackte zeile durchgehen und in einem entity ablegen
201 113 ▶ for(my $x=0, my $col=1; $x<@line; $x++, $col++)
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @line) 693 113 806
202 {
203 693 $entity->setCol($col, $line[$x]);
204 }
205 }
206
207 # wenn kein kommentar und keine schluesselzeile, dann handelt es sich um eine folgezeile.
208 # diese soll nur dann beruecksichtigt werden, wenn die schluesselzeile nicht aussortiert wurde
209 elsif (!($just_skipped))
210 {
211 20 $folgezeile++;
212
213 # die zerhackte zeile durchgehen und in einem entity ablegen
214 20 ▶ for(my $x=0, my $col=(1+($folgezeile * 10)); $x<@line; $x++, $col++)
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @line) 40 20 60
215 {
216 40 $entity->setCol($col, $line[$x]);
217 }
218 }
219 }
220 }
221  
222 # zum schluss die letzte entity ablegen
223 20 ▶ if ($entity)
Branch: 1 branch
BranchTrueFalseTotal
if ($entity) 20 0 20
224 {
225 20 ▶ if ($entity->match($refa_filter))
Branch: 1 branch
BranchTrueFalseTotal
if ($entity->match($refa_filter)) 16 4 20
226 {
227 16 $self->addEntity($entity);
228 }
229 }
230
231 }
232 #---------------------
233  
234 #---------------------
235 # split a string in chunks of 8 characters
236 #---------------------
237 sub split8
238 134 ▶{
Subroutine split8: called CC=2 SCAR=6.9
Pod undocumented
239 134 my $string = shift;
240 134 my @strings;
241 134 ▶ for (my $x=0; ($x*8) < length($string); $x++)
Branch: 1 branch
BranchTrueFalseTotal
if ($x * 8 < length $string) 739 134 873
242 {
243 739 my $substring = substr $string, ($x*8), 8;
244 739 $substring =~ s/^\s+//;
245 739 $substring =~ s/\s+$//;
246 739 push @strings, $substring;
247 }
248 134 return @strings;
249 }
250 #---------------------
251  
252 #---------------------
253 # adds an entity to show
254 # addEntity(@entities)
255 # return: -
256 #---------------------
257 sub addEntity
258 113 ▶{
Subroutine addEntity: called CC=1 SCAR=0.0
Pod documented
259 113 my $self = shift;
260 113 push @{$self->{bulk}}, @_;
261 }
262 #---------------------
263  
264 #---------------------
265 # gets the entities that match the filter. if no filter is given, returns all entities
266 # getEntity(\@filter)
267 # return: @allEntitiesThatMatch
268 #---------------------
269 sub getEntity
270 8 ▶{
Subroutine getEntity: called CC=2 SCAR=6.9
Pod documented
271 8 my $self = shift;
272  
273 # if a filter is given
274 8 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4 4 8
275 {
276 4 my $refh_filter = shift;
277 4 my $newModel = $self->filter($refh_filter);
278 4 return $newModel->getEntity();
279 }
280
281 # if no filter is given
282 else
283 {
284 4 return @{$self->{bulk}};
285 }
286  
287 }
288 #---------------------
289  
290 #---------------------
291 # filter model
292 # return a model
293 # filter array:
294 # $[0]: pattern for matching the comment
295 # $[1]: pattern for matching the row1 of entity
296 # $[2]: pattern for matching the row2 of entity
297 # an entity matches when every pattern of the given filter is found in entity at the given place.
298 #---------------------
299 sub filter
300 11 ▶{
Subroutine filter: called CC=5 SCAR=16.6
Pod documented
301 11 my $self = shift;
302 11 my $refa_filter = shift;
303 11 my $refh_param;
304
305 11 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 11 11
306 {
307 0 $refh_param = shift;
308 }
309  
310 # ein neues objekt erzeugen
311 11 my $filtered_model = CAE::Nastran::Nasmod->new();
312  
313 # alle entities durchgehen
314 11 foreach my $entity (@{$self->{'bulk'}})
315 {
316 72 ▶ if ($entity->match($refa_filter))
Branch: 1 branch
BranchTrueFalseTotal
if ($entity->match($refa_filter)) 21 51 72
317 {
318 21 $filtered_model->addEntity($entity);
319 21 ▶ if ($refh_param->{'firstonly'})
Branch: 1 branch
BranchTrueFalseTotal
if ($refh_param->{"firstonly"}) 0 21 21
320 {
321 0 return $filtered_model;
322 }
323 }
324 }
325 11 return $filtered_model;
326 }
327 #---------------------
328  
329 #---------------------
330 # getrow
331 sub getCol
332 0 ▶{
Subroutine getCol: not called CC=2 SCAR=17.9
Pod documented
333 0 my $self = shift;
334 0 my $row = shift;
335 0 my @return;
336 0 foreach my $entity (@{$self->{'bulk'}})
337 {
338 0 push @return, $entity->getrow($row);
339 }
340 0 return @return;
341 }
342 #---------------------
343  
344 #---------------------
345 # merges models to this model
346 # merge(Nasmod, Nasmod, ...)
347 # return: -
348 #---------------------
349 sub merge
350 1 ▶{
Subroutine merge: called CC=2 SCAR=6.9
Pod documented
351 1 my $self = shift;
352 1 foreach my $model (@_)
353 {
354 1 push @{$self->{'bulk'}}, @{$model->{'bulk'}};
355 }
356 }
357 #---------------------
358  
359 #---------------------
360 # count_entities
361 # count()
362 # return: int
363 #---------------------
364 sub count
365 24 ▶{
Subroutine count: called CC=1 SCAR=0.0
Pod documented
366 24 my $self = shift;
367  
368 24 return scalar(@{$self->{'bulk'}});
369 }
370 #---------------------
371 4 1;
372  
373 __END__
« Nasmod/Entity.pm ↑ Summary