lib/OWL/Base.pm

filter: Statementstmt 68.1% 218 / 320 Branchbran 35.9% 46 / 128 Conditioncond 32.6% 17 / 52 MC/DCmcdc 16.6% 5 / 30 Subroutinesub 75.8% 44 / 58 Podpod 30.0% 6 / 20 totaltotal 55.2% 336 / 608 CC 88 SCAR 31.3
CC
88
Cov
56%
_reformat_stacktrace
90.0
check_type
80.5
throw
53.6
CRAP 22.9
« lib/OWL2Perl.pm ↑ Summary lib/OWL/Utils.pm »
1 #-----------------------------------------------------------------
2 # OWL::Base
3 # Author: Edward Kawas <edward.kawas@gmail.com>
4 # Martin Senger <martin.senger@gmail.com>
5 # For copyright and disclaimer see below.
6 #
7 # $Id: Base.pm,v 1.28 2010-02-11 00:02:51 ubuntu Exp $
8 #-----------------------------------------------------------------
9 package OWL::Base;
10 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use OWL::LSID;
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 4 ▶use vars qw( $VERSION $Revision $AUTOLOAD @EXPORT @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use vars qw( $LOG $LOGGER_NAME $CONFIG_NAMESPACE );
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 # names of attribute's types
19 4 ▶use constant STRING => 'string';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use constant INTEGER => 'integer';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use constant FLOAT => 'float';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use constant BOOLEAN => 'boolean';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use constant DATETIME => 'datetime';
Subroutine BEGIN: called CC=1 SCAR=0.0
24
25 # names of attribute's properties
26 4 ▶use constant TYPE => 'type';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use constant POST => 'post';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use constant ISARRAY => 'is_array';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use constant READONLY => 'readonly';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use overload q("") => "as_string";
Subroutine BEGIN: called CC=1 SCAR=0.0
31
32 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.4
33 4 @ISA = qw( Exporter );
34 4 @EXPORT = qw( $LOG );
35 4 $VERSION = sprintf "%d.%02d", q$Revision: 1.28 $ =~ /: (\d+)\.(\d+)/;
36 4 $Revision = '$Id: Base.pm,v 1.29 2010-02-11 00:02:51 ubuntu Exp $';
37
38 # initiate error handling
39 4 require Carp;
40 4 import Carp qw( confess );
41
42 # read default configuration file and import configuration
43 # parameters into 'OWLCFG namespace'
44 4 ▶ use OWL::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 $CONFIG_NAMESPACE = 'OWLCFG';
46
47 4 ▶ sub init_config {
Subroutine init_config: called CC=1 SCAR=0.0
Pod documented
48 4 shift; # invocant ignored
49 4 OWL::Config->init(@_);
50 4 OWL::Config->import_names($CONFIG_NAMESPACE);
51 }
52 4 OWL::Base->init_config;
53
54 # initiate logging
55 4 ▶ use Log::Log4perl qw(get_logger :levels :no_extra_logdie_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 4 $LOGGER_NAME = 'generator';
57
58 4 ▶ sub init_logging {
Subroutine init_logging: called CC=8 SCAR=29.1
Pod undocumented
59 4 ▶ if ($OWLCFG::LOG_CONFIG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($OWLCFG::LOG_CONFIG) 0 4 4
60 0 eval { Log::Log4perl->init($OWLCFG::LOG_CONFIG) };
61 0 ▶ $LOG = get_logger($LOGGER_NAME) and return unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
Condition0%0/5
$@ || ($OWL::Base::LOG = get_logger($OWL::Base::LOGGER_NAME)) && (return)
l!l&&r!l&&!r
000
$OWL::Base::LOG = get_logger($OWL::Base::LOGGER_NAME) and return
!ll
00
MC/DC0%0/2
$@ || ($OWL::Base::LOG = get_logger($OWL::Base::LOGGER_NAME)) && (return)
$@ $OWL::Base::LOG = get_logger($OWL::Base::LOGGER_NAME)
Truth table0%0/3
A || B
A= $@B= $OWL::Base::LOG = get_logger($OWL::Base::LOGGER_NAME)
ABresult
1X1
011
000
62 0 print STDERR
63 "Problem with configuration file '$OWLCFG::LOG_CONFIG': $@\n";
64 }
65
66 # configuration for logging not found; make some easy logging
67 4 my $logfile = $OWLCFG::LOG_FILE;
68 4 ▶ my $loglevel = $OWLCFG::LOG_LEVEL || $INFO;
Condition33%1/3
$OWLCFG::LOG_LEVEL || $OWL::Base::INFO
l!l&&r!l&&!r
040
MC/DC0%0/2
$OWLCFG::LOG_LEVEL || $OWL::Base::INFO
$OWLCFG::LOG_LEVEL $OWL::Base::INFO
Truth table33%1/3
A || B
A= $OWLCFG::LOG_LEVELB= $OWL::Base::INFO
ABresult
1X1
011
000
69 4 ▶ my $pattern = $OWLCFG::LOG_PATTERN
Condition50%1/2
$OWLCFG::LOG_PATTERN || '%d (%r) %p> [%x] %F{1}:%L - %m%n'
l!l
04
MC/DC0%0/1
$OWLCFG::LOG_PATTERN || '%d (%r) %p> [%x] %F{1}:%L - %m%n'
$OWLCFG::LOG_PATTERN
Truth table50%1/2
A
A= $OWLCFG::LOG_PATTERN
Aresult
11
00
70 || '%d (%r) %p> [%x] %F{1}:%L - %m%n';
71 4 $LOG = get_logger($LOGGER_NAME);
72 4 $LOG->level( uc $loglevel );
73 4 ▶ my $appender =
Branch: 1 branch
BranchTrueFalseTotal
$logfile && !($logfile =~ /^stderr$/i) ? : 0 4 4
Condition33%1/3
$logfile and not $logfile =~ /^stderr$/i
!ll&&!rl&&r
400
MC/DC0%0/2
$logfile and not $logfile =~ /^stderr$/i
$logfile not $logfile =~ /^stderr$/i
Truth table33%1/3
A and not(B)
A= $logfileB= not $logfile =~ /^stderr$/i
ABresult
0X0
100
111
74 ( $logfile and $logfile !~ /^stderr$/i )
75 ? Log::Log4perl::Appender->new(
76 "Log::Log4perl::Appender::File",
77 name => 'Log',
78 filename => $logfile,
79 mode => 'append'
80 )
81 : Log::Log4perl::Appender->new( "Log::Log4perl::Appender::Screen",
82 name => 'Screen' );
83 4 $LOG->add_appender($appender);
84 4 my $layout = Log::Log4perl::Layout::PatternLayout->new($pattern);
85 4 $appender->layout($layout);
86 }
87 4 OWL::Base->init_logging;
88 }
89
90 #-----------------------------------------------------------------
91 # These methods are called by set/get methods of the sub-classes. If
92 # it comes here, it indicates that an attribute being get/set does not
93 # exist.
94 #-----------------------------------------------------------------
95 {
96 4 my %_allowed = ();
97
98 154 ▶ sub _accessible {
Subroutine _accessible: called CC=1 SCAR=0.0
99 154 my ( $self, $attr ) = @_;
100 154 exists $_allowed{$attr};
101 }
102
103 56 ▶ sub _attr_prop {
Subroutine _attr_prop: called CC=3 SCAR=12.5
104 56 my ( $self, $attr_name, $prop_name ) = @_;
105 56 my $attr = $_allowed{$attr_name};
106 56 ▶ return ref($attr) ? $attr->{$prop_name} : $attr if $attr;
Branch: 2 branches
BranchTrueFalseTotal
if $attr 0 56 56
ref $attr ? : 0 0 0
107 56 return undef;
108 }
109 }
110
111 #-----------------------------------------------------------------
112 # new
113 #-----------------------------------------------------------------
114 4 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
115 106 my ( $class, @args ) = @_;
116
117 # $LOG->debug ("NEW: $class - " . join (", ", @args)) if $LOG->is_debug;
118 # create an object
119 106 ▶ my $self = bless {}, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
01060
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
120
121 # initialize the object
122 106 $self->init();
123
124 # set all @args into this object with 'set' values
125 106 ▶ my (%args) = ( @args == 1 ? ( value => $args[0] ) : @args );
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 ? : 22 84 106
126 106 foreach my $key ( keys %args ) {
127 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
128 35 $self->$key( $args{$key} );
129 }
130
131 # done
132 106 return $self;
133 }
134
135 #-----------------------------------------------------------------
136 # init
137 #-----------------------------------------------------------------
138 112 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
139 112 my ($self) = shift;
140 }
141
142 #-----------------------------------------------------------------
143 # toString
144 #-----------------------------------------------------------------
145 0 ▶sub toString {
Subroutine toString: not called CC=2 SCAR=17.9
Pod documented
146 0 my ( $self, $something_else ) = @_;
147 0 require Data::Dumper;
148 0 ▶ if ($something_else) {
Branch: 1 branch
BranchTrueFalseTotal
if ($something_else) { } 0 0 0
149 0 return Data::Dumper->Dump( [$something_else], ['M'] );
150 } else {
151 0 return Data::Dumper->Dump( [$self], ['M'] );
152 }
153 }
154
155 #-----------------------------------------------------------------
156 # module_name_escape
157 #-----------------------------------------------------------------
158 52 ▶sub module_name_escape {
Subroutine module_name_escape: called CC=1 SCAR=0.0
Pod undocumented
159 52 my ( $self, $name ) = @_;
160 52 $name =~ tr/-/_/;
161 52 $name =~ s/~//g;
162 52 return $name;
163 }
164
165 #-----------------------------------------------------------------
166 # datatype2module
167 #-----------------------------------------------------------------
168 0 ▶sub datatype2module {
Subroutine datatype2module: not called CC=2 SCAR=17.9
Pod undocumented
169 0 my ( $self, $datatype_name ) = @_;
170 0 ▶ return undef unless $datatype_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $datatype_name 0 0 0
171
172 0 return $self->module_name_escape($datatype_name);
173 }
174
175 #-----------------------------------------------------------------
176 # oProperty2module
177 #-----------------------------------------------------------------
178 22 ▶sub oProperty2module {
Subroutine oProperty2module: called CC=2 SCAR=7.1
Pod undocumented
179 22 my ( $self, $datatype_name ) = @_;
180 22 ▶ return undef unless $datatype_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $datatype_name 0 22 22
181
182 22 return $self->module_name_escape($datatype_name);
183 }
184
185 #-----------------------------------------------------------------
186 # uri2package
187 #-----------------------------------------------------------------
188 64 ▶sub uri2package {
Subroutine uri2package: called CC=11 SCAR=24.0
Pod undocumented
189 64 my ( $self, $uri ) = @_;
190 64 ▶ return undef unless $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 64 64
191
192 # is $uri an LSID?
193 64 ▶ if ( lc($uri) =~ m/^urn\:lsid/gi ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($uri) =~ /^urn\:lsid/gi) 1 63 64
194 1 my $lsid = OWL::LSID->new( $uri );
195
196 # cheat a little ;-)
197 1 $uri =
198 'http://'
199 . $lsid->authority . '/'
200 . $lsid->namespace . '#'
201 . $lsid->object;
202 }
203 # strip off any '.owl' bits from the URI
204 64 $uri =~ s/\.owl//gi;
205 64 my $u1 = URI->new($uri);
206
207 # the domain from the uri
208 64 ▶ my $authority = $u1->authority || '';
Condition100%2/2
$u1->authority || ''
l!l
568
MC/DC100%1/1
$u1->authority || ''
$u1->authority
Truth table100%2/2
A
A= $u1->authority
Aresult
11
00
209
210 # convert . to ::
211 64 $authority =~ s/\./::/g;
212
213 # the thing after the # if it exists
214 64 ▶ my $frag = $u1->fragment || '';
Condition100%2/2
$u1->fragment || ''
l!l
3430
MC/DC100%1/1
$u1->fragment || ''
$u1->fragment
Truth table100%2/2
A
A= $u1->fragment
Aresult
11
00
215
216 # remove any leading / or #
217 64 $frag =~ s/^[\/#]*//g;
218 # remove any trailing
219 64 $frag =~ s/[\/#]*$//g;
220
221 # convert . to _
222 64 ▶ $frag =~ s/\./_/g if $frag;
Branch: 1 branch
BranchTrueFalseTotal
if $frag 34 30 64
223
224 # remove from frag ~
225 64 $frag =~ s/~//g;
226
227 # the path
228 64 ▶ my $path = $u1->path || '';
Condition50%1/2
$u1->path || ''
l!l
640
MC/DC0%0/1
$u1->path || ''
$u1->path
Truth table50%1/2
A
A= $u1->path
Aresult
11
00
229
230 # remove leading /
231 64 $path =~ s/^\///g;
232
233 # remove any trailing / or #
234 64 $path =~ s/[\/|#]*$//g;
235
236 # sub path with : to _
237 64 $path =~ s/:/_/g;
238
239 # remove from path ~
240 64 $path =~ s/~//g;
241
242 # replace any // with a single /; doesnt affect // in http://
243 64 $path =~ s/\/\//\//g;
244
245 # convert / and . to ::
246 64 $path =~ s/\/|\./::/g;
247
248 64 my $package = '';
249
250 # package name assuming that uri#foo
251 64 ▶ $package = "$authority\:\:$path\:\:$frag" if $frag and $frag ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $frag and $frag ne "" 34 30 64
Condition66%2/3
$frag and $frag ne ""
!ll&&!rl&&r
30034
MC/DC50%1/2
$frag and $frag ne ""
$frag $frag ne ""
Truth table66%2/3
A and B
A= $fragB= $frag ne ""
ABresult
0X0
100
111
252
253 # package name assuming uri/foo
254 64 ▶ $package = "$authority\:\:$path" if $package eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $package eq "" 30 34 64
255
256 # make sure that nothing funny happened ...
257 64 $package =~ s/^\:*//g;
258 64 $package =~ s/\:*$//g;
259 64 ▶ if ( $package =~ m/^genid/gi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /^genid/gi) 8 56 64
260 8 $package = "Blank::$package";
261 }
262 64 return $package;
263 }
264
265 #-----------------------------------------------------------------
266 # owlClass2module
267 #-----------------------------------------------------------------
268 30 ▶sub owlClass2module {
Subroutine owlClass2module: called CC=2 SCAR=7.1
Pod undocumented
269 30 my ( $self, $datatype_name ) = @_;
270 30 ▶ return undef unless $datatype_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $datatype_name 0 30 30
271 30 return $self->module_name_escape($datatype_name);
272 }
273
274 #-----------------------------------------------------------------
275 # escape_name
276 #-----------------------------------------------------------------
277 0 ▶sub escape_name {
Subroutine escape_name: not called CC=2 SCAR=17.9
Pod undocumented
278 0 my ( $self, $name ) = @_;
279 0 $name =~ s/\W/_/g;
280 0 ▶ return ( $name =~ /^\d/ ? "_$name" : $name );
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^\d/ ? : 0 0 0
281 }
282
283 #-----------------------------------------------------------------
284 #
285 # Error handling
286 #
287 #-----------------------------------------------------------------
288 4 my $DEFAULT_THROW_WITH_LOG = 0;
289 4 my $DEFAULT_THROW_WITH_STACK = 1;
290
291 #-----------------------------------------------------------------
292 # throw
293 #-----------------------------------------------------------------
294 0 ▶sub throw {
Subroutine throw: not called CC=8 SCAR=39.8
Pod documented
295 0 my ( $self, $msg ) = @_;
296 0 ▶ $msg .= "\n" unless $msg =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n$/ 0 0 0
297
298 # make an instance, if called as a class method
299 0 ▶ unless ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
300 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
301 0 $self = $self->new;
302 }
303
304 # add (optionally) stack trace
305 0 ▶ $msg ||= 'An error.';
Condition0%0/2
$msg ||= "An error."
l!l
00
MC/DC0%0/1
$msg ||= "An error."
$msg
Truth table0%0/2
A
A= $msg
Aresult
11
00
306 0 ▶ my $with_stack = (
Branch: 1 branch
BranchTrueFalseTotal
defined $self->enable_throw_with_stack ? : 0 0 0
307 defined $self->enable_throw_with_stack
308 ? $self->enable_throw_with_stack
309 : $DEFAULT_THROW_WITH_STACK
310 );
311 0 ▶ my $result = ( $with_stack ? $self->format_stack($msg) : $msg );
Branch: 1 branch
BranchTrueFalseTotal
$with_stack ? : 0 0 0
312
313 # die or log and die?
314 0 ▶ my $with_log = (
Branch: 1 branch
BranchTrueFalseTotal
defined $self->enable_throw_with_log ? : 0 0 0
315 defined $self->enable_throw_with_log
316 ? $self->enable_throw_with_log
317 : $DEFAULT_THROW_WITH_LOG
318 );
319 0 ▶ if ($with_log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_log) { } 0 0 0
320 0 $LOG->logdie($result);
321 } else {
322 0 die($result);
323 }
324 }
325
326 #-----------------------------------------------------------------
327 # Some throwing options
328 #
329 # These options are not set by using AUTOLOAD (as other regular
330 # attributes) because AUTOLOAD could raise exception and we would be
331 # in a deep..., well deep recursion.
332 #
333 # Default values are: NO enable_throw_with_log
334 # YES enable_throw_with_stack
335 # (but they are globally changeable by calling
336 # default_throw_with_log and default_throw_with_stack)
337 #
338 #-----------------------------------------------------------------
339 0 ▶sub enable_throw_with_log {
Subroutine enable_throw_with_log: not called CC=3 SCAR=24.8
Pod undocumented
340 0 my ( $self, $value ) = @_;
341 0 ▶ $self->{enable_throw_with_log} = ( $value ? 1 : 0 )
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 0 0 0
$value ? : 0 0 0
342 if ( defined $value );
343 0 return $self->{enable_throw_with_log};
344 }
345
346 0 ▶sub default_throw_with_log {
Subroutine default_throw_with_log: not called CC=3 SCAR=24.8
Pod undocumented
347 0 my ( $self, $value ) = @_;
348 0 ▶ $DEFAULT_THROW_WITH_LOG = ( $value ? 1 : 0 )
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 0 0 0
$value ? : 0 0 0
349 if defined $value;
350 0 return $DEFAULT_THROW_WITH_LOG;
351 }
352
353 0 ▶sub enable_throw_with_stack {
Subroutine enable_throw_with_stack: not called CC=3 SCAR=24.8
Pod undocumented
354 0 my ( $self, $value ) = @_;
355 0 ▶ $self->{enable_throw_with_stack} = ( $value ? 1 : 0 )
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 0 0 0
$value ? : 0 0 0
356 if defined $value;
357 0 return $self->{enable_throw_with_stack};
358 }
359
360 0 ▶sub default_throw_with_stack {
Subroutine default_throw_with_stack: not called CC=3 SCAR=24.8
Pod undocumented
361 0 my ( $self, $value ) = @_;
362 0 ▶ $DEFAULT_THROW_WITH_STACK = ( $value ? 1 : 0 )
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 0 0 0
$value ? : 0 0 0
363 if defined $value;
364 0 return $DEFAULT_THROW_WITH_STACK;
365 }
366
367 #-----------------------------------------------------------------
368 # format_stack
369 #-----------------------------------------------------------------
370 0 ▶sub format_stack {
Subroutine format_stack: not called CC=2 SCAR=17.9
Pod documented
371 0 my ( $self, $msg ) = @_;
372 0 my $stack = $self->_reformat_stacktrace($msg);
373 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
374 0 my $title = "------------- EXCEPTION: $class -------------";
375 0 my $footer = "\n" . '-' x CORE::length($title);
376 0 return "\n$title\nMSG: $msg\n" . $stack . $footer . "\n";
377 }
378
379 #-----------------------------------------------------------------
380 # _reformat_stacktrace
381 # Taken from bioperl.
382 #
383 # Takes one argument - an error message. It uses it to remove its
384 # repeated occurences from each line (not to print it).
385 #
386 # Reformatting of the stack:
387 # 1. Shift the file:line data in line i to line i+1.
388 # 2. change xxx::__ANON__() to "try{} block"
389 # 3. skip the "require" and "Error::subs::try" stack entries (boring)
390 # This means that the first line in the stack won't have
391 # any file:line data.
392 #-----------------------------------------------------------------
393 0 ▶sub _reformat_stacktrace {
Subroutine _reformat_stacktrace: not called CC=9 SCAR=45.0
394 0 my ( $self, $msg ) = @_;
395 0 my $stack = Carp->longmess;
396 0 $stack =~ s/\Q$msg//;
397 0 my @stack = split( /\n/, $stack );
398 0 my @new_stack = ();
399 0 my ( $method, $file, $linenum, $prev_file, $prev_linenum );
400 0 my $stack_count = 0;
401 0 foreach my $i ( 0 .. $#stack ) {
402
403 0 ▶ if ( ( $stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/ or $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/) { } 0 0 0
Condition0%0/3
$stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/ or $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/
l!l&&r!l&&!r
000
MC/DC0%0/2
$stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/ or $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/
$stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/ $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/
Truth table0%0/3
A or B
A= $stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/B= $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/
ABresult
1X1
011
000
404 || ( $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/ ) )
405 {
406 0 ( $method, $file, $linenum ) = ( $1, $2, $3 );
407 0 $stack_count++;
408 } else {
409 0 next;
410 }
411 0 ▶ if ( $stack_count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack_count == 1) 0 0 0
412 0 push @new_stack, "STACK: $method";
413 0 ( $prev_file, $prev_linenum ) = ( $file, $linenum );
414 0 next;
415 }
416 0 ▶ if ( $method =~ /__ANON__/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /__ANON__/) 0 0 0
417 0 $method = "try{} block";
418 }
419 0 ▶ if ( ( $method =~ /^require/ and $file =~ /Error\.pm/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^require/ and $file =~ /Error\.pm/ or $method =~ /^Error::subs::try/) 0 0 0
Condition0%0/6
$method =~ /^require/ and $file =~ /Error\.pm/ or $method =~ /^Error::subs::try/
l!l&&r!l&&!r
000
$method =~ /^require/ and $file =~ /Error\.pm/
!ll&&!rl&&r
000
MC/DC0%0/3
$method =~ /^require/ and $file =~ /Error\.pm/ or $method =~ /^Error::subs::try/
$method =~ /^require/ $file =~ /Error\.pm/ $method =~ /^Error::subs::try/
Truth table0%0/5
A and B or C
A= $method =~ /^require/B= $file =~ /Error\.pm/C= $method =~ /^Error::subs::try/
ABCresult
11X1
0X11
1011
0X00
1000
420 || ( $method =~ /^Error::subs::try/ ) )
421 {
422 0 last;
423 }
424 0 push @new_stack, "STACK: $method $prev_file:$prev_linenum";
425 0 ( $prev_file, $prev_linenum ) = ( $file, $linenum );
426 }
427 0 push @new_stack, "STACK: $prev_file:$prev_linenum";
428 0 return join "\n", @new_stack;
429 }
430
431 #-----------------------------------------------------------------
432 # Set methods test whether incoming value is of a correct type.
433 # Here we return message explaining that it isn't.
434 #-----------------------------------------------------------------
435 0 ▶sub _wrong_type_msg {
Subroutine _wrong_type_msg: not called CC=2 SCAR=17.9
436 0 my ( $self, $given_type_or_value, $expected_type, $method ) = @_;
437 0 my $msg = 'In method ';
438 0 ▶ if ( defined $method ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $method) { } 0 0 0
439 0 $msg .= $method;
440 } else {
441 0 $msg .= ( caller(1) )[3];
442 }
443 return (
444 0 "$msg: Trying to set '$given_type_or_value' but '$expected_type' is expected."
445 );
446 }
447
448 #-----------------------------------------------------------------
449 # Set methods test whether incoming value exceeds the cardinality
450 # constraints for the OWL Class.
451 # Here we return message explaining that it has exceeded.
452 #-----------------------------------------------------------------
453 0 ▶sub _bad_cardinality_msg {
Subroutine _bad_cardinality_msg: not called CC=2 SCAR=17.9
454 0 my ( $self, $expected_size, $method ) = @_;
455 0 my $msg = 'In method ';
456 0 ▶ if ( defined $method ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $method) { } 0 0 0
457 0 $msg .= $method;
458 } else {
459 0 $msg .= ( caller(1) )[3];
460 }
461 return (
462 0 "$msg: Trying to add property but we have exceeded cardinality constraints of '$expected_size'."
463 );
464 }
465
466 #-----------------------------------------------------------------
467 # Deal with 'set', 'get' and 'add_' methods.
468 #-----------------------------------------------------------------
469 139 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=8 SCAR=21.2
470 139 my ( $self, @new_values ) = @_;
471 139 my $ref_sub;
472 139 ▶ if ( $AUTOLOAD =~ /.*::(\w+)/ && $self->_accessible("$1") ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($OWL::Base::AUTOLOAD =~ /.*::(\w+)/ and $self->_accessible("$1")) { } 117 22 139
elsif ($OWL::Base::AUTOLOAD =~ /.*::add_(\w+)/ and $self->_accessible("$1")) { } 22 0 22
Condition66%2/3
$OWL::Base::AUTOLOAD =~ /.*::(\w+)/ and $self->_accessible("$1")
!ll&&!rl&&r
022117
MC/DC50%1/2
$OWL::Base::AUTOLOAD =~ /.*::(\w+)/ and $self->_accessible("$1")
$OWL::Base::AUTOLOAD =~ /.*::(\w+)/ $self->_accessible("$1")
Truth table66%2/3
A and B
A= $OWL::Base::AUTOLOAD =~ /.*::(\w+)/B= $self->_accessible("$1")
ABresult
0X0
100
111
473
474 # get/set method
475 117 my $attr_name = "$1";
476 117 ▶ my $attr_type = $self->_attr_prop( $attr_name, TYPE ) || STRING;
Condition100%2/2
$self->_attr_prop($attr_name, 'type') || 'string'
l!l
10314
MC/DC100%1/1
$self->_attr_prop($attr_name, 'type') || 'string'
$self->_attr_prop($attr_name, 'type')
Truth table100%2/2
A
A= $self->_attr_prop($attr_name, 'type')
Aresult
11
00
477 117 my $attr_post = $self->_attr_prop( $attr_name, POST );
478 117 my $attr_is_array = $self->_attr_prop( $attr_name, ISARRAY );
479 117 my $attr_readonly = $self->_attr_prop( $attr_name, READONLY );
480 2025 ▶ $ref_sub = sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
481 2025 local *__ANON__ = "__ANON__$attr_name" . "_" . ref($self);
482 2025 my ( $this, @values ) = @_;
483 2025 ▶ return $this->_getter($attr_name) unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 1720 305 2025
484 305 ▶ $self->throw("Sorry, the attribute '$attr_name' is read-only.")
Branch: 1 branch
BranchTrueFalseTotal
if $attr_readonly 0 305 305
485 if $attr_readonly;
486
487 # here we continue with 'set' method:
488 305 ▶ if ($attr_is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_is_array) { } 8 297 305
489 my @result =
490 0 ▶ ( ref( $values[0] ) eq 'ARRAY' ? @{ $values[0] } : @values );
Branch: 1 branch
BranchTrueFalseTotal
ref $values[0] eq 'ARRAY' ? : 0 8 8
491 8 foreach my $value (@result) {
492 8 $value = $this->check_type( $AUTOLOAD, $attr_type, $value );
493 }
494 8 $this->_setter( $attr_name, $attr_type, \@result );
495 } else {
496 # no need to check cardinality contraints here ... sets one value here ...
497 297 $this->_setter(
498 $attr_name,
499 $attr_type,
500 $this->check_type(
501 $AUTOLOAD, $attr_type, @values
502 )
503 );
504 }
505
506 # call post-procesing (if defined)
507 305 ▶ $this->$attr_post( $this->{$attr_name} ) if $attr_post;
Branch: 1 branch
BranchTrueFalseTotal
if $attr_post 87 218 305
508 305 return $this->{$attr_name};
509 117 };
510 ▶ } elsif ( $AUTOLOAD =~ /.*::add_(\w+)/ && $self->_accessible("$1") ) {
Condition33%1/3
$OWL::Base::AUTOLOAD =~ /.*::add_(\w+)/ and $self->_accessible("$1")
!ll&&!rl&&r
0022
MC/DC0%0/2
$OWL::Base::AUTOLOAD =~ /.*::add_(\w+)/ and $self->_accessible("$1")
$OWL::Base::AUTOLOAD =~ /.*::add_(\w+)/ $self->_accessible("$1")
Truth table33%1/3
A and B
A= $OWL::Base::AUTOLOAD =~ /.*::add_(\w+)/B= $self->_accessible("$1")
ABresult
0X0
100
111
511
512 # add_XXXX method
513 22 my $attr_name = "$1";
514 22 my $attr_post = $self->_attr_prop( $attr_name, POST );
515 22 ▶ if ( $self->_attr_prop( $attr_name, ISARRAY ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_attr_prop($attr_name, 'is_array')) { } 22 0 22
516 22 ▶ my $attr_type = $self->_attr_prop( $attr_name, TYPE ) || STRING;
Condition50%1/2
$self->_attr_prop($attr_name, 'type') || 'string'
l!l
220
MC/DC0%0/1
$self->_attr_prop($attr_name, 'type') || 'string'
$self->_attr_prop($attr_name, 'type')
Truth table50%1/2
A
A= $self->_attr_prop($attr_name, 'type')
Aresult
11
00
517 98 ▶ $ref_sub = sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
518 98 local *__ANON__ = "__ANON__$attr_name" . "_" . ref($self);
519 98 my ( $this, @values ) = @_;
520 98 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 98 0 98
521 my @result = (
522 ref( $values[0] ) eq 'ARRAY'
523 0 ▶ ? @{ $values[0] }
Branch: 1 branch
BranchTrueFalseTotal
ref $values[0] eq 'ARRAY' ? : 0 98 98
524 : @values );
525 # TODO check if we have cardinality constraints ( size of @results + size of existing values)
526 98 foreach my $value (@result) {
527 98 $value =
528 $this->check_type( $AUTOLOAD, $attr_type, $value );
529 }
530 98 $this->_adder( $attr_name, $attr_type, @result );
531 }
532
533 # call post-procesing (if defined)
534 98 ▶ $this->$attr_post( $this->{$attr_name} ) if $attr_post;
Branch: 1 branch
BranchTrueFalseTotal
if $attr_post 32 66 98
535 98 return $this;
536 }
537 22 } else {
538 0 $self->throw(
539 "Method '$AUTOLOAD' is allowed only for array-type attributes."
540 );
541 }
542 } else {
543 0 $self->throw("No such method: $AUTOLOAD");
544 }
545 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
546 139 *{$AUTOLOAD} = $ref_sub;
547 4 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
548 139 return $ref_sub->( $self, @new_values );
549 }
550
551 #-----------------------------------------------------------------
552 # The low level get/set methods. They are called from AUTOLOAD, and
553 # they are separated here so they can be overriten - as they are in
554 # the service skeletons, for example. Also, there may be situation
555 # that one can call them if other features (such as type checking) are
556 # not required.
557 #-----------------------------------------------------------------
558 1720 ▶sub _getter {
Subroutine _getter: called CC=1 SCAR=0.0
559 1720 my ( $self, $attr_name ) = @_;
560 1720 return $self->{$attr_name};
561 }
562
563 305 ▶sub _setter {
Subroutine _setter: called CC=1 SCAR=0.0
564 305 my ( $self, $attr_name, $attr_type, $value ) = @_;
565 305 $self->{$attr_name} = $value;
566 }
567
568 98 ▶sub _adder {
Subroutine _adder: called CC=1 SCAR=0.0
569 98 my ( $self, $attr_name, $attr_type, @values ) = @_;
570 98 push( @{ $self->{$attr_name} }, @values );
571 }
572
573 #-----------------------------------------------------------------
574 # Keep it here! The reason is the existence of AUTOLOAD...
575 #-----------------------------------------------------------------
576 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
577 }
578
579 #-----------------------------------------------------------------
580 #
581 # Check type of @value against $expected_type. Return checked $value
582 # (perhaps trimmed, or otherwise corrected - e.g. wrapped in an
583 # appropriate object), or undef if the $value is of a wrong type.
584 #
585 #-----------------------------------------------------------------
586 403 ▶sub check_type {
Subroutine check_type: called CC=17 SCAR=43.9
Pod undocumented
587 403 my ( $self, $name, $expected_type, @values ) = @_;
588 403 my $value = $values[0];
589
590 # first process cases when an expected type is a simple string,
591 # integer etc. (not OWL::Data::String etc.) - e.g. when an ID
592 # attribute is being set
593 403 ▶ if ( $expected_type eq STRING ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($expected_type eq 'string') { } 291 112 403
elsif ($expected_type eq 'integer') { } 0 112 112
elsif ($expected_type eq 'float') { } 0 112 112
elsif ($expected_type eq 'boolean') { } 40 72 112
elsif ($expected_type eq 'datetime') { } 5 67 72
594 291 return $value;
595 } elsif ( $expected_type eq INTEGER ) {
596 0 ▶ $self->throw( $self->_wrong_type_msg( $value, $expected_type, $name ) )
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\s*[+-]?\s*\d+\s*$/ 0 0 0
597 unless $value =~ m/^\s*[+-]?\s*\d+\s*$/;
598 0 $value =~ s/\s//g;
599 0 return $value;
600 } elsif ( $expected_type eq FLOAT ) {
601 0 ▶ $self->throw( $self->_wrong_type_msg( $value, $expected_type, $name ) )
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\s*[+-]?\s*(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?\s*$/ 0 0 0
602 unless $value =~
603 m/^\s*[+-]?\s*(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?\s*$/;
604 0 $value =~ s/\s//g;
605 0 return $value;
606 } elsif ( $expected_type eq BOOLEAN ) {
607 40 ▶ return ( $value =~ /true|\+|1|yes|ano/ ? '1' : '0' );
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /true|\+|1|yes|ano/ ? : 11 29 40
608 } elsif ( $expected_type eq DATETIME ) {
609 5 my $iso;
610 5 eval {
611 5 $iso = (
612 HTTP::Date::time2isoz(
613 HTTP::Date::str2time( HTTP::Date::parse_date($value) )
614 )
615 );
616 };
617 5 ▶ $self->throw( $self->_wrong_type_msg( $value, 'ISO-8601', $name ) )
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
618 if $@;
619 5 return $iso; ### $iso =~ s/ /T/; ??? TBD
620 } else {
621
622 # Then process cases when the expected type is a name of a
623 # real object (e.g. OWL::Data::String); for these cases the
624 # $value[0] can be already such object - in which case nothing
625 # to be done; or $value[0] can be HASH, or @values can be a
626 # list of name/value pairs, in which case a new object (of
627 # type $expected_type) has to be created and initialized by
628 # @values; and, still in the latter case, if the @values has
629 # just one element (XX), this element is considered a 'value':
630 # it is treated as a a hash {value => XX}.
631 67 ▶ return $value if UNIVERSAL::isa( $value, $expected_type );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($value, $expected_type) 67 0 67
632 0 ▶ $value = { value => $value }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value or @values > 1 0 0 0
Condition0%0/3
ref $value or @values > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $value or @values > 1
ref $value @values > 1
Truth table0%0/3
A or B
A= ref $valueB= @values > 1
ABresult
1X1
011
000
633 unless ref($value) || @values > 1;
634 0 my ($value_ref_type) = ref($value);
635 0 ▶ if ( $value_ref_type eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value_ref_type eq 'HASH') { } 0 0 0
elsif ($value_ref_type eq 'ARRAY') { } 0 0 0
elsif ($value_ref_type) { } 0 0 0
636
637 # e.g. $sequence->Length ( { value => 12, id => 'IR64'} )
638 0 return $self->create_member( $name, $expected_type, %$value );
639 } elsif ( $value_ref_type eq 'ARRAY' ) {
640
641 # e.g. $sequence->Length ( [ value => 12, id => 'IR64'] )
642 0 return $self->create_member( $name, $expected_type, @$value );
643 } elsif ($value_ref_type) {
644
645 # e.g. $sequence->Length ( new OWL::Data::Integer ( value => 12) )
646 0 ▶ $self->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($value, $expected_type) 0 0 0
647 $self->_wrong_type_msg(
648 $value_ref_type, $expected_type, $name
649 )
650 ) unless UNIVERSAL::isa( $value, $expected_type );
651 0 return $value;
652 } else {
653
654 # e.g. $sequence->Length (value => 12, id => 'IR64')
655 0 return $self->create_member( $name, $expected_type, @values );
656 }
657 }
658 }
659
660 #-----------------------------------------------------------------
661 #
662 #-----------------------------------------------------------------
663 0 ▶sub create_member {
Subroutine create_member: not called CC=2 SCAR=17.9
Pod undocumented
664 0 my ( $self, $name, $expected_type, @values ) = @_;
665 0 eval "require $expected_type";
666 0 ▶ $self->throw( $self->_wrong_type_msg( $values[0], $expected_type, $name ) )
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
667 if $@;
668 0 return "$expected_type"->new(@values);
669 }
670
671 #-----------------------------------------------------------------
672 # as_string (an "" operator overloading)
673 #-----------------------------------------------------------------
674 4 my $DUMPER;
675
676 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
677 4 ▶ use Dumpvalue;
Subroutine BEGIN: called CC=1 SCAR=0.0
678 4 ▶ use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
679 4 $DUMPER = Dumpvalue->new();
680
681 # $DUMPER->set (veryCompact => 1);
682 }
683
684 22 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
685 22 my $self = shift;
686 22 my $dump_str;
687 22 my $io = IO::String->new( \$dump_str );
688 22 my $oio = select($io);
689 22 $DUMPER->dumpValue( \$self );
690 22 select($oio);
691 22 return $dump_str;
692 }
693 4 1;
694 __END__
« lib/OWL2Perl.pm ↑ Summary lib/OWL/Utils.pm »