StateML/Object.pm

filter: Statementstmt 77.7% 56 / 72 Branchbran 53.1% 17 / 32 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 84.6% 11 / 13 Podpod 100% 9 / 9 totaltotal 69.1% 94 / 136 CC 21 SCAR 27.0
CC
21
Cov
67%
attribute
56.0
name
12.0
id
5.0
CRAP 14.9
« StateML/Machine.pm ↑ Summary StateML/Arc.pm »
1 package StateML::Object ;
2  
3 4 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 METHODS
7  
8 =over
9  
10 =cut
11  
12 =item new
13  
14 $self->new( attr => "val", ... ) ;
15  
16 Create a new object.
17  
18 =cut
19  
20 16 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
21 16 my $proto = shift ;
22 16 ▶ my $class = ref $proto || $proto ;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0160
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
23 16 my $self = bless {
24 @_,
25 }, $class ;
26 16 return $self ;
27 }
28  
29  
30 =item type
31  
32 $obj->type ;
33  
34 Returns "MACHINE", "STATE", "ARC", "EVENT".
35  
36 =cut
37  
38  
39 11 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
40 11 my $self = shift ;
41  
42 11 my $type = uc ref $self ;
43 11 $type =~ s/.*:// ;
44 11 return $type ;
45 }
46  
47  
48 1 ▶sub _generate_id {
Subroutine _generate_id: called CC=1 SCAR=0.0
49 1 my $self = shift ;
50 1 my $class = ref $self ;
51 4 ▶ no strict 'refs' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 $self->id( $self->type . " " . ++${"$class\::ID_COUNTER"} ) ;
53 }
54  
55  
56 =item class_ids
57  
58 A list of class ids this class inherits from.
59  
60 To set the list, this method takes a list of strings, each of which should
61 be one or more class_ids separated by a comma. Trailing commas are allowed.
62  
63 =cut
64  
65 10 ▶sub class_ids {
Subroutine class_ids: called CC=3 SCAR=11.0
Pod documented
66 10 my $self = shift;
67 10 ▶ $self->{CLASS_IDS} = [ grep length, map split( /,/ ), @_ ] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 9 10
68 10 ▶ return unless $self->{CLASS_IDS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"CLASS_IDS"} 6 4 10
69 4 return @{$self->{CLASS_IDS}};
70 }
71  
72  
73 =item id
74  
75 $obj->id( "foo" ) ;
76 $id = $obj->id ;
77  
78 Returns the id string. Creates a new unique one if not defined.
79  
80 The id "" is not valid.
81  
82 =cut
83  
84 32 ▶sub id {
Subroutine id: called CC=5 SCAR=16.1
Pod documented
85 32 my $self = shift ;
86 32 ▶ $self->{ID} = shift if @_ ;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 31 32
87 32 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 1 31 32
88 31 ▶ $self->_generate_id unless defined $self->{ID} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ID"} 1 30 31
89 31 ▶ confess "invalid id ''" unless length $self->{ID};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"ID"} 0 31 31
90 31 return $self->{ID} ;
91 }
92  
93  
94 =item name
95  
96 $obj->name( "foo" ) ;
97 $id = $obj->name ;
98  
99 Returns the name string or the id string if the name is unset.
100 The name "" is valid.
101  
102 The name is used for human readable documentation purposes, the id is
103 used for internal linking and for generated code. The name is merely
104 a comment with semantic value.
105  
106 =cut
107  
108 0 ▶sub name {
Subroutine name: not called CC=3 SCAR=24.8
Pod documented
109 0 my $self = shift ;
110 0 ▶ $self->{NAME} = shift if @_ ;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
111 return defined $self->{NAME}
112 ? $self->{NAME}
113 0 ▶ : $self->id;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'NAME'} ? : 0 0 0
114 }
115  
116  
117 =item enum_id
118  
119 $enum_id = $obj->enum_id ;
120  
121 Returns the same string as $obj->id, but cleaned up sufficiently to be
122 used in a C "enum" statement.
123  
124 =cut
125  
126 6 ▶sub enum_id {
Subroutine enum_id: called CC=3 SCAR=11.0
Pod documented
127 6 my $self = shift ;
128 6 my $oid = $self->id ;
129 6 for ( $oid ) {
130 6 ▶ $_ = "_$_" if /^[^a-zA-Z_]/ ;
Branch: 1 branch
BranchTrueFalseTotal
if /^[^a-zA-Z_]/ 6 0 6
131 6 s/[^a-zA-Z0-9_]+/_/g ;
132 }
133 6 return $oid ;
134 }
135  
136  
137 =item machine
138  
139 $obj->machine( $machine ) ;
140 $m = $obj->machine ;
141  
142 Sets/gets the StateML::Machine object this object is a part of.
143  
144 =cut
145  
146 22 ▶sub machine {
Subroutine machine: called CC=2 SCAR=6.9
Pod documented
147 22 my $self = shift ;
148 22 ▶ $self->{MACHINE} = shift if @_ ;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 9 22
149 22 return $self->{MACHINE} ;
150 }
151  
152  
153 =item attribute
154  
155 $attr = $obj->attribute( $namespace_uri, $name ) ;
156  
157 Returns the attribute identified by the XML namespace prefix and name.
158  
159 $attr = $obj->attribute( $namespace_uri, $name, $value ) ;
160  
161 The namespace_uri is the XML concept of namespace identifier.
162  
163 Checks up the class hierarchy if an attribute is not defined.
164  
165 =cut
166  
167 #If there is no attribute named "{$namespace_uri}$name", returns
168 #an attribute named $name if that exists. Returns undef otherwise.
169  
170 0 ▶sub attribute {
Subroutine attribute: not called CC=7 SCAR=40.3
Pod documented
171 0 my $self = shift ;
172 0 my ( $namespace_uri, $name, $value ) = @_ ;
173  
174 0 my $key = "{$namespace_uri}$name" ;
175 0 my $a = $self->{ATTRS} ;
176 0 ▶ if ( @_ == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) 0 0 0
177 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
178 0 $a->{$key} = $value ;
179 }
180 else {
181 0 delete $a->{$key} ;
182 }
183 }
184 0 ▶ return $a->{$key} if exists $a->{$key} && defined $a->{$key} ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $a->{$key} and defined $a->{$key} 0 0 0
Condition0%0/3
exists $a->{$key} and defined $a->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $a->{$key} and defined $a->{$key}
exists $a->{$key} defined $a->{$key}
Truth table0%0/3
A and B
A= exists $a->{$key}B= defined $a->{$key}
ABresult
0X0
100
111
185  
186 0 for ( $self->class_ids ) {
187 0 my $attr = $self->machine->object_by_id( $_ )->attribute( @_[0,1] );
188 0 ▶ return $attr if defined $attr;
Branch: 1 branch
BranchTrueFalseTotal
if defined $attr 0 0 0
189 }
190  
191 0 return undef ;
192 }
193  
194 =item attributes
195  
196 %attrs = $obj->attributes( $namespace_uri );
197 %attrs = $obj->attributes;
198  
199 If a namespace URI is passed, returns attributes in that namespace and
200 no others with the localname of the attribute as the key (ie without the
201 namespace URI).
202  
203 If no namespace URI is passed, returns all attributes with the URI encoded
204 in jclark notation.
205  
206 Compiles a list of all attributes from the class hierarchy.
207  
208 =cut
209  
210 9 ▶sub attributes {
Subroutine attributes: called CC=3 SCAR=11.0
Pod documented
211 9 my $self = shift ;
212 9 my ( $namespace_uri ) = @_ ;
213 9 my $a = $self->{ATTRS} ;
214  
215 my %inherited_attrs = map {
216 9 my $base_class = $self->machine->object_by_id( $_ );
217 confess "base class $_ not found ", $self->{LOCATION}
218 3 ▶ unless $base_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $base_class 0 3 3
219 3 $base_class->attributes( @_ );
220 } $self->class_ids;
221  
222 9 ▶ if ( ! defined $namespace_uri ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $namespace_uri) 3 6 9
223 3 return ( %inherited_attrs, %$a );
224 }
225  
226 6 $namespace_uri = "{$namespace_uri}" ;
227 6 my $l = length $namespace_uri;
228 return (
229 %inherited_attrs,
230 (
231 map {
232 6 my $name = substr( $_, $l ) ;
233 3 ( $name => $a->{$_} ) ;
234 } grep 0 == index( $_, $namespace_uri ), keys %$a
235 ),
236 );
237 }
238  
239 =back
240  
241 =cut
242  
243  
244 4 1 ;
« StateML/Machine.pm ↑ Summary StateML/Arc.pm »