Quiq/TreeFormatter.pm

filter: Statementstmt 89.0% 65 / 73 Branchbran 76.4% 26 / 34 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 84.1% 101 / 120 CC 22 SCAR 26.4
CC
22
Cov
83%
asText
21.5
new
6.0
BEGIN
1.0
CRAP 14.1
« Quiq/Duration.pm ↑ Summary Quiq/Properties.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::TreeFormatter - Erzeugung von Baumdarstellungen
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 Der Code
16  
17 use Quiq::TreeFormatter;
18
19 my $t = Quiq::TreeFormatter->new([
20 [0,'A'],
21 [1,'B'],
22 [2,'C'],
23 [3,'D'],
24 [2,'E'],
25 [2,'F'],
26 [3,'G'],
27 [4,'H'],
28 [1,'I'],
29 [1,'J'],
30 [2,'K'],
31 [2,'L'],
32 [1,'M'],
33 [2,'N'],
34 ]);
35
36 print $t->asText;
37  
38 produziert
39  
40 A
41 |
42 +--B
43 | |
44 | +--C
45 | | |
46 | | +--D
47 | |
48 | +--E
49 | |
50 | +--F
51 | |
52 | +--G
53 | |
54 | +--H
55 |
56 +--I
57 |
58 +--J
59 | |
60 | +--K
61 | |
62 | +--L
63 |
64 +--M
65 |
66 +--N
67  
68 =head1 DESCRIPTION
69  
70 Ein Objekt der Klasse repräsentiert einen Baum, der mit
71 Methoden der Klasse dargestellt (visualisiert) werden kann. Die
72 Baumstruktur wird als eine Liste von (minestens zweielementigen) Tupeln
73 [$level,$node,...] an den Konstruktor übergeben. Ein Tupel besteht aus der
74 Angabe der Ebene des Knotens, des Knotens selbst und optional weiteren
75 - frei definierbaren - Informationen, z.B. ob der Subbaum des Knotens
76 ausgeblendet wurde. Die zusätzliche Information kann in der
77 getText-Callback-Methode genutzt werden, um den betreffenden Knoten
78 besonders darzustellen. Der Knoten $node kann ein Objekt oder ein Text
79 sein. Die Ebene $level ist eine natürliche Zahl im Wertebereich von
80 0 (Wurzelknoten) bis n. Die Paar-Liste kann aus irgendeiner Baumstruktur
81 mit einer rekursiven Funktion erzeugt werden (siehe Abschnitt L<EXAMPLE|"EXAMPLE">).
82  
83 =head1 EXAMPLE
84  
85 =head2 Baumknoten als Texte
86  
87 Siehe SYNOPSIS.
88  
89 =head2 Rekursive Methode mit Stop-Kennzeichen
90  
91 Die folgende Methode erkennt, wenn ein Knoten wiederholt auftritt,
92 kennzeichnet diesen mit einem Stop-Kennzeichen und steigt dann
93 nicht in den Subbaum ab. Dies verhindert redundante Baumteile
94 und u.U. eine Endlos-Rekursion (wenn die Wiederholung auf einem
95 Pfad vorkommt).
96  
97 sub hierarchy {
98 my $self = shift;
99 my $stopH = shift || {};
100
101 my $stop = $stopH->{$self}++;
102 if (!$stop) {
103 my @arr;
104 for my $node ($self->subNodes) {
105 push @arr,map {$_->[0]++; $_} $node->hierarchy($stopH);
106 }
107 }
108 unshift @arr,[0,$self,$stop];
109
110 return wantarray? @arr: \@arr;
111 }
112  
113 =head2 Baumknoten als Objekte
114  
115 Hier ein Beispiel für eine rekursive Methode einer Anwendung, die
116 eine Klassenhierarchie für eine bestimmte Klasse ($self)
117 ermittelt. Die Methode liefert die Klassenhierarchie als
118 Paar-Liste für Quiq::TreeFormatter:
119  
120 sub classHierarchy {
121 my $self = shift;
122
123 my @arr;
124 for my $cls ($self->subClasses) {
125 push @arr,map {$_->[0]++; $_} $cls->classHierarchy;
126 }
127 unshift @arr,[0,$self];
128
129 return wantarray? @arr: \@arr;
130 }
131  
132 Hier sind die Knoten $node Objekte, deren Text für die Darstellung
133 im Baum durch eine anonyme Subroutine produziert wird.
134 Die Subroutine wird mittels der Option C<-getText> an $t->asText()
135 übergeben:
136  
137 my $cls = $cop->findEntity('Quiq/ContentProcessor/Type');
138 my $arrA = $cls->classHierarchy;
139 print Quiq::TreeFormatter->new($arrA)->asText(
140 -getText => sub {
141 my ($cls,$level,$stop) = @_;
142
143 my $str = $cls->name."\n";
144 for my $grp ($cls->groups) {
145 $str .= sprintf ": %s\n",$grp->title;
146 for my $mth ($grp->methods) {
147 $str .= sprintf ": %s()\n",$mth->name;
148 }
149 }
150
151 return $str;
152 },
153 );
154  
155 Ein Ausschnitt aus der produzierten Ausgabe:
156  
157 +--Quiq/ContentProcessor/Type
158 : Erzeugung
159 : create()
160 : Objektmethoden
161 : entityFile()
162 : entityId()
163 : entityType()
164 : files()
165 : fileSource()
166 : fileSourceRef()
167 : appendFileSource()
168 : name()
169 : pureCode()
170 : Intern
171 : needsTest()
172 : needsUpdate()
173 |
174 +--Jaz/Type
175 |
176 +--Jaz/Type/Export
177 | : Erzeugung
178 | : create()
179 | : Entitäten
180 | : entities()
181 | : Reguläre Ausdrücke
182 | : excludeRegex()
183 | : selectRegexes()
184 | : Verzeichnisse
185 | : adminDirectories()
186 | : rootDirectory()
187 | : Verzeichnisse
188 | : subDirectories()
189 | : Export
190 | : rewriteRules()
191 | : export()
192 |
193 +--Jaz/Type/Language
194 |
195 +--Jaz/Type/Library
196 |
197 +--Jaz/Type/Package
198 | : Erzeugung
199 | : create()
200 | : Eigenschaften
201 | : level()
202 | : levelName()
203 | : Entitäten
204 | : entities()
205 | : hierarchy()
206 | : package()
207 | : subPackages()
208 | : Reguläre Ausdrücke
209 | : regexes()
210 |
211 ...
212  
213 =cut
214  
215 # -----------------------------------------------------------------------------
216  
217 package Quiq::TreeFormatter;
218 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
219  
220 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
222 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
223  
224 1 our $VERSION = '1.240';
225  
226 1 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
227  
228 # -----------------------------------------------------------------------------
229  
230 =head1 METHODS
231  
232 =head2 Konstruktor
233  
234 =head3 new() - Instantiiere Baum-Objekt
235  
236 =head4 Synopsis
237  
238 $t = $class->new(\@tuples);
239  
240 =head4 Arguments
241  
242 =over 4
243  
244 =item @tuples
245  
246 Liste von Tupeln [$level, $node, ...]. Die Komponenten ... werden
247 transparent weitergereicht.
248  
249 =back
250  
251 =head4 Returns
252  
253 Referenz auf Baum-Objekt.
254  
255 =head4 Description
256  
257 Instantiiere ein Baum-Objekt und liefere eine Referenz auf dieses
258 Objekt zurück.
259  
260 =cut
261  
262 # -----------------------------------------------------------------------------
263  
264 1 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
265 1 my ($class,$tupleA) = @_;
266  
267 # Tupel in interne Liste umkopieren. Die interne Liste besitzt
268 # am Anfang ein weiteres Feld mit einem "Verbindungskennzeichen". Die
269 # Felder Verbindungskennzeichen, Level, Knoten (Objekt oder Text),
270 # und sonstige Infomation. Das Verbindungskennzeichen gibt an, ob ein
271 # Folgeknoten auf gleicher Ebene vorhanden ist. Wir initialisieren
272 # dessen Wert hier auf 0.
273  
274 1 my @arr;
275 1 for (@$tupleA) {
276 # [$follow,$level,$node,...]
277 14 push @arr,[0,@$_];
278 }
279  
280 # Setze die Kolumne mit Verbindungs-Kennzeichen auf 1, wenn
281 # ein Folgeknoten gleicher Ebene existiert, ohne dass ein
282 # Knoten einer niedrigeren Ebene dazwischen liegt.
283  
284 1 ▶ for (my $i = 0; $i < @arr-1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @arr - 1) 13 1 14
285 13 my $iLevel = $arr[$i][1];
286 13 ▶ for (my $j = $i+1; $j < @arr; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @arr) 37 2 39
287 37 my $jLevel = $arr[$j][1];
288 37 ▶ if ($jLevel == $iLevel) {
Branch: 2 branches
BranchTrueFalseTotal
if ($jLevel == $iLevel) { } 6 31 37
elsif ($jLevel < $iLevel) { } 5 26 31
289 6 $arr[$i][0] = 1;
290 6 last;
291 }
292 elsif ($jLevel < $iLevel) {
293 5 last;
294 }
295 }
296 }
297  
298 1 return $class->SUPER::new(
299 lineA => \@arr,
300 );
301 }
302  
303 # -----------------------------------------------------------------------------
304  
305 =head2 Darstellung
306  
307 =head3 asText() - Erzeuge Text-Repräsentation des Baums
308  
309 =head4 Synopsis
310  
311 $str = $t->asText(@opt);
312  
313 =head4 Options
314  
315 =over 4
316  
317 =item -format => 'debug'|'compact'|'tree' (Default: 'tree')
318  
319 Format der Ausgabe (s.u.)
320  
321 =item -getText => sub { my ($node,@args) = @_; ... return $text }
322  
323 Callback-Funktion zum Ermitteln des Textes des Knotens. Der Text
324 kann mehrzeilig sein.
325  
326 =back
327  
328 =head4 Returns
329  
330 Baumdarstellung (String)
331  
332 =head4 Description
333  
334 Erzeuge eine Text-Repräsentation des Baums und liefere diese zurück.
335  
336 B<debug>
337  
338 0 0 A
339 1 1 B
340 1 2 C
341 0 3 D
342 1 2 E
343 0 2 F
344 0 3 G
345 0 4 H
346 1 1 I
347 1 1 J
348 1 2 K
349 0 2 L
350 0 1 M
351 0 2 N
352  
353 B<compact>
354  
355 A
356 B
357 C
358 D
359 E
360 F
361 G
362 H
363 I
364 J
365 K
366 L
367 M
368 N
369  
370 B<tree>
371  
372 A
373 |
374 +--B
375 | |
376 | +--C
377 | | |
378 | | +--D
379 | |
380 | +--E
381 | |
382 | +--F
383 | |
384 | +--G
385 | |
386 | +--H
387 |
388 +--I
389 |
390 +--J
391 | |
392 | +--K
393 | |
394 | +--L
395 |
396 +--M
397 |
398 +--N
399  
400 =cut
401  
402 # -----------------------------------------------------------------------------
403  
404 3 ▶sub asText {
Subroutine asText: called CC=17 SCAR=30.7
Pod documented
405 3 my $self = shift;
406 # @_: @opt
407  
408 3 my $direction = 'down';
409 3 my $format = 'tree';
410 3 ▶ my $getText = sub {return shift};
Subroutine __ANON__: called CC=1 SCAR=0.0
411  
412 3 Quiq::Option->extract(\@_,
413 -direction => \$direction,
414 -format => \$format,
415 -getText => \$getText,
416 );
417  
418 3 my $lineA = $self->get('lineA');
419  
420 3 my $str = '';
421  
422 3 ▶ if ($format eq 'tree') {
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'tree') { } 1 2 3
elsif ($format eq 'debug') { } 1 1 2
elsif ($format eq 'compact') { } 1 0 1
423 # Array, das für jede Einrückungsspalte angibt, ob dort eine
424 # Verbindungslinie geführt wird.
425 1 my @follow;
426  
427 1 ▶ for (my $i = 0; $i < @$lineA; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lineA) 14 1 15
428 14 my ($follow,$level,$node,@args) = @{$lineA->[$i]};
429 14 $follow[$level] = $follow; # Verbindungskennzeichen für $level
430  
431 # Einrückungs-Block erzeugen. Der Einrückungs-Block
432 # enthält für jede Ebene 0 bis $level-1 Leerraum oder
433 # eine Verbindungslinie gemäß dem Verbindungskennzeichen
434 # $follow der betreffenden Ebene. Die Einrückung je
435 # Ebene beträgt 3 Zeichen.
436  
437 14 my $indent = '';
438 14 ▶ for (my $j = 0; $j < $level; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $level) 26 14 40
439 26 ▶ $indent .= $follow[$j]? '| ': ' ';
Branch: 1 branch
BranchTrueFalseTotal
$follow[$j] ? : 9 17 26
440 }
441  
442 # Knoten-Block erzeugen. Der Knoten-Block hat den Aufbau
443 #
444 # X
445 # +--$line1
446 # Y $rest
447 #
448 # wobei "Y $rest" nur bei einem mehrzeiligen Knoten-Text
449 # existiert. Y ist dann ein |, wenn das Verbindungskennzeichen
450 # für Ebene $level gesetzt ist, sonst ein Leerzeichen.
451 # X existiert nicht beim ersten Knoten, sonst ist X
452 # konstant ein |. Bei Direction 'down' wird X oberhalb,
453 # bei 'up' unterhalb des Blocks hinzugefügt.
454  
455 14 my ($line1,$rest) = split /\n/,$getText->($node,$level,@args),2;
456  
457 14 my $block = sprintf "+--%s\n",$line1;
458  
459 14 ▶ if ($rest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest) 0 14 14
460 0 ▶ if ($follow[$level]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($follow[$level]) { } 0 0 0
461 0 $rest =~ s/^/| /mg;
462 }
463 else {
464 0 $rest =~ s/^/ /mg;
465 }
466 0 $block .= $rest;
467 }
468  
469 # Wenn 'down' setzen wir | auf die Zeile davor,
470 # bei 'up' setzen wir | auf die Zeile danach.
471  
472 14 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 13 1 14
473 13 ▶ $block = $direction eq 'down'? "|\n$block": "$block|\n";
Branch: 1 branch
BranchTrueFalseTotal
$direction eq 'down' ? : 13 0 13
474 }
475  
476 # Einrückungs-Block und Knoten-Block zusammenfügen
477  
478 14 $block =~ s/^/$indent/mg;
479  
480 14 ▶ if ($direction eq 'down') {
Branch: 1 branch
BranchTrueFalseTotal
if ($direction eq 'down') { } 14 0 14
481 14 $str .= $block;
482 }
483 else { # up
484 0 $str = $block.$str;
485 }
486 }
487  
488 1 $str =~ s/^(\+--| )//mg;
489 }
490 elsif ($format eq 'debug') {
491 1 for (@$lineA) {
492 14 my ($follow,$level,$node,@args) = @$_;
493 $str .= sprintf "%d %d %s%s%s\n",
494 $follow,
495 $level,
496 ' ' x $level,
497 $getText->($node,$level,@args), # $node,
498 0 ▶ @args? ' '.join('|',map {$_ // 'undef'} @args): '';
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 14 14
Condition0%0/2
$_ // "undef"
l!l
00
MC/DC0%0/1
$_ // "undef"
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
499 }
500 }
501 elsif ($format eq 'compact') {
502 1 for (@$lineA) {
503 14 my ($follow,$level,$node,@args) = @$_;
504 14 my ($line1,$rest) = split /\n/,$getText->($node,$level,@args),2;
505 14 my $block = sprintf "%s%s\n",' ' x $level,$line1;
506 14 ▶ if ($direction eq 'down') {
Branch: 1 branch
BranchTrueFalseTotal
if ($direction eq 'down') { } 14 0 14
507 14 $str .= $block;
508 }
509 else { # up
510 0 $str = $block.$str;
511 }
512 }
513 }
514 else {
515 0 $self->throw(
516 'TREE-FORMATTER-00001: Unknown format',
517 Format => $format,
518 );
519 }
520  
521 3 return $str;
522 }
523  
524 # -----------------------------------------------------------------------------
525  
526 =head1 VERSION
527  
528 1.240
529  
530 =head1 AUTHOR
531  
532 Frank Seitz, L<http://fseitz.de/>
533  
534 =head1 COPYRIGHT
535  
536 Copyright (C) 2026 Frank Seitz
537  
538 =head1 LICENSE
539  
540 This code is free software; you can redistribute it and/or modify
541 it under the same terms as Perl itself.
542  
543 =cut
544  
545 # -----------------------------------------------------------------------------
546  
547 1 1;
548  
549 # eof
« Quiq/Duration.pm ↑ Summary Quiq/Properties.pm »