blib/lib/UML/Sequence.pm

filter: Statementstmt 77.1% 115 / 149 Branchbran 50.0% 26 / 52 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 8 / 12 Podpod 0.0% 0 / 3 totaltotal 66.8% 151 / 226 CC 35 SCAR 29.7
CC
35
Cov
69%
_build_xml_sequence
33.8
_print_tree
30.0
_build_methods_hash
6.0
CRAP 19.5
« blib/lib/Devel/CallSeq.pm ↑ Summary blib/lib/UML/Sequence/SimpleSeq.pm »
1 =head1 NAME
2  
3 UML::Sequence - Render UML sequence diagrams, often by running the code.
4  
5 =head1 SYNOPSIS
6  
7 use UML::Sequence;
8  
9 my $tree = UML::Sequence->new(\@methods, \@outline, \&parse_method);
10 print $tree->build_xml_sequence('Title');
11  
12 =head1 DESCRIPTION
13  
14 To use this package, or see how to use it, see L<genericseq.pl> and
15 L<seq2svg.pl>.
16  
17 This class helps produce UML sequence diagrams. build_xml_sequence
18 returns a string (suitable for printing to a file) which the seq2svg.pl
19 script converts into svg.
20  
21 To control the appearance of the sequence diagram, pass to the constructor:
22  
23 1 a reference to an array containing the signatures you want to hear about
24 or a reference to a hash whose keys are the signatures you want
25 2 a reference to an array containing the lines in the outline of calls
26 3 a reference to a sub which takes signatures and returns class and method
27 names
28  
29 To build the array references and supply the code reference consult
30 UML::Sequence::SimpleSeq, UML::Sequence::JavaSeq, or UML::Sequence::PerlSeq.
31 To see one way to call these look in the supplied genericseq script.
32  
33 =head2 EXPORT
34  
35 None, this module is object oriented.
36  
37 =cut
38  
39 package UML::Sequence;
40  
41 4 require 5.005_62;
42 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 4 our $VERSION = '0.08';
46  
47 4 ▶use UML::Sequence::Activation;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 4 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
50 4 my $class = shift; # standard
51 4 my $methods_to_include = shift; # array or hash of methods you want to see
52 4 my $input = shift; # the outline of calls
53 4 my $parse_signature = shift; # code ref which returns class and method
54 4 my $grab_methods = shift; # coderef to return method name
55  
56 4 my $methods_hash;
57 4 ▶ if (ref($methods_to_include) =~ /ARRAY/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($methods_to_include) =~ /ARRAY/) { } 0 4 4
58 0 $methods_hash = _build_methods_hash($methods_to_include);
59 }
60 else {
61 4 $methods_hash = $methods_to_include;
62 }
63  
64 4 my $stack = [];
65 4 my $root = {
66 LEVEL => -1,
67 DATA => [],
68 NAME => scalar &$parse_signature($input->[0]),
69 INPUT => $input->[0],
70 DISCARD => 0,
71 };
72  
73 4 shift @$input;
74  
75 4 my $self = {};
76 4 $self->{TREE} = $root;
77 4 $self->{STACK} = $stack;
78 4 $self->{INCLUDE} = $methods_hash;
79 4 $self->{SIGPARSE} = $parse_signature;
80 4 $self->{GRABMETHODS} = $grab_methods;
81 4 bless $self, $class;
82  
83 4 push @$stack, $root;
84  
85 # 2.
86 4 foreach (@$input) {
87 52 my $input_line = $_;
88 52 my $depth;
89  
90 52 ▶ $depth = ($input_line =~ s/^(\s+)//) ? length($1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$input_line =~ s/^(\s+)// ? : 52 0 52
91 52 $self->_update_stack($input_line, $depth);
92 }
93 4 return $self;
94 }
95  
96 0 ▶sub _grab_outline_text {
Subroutine _grab_outline_text: not called CC=1 SCAR=6.9
97 # 1a.
98 0 _run_dprof(@_);
99  
100 # 1b.
101 0 my $input = _read_dprofpp();
102 # The next line uses the sample data in __DATA__ see the comment there
103 # my $input = _read_sample();
104 0 return $input;
105 }
106  
107 0 ▶sub _build_methods_hash {
Subroutine _build_methods_hash: not called CC=2 SCAR=17.9
108 0 my $methods_list = shift;
109 0 my %methods_hash;
110  
111 0 foreach my $method (@$methods_list) {
112 0 $methods_hash{$method}++;
113 }
114 0 return \%methods_hash;
115 }
116  
117 #sub _read_sample {
118 # my @retval = map { chomp $_; $_; } <DATA>;
119 # return \@retval;
120 #}
121  
122 52 ▶sub _update_stack {
Subroutine _update_stack: called CC=5 SCAR=16.3
123 52 my $self = shift;
124 52 my $method = shift;
125 52 my $level = shift;
126  
127 52 my $new_node = {
128 LEVEL => $level,
129 DATA => [],
130 NAME => $method,
131 #
132 # DAA save original input line, which may contain
133 # extra stuff
134 #
135 INPUT => $method,
136 # DISCARD => 0,
137 };
138  
139 48 pop @{$self->{STACK}}
140 52 ▶ while ($level <= $self->{STACK}[-1]{LEVEL});
Branch: 1 branch
BranchTrueFalseTotal
$level <= $self->{"STACK"}[-1]{"LEVEL"} and do { pop @{$$self{"STACK"};} } 48 52 100
141  
142 52 $new_node->{DISCARD} = $self->{STACK}[-1]{DISCARD};
143 52 ▶ unless (defined($self->{INCLUDE}{$method})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"INCLUDE"}{$method}) 10 42 52
144 #
145 # the line may have magic, try to capture the extra stuff
146 #
147 10 my $methods = $self->{GRABMETHODS}->([ $method ]);
148  
149 10 my @methods;
150 10 ▶ if ( ref( $methods ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $methods eq 'ARRAY') { } 0 10 10
151 0 @methods = @{ $methods };
152 }
153 else {
154 10 @methods = keys %$methods;
155 }
156  
157 10 $method = shift @methods;
158 10 ▶ $new_node->{DISCARD} = ($method && $self->{INCLUDE}{$method});
Condition33%1/3
$method && $self->{'INCLUDE'}{$method}
!ll&&!rl&&r
1000
MC/DC0%0/2
$method && $self->{'INCLUDE'}{$method}
$method $self->{'INCLUDE'}{$method}
Truth table33%1/3
A && B
A= $methodB= $self->{'INCLUDE'}{$method}
ABresult
0X0
100
111
159 }
160  
161 52 push @{$self->{STACK}[-1]{DATA}}, $new_node;
162 52 push @{$self->{STACK}}, $new_node;
163  
164 }
165  
166 0 ▶sub print_tree {
Subroutine print_tree: not called CC=1 SCAR=6.9
Pod undocumented
167 0 my $self = shift;
168  
169 0 return _print_tree($self->{TREE}, "");
170 }
171  
172 0 ▶sub _print_tree {
Subroutine _print_tree: not called CC=5 SCAR=34.0
173 0 my $root = shift;
174 0 my $indent = shift;
175 0 my $retval;
176  
177 0 ▶ return unless defined $root; # recursion base
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 0 0 0
178 0 ▶ return if ($root->{DISCARD});
Branch: 1 branch
BranchTrueFalseTotal
if $root->{"DISCARD"} 0 0 0
179  
180 0 $retval = "$indent$root->{NAME}\n";
181  
182 0 foreach my $child (@{$root->{DATA}}) {
183 0 my $child_output = _print_tree($child, "$indent ");
184 0 ▶ $retval .= $child_output if $child_output;
Branch: 1 branch
BranchTrueFalseTotal
if $child_output 0 0 0
185 }
186 0 return $retval;
187 }
188  
189 1 ▶sub build_xml_sequence {
Subroutine build_xml_sequence: called CC=2 SCAR=7.0
Pod undocumented
190 1 my $self = shift;
191 1 my $title = shift;
192  
193 1 $self->{ARROW_NUM} = 0;
194 1 $self->{ARROW_LIST} = "<arrow_list>\n";
195  
196 1 $self->_build_xml_sequence($self->{TREE});
197 1 $self->{ARROW_LIST} .= "</arrow_list>\n";
198  
199 1 $self->_build_class_list();
200 1 ▶ if ($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) { } 1 0 1
201 1 return "<?xml version='1.0' ?>\n<sequence title='$title'>\n"
202 . "$self->{CLASS_LIST}\n"
203 . "$self->{ARROW_LIST}</sequence>\n";
204 }
205 else {
206 0 return "<?xml version='1.0' ?>\n<sequence>\n$self->{CLASS_LIST}\n"
207 . "$self->{ARROW_LIST}</sequence>\n";
208 }
209 }
210  
211 13 ▶sub _build_xml_sequence {
Subroutine _build_xml_sequence: called CC=20 SCAR=35.2
212 13 my $self = shift;
213 13 my $root = shift; # you must pass this in, $self->{TREE} never changes
214 13 my $hasreturn = shift;
215  
216 # recursion bases
217 13 ▶ return unless defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 0 13 13
218 13 ▶ return if $root->{DISCARD};
Branch: 1 branch
BranchTrueFalseTotal
if $root->{"DISCARD"} 0 13 13
219 13 my $root_call = $root->{NAME};
220 13 ▶ return unless defined $root_call;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root_call 0 13 13
221  
222 13 my $class = $self->{SIGPARSE}($root_call);
223 # put into to class list, if it isn't already there
224  
225 4 push @{$self->{CLASSES}}, $class
226 13 ▶ unless defined $self->{ACTIVATIONS}{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ACTIVATIONS"}{$class} 4 9 13
227  
228 # create activation and add it to the list for this class
229 13 my $activation = UML::Sequence::Activation->new();
230 13 $activation->starts($self->{ARROW_NUM});
231 my $offset = UML::Sequence::Activation
232 13 ->find_offset($self->{ACTIVATIONS}{$class});
233 13 $activation->offset($offset);
234  
235 13 push @{$self->{ACTIVATIONS}{$class}}, $activation;
236 13 my $asyncs = 0;
237 # visit children
238 13 foreach my $child (@{$root->{DATA}}) {
239 12 ▶ next if $child->{DISCARD};
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"DISCARD"} 0 12 12
240 #
241 # DAA updated to report returnlist, iterator, conditional, urgency,
242 # and annotation
243 #
244 my ($child_class, $method, $returns, $iterator, $urgent, $condition,
245 $annot) =
246 12 $self->{SIGPARSE}($child->{INPUT});
247  
248 my $child_offset =
249 UML::Sequence::Activation
250 12 ▶ ->find_offset($self->{ACTIVATIONS}{$child_class})
Branch: 1 branch
BranchTrueFalseTotal
unless $child_class eq "_EXTERNAL" 12 0 12
251 unless ($child_class eq '_EXTERNAL');
252  
253 #
254 # DAA add pending annotation
255 #
256 12 my $closetag = "/>\n";
257 #
258 # until we figure out how to use CDATA and a text element w/
259 # XML::DOM, we'll have to force dquotes to squotes
260 #
261 12 ▶ $annot=~s/"/'/g,
Branch: 1 branch
BranchTrueFalseTotal
if $annot 0 12 12
262 $closetag =
263 ">\n<annotation text=\"$annot\" />\n</arrow>\n",
264 $annot = undef
265 if $annot;
266  
267 12 $self->{ARROW_NUM}++;
268 12 $method=~s/\s+$//;
269 12 ▶ $method .= ' !' if $urgent;
Branch: 1 branch
BranchTrueFalseTotal
if $urgent 0 12 12
270 12 ▶ $method = '* ' . $method if $iterator;
Branch: 1 branch
BranchTrueFalseTotal
if $iterator 0 12 12
271 12 ▶ $method = "$condition $method" if $condition;
Branch: 1 branch
BranchTrueFalseTotal
if $condition 0 12 12
272 12 ▶ my $type = ($child_class eq '_EXTERNAL') ? 'async' : 'call';
Branch: 1 branch
BranchTrueFalseTotal
$child_class eq '_EXTERNAL' ? : 0 12 12
273 12 ▶ $asyncs++ if ($type eq 'async');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "async" 0 12 12
274 12 ▶ $self->{ARROW_LIST} .= ($type eq 'async') ?
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'async' ? : 0 12 12
275 " <arrow from='_EXTERNAL' to='$class' type='async' label='$method'
276 from-offset='$offset' to-offset='$offset' $closetag" :
277  
278 " <arrow from='$class' to='$child_class' type='call' label='$method'
279 from-offset='$offset' to-offset='$child_offset' $closetag";
280 #
281 # recurse to handle called class/method
282 #
283 12 ▶ $self->_build_xml_sequence($child, $returns)
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "async" 12 0 12
284 unless ($type eq 'async');
285 #
286 # DAA add return values if any
287 #
288 $self->{ARROW_LIST} .=
289 12 ▶" <arrow from='$child_class' to='$class' type='return' label='$returns'\n
Branch: 1 branch
BranchTrueFalseTotal
if $returns 0 12 12
290 from-offset='$child_offset' to-offset='$offset' />\n"
291 if $returns;
292 }
293  
294 13 ▶ $self->{ARROW_NUM}++
Branch: 1 branch
BranchTrueFalseTotal
if $hasreturn 0 13 13
295 if $hasreturn;
296  
297 13 $activation->ends($self->{ARROW_NUM});
298 #
299 # if outermost, and it had an external, add external class
300 # to output
301 #
302 13 ▶ if ($asyncs && ($self->{TREE}{NAME} eq $root->{NAME})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asyncs and $self->{"TREE"}{"NAME"} eq $root->{"NAME"}) 0 13 13
Condition33%1/3
$asyncs and $self->{"TREE"}{"NAME"} eq $root->{"NAME"}
!ll&&!rl&&r
1300
MC/DC0%0/2
$asyncs and $self->{"TREE"}{"NAME"} eq $root->{"NAME"}
$asyncs $self->{"TREE"}{"NAME"} eq $root->{"NAME"}
Truth table33%1/3
A and B
A= $asyncsB= $self->{"TREE"}{"NAME"} eq $root->{"NAME"}
ABresult
0X0
100
111
303 0 unshift @{$self->{CLASSES}}, '_EXTERNAL';
304 # create activation and add it to the list for this class
305 0 my $activation = UML::Sequence::Activation->new();
306 0 $activation->starts(0);
307 my $offset = UML::Sequence::Activation
308 0 ->find_offset($self->{ACTIVATIONS}{_EXTERNAL});
309 0 $activation->offset($offset);
310 0 $activation->ends($self->{ARROW_NUM});
311  
312 0 push @{$self->{ACTIVATIONS}{_EXTERNAL}}, $activation;
313 }
314 }
315  
316 1 ▶sub _build_class_list {
Subroutine _build_class_list: called CC=3 SCAR=11.0
317 1 my $self = shift;
318 1 $self->{CLASS_LIST} = "<class_list>\n";
319  
320 1 foreach my $class (@{$self->{CLASSES}}) {
321 my ($starts, $ends) =
322 UML::Sequence::Activation
323 4 ->find_bounds($self->{ACTIVATIONS}{$class});
324 $self->{CLASS_LIST} .=
325 4 " <class name='$class' born='$starts' extends-to='$ends'>\n" .
326 " <activation_list>\n";
327  
328 4 foreach my $activation (@{$self->{ACTIVATIONS}{$class}}) {
329 13 my $act_start = $activation->starts();
330 13 my $act_end = $activation->ends();
331 13 my $act_offset = $activation->offset();
332 $self->{CLASS_LIST} .=
333 13 " <activation born='$act_start' extends-to='$act_end' " .
334 "offset='$act_offset' />\n";
335 }
336 4 $self->{CLASS_LIST} .= " </activation_list>\n </class>\n";
337 }
338 1 $self->{CLASS_LIST} .= "</class_list>\n";
339 }
340  
341 4 1;
342  
343 =head1 AUTHOR
344  
345 Phil Crow, <philcrow2000@yahoo.com>
346 Version 0.06 updates by Dean Arnold, <darnold@presicient.com>
347  
348 =head1 SEE ALSO
349  
350 L<genericseq.pl>
351 L<seq2svg.pl>
352 L<seq2rast.pl>
353  
354 =head1 COPYRIGHT
355  
356 Copyright(C) 2003-2006, Philip Crow, all rights reserved.
357  
358 You may modify and/or redistribute this code in the same manner as
359 Perl itself.
360  
361 =cut
362  
363 # This data is a small subset of a typical dprofpp -T output.
364 # It's used by _read_sample so you can debug with a small input set.
365 # Use _read_sample in place of _read_dprofpp to switch to this set.
366 __DATA__
« blib/lib/Devel/CallSeq.pm ↑ Summary blib/lib/UML/Sequence/SimpleSeq.pm »