| 1 | package MarpaX::Languages::Dash::Renderer; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 6 | use charnames qw(:full :short); # Unneeded in v5.16. | ||
| 7 | |||
| 8 | use GraphViz2; | ||
| 9 | |||
| 10 | use Log::Handler; | ||
| 11 | |||
| 12 | use Moo; | ||
| 13 | |||
| 14 | use Types::Standard qw/Any Int Str/; | ||
| 15 | |||
| 16 | has dot_input_file => | ||
| 17 | ( | ||
| 18 | default => sub{return ''}, | ||
| 19 | is => 'rw', | ||
| 20 | isa => Str, | ||
| 21 | required => 0, | ||
| 22 | ); | ||
| 23 | |||
| 24 | has format => | ||
| 25 | ( | ||
| 26 | default => sub{return 'svg'}, | ||
| 27 | is => 'rw', | ||
| 28 | isa => Str, | ||
| 29 | required => 0, | ||
| 30 | ); | ||
| 31 | |||
| 32 | has graph => | ||
| 33 | ( | ||
| 34 | default => sub{return ''}, | ||
| 35 | is => 'rw', | ||
| 36 | isa => Any, | ||
| 37 | required => 0, | ||
| 38 | ); | ||
| 39 | |||
| 40 | has graphviz_tree => | ||
| 41 | ( | ||
| 42 | default => sub{return 0}, | ||
| 43 | is => 'rw', | ||
| 44 | isa => Int, | ||
| 45 | required => 0, | ||
| 46 | ); | ||
| 47 | |||
| 48 | has logger => | ||
| 49 | ( | ||
| 50 | default => sub{return undef}, | ||
| 51 | is => 'rw', | ||
| 52 | isa => Any, | ||
| 53 | required => 0, | ||
| 54 | ); | ||
| 55 | |||
| 56 | has maxlevel => | ||
| 57 | ( | ||
| 58 | default => sub{return 'notice'}, | ||
| 59 | is => 'rw', | ||
| 60 | isa => Str, | ||
| 61 | required => 0, | ||
| 62 | ); | ||
| 63 | |||
| 64 | has minlevel => | ||
| 65 | ( | ||
| 66 | default => sub{return 'error'}, | ||
| 67 | is => 'rw', | ||
| 68 | isa => Str, | ||
| 69 | required => 0, | ||
| 70 | ); | ||
| 71 | |||
| 72 | has output_file => | ||
| 73 | ( | ||
| 74 | default => sub{return ''}, | ||
| 75 | is => 'rw', | ||
| 76 | isa => Str, | ||
| 77 | required => 0, | ||
| 78 | ); | ||
| 79 | |||
| 80 | has rankdir => | ||
| 81 | ( | ||
| 82 | default => sub{return 'TB'}, | ||
| 83 | is => 'rw', | ||
| 84 | isa => Str, | ||
| 85 | required => 0, | ||
| 86 | ); | ||
| 87 | |||
| 88 | has tree => | ||
| 89 | ( | ||
| 90 | default => sub{return ''}, | ||
| 91 | is => 'rw', | ||
| 92 | isa => Any, | ||
| 93 | required => 0, | ||
| 94 | ); | ||
| 95 | |||
| 96 | has uid => | ||
| 97 | ( | ||
| 98 | default => sub{return 0}, | ||
| 99 | is => 'rw', | ||
| 100 | isa => Int, | ||
| 101 | required => 0, | ||
| 102 | ); | ||
| 103 | |||
| 104 | our $VERSION = '1.00'; | ||
| 105 | |||
| 106 | # -------------------------------------------------- | ||
| 107 | |||
| 108 | sub BUILD | ||
| 109 | { | ||
| 110 | my($self) = @_; | ||
| 111 | |||
| 112 | if (! defined $self -> logger) | ||
| 113 | { | ||
| 114 | $self -> logger(Log::Handler -> new); | ||
| 115 | $self -> logger -> add | ||
| 116 | ( | ||
| 117 | screen => | ||
| 118 | { | ||
| 119 | alias => 'logger', | ||
| 120 | maxlevel => $self -> maxlevel, | ||
| 121 | message_layout => '%m', | ||
| 122 | minlevel => $self -> minlevel, | ||
| 123 | } | ||
| 124 | ); | ||
| 125 | } | ||
| 126 | |||
| 127 | } # End of BUILD. | ||
| 128 | |||
| 129 | # ------------------------------------------------ | ||
| 130 | |||
| 131 | sub _add_anonymous_daughter | ||
| 132 | { | ||
| 133 | my($self, $mother, $i) = @_; | ||
| 134 | my($new_node) = $self -> _create_daughter('node_id', {}); | ||
| 135 | |||
| 136 | $new_node -> add_daughter($self -> _create_daughter('literal', {value => '{'}) ); | ||
| 137 | $new_node -> add_daughter($self -> _create_daughter('color', {value => 'invis'}) ); | ||
| 138 | $new_node -> add_daughter($self -> _create_daughter('label', {value => ''}) ); | ||
| 139 | $new_node -> add_daughter($self -> _create_daughter('shape', {value => 'point'}) ); | ||
| 140 | $new_node -> add_daughter($self -> _create_daughter('width', {value => 0}) ); | ||
| 141 | $new_node -> add_daughter($self -> _create_daughter('literal', {value => '}'}) ); | ||
| 142 | |||
| 143 | my(@daughters) = $mother -> daughters; | ||
| 144 | |||
| 145 | splice(@daughters, $i, 0, $new_node); | ||
| 146 | |||
| 147 | $mother -> set_daughters(@daughters); | ||
| 148 | |||
| 149 | } # End of _add_anonymous_daughter. | ||
| 150 | |||
| 151 | # ------------------------------------------------ | ||
| 152 | |||
| 153 | sub _bump_uid | ||
| 154 | { | ||
| 155 | my($self) = @_; | ||
| 156 | |||
| 157 | return $self -> uid($self -> uid + 1); | ||
| 158 | |||
| 159 | } # End of _bump_uid. | ||
| 160 | |||
| 161 | # ------------------------------------------------ | ||
| 162 | |||
| 163 | sub _create_daughter | ||
| 164 | { | ||
| 165 | my($self, $name, $attributes) = @_; | ||
| 166 | $$attributes{uid} = $self -> _bump_uid; | ||
| 167 | $$attributes{value} = $self -> uid if (! defined $$attributes{value}); # Just for the node_id. | ||
| 168 | |||
| 169 | return Tree::DAG_Node -> new({name => $name, attributes => $attributes}); | ||
| 170 | |||
| 171 | } # End of _create_daughter. | ||
| 172 | |||
| 173 | # -------------------------------------------------- | ||
| 174 | |||
| 175 | sub _determine_digraph_status | ||
| 176 | { | ||
| 177 | my($self) = @_; | ||
| 178 | |||
| 179 | my(@daughters) = $self -> tree -> daughters; | ||
| 180 | my($index) = 1; # 0 => prolog, 1 => graph. | ||
| 181 | my($directed) = 1; | ||
| 182 | |||
| 183 | my($attributes); | ||
| 184 | my($type); | ||
| 185 | |||
| 186 | $daughters[$index] -> walk_down | ||
| 187 | ({ | ||
| 188 | callback => sub | ||
| 189 | { | ||
| 190 | my($node) = @_; | ||
| 191 | $type = $node -> name; | ||
| 192 | |||
| 193 | return 1 if ($type ne 'edge_id'); | ||
| 194 | |||
| 195 | $attributes = $node -> attributes; | ||
| 196 | $directed = 0 if ($$attributes{value} eq '--'); | ||
| 197 | |||
| 198 | # Stop recursing. | ||
| 199 | |||
| 200 | return 0; | ||
| 201 | }, | ||
| 202 | _depth => 0, | ||
| 203 | }); | ||
| 204 | |||
| 205 | return $directed; | ||
| 206 | |||
| 207 | } # End of _determine_digraph_status. | ||
| 208 | |||
| 209 | # -------------------------------------------------- | ||
| 210 | |||
| 211 | sub _find_contiguous_edges | ||
| 212 | { | ||
| 213 | my($self) = @_; | ||
| 214 | |||
| 215 | # Graphviz won't accept a graphs with edges side-by-side, so ... | ||
| 216 | # ... we walk the tree looking for such cases, and stash them. | ||
| 217 | # Then we insert anonymous nodes between any such cases. | ||
| 218 | # The Tree::DAG_Node docs warn against modifying the tree during a walk, | ||
| 219 | # so we need to stash some information and process it later. | ||
| 220 | |||
| 221 | my(@daughters) = $self -> tree -> daughters; | ||
| 222 | my($index) = 1; # 0 => prolog, 1 => graph. | ||
| 223 | |||
| 224 | my($attributes); | ||
| 225 | my($daughter_uid); | ||
| 226 | my($i); | ||
| 227 | my($name); | ||
| 228 | my(@sisters, %seen, @stack); | ||
| 229 | my($mother_uid); | ||
| 230 | |||
| 231 | $daughters[$index] -> walk_down | ||
| 232 | ({ | ||
| 233 | callback => sub | ||
| 234 | { | ||
| 235 | my($node) = @_; | ||
| 236 | |||
| 237 | # Skip the 'graph' node itself. | ||
| 238 | |||
| 239 | return 1 if ($node -> mother -> is_root); | ||
| 240 | |||
| 241 | # Skip everything but edges. | ||
| 242 | |||
| 243 | $name = $node -> name; | ||
| 244 | |||
| 245 | return 1 if ($name ne 'edge_id'); | ||
| 246 | |||
| 247 | # Only process 1 edge per mother, since this code covers all her daughters. | ||
| 248 | |||
| 249 | $attributes = $node -> mother -> attributes; | ||
| 250 | $mother_uid = $$attributes{uid}; | ||
| 251 | |||
| 252 | return 1 if ($seen{$mother_uid}); | ||
| 253 | |||
| 254 | $seen{$mother_uid} = 1; | ||
| 255 | |||
| 256 | @sisters = $node -> self_and_sisters; | ||
| 257 | |||
| 258 | for $i (1 .. $#sisters) | ||
| 259 | { | ||
| 260 | $name = $sisters[$i - 1] -> name; | ||
| 261 | |||
| 262 | next if ($name ne 'edge_id'); | ||
| 263 | |||
| 264 | $name = $sisters[$i] -> name; | ||
| 265 | |||
| 266 | next if ($name ne 'edge_id'); | ||
| 267 | |||
| 268 | push @stack, $node -> mother; | ||
| 269 | |||
| 270 | last; | ||
| 271 | } | ||
| 272 | |||
| 273 | # Keep recursing. | ||
| 274 | |||
| 275 | return 1; | ||
| 276 | }, | ||
| 277 | _depth => 0, | ||
| 278 | }); | ||
| 279 | |||
| 280 | my($added_anonymous_node); | ||
| 281 | |||
| 282 | for my $node (@stack) | ||
| 283 | { | ||
| 284 | $added_anonymous_node = 1; | ||
| 285 | |||
| 286 | while ($added_anonymous_node) | ||
| 287 | { | ||
| 288 | @daughters = $node -> daughters; | ||
| 289 | $added_anonymous_node = 0; | ||
| 290 | |||
| 291 | for $i (1 .. $#daughters) | ||
| 292 | { | ||
| 293 | $name = $daughters[$i - 1] -> name; | ||
| 294 | |||
| 295 | next if ($name ne 'edge_id'); | ||
| 296 | |||
| 297 | $name = $daughters[$i] -> name; | ||
| 298 | |||
| 299 | next if ($name ne 'edge_id'); | ||
| 300 | |||
| 301 | $added_anonymous_node = 1; | ||
| 302 | |||
| 303 | $self -> _add_anonymous_daughter($node, $i); | ||
| 304 | |||
| 305 | last; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 | } # End of _find_contiguous_edges. | ||
| 311 | |||
| 312 | # -------------------------------------------------- | ||
| 313 | |||
| 314 | sub _find_first_edge | ||
| 315 | { | ||
| 316 | my($self) = @_; | ||
| 317 | |||
| 318 | # Graphviz won't accept a graph starting with an edge, so ... | ||
| 319 | # ... we scan the daughters of the 'graph' node, looking for a node or an edge. | ||
| 320 | # If the first thing we find is an edge, we output a dummy, invisible node before it. | ||
| 321 | |||
| 322 | my(@daughters) = $self -> tree -> daughters; | ||
| 323 | my($index) = 1; # 0 => prolog, 1 => graph. | ||
| 324 | |||
| 325 | my($first_edge); | ||
| 326 | my($name); | ||
| 327 | |||
| 328 | for my $daughter ($daughters[$index] -> daughters) | ||
| 329 | { | ||
| 330 | $name = $daughter -> name; | ||
| 331 | |||
| 332 | # If there is a node before the first edge, we quit. | ||
| 333 | |||
| 334 | last if ($name eq 'node_id'); | ||
| 335 | |||
| 336 | # There is no node before the first edge. | ||
| 337 | # If there is an edge, remember it and quit. | ||
| 338 | # If there is no no edge, just quit. | ||
| 339 | |||
| 340 | $first_edge = $daughter if ($name eq 'edge_id'); | ||
| 341 | |||
| 342 | last; | ||
| 343 | } | ||
| 344 | |||
| 345 | if ($first_edge) | ||
| 346 | { | ||
| 347 | $self -> _add_anonymous_daughter($daughters[$index], 0); | ||
| 348 | } | ||
| 349 | |||
| 350 | return $daughters[$index]; | ||
| 351 | |||
| 352 | } # End of _find_first_edge. | ||
| 353 | |||
| 354 | # -------------------------------------------------- | ||
| 355 | |||
| 356 | sub _find_last_edge | ||
| 357 | { | ||
| 358 | my($self) = @_; | ||
| 359 | |||
| 360 | # Graphviz won't accept a graph ending with an edge, so ... | ||
| 361 | # ... we examine the daughters of the 'graph' node, looking for an edge. | ||
| 362 | # If the last thing we find is an edge, we output a dummy, invisible node after it. | ||
| 363 | |||
| 364 | my(@daughters1) = $self -> tree -> daughters; | ||
| 365 | my($index) = 1; # 0 => prolog, 1 => graph. | ||
| 366 | my(@daughters2) = $daughters1[$index] -> daughters; | ||
| 367 | |||
| 368 | if ( ($#daughters2 >= 0) && ($daughters2[$#daughters2] -> name eq 'edge_id') ) | ||
| 369 | { | ||
| 370 | $self -> _add_anonymous_daughter($daughters1[$index], scalar @daughters2); | ||
| 371 | } | ||
| 372 | |||
| 373 | } # End of _find_last_edge. | ||
| 374 | |||
| 375 | # -------------------------------------------------- | ||
| 376 | |||
| 377 | sub _find_head_and_tail | ||
| 378 | { | ||
| 379 | my($self, $node) = @_; | ||
| 380 | my($index) = $node -> my_daughter_index; | ||
| 381 | my(@daughters) = $node -> mother -> daughters; | ||
| 382 | my($from_attributes) = $daughters[$index - 1] -> attributes; | ||
| 383 | my($to_attributes) = $daughters[$index + 1] -> attributes; | ||
| 384 | |||
| 385 | return ($$from_attributes{value}, $$to_attributes{value}); | ||
| 386 | |||
| 387 | } # End of _find_head_and_tail. | ||
| 388 | |||
| 389 | # -------------------------------------------------- | ||
| 390 | |||
| 391 | sub _initialize_uid | ||
| 392 | { | ||
| 393 | my($self) = @_; | ||
| 394 | my($max_uid) = 0; | ||
| 395 | |||
| 396 | my($attributes); | ||
| 397 | my($uid); | ||
| 398 | |||
| 399 | $self -> tree -> walk_down | ||
| 400 | ({ | ||
| 401 | callback => sub | ||
| 402 | { | ||
| 403 | my($node) = @_; | ||
| 404 | $attributes = $node -> attributes; | ||
| 405 | $uid = $$attributes{uid}; | ||
| 406 | $max_uid = $uid if ($uid > $max_uid); | ||
| 407 | |||
| 408 | # Keep recursing. | ||
| 409 | |||
| 410 | return 1; | ||
| 411 | }, | ||
| 412 | _depth => 0, | ||
| 413 | }); | ||
| 414 | |||
| 415 | $self -> uid($max_uid); | ||
| 416 | |||
| 417 | } # End of _initialize_uid. | ||
| 418 | |||
| 419 | # -------------------------------------------------- | ||
| 420 | |||
| 421 | sub log | ||
| 422 | { | ||
| 423 | my($self, $level, $s) = @_; | ||
| 424 | |||
| 425 | $self -> logger -> log($level => $s) if ($self -> logger); | ||
| 426 | |||
| 427 | } # End of log. | ||
| 428 | |||
| 429 | # -------------------------------------------------- | ||
| 430 | |||
| 431 | sub output_all_edges | ||
| 432 | { | ||
| 433 | my($self, $graph_subtree) = @_; | ||
| 434 | |||
| 435 | $graph_subtree -> walk_down | ||
| 436 | ({ | ||
| 437 | callback => sub | ||
| 438 | { | ||
| 439 | my($node) = @_; | ||
| 440 | |||
| 441 | # Skip the 'graph' node itself. | ||
| 442 | |||
| 443 | return 1 if ($node -> mother -> is_root); | ||
| 444 | |||
| 445 | $self -> output_node($node) if ($node -> name eq 'edge_id'); | ||
| 446 | |||
| 447 | # Keep recursing. | ||
| 448 | |||
| 449 | return 1; | ||
| 450 | }, | ||
| 451 | _depth => 0, | ||
| 452 | }); | ||
| 453 | |||
| 454 | } # End of output_all_edges. | ||
| 455 | |||
| 456 | # -------------------------------------------------- | ||
| 457 | |||
| 458 | sub output_all_nodes | ||
| 459 | { | ||
| 460 | my($self, $graph_subtree) = @_; | ||
| 461 | |||
| 462 | my($attributes); | ||
| 463 | my(%seen); | ||
| 464 | my($type); | ||
| 465 | my($uid); | ||
| 466 | |||
| 467 | $graph_subtree -> walk_down | ||
| 468 | ({ | ||
| 469 | callback => sub | ||
| 470 | { | ||
| 471 | my($node) = @_; | ||
| 472 | |||
| 473 | # Skip the 'graph' node itself. | ||
| 474 | |||
| 475 | return 1 if ($node -> mother -> is_root); | ||
| 476 | |||
| 477 | $type = $node -> name; | ||
| 478 | $attributes = $node -> attributes; | ||
| 479 | $uid = $$attributes{value}; | ||
| 480 | |||
| 481 | # Skip any node we've seen before. | ||
| 482 | |||
| 483 | return 1 if ($type ne 'node_id'); | ||
| 484 | return 1 if ($seen{$uid}); | ||
| 485 | |||
| 486 | $seen{$uid} = 1; | ||
| 487 | |||
| 488 | $self -> output_node($node); | ||
| 489 | |||
| 490 | # Keep recursing. | ||
| 491 | |||
| 492 | return 1; | ||
| 493 | }, | ||
| 494 | _depth => 0, | ||
| 495 | }); | ||
| 496 | |||
| 497 | } # End of output_all_nodes. | ||
| 498 | |||
| 499 | # -------------------------------------------------- | ||
| 500 | |||
| 501 | sub output_node | ||
| 502 | { | ||
| 503 | my($self, $node) = @_; | ||
| 504 | my($node_type) = $node -> name; | ||
| 505 | my($node_name) = ${$node -> attributes}{value}; | ||
| 506 | |||
| 507 | # Accumulate all the attributes of the node's daughters. | ||
| 508 | |||
| 509 | my($child_attributes, %node_attributes); | ||
| 510 | my($child_name); | ||
| 511 | |||
| 512 | for my $daughter ($node -> daughters) | ||
| 513 | { | ||
| 514 | $child_name = $daughter -> name; | ||
| 515 | |||
| 516 | # Skip nodes called '{' and '}'. | ||
| 517 | |||
| 518 | next if ($child_name eq 'literal'); | ||
| 519 | |||
| 520 | $child_attributes = $daughter -> attributes; | ||
| 521 | $node_attributes{$child_name} = $$child_attributes{value}; | ||
| 522 | } | ||
| 523 | |||
| 524 | if ($node_type eq 'node_id') | ||
| 525 | { | ||
| 526 | $self -> graph -> add_node(name => $node_name, %node_attributes); | ||
| 527 | } | ||
| 528 | else | ||
| 529 | { | ||
| 530 | # Use the mother's daughters to get the nodes on either side of the edge. | ||
| 531 | |||
| 532 | my($from_name, $to_name) = $self -> _find_head_and_tail($node); | ||
| 533 | |||
| 534 | $self -> graph -> add_edge(from => $from_name, to => $to_name, %node_attributes); | ||
| 535 | } | ||
| 536 | |||
| 537 | } # End of output_node. | ||
| 538 | |||
| 539 | # -------------------------------------------------- | ||
| 540 | |||
| 541 | sub run | ||
| 542 | { | ||
| 543 | my($self, %arg) = @_; | ||
| 544 | my($dot_input_file) = $arg{dot_input_file} || $self -> dot_input_file; | ||
| 545 | my($format) = $arg{format} || $self -> format; | ||
| 546 | my($output_file) = $arg{output_file} || $self -> output_file; | ||
| 547 | my($graphviz_tree) = $arg{graphviz_tree} || $self -> graphviz_tree; | ||
| 548 | my($rankdir) = $arg{rankdir} || $self -> rankdir; | ||
| 549 | |||
| 550 | $self -> graph | ||
| 551 | ( | ||
| 552 | GraphViz2 -> new | ||
| 553 | ( | ||
| 554 | edge => {color => 'grey'}, | ||
| 555 | global => {directed => $self -> _determine_digraph_status}, | ||
| 556 | graph => {rankdir => $rankdir}, | ||
| 557 | logger => $self -> logger, | ||
| 558 | node => {shape => 'oval'}, | ||
| 559 | verbose => 0, | ||
| 560 | ) | ||
| 561 | ); | ||
| 562 | $self -> _initialize_uid; | ||
| 563 | |||
| 564 | my($graph_subtree) = $self -> _find_first_edge; | ||
| 565 | |||
| 566 | $self -> _find_last_edge; | ||
| 567 | $self -> _find_contiguous_edges; | ||
| 568 | $self -> output_all_nodes($graph_subtree); | ||
| 569 | $self -> output_all_edges($graph_subtree); | ||
| 570 | |||
| 571 | # Save the dot input in case dot exits abnormally. | ||
| 572 | # Note: We can't use $self -> graph -> dot_input() | ||
| 573 | # until after $self -> graph -> run() is called. | ||
| 574 | # So we use $self -> graph -> command -> print() instead. | ||
| 575 | |||
| 576 | if ($dot_input_file) | ||
| 577 | { | ||
| 578 | open(my $fh, '>:encoding(utf-8)', $dot_input_file); | ||
| 579 | print $fh (map{$_} @{$self -> graph -> command -> print}), "}\n"; | ||
| 580 | close $fh; | ||
| 581 | } | ||
| 582 | |||
| 583 | if ($graphviz_tree) | ||
| 584 | { | ||
| 585 | $self -> logger -> set_level(logger => {maxlevel => 'info'}); | ||
| 586 | $self -> log(info => join("\n", @{$self -> tree -> tree2string}) ); | ||
| 587 | } | ||
| 588 | |||
| 589 | $self -> graph -> run(format => $format, output_file => $output_file); | ||
| 590 | |||
| 591 | # Return 0 for success and 1 for failure. | ||
| 592 | |||
| 593 | return 0; | ||
| 594 | |||
| 595 | } # End of run. | ||
| 596 | |||
| 597 | # -------------------------------------------------- | ||
| 598 | |||
| 599 | 1; | ||
| 600 | |||
| 601 | =pod | ||
| 602 | |||
| 603 | =head1 NAME | ||
| 604 | |||
| 605 | L<MarpaX::Languages::Dash::Renderer> - The default rendering engine for MarpaX::Languages::Dash | ||
| 606 | |||
| 607 | =head1 Synopsis | ||
| 608 | |||
| 609 | See L<MarpaX::Languages::Dash/Synopsis>. | ||
| 610 | |||
| 611 | In particular, L<MarpaX::Languages::Dash/DASH> describes DASH, the Graphviz-like graph | ||
| 612 | definition language supported by these modules. | ||
| 613 | |||
| 614 | See also scripts/render.pl and scripts/render.sh. | ||
| 615 | |||
| 616 | =head1 Description | ||
| 617 | |||
| 618 | This module is the default rendering engine for L<MarpaX::Languages::Dash>. | ||
| 619 | |||
| 620 | It provides a L<GraphViz2>-based renderer for DASH, as parsed by L<MarpaX::Languages::Dash>. | ||
| 621 | |||
| 622 | For more details, see L<MarpaX::Languages::Dash/Description>. | ||
| 623 | |||
| 624 | =head1 Installation | ||
| 625 | |||
| 626 | Install L<MarpaX::Languages::Dash> as you would for any C<Perl> module: | ||
| 627 | |||
| 628 | Run: | ||
| 629 | |||
| 630 | cpanm MarpaX::Languages::Dash | ||
| 631 | |||
| 632 | or run: | ||
| 633 | |||
| 634 | sudo cpan MarpaX::Languages::Dash | ||
| 635 | |||
| 636 | or unpack the distro, and then either: | ||
| 637 | |||
| 638 | perl Build.PL | ||
| 639 | ./Build | ||
| 640 | ./Build test | ||
| 641 | sudo ./Build install | ||
| 642 | |||
| 643 | or: | ||
| 644 | |||
| 645 | perl Makefile.PL | ||
| 646 | make (or dmake or nmake) | ||
| 647 | make test | ||
| 648 | make install | ||
| 649 | |||
| 650 | =head1 Constructor and Initialization | ||
| 651 | |||
| 652 | C<new()> is called as C<< my($parser) = MarpaX::Languages::Dash::Renderer -> new(k1 => v1, k2 => v2, ...) >>. | ||
| 653 | |||
| 654 | It returns a new object of type C<MarpaX::Languages::Dash::Renderer>. | ||
| 655 | |||
| 656 | Key-value pairs accepted in the parameter list (see corresponding methods for details | ||
| 657 | [e.g. maxlevel()]): | ||
| 658 | |||
| 659 | =over 4 | ||
| 660 | |||
| 661 | =item o dot_input_file => $file_name | ||
| 662 | |||
| 663 | Specify the name of a file that the rendering engine can write to, which will contain the input | ||
| 664 | to dot (or whatever). This is good for debugging. | ||
| 665 | |||
| 666 | Default: ''. | ||
| 667 | |||
| 668 | If '', the file will not be created. | ||
| 669 | |||
| 670 | =item o format => $format | ||
| 671 | |||
| 672 | This is the format of the output file. | ||
| 673 | |||
| 674 | You can also pass this value into L</run(%arg)>. | ||
| 675 | |||
| 676 | The value passed in to run() takes precedence over the value passed in to new(). | ||
| 677 | |||
| 678 | Default: 'svg'. | ||
| 679 | |||
| 680 | =item o graphviz_tree => $Boolean | ||
| 681 | |||
| 682 | Specify whether (1) or not (0) to display the tree of nodes after this module has inserted any | ||
| 683 | anonymous nodes necessary to keep Graphviz happy. | ||
| 684 | |||
| 685 | Default: 0. | ||
| 686 | |||
| 687 | =item o logger => $logger_object | ||
| 688 | |||
| 689 | Specify a logger object. | ||
| 690 | |||
| 691 | To disable logging, just set logger to the empty string. | ||
| 692 | |||
| 693 | Default: An object of type L<Log::Handler>. | ||
| 694 | |||
| 695 | =item o maxlevel => $level | ||
| 696 | |||
| 697 | This option is only used if this module creates an object of type L<Log::Handler>. | ||
| 698 | |||
| 699 | See L<Log::Handler::Levels>. | ||
| 700 | |||
| 701 | Default: 'notice'. A typical choice is 'info' or 'debug'. | ||
| 702 | |||
| 703 | =item o minlevel => $level | ||
| 704 | |||
| 705 | This option is only used if this module creates an object of type L<Log::Handler>. | ||
| 706 | |||
| 707 | See L<Log::Handler::Levels>. | ||
| 708 | |||
| 709 | Default: 'error'. | ||
| 710 | |||
| 711 | No lower levels are used. | ||
| 712 | |||
| 713 | =item o output_file => $file_name | ||
| 714 | |||
| 715 | Specify the name of the output file to write. | ||
| 716 | |||
| 717 | You can also pass this value into L</run(%arg)>. | ||
| 718 | |||
| 719 | The value passed in to run() takes precedence over the value passed in to new(). | ||
| 720 | |||
| 721 | Default: ''. | ||
| 722 | |||
| 723 | =item o rankdir => $direction | ||
| 724 | |||
| 725 | $direction must be one of: LR or RL or TB or BT. | ||
| 726 | |||
| 727 | Specify the rankdir of the graph as a whole. | ||
| 728 | |||
| 729 | The value for I<rankdir> is passed to L<MarpaX::Languages::Dash::Renderer>. | ||
| 730 | |||
| 731 | Default: 'TB'. | ||
| 732 | |||
| 733 | =back | ||
| 734 | |||
| 735 | =head1 Methods | ||
| 736 | |||
| 737 | =head2 dot_input_file([$file_name]) | ||
| 738 | |||
| 739 | Here, the [] indicate an optional parameter. | ||
| 740 | |||
| 741 | Get or set the name of the file into which the rendering engine will write to input to dot (or whatever). | ||
| 742 | |||
| 743 | You can pass 'dot_input_file' as a key into new() and run(). | ||
| 744 | |||
| 745 | The value passed in to run() takes precedence over the value passed in to new(). | ||
| 746 | |||
| 747 | 'dot_input_file' is a parameter to L</new()>. See L</Constructor and Initialization> for details. | ||
| 748 | |||
| 749 | =head2 format([$format]) | ||
| 750 | |||
| 751 | Here, the [] indicate an optional parameter. | ||
| 752 | |||
| 753 | Get or set the format of the output file. | ||
| 754 | |||
| 755 | You can pass 'format' as a key into new() and run(). | ||
| 756 | |||
| 757 | The value passed in to run() takes precedence over the value passed in to new(). | ||
| 758 | |||
| 759 | 'format' is a parameter to L</new()>. See L</Constructor and Initialization> for details. | ||
| 760 | |||
| 761 | =head2 graphviz_tree([$Boolean]) | ||
| 762 | |||
| 763 | Here, the [] indicate an optional parameter. | ||
| 764 | |||
| 765 | Get or set whether or not to display the tree of nodes after this module has inserted any anonymous | ||
| 766 | nodes necessary to keep Graphviz happy. | ||
| 767 | |||
| 768 | You can pass 'graphviz_tree' as a key into new() and run(). | ||
| 769 | |||
| 770 | The value passed in to run() takes precedence over the value passed in to new(). | ||
| 771 | |||
| 772 | 'graphviz_tree' is a parameter to L</new()>. See L</Constructor and Initialization> for details. | ||
| 773 | |||
| 774 | =head2 log($level, $s) | ||
| 775 | |||
| 776 | If a logger is defined, this logs the message $s at level $level. | ||
| 777 | |||
| 778 | =head2 logger([$logger_object]) | ||
| 779 | |||
| 780 | Here, the [] indicate an optional parameter. | ||
| 781 | |||
| 782 | Get or set the logger object. | ||
| 783 | |||
| 784 | To disable logging, just set logger to the empty string. | ||
| 785 | |||
| 786 | You can pass 'logger' as a key into new() and run(). | ||
| 787 | |||
| 788 | The value passed in to run() takes precedence over the value passed in to new(). | ||
| 789 | |||
| 790 | 'logger' is a parameter to L</new()>. See L</Constructor and Initialization> for details. | ||
| 791 | |||
| 792 | =head2 maxlevel([$string]) | ||
| 793 | |||
| 794 | Here, the [] indicate an optional parameter. | ||
| 795 | |||
| 796 | Get or set the value used by the logger object. | ||
| 797 | |||
| 798 | This option is only used if this module creates an object of type L<Log::Handler>. | ||
| 799 | |||
| 800 | See L<Log::Handler::Levels>. | ||
| 801 | |||
| 802 | 'maxlevel' is a parameter to L</new()>. See L</Constructor and Initialization> for details. | ||
| 803 | |||
| 804 | =head2 minlevel([$string]) | ||
| 805 | |||
| 806 | Here, the [] indicate an optional parameter. | ||
| 807 | |||
| 808 | Get or set the value used by the logger object. | ||
| 809 | |||
| 810 | This option is only used if this module creates an object of type L<Log::Handler>. | ||
| 811 | |||
| 812 | See L<Log::Handler::Levels>. | ||
| 813 | |||
| 814 | 'minlevel' is a parameter to L</new()>. See L</Constructor and Initialization> for details. | ||
| 815 | |||
| 816 | =head2 output_all_edges($graph_subtree) | ||
| 817 | |||
| 818 | Walk the tree and output, via L</output_node($node)>, all edges and the nodes on either side of it. | ||
| 819 | |||
| 820 | =head2 output_all_nodes($graph_subtree) | ||
| 821 | |||
| 822 | Walk the tree and output, via L</output_node($node)>, all nodes. | ||
| 823 | |||
| 824 | =head2 output_node($node) | ||
| 825 | |||
| 826 | Convert, via L<GraphViz2>, the node into DOT format. | ||
| 827 | |||
| 828 | This is a tree node, not a DOT node. | ||
| 829 | |||
| 830 | =head2 output_file([$file_name]) | ||
| 831 | |||
| 832 | Here, the [] indicate an optional parameter. | ||
| 833 | |||
| 834 | Get or set the name of the output file. | ||
| 835 | |||
| 836 | You can pass 'output_file' as a key into new() and run(). | ||
| 837 | |||
| 838 | The value passed in to run() takes precedence over the value passed in to new(). | ||
| 839 | |||
| 840 | 'output_file' is a parameter to L</new()>. See L</Constructor and Initialization> for details. | ||
| 841 | |||
| 842 | =head2 rankdir([$direction]) | ||
| 843 | |||
| 844 | Here, the [] indicate an optional parameter. | ||
| 845 | |||
| 846 | Get or set the rankdir of the graph as a whole. | ||
| 847 | |||
| 848 | You can pass 'rankdir' as a key into new() and run(). | ||
| 849 | |||
| 850 | The value passed in to run() takes precedence over the value passed in to new(). | ||
| 851 | |||
| 852 | 'rankdir' is a parameter to L</new()>. See L</Constructor and Initialization> for details. | ||
| 853 | |||
| 854 | =head2 run(%arg) | ||
| 855 | |||
| 856 | Renders a set of items as an image, using L<GraphViz2>. | ||
| 857 | |||
| 858 | Keys and values in %arg are (see for L</Constructor and Initialization> details): | ||
| 859 | |||
| 860 | =over 4 | ||
| 861 | |||
| 862 | =item o dot_output_file => $file_name | ||
| 863 | |||
| 864 | =item o format => $format | ||
| 865 | |||
| 866 | =item o graphtree_viz => $Boolean | ||
| 867 | |||
| 868 | =item o output_file => $file_name | ||
| 869 | |||
| 870 | =item o rankdir => $string | ||
| 871 | |||
| 872 | =back | ||
| 873 | |||
| 874 | =head1 FAQ | ||
| 875 | |||
| 876 | =head2 What is DASH? | ||
| 877 | |||
| 878 | See L<MarpaX::Languages::Dash/DASH>. | ||
| 879 | |||
| 880 | This module does not use DASH. Rather, it interprets the tree of parsed nodes output by | ||
| 881 | L<MarpaX::Languages::Dash>. | ||
| 882 | |||
| 883 | =head2 How is the parsed graph stored in RAM? | ||
| 884 | |||
| 885 | See L<MarpaX::Languages::Dash/How is the parsed graph stored in RAM?>. | ||
| 886 | |||
| 887 | =head2 What are the defaults for GraphViz2? | ||
| 888 | |||
| 889 | GraphViz2 -> new | ||
| 890 | ( | ||
| 891 | edge => {color => 'grey'}, | ||
| 892 | global => {directed => $self -> _determine_digraph_status}, | ||
| 893 | graph => {rankdir => $self -> rankdir}, | ||
| 894 | logger => $self -> logger, | ||
| 895 | node => {shape => 'oval'}, | ||
| 896 | verbose => 0, | ||
| 897 | ) | ||
| 898 | |||
| 899 | =head2 Why is _determine_digraph_status() undocumented? | ||
| 900 | |||
| 901 | It would never be overridden by a sub-class. | ||
| 902 | |||
| 903 | It determines if the output DOT file contains 'graph' or 'digraph', by examining the first edge in | ||
| 904 | the tree. '--' means 'graph' and '->' means 'digraph'. | ||
| 905 | |||
| 906 | Yes, I know I just documented it! It's an Easter Egg. | ||
| 907 | |||
| 908 | =head1 See also | ||
| 909 | |||
| 910 | L<Marpa::R2>, L<GraphViz2> and L<GraphViz2::Marpa>. | ||
| 911 | |||
| 912 | L<The Marpa homepage|http://savage.net.au/Marpa.html>. | ||
| 913 | |||
| 914 | =head1 Machine-Readable Change Log | ||
| 915 | |||
| 916 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 917 | |||
| 918 | =head1 Version Numbers | ||
| 919 | |||
| 920 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 921 | |||
| 922 | =head1 Support | ||
| 923 | |||
| 924 | Email the author, or log a bug on RT: | ||
| 925 | |||
| 926 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=MarpaX::Languages::Dash>. | ||
| 927 | |||
| 928 | =head1 Author | ||
| 929 | |||
| 930 | L<MarpaX::Languages::Dash> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2013. | ||
| 931 | |||
| 932 | Home page: L<http://savage.net.au/index.html>. | ||
| 933 | |||
| 934 | =head1 Copyright | ||
| 935 | |||
| 936 | Australian copyright (c) 2013, Ron Savage. | ||
| 937 | |||
| 938 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 939 | you can redistribute them and/or modify them under the terms of | ||
| 940 | The Artistic License, a copy of which is available at: | ||
| 941 | http://www.opensource.org/licenses/index.html | ||
| 942 | |||
| 943 | =cut |