| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; use warnings; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | drawstatexml.pl - turns a simple xml description into a state diagram | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | drawstatexml.pl diagram.xml > diagram.svg | ||
| 11 | java -jar batik-rasterizer.jar diagram.svg | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | The xml format is described by the (unenforced) dtd in the samples directory | ||
| 16 | included with the distribution. The advantage of the xml format is that | ||
| 17 | it lets you name the states and specify edges in terms of the names. | ||
| 18 | The samples directory also includes examples in the files ending in xml. See | ||
| 19 | the samples/README. | ||
| 20 | |||
| 21 | If you don't like xml, feel free to use the text format supported by | ||
| 22 | drawstate.pl. You could also use UML::State directly to support a format | ||
| 23 | of your choice. | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | use XML::DOM; | ||
| 28 | use UML::State; | ||
| 29 | |||
| 30 | my $dom_parser = XML::DOM::Parser->new(); | ||
| 31 | my $file = shift; | ||
| 32 | |||
| 33 | open STDIN, "$file" if (defined $file); | ||
| 34 | |||
| 35 | my $doc = $dom_parser->parse(*STDIN); | ||
| 36 | |||
| 37 | my ($states, | ||
| 38 | $names, | ||
| 39 | $starts, | ||
| 40 | $accepting) = grab_states($doc); | ||
| 41 | |||
| 42 | my $edges = grab_transitions($doc, $names); | ||
| 43 | |||
| 44 | my $diagram = UML::State->new( | ||
| 45 | $states, $starts, $accepting, $edges | ||
| 46 | ); | ||
| 47 | |||
| 48 | print $diagram->draw(); | ||
| 49 | |||
| 50 | #use Data::Dumper; print Dumper($edges); | ||
| 51 | |||
| 52 | sub grab_states { | ||
| 53 | my $doc = shift; | ||
| 54 | my @states; | ||
| 55 | my %states; | ||
| 56 | my @starts; | ||
| 57 | my @accepting; | ||
| 58 | |||
| 59 | my $i = 0; | ||
| 60 | foreach my $state_row ($doc->getElementsByTagName("state_row")) { | ||
| 61 | |||
| 62 | my @row; | ||
| 63 | my $j = 0; | ||
| 64 | foreach my $state ($state_row->getElementsByTagName("state")) { | ||
| 65 | my $name = $state->getAttribute("name"); | ||
| 66 | if ($name eq 'filler') { | ||
| 67 | push @row, ""; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | push @row, $state->getAttribute("label"); | ||
| 71 | |||
| 72 | my $accepting = $state->getAttribute("accepting"); | ||
| 73 | push @accepting, "$j,$i" | ||
| 74 | if (defined $accepting and $accepting eq 'yes'); | ||
| 75 | |||
| 76 | my $start_from = $state->getAttribute("start_from"); | ||
| 77 | my $start_to = $state->getAttribute("start_to" ); | ||
| 78 | if ($start_from and $start_to) { | ||
| 79 | push @starts, "$j,$i,$start_from $start_to"; | ||
| 80 | } | ||
| 81 | |||
| 82 | $states{$name} = "$j,$i"; | ||
| 83 | } | ||
| 84 | $j++; | ||
| 85 | } | ||
| 86 | push @states, \@row; | ||
| 87 | $i++; | ||
| 88 | |||
| 89 | } | ||
| 90 | return (\@states, \%states, \@starts, \@accepting); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub grab_transitions { | ||
| 94 | my $doc = shift; | ||
| 95 | my $names= shift; | ||
| 96 | my %edges; | ||
| 97 | |||
| 98 | foreach my $transition_set ($doc->getElementsByTagName("transition_set")) { | ||
| 99 | my $label = $transition_set->getAttribute("label"); | ||
| 100 | my @list; | ||
| 101 | foreach my $transition | ||
| 102 | ($transition_set->getElementsByTagName("transition")) | ||
| 103 | { | ||
| 104 | my $from_name = $transition->getAttribute("from" ); | ||
| 105 | my $to_name = $transition->getAttribute("to" ); | ||
| 106 | my $arc = ucfirst $transition->getAttribute("arc"); | ||
| 107 | my $from_side = $transition->getAttribute("from_side" ); | ||
| 108 | my $to_side = $transition->getAttribute("to_side" ); | ||
| 109 | |||
| 110 | my $from = $names->{$from_name}; | ||
| 111 | my $to = $names->{$to_name}; | ||
| 112 | |||
| 113 | unless ($from_side and $to_side) { | ||
| 114 | ($from_side, $to_side) = find_ends($from, $to); | ||
| 115 | } | ||
| 116 | |||
| 117 | my $edge = "$from,$from_side $to,$to_side $arc"; | ||
| 118 | $edge =~ s/\s+$//; | ||
| 119 | |||
| 120 | push @list, $edge; | ||
| 121 | } | ||
| 122 | $edges{$label} = \@list; | ||
| 123 | } | ||
| 124 | return \%edges; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub find_ends { | ||
| 128 | my $from = shift; | ||
| 129 | my $to = shift; | ||
| 130 | |||
| 131 | my ($from_x, $from_y) = split /,/, $from; | ||
| 132 | my ($to_x, $to_y ) = split /,/, $to; | ||
| 133 | |||
| 134 | my $delx = $to_x - $from_x; | ||
| 135 | my $dely = $to_y - $from_y; | ||
| 136 | |||
| 137 | if ($delx == 0 and $dely == 0) { | ||
| 138 | return ("N", "N"); | ||
| 139 | } | ||
| 140 | |||
| 141 | if ($delx == 0) { | ||
| 142 | if ($dely < 0) { return ("N", "S"); } | ||
| 143 | else { return ("S", "N"); } | ||
| 144 | } | ||
| 145 | elsif ($dely == 0) { | ||
| 146 | if ($delx < 0) { return ("W", "E"); } | ||
| 147 | else { return ("E", "W"); } | ||
| 148 | } | ||
| 149 | |||
| 150 | return ("W", "E"); | ||
| 151 | } | ||
| 152 | |||
| 153 | =head1 PREREQUISTE | ||
| 154 | |||
| 155 | XML::DOM | ||
| 156 | |||
| 157 | =head1 BUGS | ||
| 158 | |||
| 159 | The state.dtd description is not enforced. This can lead to hard to diagnose | ||
| 160 | errors in the input file. | ||
| 161 | |||
| 162 | All edges using the same label must be in the same transition_set tag. | ||
| 163 | If a transition_set tag repeats a label used by an earlier transition_set tag, | ||
| 164 | it overwrites that transition_set. (Think of the labels as hash keys, I do.) | ||
| 165 | |||
| 166 | The bugs in UML::State apply here as well, since this script uses it | ||
| 167 | to make the pictures. See perldoc UML::State for those bugs. | ||
| 168 | |||
| 169 | =head1 AUTHOR | ||
| 170 | |||
| 171 | Phil Crow E<lt>philcrow2000@yahoo.com<gt> | ||
| 172 | |||
| 173 | =head1 COPYRIGHT AND LICENSE | ||
| 174 | |||
| 175 | Copyright 2003 by Phil Crow. All rights reserved. This is free software. | ||
| 176 | You may modify and/or redistribute it under the same terms as Perl 5.8.0. | ||
| 177 | |||
| 178 | =cut |