blib/lib/GraphViz2/Marpa/Extractor.pm untested

filter: Statementstmt 0.0% 0 / 454 Branchbran 0.0% 0 / 138 Conditioncond 0.0% 0 / 201 MC/DCmcdc 0.0% 0 / 116 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 943 CC 70 SCAR 47.8
CC
70
Cov
0%
_extract_graph
240.0
_extract_subgraph
240.0
_extract_edge_chain_from_group
90.0
CRAP 118.6
1  
2 package GraphViz2::Marpa::Extractor;
3  
4 use strict;
5 use warnings;
6  
7 our $VERSION = '0.1.0' ;
8  
9 our $ANON_SUBGRAPH_COUNTER = 0;
10  
11 # Public entry point.
12 # Returns arrayref of graphs; each graph has:
13 # - name, kind, attrs
14 # - node_defaults, edge_defaults
15 # - nodes (hash), node_order (array)
16 # - edges (array)
17 # - subgraphs (array)
18 # - adjacency (hash)
19 # - adjacency_scoped (hash)
20 # - flat (nodes, edges)
21 # - symbols (nodes, subgraphs, ports, attrs)
22 # - constraints (ranks, clusters, separations, visibility)
23 sub extract_all_graphs_hierarchical {
24 my ($root) = @_;
25  
26 my ($kind, $graph_nodes) = _find_graphs_and_kind($root);
27  
28 my @graphs;
29 for my $g (@$graph_nodes) {
30 my %symbols = (
31 nodes => {},
32 subgraphs => {},
33 ports => {},
34 attrs => {},
35 );
36  
37 my $graph = _extract_graph(
38 $g,
39 $kind,
40 {}, # inherited node defaults
41 {}, # inherited edge defaults
42 \%symbols,
43 );
44  
45 _build_flat_and_scoped($graph);
46 _extract_constraints($graph);
47  
48 push @graphs, $graph;
49 }
50  
51 return \@graphs;
52 }
53  
54 sub _find_graphs_and_kind {
55 my ($root) = @_;
56  
57 my $kind = 'digraph';
58 my @graphs;
59  
60 for my $child (@{ $root->{daughters} || [] }) {
61 my $name = $child->{name} // '';
62  
63 if ($name eq 'prolog') {
64 for my $c (@{ $child->{daughters} || [] }) {
65 my $t = $c->{attributes}{type} // '';
66 if ($t eq 'digraph_literal') {
67 $kind = 'digraph';
68 }
69 elsif ($t eq 'graph_literal') {
70 $kind = 'graph';
71 }
72 }
73 }
74 elsif ($name eq 'graph') {
75 push @graphs, $child;
76 }
77 }
78  
79 return ($kind, \@graphs);
80 }
81  
82 sub _extract_graph {
83 my ($graph_node, $kind, $inherited_node_defaults, $inherited_edge_defaults, $symbols) = @_;
84  
85 my $id_node = $graph_node->{daughters}[0];
86 my $name = $id_node->{attributes}{value};
87  
88 $symbols->{subgraphs}{$name} = 1;
89  
90 my $brace_lit = $graph_node->{daughters}[1];
91 my @body = @{ $brace_lit->{daughters} || [] };
92  
93 my %attrs;
94 my %local_node_defaults;
95 my %local_edge_defaults;
96 my %nodes;
97 my @node_order;
98 my @edges;
99 my @subgraphs;
100  
101 my %effective_node_defaults = (%$inherited_node_defaults);
102 my %effective_edge_defaults = (%$inherited_edge_defaults);
103  
104 for (my $i = 0; $i < @body; $i++) {
105 my $n = $body[$i];
106 my $nm = $n->{name} // '';
107 my $t = $n->{attributes}{type} // '';
108  
109 # Graph-level attributes and rank constraints.
110 if ($nm eq 'attribute') {
111 my $key = $t || $n->{attributes}{name};
112 my $val = $n->{attributes}{value};
113 $attrs{$key} = $val;
114 $symbols->{attrs}{$key} = 1;
115 next;
116 }
117  
118 # Class defaults: node / edge / graph.
119 if ($nm eq 'class' && ($n->{attributes}{type} // '') eq 'class') {
120 my $class_name = $n->{attributes}{value}; # 'node', 'edge', 'graph'
121 my %def;
122 for my $c (@{ $n->{daughters} || [] }) {
123 next unless ($c->{name} // '') eq 'attribute';
124 my $ct = $c->{attributes}{type} || $c->{attributes}{name};
125 my $val = $c->{attributes}{value};
126 $def{$ct} = $val;
127 $symbols->{attrs}{$ct} = 1;
128 }
129 if ($class_name eq 'node') {
130 %local_node_defaults = (%local_node_defaults, %def);
131 %effective_node_defaults = (%$inherited_node_defaults, %local_node_defaults);
132 }
133 elsif ($class_name eq 'edge') {
134 %local_edge_defaults = (%local_edge_defaults, %def);
135 %effective_edge_defaults = (%$inherited_edge_defaults, %local_edge_defaults);
136 }
137 elsif ($class_name eq 'graph') {
138 %attrs = (%attrs, %def);
139 }
140 next;
141 }
142  
143 # Named subgraph.
144 if ($nm eq 'literal' && $t eq 'subgraph_literal') {
145 my $id_node2 = $body[$i + 1];
146 my $sub_name = $id_node2->{attributes}{value};
147 my $sub_brace_lit = $body[$i + 2];
148 my $sub_kind = ($sub_name =~ /^cluster_/) ? 'cluster' : 'subgraph';
149  
150 $symbols->{subgraphs}{$sub_name} = 1;
151  
152 my $sub = _extract_subgraph(
153 $sub_name,
154 $sub_kind,
155 $sub_brace_lit,
156 $kind,
157 \%effective_node_defaults,
158 \%effective_edge_defaults,
159 $symbols,
160 );
161 push @subgraphs, $sub;
162 next;
163 }
164  
165 # Anonymous subgraph: bare '{ ... }' literal.
166 if ($nm eq 'literal' && $t eq 'brace_literal') {
167 my $sub_name = '_anon_' . (++$ANON_SUBGRAPH_COUNTER);
168 my $sub_kind = 'subgraph';
169  
170 $symbols->{subgraphs}{$sub_name} = 1;
171  
172 my $sub = _extract_subgraph(
173 $sub_name,
174 $sub_kind,
175 $n,
176 $kind,
177 \%effective_node_defaults,
178 \%effective_edge_defaults,
179 $symbols,
180 );
181 push @subgraphs, $sub;
182 next;
183 }
184  
185 # Edge chains and grouped/mixed RHS: start at node_id followed by edge_id.
186 if ($nm eq 'node_id' && $t eq 'node_id') {
187 if ($i + 1 < @body && ($body[$i + 1]{name} // '') eq 'edge_id') {
188 my $consumed = _extract_edge_chain(
189 \@body,
190 $i,
191 $kind,
192 \%nodes,
193 \@node_order,
194 \@edges,
195 \%effective_node_defaults,
196 \%effective_edge_defaults,
197 $symbols,
198 );
199 $i = $consumed;
200 next;
201 }
202  
203 # Standalone node declaration.
204 my $node_name = $n->{attributes}{name};
205 $symbols->{nodes}{$node_name} = 1;
206  
207 my %node_attr = _collect_attributes_from_node($n, $symbols);
208  
209 my $merged_attr = _merge_attrs(\%effective_node_defaults, \%node_attr);
210  
211 if (exists $nodes{$node_name}) {
212 $nodes{$node_name}{attrs} = _merge_attrs($nodes{$node_name}{attrs}, $merged_attr);
213 }
214 else {
215 $nodes{$node_name} = { attrs => $merged_attr };
216 push @node_order, $node_name;
217 }
218  
219 next;
220 }
221  
222 # Edge chain starting from a grouped LHS: { B C } -> D ...
223 if ($nm eq 'literal' && $t eq 'brace_literal') {
224 if ($i + 1 < @body && ($body[$i + 1]{name} // '') eq 'edge_id') {
225 my $consumed = _extract_edge_chain_from_group(
226 \@body,
227 $i,
228 $kind,
229 \%nodes,
230 \@node_order,
231 \@edges,
232 \%effective_node_defaults,
233 \%effective_edge_defaults,
234 $symbols,
235 );
236 $i = $consumed;
237 next;
238 }
239 }
240 }
241  
242 my $adjacency = _build_adjacency(\@edges, $kind);
243  
244 my $graph = {
245 name => $name,
246 kind => $kind,
247 attrs => \%attrs,
248 node_defaults => \%effective_node_defaults,
249 edge_defaults => \%effective_edge_defaults,
250 nodes => \%nodes,
251 node_order => \@node_order,
252 edges => \@edges,
253 subgraphs => \@subgraphs,
254 adjacency => $adjacency,
255 adjacency_scoped => {}, # filled later
256 flat => {}, # filled later
257 symbols => $symbols,
258 constraints => {}, # filled later
259 };
260  
261 return $graph;
262 }
263  
264 sub _extract_subgraph {
265 my ($name, $sub_kind, $brace_lit, $graph_kind, $inherited_node_defaults, $inherited_edge_defaults, $symbols) = @_;
266  
267 my @body = @{ $brace_lit->{daughters} || [] };
268  
269 my %attrs;
270 my %local_node_defaults;
271 my %local_edge_defaults;
272 my %nodes;
273 my @node_order;
274 my @edges;
275 my @subgraphs;
276  
277 my %effective_node_defaults = (%$inherited_node_defaults);
278 my %effective_edge_defaults = (%$inherited_edge_defaults);
279  
280 for (my $i = 0; $i < @body; $i++) {
281 my $n = $body[$i];
282 my $nm = $n->{name} // '';
283 my $t = $n->{attributes}{type} // '';
284  
285 # Subgraph attributes (including rank).
286 if ($nm eq 'attribute') {
287 my $key = $t || $n->{attributes}{name};
288 my $val = $n->{attributes}{value};
289 $attrs{$key} = $val;
290 $symbols->{attrs}{$key} = 1;
291 next;
292 }
293  
294 # Class defaults inside subgraph.
295 if ($nm eq 'class' && ($n->{attributes}{type} // '') eq 'class') {
296 my $class_name = $n->{attributes}{value};
297 my %def;
298 for my $c (@{ $n->{daughters} || [] }) {
299 next unless ($c->{name} // '') eq 'attribute';
300 my $ct = $c->{attributes}{type} || $c->{attributes}{name};
301 my $val = $c->{attributes}{value};
302 $def{$ct} = $val;
303 $symbols->{attrs}{$ct} = 1;
304 }
305 if ($class_name eq 'node') {
306 %local_node_defaults = (%local_node_defaults, %def);
307 %effective_node_defaults = (%$inherited_node_defaults, %local_node_defaults);
308 }
309 elsif ($class_name eq 'edge') {
310 %local_edge_defaults = (%local_edge_defaults, %def);
311 %effective_edge_defaults = (%$inherited_edge_defaults, %local_edge_defaults);
312 }
313 elsif ($class_name eq 'graph') {
314 %attrs = (%attrs, %def);
315 }
316 next;
317 }
318  
319 # Nested named subgraph.
320 if ($nm eq 'literal' && $t eq 'subgraph_literal') {
321 my $id_node2 = $body[$i + 1];
322 my $sub_name = $id_node2->{attributes}{value};
323 my $sub_brace_lit = $body[$i + 2];
324 my $nested_kind = ($sub_name =~ /^cluster_/) ? 'cluster' : 'subgraph';
325  
326 $symbols->{subgraphs}{$sub_name} = 1;
327  
328 my $sub = _extract_subgraph(
329 $sub_name,
330 $nested_kind,
331 $sub_brace_lit,
332 $graph_kind,
333 \%effective_node_defaults,
334 \%effective_edge_defaults,
335 $symbols,
336 );
337 push @subgraphs, $sub;
338 next;
339 }
340  
341 # Nested anonymous subgraph.
342 if ($nm eq 'literal' && $t eq 'brace_literal') {
343 my $sub_name = '_anon_' . (++$ANON_SUBGRAPH_COUNTER);
344 my $nested_kind = 'subgraph';
345  
346 $symbols->{subgraphs}{$sub_name} = 1;
347  
348 my $sub = _extract_subgraph(
349 $sub_name,
350 $nested_kind,
351 $n,
352 $graph_kind,
353 \%effective_node_defaults,
354 \%effective_edge_defaults,
355 $symbols,
356 );
357 push @subgraphs, $sub;
358 next;
359 }
360  
361 # Edge chains and grouped RHS inside subgraph.
362 if ($nm eq 'node_id' && $t eq 'node_id') {
363 if ($i + 1 < @body && ($body[$i + 1]{name} // '') eq 'edge_id') {
364 my $consumed = _extract_edge_chain(
365 \@body,
366 $i,
367 $graph_kind,
368 \%nodes,
369 \@node_order,
370 \@edges,
371 \%effective_node_defaults,
372 \%effective_edge_defaults,
373 $symbols,
374 );
375 $i = $consumed;
376 next;
377 }
378  
379 # Standalone node declaration.
380 my $node_name = $n->{attributes}{name};
381 $symbols->{nodes}{$node_name} = 1;
382  
383 my %node_attr = _collect_attributes_from_node($n, $symbols);
384  
385 my $merged_attr = _merge_attrs(\%effective_node_defaults, \%node_attr);
386  
387 if (exists $nodes{$node_name}) {
388 $nodes{$node_name}{attrs} = _merge_attrs($nodes{$node_name}{attrs}, $merged_attr);
389 }
390 else {
391 $nodes{$node_name} = { attrs => $merged_attr };
392 push @node_order, $node_name;
393 }
394  
395 next;
396 }
397  
398 # Edge chain starting from grouped LHS inside subgraph.
399 if ($nm eq 'literal' && $t eq 'brace_literal') {
400 if ($i + 1 < @body && ($body[$i + 1]{name} // '') eq 'edge_id') {
401 my $consumed = _extract_edge_chain_from_group(
402 \@body,
403 $i,
404 $graph_kind,
405 \%nodes,
406 \@node_order,
407 \@edges,
408 \%effective_node_defaults,
409 \%effective_edge_defaults,
410 $symbols,
411 );
412 $i = $consumed;
413 next;
414 }
415 }
416 }
417  
418 my $adjacency = _build_adjacency(\@edges, $graph_kind);
419  
420 my $subgraph = {
421 name => $name,
422 kind => $sub_kind,
423 attrs => \%attrs,
424 node_defaults => \%effective_node_defaults,
425 edge_defaults => \%effective_edge_defaults,
426 nodes => \%nodes,
427 node_order => \@node_order,
428 edges => \@edges,
429 subgraphs => \@subgraphs,
430 adjacency => $adjacency,
431 adjacency_scoped => {}, # filled later
432 flat => {}, # filled later
433 symbols => $symbols,
434 constraints => {}, # filled later
435 };
436  
437 return $subgraph;
438 }
439  
440 sub _extract_edge_chain {
441 my ($body, $start_idx, $kind, $nodes, $node_order, $edges, $node_defaults, $edge_defaults, $symbols) = @_;
442  
443 my $i = $start_idx;
444  
445 my $left_node = $body->[$i];
446 my $left_name = $left_node->{attributes}{name};
447 $symbols->{nodes}{$left_name} = 1;
448  
449 my ($left_port, $left_compass) = _parse_port(
450 $left_node->{attributes}{port},
451 $left_node->{attributes}{value},
452 $symbols,
453 );
454  
455 # Ensure left node exists (auto-declare if needed).
456 if (!exists $nodes->{$left_name}) {
457 my $attrs = _merge_attrs($node_defaults, {});
458 $attrs->{_auto_declared} = 1;
459 $nodes->{$left_name} = { attrs => $attrs };
460 push @$node_order, $left_name;
461 }
462  
463 $i++; # move to first edge_id
464  
465 while ($i < @$body && ($body->[$i]{name} // '') eq 'edge_id') {
466 my $edge_id = $body->[$i];
467  
468 # Collect attributes attached to edge_id itself.
469 my %edge_attr = _collect_attributes_from_node($edge_id, $symbols);
470  
471 $i++; # move to right side
472  
473 last if $i >= @$body;
474  
475 my $right = $body->[$i];
476 my $rname = $right->{name} // '';
477  
478 if ($rname eq 'node_id') {
479 my $right_name = $right->{attributes}{name};
480 $symbols->{nodes}{$right_name} = 1;
481  
482 my ($right_port, $right_compass) = _parse_port(
483 $right->{attributes}{port},
484 $right->{attributes}{value},
485 $symbols,
486 );
487  
488 # Merge attributes from right node's attached attribute lists.
489 my %right_edge_attr = _collect_attributes_from_node($right, $symbols);
490 %edge_attr = (%edge_attr, %right_edge_attr);
491  
492 my $merged_attr = _merge_attrs($edge_defaults, \%edge_attr);
493  
494 my $edge = {
495 from => $left_name,
496 from_port => $left_port,
497 from_compass => $left_compass,
498 to => $right_name,
499 to_port => $right_port,
500 to_compass => $right_compass,
501 attrs => $merged_attr,
502 };
503  
504 push @$edges, $edge;
505  
506 # Ensure right node exists (auto-declare if needed).
507 if (!exists $nodes->{$right_name}) {
508 my $attrs = _merge_attrs($node_defaults, {});
509 $attrs->{_auto_declared} = 1;
510 $nodes->{$right_name} = { attrs => $attrs };
511 push @$node_order, $right_name;
512 }
513  
514 # Chain: next segment starts from this right node.
515 $left_name = $right_name;
516 $left_port = $right_port;
517 $left_compass = $right_compass;
518  
519 $i++;
520 next;
521 }
522 elsif ($rname eq 'literal' && ($right->{attributes}{type} // '') eq 'brace_literal') {
523 # Grouped RHS: A -> { B C D }
524 for my $child (@{ $right->{daughters} || [] }) {
525 next unless ($child->{name} // '') eq 'node_id';
526 my $right_name = $child->{attributes}{name};
527 $symbols->{nodes}{$right_name} = 1;
528  
529 my ($right_port, $right_compass) = _parse_port(
530 $child->{attributes}{port},
531 $child->{attributes}{value},
532 $symbols,
533 );
534  
535 my %right_edge_attr = _collect_attributes_from_node($child, $symbols);
536 my %ea = (%edge_attr, %right_edge_attr);
537 my $merged_attr = _merge_attrs($edge_defaults, \%ea);
538  
539 my $edge = {
540 from => $left_name,
541 from_port => $left_port,
542 from_compass => $left_compass,
543 to => $right_name,
544 to_port => $right_port,
545 to_compass => $right_compass,
546 attrs => $merged_attr,
547 };
548  
549 push @$edges, $edge;
550  
551 if (!exists $nodes->{$right_name}) {
552 my $attrs = _merge_attrs($node_defaults, {});
553 $attrs->{_auto_declared} = 1;
554 $nodes->{$right_name} = { attrs => $attrs };
555 push @$node_order, $right_name;
556 }
557 }
558  
559 $i++;
560 next;
561 }
562 else {
563 last;
564 }
565 }
566  
567 return $i - 1;
568 }
569  
570 sub _extract_edge_chain_from_group {
571 my ($body, $start_idx, $kind, $nodes, $node_order, $edges, $node_defaults, $edge_defaults, $symbols) = @_;
572  
573 my $i = $start_idx;
574  
575 my $group = $body->[$i];
576  
577 my @left_nodes;
578 for my $child (@{ $group->{daughters} || [] }) {
579 next unless ($child->{name} // '') eq 'node_id';
580 my $name = $child->{attributes}{name};
581 $symbols->{nodes}{$name} = 1;
582  
583 my ($port, $compass) = _parse_port(
584 $child->{attributes}{port},
585 $child->{attributes}{value},
586 $symbols,
587 );
588  
589 if (!exists $nodes->{$name}) {
590 my $attrs = _merge_attrs($node_defaults, {});
591 $attrs->{_auto_declared} = 1;
592 $nodes->{$name} = { attrs => $attrs };
593 push @$node_order, $name;
594 }
595  
596 push @left_nodes, {
597 name => $name,
598 port => $port,
599 compass => $compass,
600 };
601 }
602  
603 $i++; # move to edge_id
604  
605 while ($i < @$body && ($body->[$i]{name} // '') eq 'edge_id') {
606 my $edge_id = $body->[$i];
607  
608 my %edge_attr = _collect_attributes_from_node($edge_id, $symbols);
609  
610 $i++; # move to right side
611 last if $i >= @$body;
612  
613 my $right = $body->[$i];
614 my $rname = $right->{name} // '';
615  
616 if ($rname eq 'node_id') {
617 my $right_name = $right->{attributes}{name};
618 $symbols->{nodes}{$right_name} = 1;
619  
620 my ($right_port, $right_compass) = _parse_port(
621 $right->{attributes}{port},
622 $right->{attributes}{value},
623 $symbols,
624 );
625  
626 my %right_edge_attr = _collect_attributes_from_node($right, $symbols);
627 %edge_attr = (%edge_attr, %right_edge_attr);
628  
629 my $merged_attr = _merge_attrs($edge_defaults, \%edge_attr);
630  
631 for my $ln (@left_nodes) {
632 my $edge = {
633 from => $ln->{name},
634 from_port => $ln->{port},
635 from_compass => $ln->{compass},
636 to => $right_name,
637 to_port => $right_port,
638 to_compass => $right_compass,
639 attrs => $merged_attr,
640 };
641 push @$edges, $edge;
642 }
643  
644 if (!exists $nodes->{$right_name}) {
645 my $attrs = _merge_attrs($node_defaults, {});
646 $attrs->{_auto_declared} = 1;
647 $nodes->{$right_name} = { attrs => $attrs };
648 push @$node_order, $right_name;
649 }
650  
651 $i++;
652 next;
653 }
654 elsif ($rname eq 'literal' && ($right->{attributes}{type} // '') eq 'brace_literal') {
655 # { B C } -> { D E }
656 for my $child (@{ $right->{daughters} || [] }) {
657 next unless ($child->{name} // '') eq 'node_id';
658 my $right_name = $child->{attributes}{name};
659 $symbols->{nodes}{$right_name} = 1;
660  
661 my ($right_port, $right_compass) = _parse_port(
662 $child->{attributes}{port},
663 $child->{attributes}{value},
664 $symbols,
665 );
666  
667 my %right_edge_attr = _collect_attributes_from_node($child, $symbols);
668 my %ea = (%edge_attr, %right_edge_attr);
669 my $merged_attr = _merge_attrs($edge_defaults, \%ea);
670  
671 for my $ln (@left_nodes) {
672 my $edge = {
673 from => $ln->{name},
674 from_port => $ln->{port},
675 from_compass => $ln->{compass},
676 to => $right_name,
677 to_port => $right_port,
678 to_compass => $right_compass,
679 attrs => $merged_attr,
680 };
681 push @$edges, $edge;
682 }
683  
684 if (!exists $nodes->{$right_name}) {
685 my $attrs = _merge_attrs($node_defaults, {});
686 $attrs->{_auto_declared} = 1;
687 $nodes->{$right_name} = { attrs => $attrs };
688 push @$node_order, $right_name;
689 }
690 }
691  
692 $i++;
693 next;
694 }
695 else {
696 last;
697 }
698 }
699  
700 return $i - 1;
701 }
702  
703 sub _collect_attributes_from_node {
704 my ($node, $symbols) = @_;
705  
706 my %attr;
707  
708 for my $c (@{ $node->{daughters} || [] }) {
709 next unless ($c->{name} // '') eq 'attribute';
710 my $ct = $c->{attributes}{type} || $c->{attributes}{name};
711 my $val = $c->{attributes}{value};
712 $attr{$ct} = $val;
713 $symbols->{attrs}{$ct} = 1;
714 }
715  
716 return %attr;
717 }
718  
719 sub _parse_port {
720 my ($port_field, $value_field, $symbols) = @_;
721  
722 my $raw = $port_field;
723  
724 if (!defined $raw || $raw eq '') {
725 if (defined $value_field && $value_field =~ /:(.+)$/) {
726 $raw = $1;
727 }
728 }
729  
730 return ('', '') unless defined $raw && $raw ne '';
731  
732 # Handle compass-only (:n, :s, etc.)
733 if ($raw =~ /^(n|s|e|w|ne|nw|se|sw)$/) {
734 $symbols->{ports}{"::$raw"} = 1;
735 return ('', $raw);
736 }
737  
738 my ($port, $compass) = split /:/, $raw, 2;
739 $compass //= '';
740  
741 # Validate compass.
742 if ($compass ne '' && $compass !~ /^(n|s|e|w|ne|nw|se|sw)$/) {
743 $compass = '';
744 }
745  
746 if ($port ne '') {
747 my $key = $port;
748 $key .= ":$compass" if $compass ne '';
749 $symbols->{ports}{$key} = 1;
750 }
751  
752 return ($port, $compass);
753 }
754  
755 sub _merge_attrs {
756 my ($defaults, $specific) = @_;
757  
758 my %merged = (%{ $defaults || {} }, %{ $specific || {} });
759 return \%merged;
760 }
761  
762 sub _build_adjacency {
763 my ($edges, $kind) = @_;
764  
765 my %adj;
766  
767 for my $e (@{ $edges || [] }) {
768 my $from = $e->{from};
769 my $to = $e->{to};
770  
771 push @{ $adj{$from} ||= [] }, $to;
772  
773 if ($kind && $kind eq 'graph') {
774 push @{ $adj{$to} ||= [] }, $from;
775 }
776 }
777  
778 return \%adj;
779 }
780  
781 sub _build_flat_and_scoped {
782 my ($graph) = @_;
783  
784 my %flat_nodes;
785 my @flat_edges;
786 my %adj_scoped;
787  
788 _sub_flat_collect($graph, \%flat_nodes, \@flat_edges, \%adj_scoped);
789  
790 $graph->{flat} = {
791 nodes => \%flat_nodes,
792 edges => \@flat_edges,
793 };
794  
795 $graph->{adjacency_scoped} = \%adj_scoped;
796 }
797  
798 sub _sub_flat_collect {
799 my ($g, $flat_nodes, $flat_edges, $adj_scoped) = @_;
800  
801 $adj_scoped->{ $g->{name} } = $g->{adjacency};
802  
803 for my $n (@{ $g->{node_order} || [] }) {
804 $flat_nodes->{$n} ||= $g->{nodes}{$n};
805 }
806  
807 for my $e (@{ $g->{edges} || [] }) {
808 push @$flat_edges, $e;
809 }
810  
811 for my $sg (@{ $g->{subgraphs} || [] }) {
812 _sub_flat_collect($sg, $flat_nodes, $flat_edges, $adj_scoped);
813 }
814 }
815  
816 sub _extract_constraints {
817 my ($graph) = @_;
818  
819 my %constraints = (
820 ranks => [],
821 clusters => [],
822 separations => [],
823 visibility => [],
824 );
825  
826 _collect_constraints_recursive($graph, \%constraints);
827  
828 $graph->{constraints} = \%constraints;
829 }
830  
831 sub _collect_constraints_recursive {
832 my ($g, $constraints) = @_;
833  
834 my $name = $g->{name};
835 my $kind = $g->{kind};
836 my $attrs = $g->{attrs} || {};
837  
838 # Rank constraints from subgraph attrs.
839 if (exists $attrs->{rank}) {
840 my $type = $attrs->{rank};
841 my @nodes = @{ $g->{node_order} || [] };
842 push @{ $constraints->{ranks} }, {
843 scope => $name,
844 type => $type,
845 nodes => \@nodes,
846 };
847 }
848  
849 # Cluster constraints.
850 if ($kind && $kind eq 'cluster') {
851 my @nodes = @{ $g->{node_order} || [] };
852 my %c = (
853 name => $name,
854 nodes => \@nodes,
855 );
856 $c{label} = $attrs->{label} if exists $attrs->{label};
857 push @{ $constraints->{clusters} }, \%c;
858 }
859  
860 # Edge-based constraints: minlen, constraint, style=invis, weight.
861 for my $e (@{ $g->{edges} || [] }) {
862 my $ea = $e->{attrs} || {};
863  
864 if (exists $ea->{minlen}) {
865 push @{ $constraints->{separations} }, {
866 from => $e->{from},
867 to => $e->{to},
868 min_dist => $ea->{minlen},
869 scope => $name,
870 };
871 }
872  
873 if (exists $ea->{constraint}) {
874 push @{ $constraints->{visibility} }, {
875 from => $e->{from},
876 to => $e->{to},
877 constraint => $ea->{constraint},
878 scope => $name,
879 };
880 }
881  
882 if (exists $ea->{style} && $ea->{style} eq 'invis') {
883 push @{ $constraints->{visibility} }, {
884 from => $e->{from},
885 to => $e->{to},
886 style => 'invis',
887 scope => $name,
888 };
889 }
890  
891 if (exists $ea->{weight}) {
892 push @{ $constraints->{separations} }, {
893 from => $e->{from},
894 to => $e->{to},
895 weight => $ea->{weight},
896 scope => $name,
897 };
898 }
899 }
900  
901 for my $sg (@{ $g->{subgraphs} || [] }) {
902 _collect_constraints_recursive($sg, $constraints);
903 }
904 }
905  
906 # Simple debug renderer: logs what it would render.
907 sub render_to_stdout {
908 my ($graph) = @_;
909 _print_graph($graph, 0);
910 }
911  
912 sub _print_graph {
913 my ($g, $depth) = @_;
914 my $indent = ' ' x $depth;
915  
916 print "${indent}Render $g->{kind} '$g->{name}'\n";
917  
918 print "${indent} Nodes (in order):\n";
919 for my $n (@{ $g->{node_order} || [] }) {
920 my $attrs = $g->{nodes}{$n}{attrs} || {};
921 print "${indent} node $n";
922 if (%$attrs) {
923 my @pairs = map { "$_=$attrs->{$_}" } sort keys %$attrs;
924 print " [", join(' ', @pairs), "]";
925 }
926 print "\n";
927 }
928  
929 print "${indent} Edges:\n";
930 for my $e (@{ $g->{edges} || [] }) {
931 my $from = $e->{from};
932 my $to = $e->{to};
933 print "${indent} $from";
934 print ":$e->{from_port}" if $e->{from_port};
935 print ":$e->{from_compass}" if $e->{from_compass};
936 print " -> $to";
937 print ":$e->{to_port}" if $e->{to_port};
938 print ":$e->{to_compass}" if $e->{to_compass};
939  
940 my $attrs = $e->{attrs} || {};
941 if (%$attrs) {
942 my @pairs = map { "$_=$attrs->{$_}" } sort keys %$attrs;
943 print " [", join(' ', @pairs), "]";
944 }
945 print "\n";
946 }
947  
948 for my $sg (@{ $g->{subgraphs} || [] }) {
949 _print_graph($sg, $depth + 1);
950 }
951 }
952  
953 1;