| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; use warnings; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | drawstate.pl - a script to turn text files into simple UML state diagrams | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | drawstate.pl diagram_text_file > diagram.svg | ||
| 11 | java -jar batik-rasterizer.jar diagram.svg | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | The diagram_text_file is processed in paragraph mode, each paragraph is | ||
| 16 | separated from the next by a blank line. The order of the paragraphs is | ||
| 17 | important (that is a minor bug). The order is: | ||
| 18 | |||
| 19 | node layout | ||
| 20 | starting states | ||
| 21 | accepting states | ||
| 22 | edges... | ||
| 23 | |||
| 24 | You may include as many edge paragraphs as you like, but each one should | ||
| 25 | have a different label. | ||
| 26 | |||
| 27 | At least two example input files are included in the distribution. Look | ||
| 28 | in the samples subdirectory for files ending in .input. The README file | ||
| 29 | in that directory explains what these input files represent. The | ||
| 30 | corresponding .svg and .png files hold the resulting images. The .png | ||
| 31 | images were produced with the freely available Batik rasterizer, | ||
| 32 | which is part of the Batik project from Apache. | ||
| 33 | See http://xml.apache.org/batik/ for information on Apache Batik. | ||
| 34 | |||
| 35 | In case you have trouble finding those files, one is included here: | ||
| 36 | |||
| 37 | (0) () (5) | ||
| 38 | () () (10) | ||
| 39 | (25)(15) | ||
| 40 | () (20)(30) | ||
| 41 | |||
| 42 | Starting | ||
| 43 | 0,0,N N | ||
| 44 | |||
| 45 | Accepting | ||
| 46 | 0,2 | ||
| 47 | 2,3 | ||
| 48 | |||
| 49 | 5 | ||
| 50 | 0,0,E 2,0,W | ||
| 51 | 2,0,S 2,1,N | ||
| 52 | 2,1,W 1,2,E | ||
| 53 | 1,2,S 1,3,N | ||
| 54 | 1,3,W 0,2,S | ||
| 55 | |||
| 56 | 10 | ||
| 57 | 0,0,S 2,1,W | ||
| 58 | 1,2,W 0,2,E | ||
| 59 | 2,1,S 1,3,E | ||
| 60 | 1,3,E 2,3,W | ||
| 61 | |||
| 62 | 25 | ||
| 63 | 0,0,S 0,2,N Counter | ||
| 64 | 2,0,E 2,3,E Clock | ||
| 65 | |||
| 66 | This represents the states in a coin operated vending machine which accepts | ||
| 67 | coins with values of 5, 10, and 25. The purchase price for the machine is | ||
| 68 | 25, so 25 and 30 are accepting states. Zero is the start state. Each | ||
| 69 | deposited coin increases the total, which is used to label the nodes. | ||
| 70 | Edges represent deposits and are labeled with the value of the deposited | ||
| 71 | coin. (The diagram is not complete. It is meant to show features of | ||
| 72 | diagrams. If you have a coin-op machine you need to build, feel free | ||
| 73 | to extend it as needed.) | ||
| 74 | |||
| 75 | Each paragraph except the one for node layout has a label. The labels are | ||
| 76 | only used for edge paragraphs. All coordinates are in terms of the node | ||
| 77 | layout which is numbered from 0. Columns are listed first, then rows | ||
| 78 | (think x and y from algebra). Sides of the nodes (where edges begin and | ||
| 79 | end) are labeled with compass points N(orth), S(outh), E(ast), and W(est). | ||
| 80 | |||
| 81 | This input format is idiosyncratic. If you don't like it, you should design | ||
| 82 | your own and interface directly with UML::State. If you don't like listing | ||
| 83 | columns first, or numbering from zero, you will need to account for that | ||
| 84 | in your new script or change UML::State. | ||
| 85 | |||
| 86 | =head1 BUGS | ||
| 87 | |||
| 88 | The order of the paragraphs matters. All paragraphs except the node layout | ||
| 89 | must be labeled. | ||
| 90 | |||
| 91 | All edges using the same label must be in the same paragraph. | ||
| 92 | If an edge paragraph repeats a label used by an earlier edge paragraph, | ||
| 93 | it overwrites that paragraph. (Think of the labels as hash keys, I do.) | ||
| 94 | |||
| 95 | The bugs in UML::State apply here as well, since this script uses it | ||
| 96 | to make the pictures. See perldoc UML::State for those bugs. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | use UML::State; | ||
| 101 | |||
| 102 | $/ = ""; # paragraph mode | ||
| 103 | |||
| 104 | my $node_data = <>; | ||
| 105 | my $start_states = <>; | ||
| 106 | my $accepting_states = <>; | ||
| 107 | |||
| 108 | my $nodes = make_2D_array($node_data ); | ||
| 109 | my $starts = make_array ($start_states ); | ||
| 110 | my $accepting = make_array ($accepting_states); | ||
| 111 | |||
| 112 | my $edges = {}; | ||
| 113 | |||
| 114 | while (<>) { | ||
| 115 | my ($label, @arrows) = split /\n/; | ||
| 116 | $edges->{$label} = \@arrows; | ||
| 117 | } | ||
| 118 | |||
| 119 | my $machine = UML::State->new($nodes, $starts, $accepting, $edges); | ||
| 120 | |||
| 121 | print $machine->draw(); | ||
| 122 | |||
| 123 | sub make_2D_array { | ||
| 124 | my $input = shift; | ||
| 125 | my @answer; | ||
| 126 | |||
| 127 | my @rows = split /\n/, $input; | ||
| 128 | |||
| 129 | foreach my $row (@rows) { | ||
| 130 | $row =~ s/^\s*\(//; # remove the first ( | ||
| 131 | $row =~ s/\)\s*$//; # remove the last ) | ||
| 132 | my @elements = split /\)\s*\(/, $row; # split on ') (' or similar | ||
| 133 | push @answer, \@elements; | ||
| 134 | } | ||
| 135 | return \@answer; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub make_array { | ||
| 139 | my $input = shift; | ||
| 140 | my @answer = split /\n/, $input; | ||
| 141 | shift @answer; | ||
| 142 | return \@answer; | ||
| 143 | } | ||
| 144 |