| 1 | |||
| 2 | package Graph::Easy::Introspect ; | ||
| 3 | |||
| 4 | use strict ; | ||
| 5 | use warnings ; | ||
| 6 | |||
| 7 | our $VERSION = '0.01' ; | ||
| 8 | |||
| 9 | use Graph::Easy::Edge::Cell qw( | ||
| 10 | EDGE_HOR EDGE_VER EDGE_CROSS EDGE_HOLE | ||
| 11 | EDGE_N_E EDGE_N_W EDGE_S_E EDGE_S_W | ||
| 12 | EDGE_START_E EDGE_START_W EDGE_START_N EDGE_START_S | ||
| 13 | EDGE_END_E EDGE_END_W EDGE_END_N EDGE_END_S | ||
| 14 | ) ; | ||
| 15 | |||
| 16 | use constant EDGE_TYPE_MASK => 0x000F ; | ||
| 17 | use constant EDGE_END_MASK => 0x00F0 ; | ||
| 18 | use constant EDGE_START_MASK => 0x0F00 ; | ||
| 19 | use constant EDGE_LABEL_CELL => 0x1000 ; | ||
| 20 | |||
| 21 | my %CELL_TYPE_NAME = | ||
| 22 | ( | ||
| 23 | EDGE_CROSS() => 'CROSS', | ||
| 24 | EDGE_HOR() => 'HOR', | ||
| 25 | EDGE_VER() => 'VER', | ||
| 26 | EDGE_N_E() => 'N_E', | ||
| 27 | EDGE_N_W() => 'N_W', | ||
| 28 | EDGE_S_E() => 'S_E', | ||
| 29 | EDGE_S_W() => 'S_W', | ||
| 30 | EDGE_HOLE() => 'HOLE', | ||
| 31 | 7 => 'S_E_W', | ||
| 32 | 8 => 'N_E_W', | ||
| 33 | 9 => 'E_N_S', | ||
| 34 | 10 => 'W_N_S', | ||
| 35 | 12 => 'N_W_S', | ||
| 36 | 13 => 'S_W_N', | ||
| 37 | 14 => 'E_S_W', | ||
| 38 | 15 => 'W_S_E', | ||
| 39 | ) ; | ||
| 40 | |||
| 41 | our $WRAPPER_INSTALLED = 0 ; | ||
| 42 | |||
| 43 | # ------------------------------------------------------------------------------ | ||
| 44 | |||
| 45 | sub install_layout_wrapper | ||
| 46 | { | ||
| 47 | return if $WRAPPER_INSTALLED ; | ||
| 48 | |||
| 49 | require Graph::Easy::Layout::Grid ; | ||
| 50 | |||
| 51 | { | ||
| 52 | no warnings 'redefine' ; | ||
| 53 | |||
| 54 | my $orig = \&Graph::Easy::_prepare_layout ; | ||
| 55 | |||
| 56 | *Graph::Easy::_prepare_layout = sub | ||
| 57 | { | ||
| 58 | my ($self, $format) = @_ ; | ||
| 59 | |||
| 60 | my ($rows, $cols, $max_x, $max_y) = $orig->($self, $format) ; | ||
| 61 | |||
| 62 | if (($format // '') eq 'ascii' && !$self->{_introspect_captured}) | ||
| 63 | { | ||
| 64 | my $align = eval { $self->attribute('align') } // 'left' ; | ||
| 65 | my ($label) = eval { $self->_aligned_label($align) } ; | ||
| 66 | $label //= [] ; | ||
| 67 | my $label_pos = eval { $self->attribute('graph', 'label-pos') } // 'top' ; | ||
| 68 | |||
| 69 | my $y_start = 0 ; | ||
| 70 | my $x_start = 0 ; | ||
| 71 | |||
| 72 | if (@$label > 0) | ||
| 73 | { | ||
| 74 | unshift @$label, '' ; | ||
| 75 | push @$label, '' ; | ||
| 76 | |||
| 77 | $y_start = scalar @$label if $label_pos eq 'top' ; | ||
| 78 | |||
| 79 | my $old_max_x = $max_x ; | ||
| 80 | |||
| 81 | for my $l (@$label) | ||
| 82 | { | ||
| 83 | $max_x = length($l) + 2 if length($l) > $max_x + 2 ; | ||
| 84 | } | ||
| 85 | |||
| 86 | $x_start = int(($max_x - $old_max_x) / 2) ; | ||
| 87 | } | ||
| 88 | |||
| 89 | my %char_pos ; | ||
| 90 | |||
| 91 | for my $c (values %{$self->{cells}}) | ||
| 92 | { | ||
| 93 | $char_pos{"$c->{x},$c->{y}"} = | ||
| 94 | { | ||
| 95 | char_x => ($cols->{$c->{x}} // 0) + $x_start, | ||
| 96 | char_y => ($rows->{$c->{y}} // 0) + $y_start, | ||
| 97 | render_w => $c->{w} // 0, | ||
| 98 | render_h => $c->{h} // 0, | ||
| 99 | ref => ref($c), | ||
| 100 | name => ($c->can('name') ? ($c->name // '') : ''), | ||
| 101 | } ; | ||
| 102 | } | ||
| 103 | |||
| 104 | $self->{_introspect_char_pos} = \%char_pos ; | ||
| 105 | $self->{_introspect_captured} = 1 ; | ||
| 106 | } | ||
| 107 | |||
| 108 | return ($rows, $cols, $max_x, $max_y) ; | ||
| 109 | } ; | ||
| 110 | } | ||
| 111 | |||
| 112 | $WRAPPER_INSTALLED = 1 ; | ||
| 113 | } | ||
| 114 | |||
| 115 | # ------------------------------------------------------------------------------ | ||
| 116 | |||
| 117 | sub node_bbox | ||
| 118 | { | ||
| 119 | my ($node) = @_ ; | ||
| 120 | |||
| 121 | my $x = $node->x // 0 ; | ||
| 122 | my $y = $node->y // 0 ; | ||
| 123 | my $cx = $node->{cx} // 1 ; | ||
| 124 | my $cy = $node->{cy} // 1 ; | ||
| 125 | |||
| 126 | return ($x, $y, $x + $cx - 1, $y + $cy - 1) ; | ||
| 127 | } | ||
| 128 | |||
| 129 | # ------------------------------------------------------------------------------ | ||
| 130 | |||
| 131 | sub port_side | ||
| 132 | { | ||
| 133 | my ($node, $port) = @_ ; | ||
| 134 | |||
| 135 | return undef unless $port ; | ||
| 136 | |||
| 137 | my ($x1, $y1, $x2, $y2) = node_bbox($node) ; | ||
| 138 | my ($px, $py) = @{$port}{qw/x y/} ; | ||
| 139 | |||
| 140 | return 'right' if $px > $x2 && $py >= $y1 && $py <= $y2 ; | ||
| 141 | return 'left' if $px < $x1 && $py >= $y1 && $py <= $y2 ; | ||
| 142 | return 'bottom' if $py > $y2 && $px >= $x1 && $px <= $x2 ; | ||
| 143 | return 'top' if $py < $y1 && $px >= $x1 && $px <= $x2 ; | ||
| 144 | |||
| 145 | return 'unknown' ; | ||
| 146 | } | ||
| 147 | |||
| 148 | # ------------------------------------------------------------------------------ | ||
| 149 | |||
| 150 | sub cell_type_name | ||
| 151 | { | ||
| 152 | my ($type_code) = @_ ; | ||
| 153 | |||
| 154 | return $CELL_TYPE_NAME{$type_code} // "UNKNOWN_$type_code" ; | ||
| 155 | } | ||
| 156 | |||
| 157 | # ------------------------------------------------------------------------------ | ||
| 158 | |||
| 159 | sub extract_attrs | ||
| 160 | { | ||
| 161 | my ($obj) = @_ ; | ||
| 162 | |||
| 163 | my $att = $obj->{att} ; | ||
| 164 | return {} unless defined $att && ref $att eq 'HASH' ; | ||
| 165 | |||
| 166 | my %attrs ; | ||
| 167 | |||
| 168 | for my $key (sort keys %$att) | ||
| 169 | { | ||
| 170 | my $val = $att->{$key} ; | ||
| 171 | next unless defined $val ; | ||
| 172 | next if ref $val ; | ||
| 173 | next if $val eq '' ; | ||
| 174 | $attrs{$key} = $val ; | ||
| 175 | } | ||
| 176 | |||
| 177 | return \%attrs ; | ||
| 178 | } | ||
| 179 | |||
| 180 | # ------------------------------------------------------------------------------ | ||
| 181 | |||
| 182 | sub extract_graph_attrs | ||
| 183 | { | ||
| 184 | my ($g) = @_ ; | ||
| 185 | |||
| 186 | my $att = $g->{att} ; | ||
| 187 | return {} unless defined $att && ref $att eq 'HASH' ; | ||
| 188 | |||
| 189 | my $graph_att = $att->{graph} ; | ||
| 190 | return {} unless defined $graph_att && ref $graph_att eq 'HASH' ; | ||
| 191 | |||
| 192 | my %attrs ; | ||
| 193 | |||
| 194 | for my $key (sort keys %$graph_att) | ||
| 195 | { | ||
| 196 | my $val = $graph_att->{$key} ; | ||
| 197 | next unless defined $val ; | ||
| 198 | next if ref $val ; | ||
| 199 | next if $val eq '' ; | ||
| 200 | $attrs{$key} = $val ; | ||
| 201 | } | ||
| 202 | |||
| 203 | return \%attrs ; | ||
| 204 | } | ||
| 205 | |||
| 206 | # ------------------------------------------------------------------------------ | ||
| 207 | |||
| 208 | sub compute_components | ||
| 209 | { | ||
| 210 | my ($g) = @_ ; | ||
| 211 | |||
| 212 | my %adj ; | ||
| 213 | |||
| 214 | for my $e ($g->edges) | ||
| 215 | { | ||
| 216 | my $f = $e->from->name ; | ||
| 217 | my $t = $e->to->name ; | ||
| 218 | |||
| 219 | push @{$adj{$f}}, $t ; | ||
| 220 | push @{$adj{$t}}, $f unless $f eq $t ; | ||
| 221 | } | ||
| 222 | |||
| 223 | my %component ; | ||
| 224 | my $comp_id = 0 ; | ||
| 225 | |||
| 226 | for my $node (sort { $a->name cmp $b->name } $g->nodes) | ||
| 227 | { | ||
| 228 | my $name = $node->name ; | ||
| 229 | next if exists $component{$name} ; | ||
| 230 | |||
| 231 | my @queue = ($name) ; | ||
| 232 | |||
| 233 | while (@queue) | ||
| 234 | { | ||
| 235 | my $n = shift @queue ; | ||
| 236 | next if exists $component{$n} ; | ||
| 237 | |||
| 238 | $component{$n} = $comp_id ; | ||
| 239 | |||
| 240 | push @queue, grep { !exists $component{$_} } @{$adj{$n} || []} ; | ||
| 241 | } | ||
| 242 | |||
| 243 | $comp_id++ ; | ||
| 244 | } | ||
| 245 | |||
| 246 | return %component ; | ||
| 247 | } | ||
| 248 | |||
| 249 | # ------------------------------------------------------------------------------ | ||
| 250 | |||
| 251 | sub sorted_nodes | ||
| 252 | { | ||
| 253 | my ($g) = @_ ; | ||
| 254 | |||
| 255 | return sort { $a->name cmp $b->name } $g->nodes ; | ||
| 256 | } | ||
| 257 | |||
| 258 | # ------------------------------------------------------------------------------ | ||
| 259 | |||
| 260 | sub sorted_edges | ||
| 261 | { | ||
| 262 | my ($g) = @_ ; | ||
| 263 | |||
| 264 | return sort | ||
| 265 | { | ||
| 266 | $a->from->name cmp $b->from->name || | ||
| 267 | $a->to->name cmp $b->to->name | ||
| 268 | } $g->edges ; | ||
| 269 | } | ||
| 270 | |||
| 271 | # ------------------------------------------------------------------------------ | ||
| 272 | |||
| 273 | sub group_char_bbox | ||
| 274 | { | ||
| 275 | my ($g, $g_ast, $char_pos) = @_ ; | ||
| 276 | |||
| 277 | my $bx1 = $g_ast->{bbox}{x1} ; | ||
| 278 | my $by1 = $g_ast->{bbox}{y1} ; | ||
| 279 | my $bx2 = $g_ast->{bbox}{x2} ; | ||
| 280 | my $by2 = $g_ast->{bbox}{y2} ; | ||
| 281 | |||
| 282 | my $tl_key = ($bx1 - 1) . ',' . ($by1 - 1) ; | ||
| 283 | my $br_key = ($bx2 + 1) . ',' . ($by2 + 1) ; | ||
| 284 | |||
| 285 | my $tl = $char_pos->{$tl_key} ; | ||
| 286 | my $br = $char_pos->{$br_key} ; | ||
| 287 | |||
| 288 | return undef unless $tl && $br ; | ||
| 289 | |||
| 290 | return | ||
| 291 | { | ||
| 292 | x => $tl->{char_x}, | ||
| 293 | y => $tl->{char_y}, | ||
| 294 | x2 => $br->{char_x} + $br->{render_w} - 1, | ||
| 295 | y2 => $br->{char_y} + $br->{render_h} - 1, | ||
| 296 | w => ($br->{char_x} + $br->{render_w} - 1) - $tl->{char_x} + 1, | ||
| 297 | h => ($br->{char_y} + $br->{render_h} - 1) - $tl->{char_y} + 1, | ||
| 298 | } ; | ||
| 299 | } | ||
| 300 | |||
| 301 | # ------------------------------------------------------------------------------ | ||
| 302 | # Compute the char-space face coordinates for an edge port attachment point. | ||
| 303 | # Returns the point ON the node face (border character row/col) at which the | ||
| 304 | # edge enters or leaves the node. This is distinct from the edge cell's | ||
| 305 | # top-left corner, which for short single-cell edges is the same cell for | ||
| 306 | # both from_port and to_port. | ||
| 307 | |||
| 308 | sub face_char | ||
| 309 | { | ||
| 310 | my ($node_ast, $side) = @_ ; | ||
| 311 | |||
| 312 | my $nx = $node_ast->{char_x} ; | ||
| 313 | my $ny = $node_ast->{char_y} ; | ||
| 314 | my $nw = $node_ast->{char_width} ; | ||
| 315 | my $nh = $node_ast->{char_height} ; | ||
| 316 | |||
| 317 | # Use intrinsic width/height for the center calculation. | ||
| 318 | # Group membership expands char_width but the edge path uses the intrinsic | ||
| 319 | # node center, so int(intrinsic/2) gives the correct column/row offset. | ||
| 320 | my $iw = $node_ast->{width} || $nw ; | ||
| 321 | my $ih = $node_ast->{height} || $nh ; | ||
| 322 | |||
| 323 | return ($nx + $nw, $ny + int($ih / 2)) if $side eq 'right' ; | ||
| 324 | return ($nx - 1, $ny + int($ih / 2)) if $side eq 'left' ; | ||
| 325 | return ($nx + int($iw / 2), $ny + $nh) if $side eq 'bottom' ; | ||
| 326 | return ($nx + int($iw / 2), $ny - 1) if $side eq 'top' ; | ||
| 327 | |||
| 328 | return ($nx, $ny) ; | ||
| 329 | } | ||
| 330 | |||
| 331 | # ------------------------------------------------------------------------------ | ||
| 332 | |||
| 333 | package Graph::Easy::Edge ; | ||
| 334 | |||
| 335 | use strict ; | ||
| 336 | use warnings ; | ||
| 337 | |||
| 338 | sub path | ||
| 339 | { | ||
| 340 | my ($self) = @_ ; | ||
| 341 | |||
| 342 | return [ map { [$_->{x}, $_->{y}] } @{$self->{cells} || []} ] ; | ||
| 343 | } | ||
| 344 | |||
| 345 | # ------------------------------------------------------------------------------ | ||
| 346 | |||
| 347 | sub from_port | ||
| 348 | { | ||
| 349 | my ($self) = @_ ; | ||
| 350 | |||
| 351 | return undef if $self->from->name eq $self->to->name ; | ||
| 352 | |||
| 353 | my $from = $self->from ; | ||
| 354 | my ($x1, $y1, $x2, $y2) = Graph::Easy::Introspect::node_bbox($from) ; | ||
| 355 | |||
| 356 | my @adjacent = grep | ||
| 357 | { | ||
| 358 | my ($cx, $cy) = ($_->{x}, $_->{y}) ; | ||
| 359 | ($cx > $x2 && $cy >= $y1 && $cy <= $y2) || | ||
| 360 | ($cx < $x1 && $cy >= $y1 && $cy <= $y2) || | ||
| 361 | ($cy > $y2 && $cx >= $x1 && $cx <= $x2) || | ||
| 362 | ($cy < $y1 && $cx >= $x1 && $cx <= $x2) | ||
| 363 | } @{$self->{cells} || []} ; | ||
| 364 | |||
| 365 | return undef unless @adjacent ; | ||
| 366 | |||
| 367 | my $ncx = ($x1 + $x2) / 2 ; | ||
| 368 | my $ncy = ($y1 + $y2) / 2 ; | ||
| 369 | |||
| 370 | my ($best) = sort | ||
| 371 | { | ||
| 372 | abs($a->{x} - $ncx) + abs($a->{y} - $ncy) | ||
| 373 | <=> | ||
| 374 | abs($b->{x} - $ncx) + abs($b->{y} - $ncy) | ||
| 375 | } @adjacent ; | ||
| 376 | |||
| 377 | return { x => $best->{x}, y => $best->{y} } ; | ||
| 378 | } | ||
| 379 | |||
| 380 | # ------------------------------------------------------------------------------ | ||
| 381 | |||
| 382 | sub to_port | ||
| 383 | { | ||
| 384 | my ($self) = @_ ; | ||
| 385 | |||
| 386 | return undef if $self->from->name eq $self->to->name ; | ||
| 387 | |||
| 388 | my $to = $self->to ; | ||
| 389 | my ($x1, $y1, $x2, $y2) = Graph::Easy::Introspect::node_bbox($to) ; | ||
| 390 | |||
| 391 | my @adjacent = grep | ||
| 392 | { | ||
| 393 | my ($cx, $cy) = ($_->{x}, $_->{y}) ; | ||
| 394 | ($cx > $x2 && $cy >= $y1 && $cy <= $y2) || | ||
| 395 | ($cx < $x1 && $cy >= $y1 && $cy <= $y2) || | ||
| 396 | ($cy > $y2 && $cx >= $x1 && $cx <= $x2) || | ||
| 397 | ($cy < $y1 && $cx >= $x1 && $cx <= $x2) | ||
| 398 | } @{$self->{cells} || []} ; | ||
| 399 | |||
| 400 | return undef unless @adjacent ; | ||
| 401 | |||
| 402 | my $ncx = ($x1 + $x2) / 2 ; | ||
| 403 | my $ncy = ($y1 + $y2) / 2 ; | ||
| 404 | |||
| 405 | my ($best) = sort | ||
| 406 | { | ||
| 407 | abs($a->{x} - $ncx) + abs($a->{y} - $ncy) | ||
| 408 | <=> | ||
| 409 | abs($b->{x} - $ncx) + abs($b->{y} - $ncy) | ||
| 410 | } @adjacent ; | ||
| 411 | |||
| 412 | return { x => $best->{x}, y => $best->{y} } ; | ||
| 413 | } | ||
| 414 | |||
| 415 | # ------------------------------------------------------------------------------ | ||
| 416 | |||
| 417 | sub arrow_dir | ||
| 418 | { | ||
| 419 | my ($self) = @_ ; | ||
| 420 | |||
| 421 | for my $c (@{$self->{cells} || []}) | ||
| 422 | { | ||
| 423 | my $end = ($c->{type} // 0) & Graph::Easy::Introspect::EDGE_END_MASK ; | ||
| 424 | |||
| 425 | return 'right' if $end == Graph::Easy::Introspect::EDGE_END_E ; | ||
| 426 | return 'left' if $end == Graph::Easy::Introspect::EDGE_END_W ; | ||
| 427 | return 'down' if $end == Graph::Easy::Introspect::EDGE_END_S ; | ||
| 428 | return 'up' if $end == Graph::Easy::Introspect::EDGE_END_N ; | ||
| 429 | } | ||
| 430 | |||
| 431 | return undef ; | ||
| 432 | } | ||
| 433 | |||
| 434 | # ------------------------------------------------------------------------------ | ||
| 435 | |||
| 436 | package Graph::Easy ; | ||
| 437 | |||
| 438 | use strict ; | ||
| 439 | use warnings ; | ||
| 440 | |||
| 441 | sub ast | ||
| 442 | { | ||
| 443 | my ($self) = @_ ; | ||
| 444 | |||
| 445 | Graph::Easy::Introspect::install_layout_wrapper() ; | ||
| 446 | |||
| 447 | delete $self->{_introspect_captured} ; | ||
| 448 | delete $self->{_introspect_char_pos} ; | ||
| 449 | delete $self->{_introspect_grid} ; | ||
| 450 | delete $self->{_introspect_cell_grid} ; | ||
| 451 | |||
| 452 | my $ascii ; | ||
| 453 | |||
| 454 | eval { $ascii = $self->as_ascii } ; | ||
| 455 | |||
| 456 | if ($@) | ||
| 457 | { | ||
| 458 | return | ||
| 459 | { | ||
| 460 | error => "$@", | ||
| 461 | meta => { introspect_version => $Graph::Easy::Introspect::VERSION }, | ||
| 462 | } ; | ||
| 463 | } | ||
| 464 | |||
| 465 | my $char_pos = $self->{_introspect_char_pos} || {} ; | ||
| 466 | my $grid = [ map { [split //, $_] } split /\n/, $ascii ] ; | ||
| 467 | |||
| 468 | my $total_height = scalar @$grid ; | ||
| 469 | my $total_width = 0 ; | ||
| 470 | |||
| 471 | for my $row (@$grid) | ||
| 472 | { | ||
| 473 | $total_width = scalar @$row if scalar @$row > $total_width ; | ||
| 474 | } | ||
| 475 | |||
| 476 | $self->{_introspect_grid} = $grid ; | ||
| 477 | |||
| 478 | my @sorted_nodes = Graph::Easy::Introspect::sorted_nodes($self) ; | ||
| 479 | my @sorted_edges = Graph::Easy::Introspect::sorted_edges($self) ; | ||
| 480 | |||
| 481 | my %node_index ; | ||
| 482 | my $ni = 0 ; | ||
| 483 | |||
| 484 | for my $n (@sorted_nodes) | ||
| 485 | { | ||
| 486 | $node_index{$n->name} = $ni++ ; | ||
| 487 | } | ||
| 488 | |||
| 489 | my %component = Graph::Easy::Introspect::compute_components($self) ; | ||
| 490 | |||
| 491 | my %mult_groups ; | ||
| 492 | |||
| 493 | { | ||
| 494 | my $eid = 0 ; | ||
| 495 | |||
| 496 | for my $e (@sorted_edges) | ||
| 497 | { | ||
| 498 | my $key = $e->from->name . '|' . $e->to->name ; | ||
| 499 | push @{$mult_groups{$key}}, $eid++ ; | ||
| 500 | } | ||
| 501 | } | ||
| 502 | |||
| 503 | my $is_directed = do | ||
| 504 | { | ||
| 505 | my $r = eval { $self->is_directed } ; | ||
| 506 | defined $r ? ($r ? 1 : 0) : 1 ; | ||
| 507 | } ; | ||
| 508 | |||
| 509 | my $graph_attrs = Graph::Easy::Introspect::extract_graph_attrs($self) ; | ||
| 510 | my $graph_label = do { my $l = eval { $self->label } ; (defined $l && $l ne '') ? $l : undef } ; | ||
| 511 | my $layout_algo = eval { $self->attribute('flow') } // 'default' ; | ||
| 512 | |||
| 513 | my @nodes_ast ; | ||
| 514 | |||
| 515 | for my $node (@sorted_nodes) | ||
| 516 | { | ||
| 517 | my $name = $node->name ; | ||
| 518 | my $cell_x = $node->x // 0 ; | ||
| 519 | my $cell_y = $node->y // 0 ; | ||
| 520 | my $cx = $node->{cx} // 1 ; | ||
| 521 | my $cy = $node->{cy} // 1 ; | ||
| 522 | my $cell_key = "$cell_x,$cell_y" ; | ||
| 523 | my $cp = $char_pos->{$cell_key} // {} ; | ||
| 524 | my $char_x = $cp->{char_x} // 0 ; | ||
| 525 | my $char_y = $cp->{char_y} // 0 ; | ||
| 526 | my $char_w = $cp->{render_w} // ($node->width // 0) ; | ||
| 527 | my $char_h = $cp->{render_h} // ($node->height // 0) ; | ||
| 528 | my $label = do { my $l = eval { $node->label } ; (defined $l && $l ne '') ? $l : $name } ; | ||
| 529 | my $is_anon = eval { $node->isa('Graph::Easy::Node::Anon') } ? 1 : 0 ; | ||
| 530 | |||
| 531 | push @nodes_ast, | ||
| 532 | { | ||
| 533 | id => $name, | ||
| 534 | label => $label, | ||
| 535 | is_anon => $is_anon, | ||
| 536 | is_isolated => 0, | ||
| 537 | x => $cell_x, | ||
| 538 | y => $cell_y, | ||
| 539 | char_x => $char_x, | ||
| 540 | char_y => $char_y, | ||
| 541 | char_width => $char_w, | ||
| 542 | char_height => $char_h, | ||
| 543 | width => $node->width // 0, | ||
| 544 | height => $node->height // 0, | ||
| 545 | bbox => | ||
| 546 | { | ||
| 547 | x1 => $cell_x, | ||
| 548 | y1 => $cell_y, | ||
| 549 | x2 => $cell_x + $cx - 1, | ||
| 550 | y2 => $cell_y + $cy - 1, | ||
| 551 | }, | ||
| 552 | component => $component{$name} // 0, | ||
| 553 | groups => [], | ||
| 554 | edges_in => [], | ||
| 555 | edges_out => [], | ||
| 556 | ports => | ||
| 557 | { | ||
| 558 | left => [], | ||
| 559 | right => [], | ||
| 560 | top => [], | ||
| 561 | bottom => [], | ||
| 562 | unknown => [], | ||
| 563 | }, | ||
| 564 | attrs => Graph::Easy::Introspect::extract_attrs($node), | ||
| 565 | } ; | ||
| 566 | } | ||
| 567 | |||
| 568 | my @edges_ast ; | ||
| 569 | my $edge_id = 0 ; | ||
| 570 | |||
| 571 | for my $edge (@sorted_edges) | ||
| 572 | { | ||
| 573 | my $from = $edge->from ; | ||
| 574 | my $to = $edge->to ; | ||
| 575 | |||
| 576 | my $is_self_loop = $from->name eq $to->name ? 1 : 0 ; | ||
| 577 | my $is_bidi = do | ||
| 578 | { | ||
| 579 | my $b = 0 ; | ||
| 580 | $b = 1 if ref($edge) =~ /Bidirectional/i ; | ||
| 581 | $b = 1 if !$b && (eval { $edge->bidirectional } // 0) ; | ||
| 582 | $b ; | ||
| 583 | } ; | ||
| 584 | |||
| 585 | my $from_port = $edge->from_port ; | ||
| 586 | my $to_port = $edge->to_port ; | ||
| 587 | my $arrow_dir = $edge->arrow_dir ; | ||
| 588 | |||
| 589 | my $from_side = Graph::Easy::Introspect::port_side($from, $from_port) ; | ||
| 590 | my $to_side = Graph::Easy::Introspect::port_side($to, $to_port) ; | ||
| 591 | |||
| 592 | my $key = $from->name . '|' . $to->name ; | ||
| 593 | my $multiplicity = scalar @{$mult_groups{$key} || []} ; | ||
| 594 | |||
| 595 | my $edge_label = do { my $l = eval { $edge->label } ; (defined $l && $l ne '') ? $l : undef } ; | ||
| 596 | |||
| 597 | # from_port and to_port char coords are ON the node face, not the edge cell. | ||
| 598 | # This gives correct distinct coords even for single-cell short edges. | ||
| 599 | my $from_ast_node = $nodes_ast[ $node_index{$from->name} ] ; | ||
| 600 | my $to_ast_node = $nodes_ast[ $node_index{$to->name} ] ; | ||
| 601 | |||
| 602 | my $build_port = sub | ||
| 603 | { | ||
| 604 | my ($port, $node_ast, $side) = @_ ; | ||
| 605 | return undef unless $port ; | ||
| 606 | |||
| 607 | my ($cx, $cy) = Graph::Easy::Introspect::face_char($node_ast, $side // 'unknown') ; | ||
| 608 | |||
| 609 | return | ||
| 610 | { | ||
| 611 | x => $port->{x}, | ||
| 612 | y => $port->{y}, | ||
| 613 | char_x => $cx, | ||
| 614 | char_y => $cy, | ||
| 615 | } ; | ||
| 616 | } ; | ||
| 617 | |||
| 618 | my ($label_x, $label_y, $label_char_x, $label_char_y) ; | ||
| 619 | my @path ; | ||
| 620 | |||
| 621 | for my $c (@{$edge->{cells} || []}) | ||
| 622 | { | ||
| 623 | my $type = $c->{type} // 0 ; | ||
| 624 | my $type_base = $type & Graph::Easy::Introspect::EDGE_TYPE_MASK ; | ||
| 625 | my $is_label = ($type & Graph::Easy::Introspect::EDGE_LABEL_CELL) ? 1 : 0 ; | ||
| 626 | my $ckey = "$c->{x},$c->{y}" ; | ||
| 627 | my $cp = $char_pos->{$ckey} // {} ; | ||
| 628 | |||
| 629 | if ($is_label && !defined $label_x) | ||
| 630 | { | ||
| 631 | $label_x = $c->{x} ; | ||
| 632 | $label_y = $c->{y} ; | ||
| 633 | $label_char_x = $cp->{char_x} ; | ||
| 634 | $label_char_y = $cp->{char_y} ; | ||
| 635 | } | ||
| 636 | |||
| 637 | my $cx = $cp->{char_x} // 0 ; | ||
| 638 | my $cy = $cp->{char_y} // 0 ; | ||
| 639 | my $cx2 = $cx + ($cp->{render_w} // 1) - 1 ; | ||
| 640 | my $cy2 = $cy + ($cp->{render_h} // 1) - 1 ; | ||
| 641 | |||
| 642 | push @path, | ||
| 643 | { | ||
| 644 | x => $c->{x}, | ||
| 645 | y => $c->{y}, | ||
| 646 | char_x => $cx, | ||
| 647 | char_y => $cy, | ||
| 648 | char_x2 => $cx2, | ||
| 649 | char_y2 => $cy2, | ||
| 650 | line_x1 => 0, | ||
| 651 | line_y1 => 0, | ||
| 652 | line_x2 => 0, | ||
| 653 | line_y2 => 0, | ||
| 654 | type => Graph::Easy::Introspect::cell_type_name($type_base), | ||
| 655 | type_code => $type_base, | ||
| 656 | is_label => $is_label, | ||
| 657 | } ; | ||
| 658 | } | ||
| 659 | |||
| 660 | # Sort path cells into traversal order by adjacency walk from from_port. | ||
| 661 | # $edge->{cells} is a hash so storage order is undefined. | ||
| 662 | if (!$is_self_loop && $from_port && @path > 1) | ||
| 663 | { | ||
| 664 | my %by_pos = map { my $k = "$_->{x},$_->{y}" ; $k => $_ } @path ; | ||
| 665 | my $start_key = "$from_port->{x},$from_port->{y}" ; | ||
| 666 | |||
| 667 | unless (exists $by_pos{$start_key}) | ||
| 668 | { | ||
| 669 | for my $d ([-1,0],[1,0],[0,-1],[0,1]) | ||
| 670 | { | ||
| 671 | my $k = ($from_port->{x}+$d->[0]) . ',' . ($from_port->{y}+$d->[1]) ; | ||
| 672 | if (exists $by_pos{$k}) { $start_key = $k ; last } | ||
| 673 | } | ||
| 674 | } | ||
| 675 | |||
| 676 | my @sorted ; | ||
| 677 | my %visited ; | ||
| 678 | my $cur = $start_key ; | ||
| 679 | |||
| 680 | while (exists $by_pos{$cur} && !$visited{$cur}) | ||
| 681 | { | ||
| 682 | $visited{$cur} = 1 ; | ||
| 683 | push @sorted, $by_pos{$cur} ; | ||
| 684 | my $c = $by_pos{$cur} ; | ||
| 685 | my $next ; | ||
| 686 | |||
| 687 | for my $d ([-1,0],[1,0],[0,-1],[0,1]) | ||
| 688 | { | ||
| 689 | my $nk = ($c->{x}+$d->[0]) . ',' . ($c->{y}+$d->[1]) ; | ||
| 690 | next unless exists $by_pos{$nk} && !$visited{$nk} ; | ||
| 691 | $next = $nk ; | ||
| 692 | last ; | ||
| 693 | } | ||
| 694 | |||
| 695 | last unless defined $next ; | ||
| 696 | $cur = $next ; | ||
| 697 | } | ||
| 698 | |||
| 699 | @path = @sorted if @sorted == @path ; | ||
| 700 | } | ||
| 701 | |||
| 702 | # Compute line_* for each path cell using a waypoint-based polyline model. | ||
| 703 | # | ||
| 704 | # Waypoints: [from_port_char, corner_0_bend, ..., corner_n_bend, to_port_char] | ||
| 705 | # Each corner cell either introduces a straight run (next cell is VER/HOR) | ||
| 706 | # or terminates one (prev cell is VER/HOR). | ||
| 707 | # Introducing corner: assign wp[ci]->wp[ci+1], then advance ci. | ||
| 708 | # Terminating corner: advance ci first, then assign wp[ci]->wp[ci+1]. | ||
| 709 | # VER/HOR cells: always assign wp[ci]->wp[ci+1], never advance ci. | ||
| 710 | # | ||
| 711 | # This gives contiguous, directed segments: each cell's endpoint equals | ||
| 712 | # the next cell's start point. | ||
| 713 | |||
| 714 | my %is_straight_type = (VER => 1, HOR => 1, CROSS => 1, HOLE => 1) ; | ||
| 715 | |||
| 716 | my ($fp_lx, $fp_ly) = (0, 0) ; | ||
| 717 | my ($tp_lx, $tp_ly) = (0, 0) ; | ||
| 718 | |||
| 719 | unless ($is_self_loop) | ||
| 720 | { | ||
| 721 | ($fp_lx, $fp_ly) = Graph::Easy::Introspect::face_char($from_ast_node, $from_side) | ||
| 722 | if $from_side ne 'unknown' ; | ||
| 723 | ($tp_lx, $tp_ly) = Graph::Easy::Introspect::face_char($to_ast_node, $to_side) | ||
| 724 | if $to_side ne 'unknown' ; | ||
| 725 | } | ||
| 726 | |||
| 727 | # Build mid_x/mid_y for each cell (needed for corner bends). | ||
| 728 | my %cell_mid ; | ||
| 729 | for my $p (@path) | ||
| 730 | { | ||
| 731 | my $cp2 = $char_pos->{"$p->{x},$p->{y}"} // {} ; | ||
| 732 | my $midx = ($cp2->{char_x} // 0) + int(($cp2->{render_w} // 1) / 2) ; | ||
| 733 | my $midy = ($cp2->{char_y} // 0) + int(($cp2->{render_h} // 1) / 2) ; | ||
| 734 | $cell_mid{"$p->{x},$p->{y}"} = [$midx, $midy] ; | ||
| 735 | } | ||
| 736 | |||
| 737 | # Build waypoint list. | ||
| 738 | my @wp = ([$fp_lx, $fp_ly]) ; | ||
| 739 | |||
| 740 | for my $p (@path) | ||
| 741 | { | ||
| 742 | next if $is_straight_type{$p->{type}} ; | ||
| 743 | my $m = $cell_mid{"$p->{x},$p->{y}"} ; | ||
| 744 | push @wp, [$m->[0], $m->[1]] ; | ||
| 745 | } | ||
| 746 | |||
| 747 | push @wp, [$tp_lx, $tp_ly] ; | ||
| 748 | |||
| 749 | # Assign line_* to each cell. | ||
| 750 | my $ci = 0 ; | ||
| 751 | |||
| 752 | for my $i (0 .. $#path) | ||
| 753 | { | ||
| 754 | my $p = $path[$i] ; | ||
| 755 | my $prev = $i > 0 ? $path[$i-1] : undef ; | ||
| 756 | |||
| 757 | if ($is_straight_type{$p->{type}}) | ||
| 758 | { | ||
| 759 | my $next_ci = $ci + 1 < $#wp ? $ci + 1 : $#wp ; | ||
| 760 | $p->{line_x1} = $wp[$ci][0] ; | ||
| 761 | $p->{line_y1} = $wp[$ci][1] ; | ||
| 762 | $p->{line_x2} = $wp[$next_ci][0] ; | ||
| 763 | $p->{line_y2} = $wp[$next_ci][1] ; | ||
| 764 | } | ||
| 765 | else | ||
| 766 | { | ||
| 767 | my $prev_straight = $prev && $is_straight_type{$prev->{type}} ; | ||
| 768 | |||
| 769 | $ci++ if $prev_straight ; | ||
| 770 | |||
| 771 | my $next_ci = $ci + 1 < $#wp ? $ci + 1 : $#wp ; | ||
| 772 | $p->{line_x1} = $wp[$ci][0] ; | ||
| 773 | $p->{line_y1} = $wp[$ci][1] ; | ||
| 774 | $p->{line_x2} = $wp[$next_ci][0] ; | ||
| 775 | $p->{line_y2} = $wp[$next_ci][1] ; | ||
| 776 | |||
| 777 | my $m = $cell_mid{"$p->{x},$p->{y}"} ; | ||
| 778 | $p->{bend_x} = $m->[0] ; | ||
| 779 | $p->{bend_y} = $m->[1] ; | ||
| 780 | |||
| 781 | $ci++ unless $prev_straight ; | ||
| 782 | } | ||
| 783 | } | ||
| 784 | |||
| 785 | push @edges_ast, | ||
| 786 | { | ||
| 787 | id => $edge_id, | ||
| 788 | from => $from->name, | ||
| 789 | to => $to->name, | ||
| 790 | is_self_loop => $is_self_loop, | ||
| 791 | is_bidirectional => $is_bidi, | ||
| 792 | multiplicity => $multiplicity, | ||
| 793 | arrow_dir => $arrow_dir, | ||
| 794 | from_port => $build_port->($from_port, $from_ast_node, $from_side), | ||
| 795 | to_port => $build_port->($to_port, $to_ast_node, $to_side), | ||
| 796 | from_side => $from_side, | ||
| 797 | to_side => $to_side, | ||
| 798 | label => $edge_label, | ||
| 799 | label_x => $label_x, | ||
| 800 | label_y => $label_y, | ||
| 801 | label_char_x => $label_char_x, | ||
| 802 | label_char_y => $label_char_y, | ||
| 803 | path => \@path, | ||
| 804 | attrs => Graph::Easy::Introspect::extract_attrs($edge), | ||
| 805 | } ; | ||
| 806 | |||
| 807 | $edge_id++ ; | ||
| 808 | } | ||
| 809 | |||
| 810 | my @groups_ast ; | ||
| 811 | my %group_index ; | ||
| 812 | my $gi = 0 ; | ||
| 813 | |||
| 814 | my @graph_groups ; | ||
| 815 | eval { @graph_groups = $self->groups } ; | ||
| 816 | |||
| 817 | for my $group (sort { $a->name cmp $b->name } @graph_groups) | ||
| 818 | { | ||
| 819 | my $gname = $group->name ; | ||
| 820 | my @gnodes = eval { map { $_->name } $group->nodes } ; | ||
| 821 | |||
| 822 | $group_index{$gname} = $gi ; | ||
| 823 | |||
| 824 | my ($bx1, $by1, $bx2, $by2) ; | ||
| 825 | |||
| 826 | for my $nname (@gnodes) | ||
| 827 | { | ||
| 828 | my $n = $self->node($nname) ; | ||
| 829 | next unless $n ; | ||
| 830 | |||
| 831 | my ($nx1, $ny1, $nx2, $ny2) = Graph::Easy::Introspect::node_bbox($n) ; | ||
| 832 | |||
| 833 | $bx1 = $nx1 if !defined $bx1 || $nx1 < $bx1 ; | ||
| 834 | $by1 = $ny1 if !defined $by1 || $ny1 < $by1 ; | ||
| 835 | $bx2 = $nx2 if !defined $bx2 || $nx2 > $bx2 ; | ||
| 836 | $by2 = $ny2 if !defined $by2 || $ny2 > $by2 ; | ||
| 837 | } | ||
| 838 | |||
| 839 | my $cell_bbox = | ||
| 840 | { | ||
| 841 | x1 => $bx1 // 0, | ||
| 842 | y1 => $by1 // 0, | ||
| 843 | x2 => $bx2 // 0, | ||
| 844 | y2 => $by2 // 0, | ||
| 845 | } ; | ||
| 846 | |||
| 847 | my $glabel = do { my $l = eval { $group->label } ; (defined $l && $l ne '') ? $l : $gname } ; | ||
| 848 | |||
| 849 | my $gcb = Graph::Easy::Introspect::group_char_bbox($self, { bbox => $cell_bbox }, $char_pos) ; | ||
| 850 | |||
| 851 | push @groups_ast, | ||
| 852 | { | ||
| 853 | id => $gname, | ||
| 854 | label => $glabel, | ||
| 855 | nodes => \@gnodes, | ||
| 856 | bbox => $cell_bbox, | ||
| 857 | char_x => $gcb ? $gcb->{x} : 0, | ||
| 858 | char_y => $gcb ? $gcb->{y} : 0, | ||
| 859 | char_width => $gcb ? $gcb->{w} : 0, | ||
| 860 | char_height => $gcb ? $gcb->{h} : 0, | ||
| 861 | attrs => Graph::Easy::Introspect::extract_attrs($group), | ||
| 862 | } ; | ||
| 863 | |||
| 864 | $gi++ ; | ||
| 865 | } | ||
| 866 | |||
| 867 | $edge_id = 0 ; | ||
| 868 | |||
| 869 | for my $e_ast (@edges_ast) | ||
| 870 | { | ||
| 871 | my $from_idx = $node_index{$e_ast->{from}} ; | ||
| 872 | my $to_idx = $node_index{$e_ast->{to}} ; | ||
| 873 | my $eid = $e_ast->{id} ; | ||
| 874 | |||
| 875 | push @{$nodes_ast[$from_idx]{edges_out}}, $eid ; | ||
| 876 | push @{$nodes_ast[$to_idx]{edges_in}}, $eid ; | ||
| 877 | |||
| 878 | if ($e_ast->{from_port}) | ||
| 879 | { | ||
| 880 | my $side = $e_ast->{from_side} || 'unknown' ; | ||
| 881 | |||
| 882 | push @{$nodes_ast[$from_idx]{ports}{$side}}, | ||
| 883 | { | ||
| 884 | edge_id => $eid, | ||
| 885 | role => 'out', | ||
| 886 | x => $e_ast->{from_port}{x}, | ||
| 887 | y => $e_ast->{from_port}{y}, | ||
| 888 | char_x => $e_ast->{from_port}{char_x}, | ||
| 889 | char_y => $e_ast->{from_port}{char_y}, | ||
| 890 | } ; | ||
| 891 | } | ||
| 892 | |||
| 893 | if ($e_ast->{to_port}) | ||
| 894 | { | ||
| 895 | my $side = $e_ast->{to_side} || 'unknown' ; | ||
| 896 | |||
| 897 | push @{$nodes_ast[$to_idx]{ports}{$side}}, | ||
| 898 | { | ||
| 899 | edge_id => $eid, | ||
| 900 | role => 'in', | ||
| 901 | x => $e_ast->{to_port}{x}, | ||
| 902 | y => $e_ast->{to_port}{y}, | ||
| 903 | char_x => $e_ast->{to_port}{char_x}, | ||
| 904 | char_y => $e_ast->{to_port}{char_y}, | ||
| 905 | } ; | ||
| 906 | } | ||
| 907 | |||
| 908 | $edge_id++ ; | ||
| 909 | } | ||
| 910 | |||
| 911 | for my $n_ast (@nodes_ast) | ||
| 912 | { | ||
| 913 | for my $side (qw/left right/) | ||
| 914 | { | ||
| 915 | $n_ast->{ports}{$side} = | ||
| 916 | [sort { $a->{y} <=> $b->{y} } @{$n_ast->{ports}{$side}}] ; | ||
| 917 | } | ||
| 918 | |||
| 919 | for my $side (qw/top bottom/) | ||
| 920 | { | ||
| 921 | $n_ast->{ports}{$side} = | ||
| 922 | [sort { $a->{x} <=> $b->{x} } @{$n_ast->{ports}{$side}}] ; | ||
| 923 | } | ||
| 924 | |||
| 925 | for my $side (qw/left right top bottom unknown/) | ||
| 926 | { | ||
| 927 | my $seq = 0 ; | ||
| 928 | |||
| 929 | for my $p (@{$n_ast->{ports}{$side}}) | ||
| 930 | { | ||
| 931 | $p->{seq} = $seq++ ; | ||
| 932 | } | ||
| 933 | } | ||
| 934 | |||
| 935 | $n_ast->{is_isolated} = | ||
| 936 | (scalar(@{$n_ast->{edges_in}}) == 0 && scalar(@{$n_ast->{edges_out}}) == 0) ? 1 : 0 ; | ||
| 937 | } | ||
| 938 | |||
| 939 | for my $g_ast (@groups_ast) | ||
| 940 | { | ||
| 941 | for my $nname (@{$g_ast->{nodes}}) | ||
| 942 | { | ||
| 943 | my $idx = $node_index{$nname} ; | ||
| 944 | next unless defined $idx ; | ||
| 945 | push @{$nodes_ast[$idx]{groups}}, $g_ast->{id} ; | ||
| 946 | } | ||
| 947 | } | ||
| 948 | |||
| 949 | my %cell_grid ; | ||
| 950 | |||
| 951 | for my $key (keys %$char_pos) | ||
| 952 | { | ||
| 953 | my $cp = $char_pos->{$key} ; | ||
| 954 | my $type = 'unknown' ; | ||
| 955 | |||
| 956 | if ($cp->{ref} =~ /::Node$/) | ||
| 957 | { | ||
| 958 | $type = 'node' ; | ||
| 959 | } | ||
| 960 | elsif ($cp->{ref} =~ /Edge/) | ||
| 961 | { | ||
| 962 | $type = 'edge' ; | ||
| 963 | } | ||
| 964 | elsif ($cp->{ref} =~ /Group/) | ||
| 965 | { | ||
| 966 | $type = 'group' ; | ||
| 967 | } | ||
| 968 | elsif ($cp->{ref} =~ /Node::Cell/) | ||
| 969 | { | ||
| 970 | $type = 'empty' ; | ||
| 971 | } | ||
| 972 | |||
| 973 | $cell_grid{$key} = | ||
| 974 | { | ||
| 975 | type => $type, | ||
| 976 | name => $cp->{name}, | ||
| 977 | char_x => $cp->{char_x}, | ||
| 978 | char_y => $cp->{char_y}, | ||
| 979 | render_w => $cp->{render_w}, | ||
| 980 | render_h => $cp->{render_h}, | ||
| 981 | } ; | ||
| 982 | } | ||
| 983 | |||
| 984 | $self->{_introspect_cell_grid} = \%cell_grid ; | ||
| 985 | |||
| 986 | return | ||
| 987 | { | ||
| 988 | meta => | ||
| 989 | { | ||
| 990 | introspect_version => $Graph::Easy::Introspect::VERSION, | ||
| 991 | graph_easy_version => $Graph::Easy::VERSION, | ||
| 992 | generated_at => time(), | ||
| 993 | layout_algorithm => $layout_algo, | ||
| 994 | }, | ||
| 995 | graph => | ||
| 996 | { | ||
| 997 | is_directed => $is_directed, | ||
| 998 | label => $graph_label, | ||
| 999 | total_width => $total_width, | ||
| 1000 | total_height => $total_height, | ||
| 1001 | attrs => $graph_attrs, | ||
| 1002 | }, | ||
| 1003 | nodes => \@nodes_ast, | ||
| 1004 | edges => \@edges_ast, | ||
| 1005 | groups => \@groups_ast, | ||
| 1006 | } ; | ||
| 1007 | } | ||
| 1008 | |||
| 1009 | # ------------------------------------------------------------------------------ | ||
| 1010 | |||
| 1011 | sub ast_grid | ||
| 1012 | { | ||
| 1013 | my ($self) = @_ ; | ||
| 1014 | |||
| 1015 | return $self->{_introspect_grid} ; | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | # ------------------------------------------------------------------------------ | ||
| 1019 | |||
| 1020 | sub ast_cell_grid | ||
| 1021 | { | ||
| 1022 | my ($self) = @_ ; | ||
| 1023 | |||
| 1024 | return $self->{_introspect_cell_grid} ; | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | 1 ; | ||
| 1028 | |||
| 1029 | __END__ |