-
Notifications
You must be signed in to change notification settings - Fork 256
/
make-diagram.pl
executable file
·163 lines (136 loc) · 4.29 KB
/
make-diagram.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/perl -w
# This file is part of SCIgen.
#
# SCIgen is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# SCIgen is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SCIgen; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
use strict;
use scigen;
use Getopt::Long;
my $tmp_dir = "/tmp";
my $tmp_pre = "/$tmp_dir/scimakediagram.$$";
my $viz_file = "$tmp_pre.viz";
my $eps_file = "$tmp_pre.eps";
my $sysname;
my $filename;
my $seed;
sub usage {
select(STDERR);
print <<EOUsage;
$0 [options]
Options:
--help Display this help message
--seed <seed> Seed the prng with this
--file <file> Save the postscript in this file
--sysname <file> What is the system called?
EOUsage
exit(1);
}
# Get the user-defined parameters.
# First parse options
my %options;
&GetOptions( \%options, "help|?", "seed=s", "file=s", "sysname=s" )
or &usage;
if( $options{"help"} ) {
&usage();
}
if( defined $options{"file"} ) {
$filename = $options{"file"};
}
if( defined $options{"sysname"} ) {
$sysname = $options{"sysname"};
} else {
die( "--sysname required" );
}
if( defined $options{"seed"} ) {
$seed = $options{"seed"};
} else {
$seed = int rand 0xffffffff;
}
srand($seed);
if( defined $filename ) {
$eps_file = $filename;
}
my @label_types = qw( NODE_LABEL_LET NODE_LABEL_PROG
NODE_LABEL_NET NODE_LABEL_IP NODE_LABEL_HW
NODE_LABEL_DEC);
my @edge_label_types = ( "\"\"", "\"\"", "\"\"", "\"\"", "\"\"",
"EDGE_LABEL_YESNO" );
my %types = ("digraph" => "DIR_LAYOUT",
"graph" => "UNDIR_LAYOUT" );
my %edges = ("digraph" => "->",
"graph" => "--" );
my $dat = {};
my $RE = undef;
my $fh = new IO::File ("<graphviz.in");
scigen::read_rules ($fh, $dat, \$RE, 0);
my $num_nodes = scigen::generate ($dat, "NUM_NODES", $RE, 0, 0);
my $graph_type = scigen::generate ($dat, "PICK_GRAPH_TYPE", $RE, 0, 0);
my $label_type = scigen::generate ($dat, "PICK_LABEL_TYPE", $RE, 0, 0);
my $shape_type = scigen::generate ($dat, "PICK_SHAPE_TYPE", $RE, 0, 0);
my $edge_label_type = $edge_label_types[$label_type];
$label_type = $label_types[$label_type];
my $dir_rule = $types{$graph_type};
my $edge_type = $edges{$graph_type};
my $program = scigen::generate ($dat, $dir_rule, $RE, 0, 0);
#good number of edges: n-1 -> 2n-1
my $num_edges = int rand($num_nodes-1);
$num_edges += $num_nodes;
if( $num_edges > 16 ) {
$num_edges = 16;
} elsif( $num_edges == 0 ) {
$num_edges = 1;
}
my @a = ($graph_type);
$dat->{"GRAPH_DIR"} = \@a;
my @b = ($label_type);
$dat->{"NODE_LABEL"} = \@b;
my @c = ($edge_type);
$dat->{"EDGEOP"} = \@c;
# can't be in italics
if( $sysname =~ /\{\\em (.*)\}/ ) {
$sysname = $1;
}
my @d = ($sysname);
$dat->{"SYSNAME"} = \@d;
my @e = ();
my @shapes = split( /\s+/, $shape_type );
foreach my $s (@shapes) {
push @e, $s
}
$dat->{"SHAPE_TYPE"} = \@e;
my @f = ($edge_label_type);
$dat->{"EDGE_LABEL"} = \@f;
my @g = ("NODES_$num_nodes");
$dat->{"NODES"} = \@g;
my @h = ("EDGES_$num_edges");
$dat->{"EDGES"} = \@h;
scigen::compute_re( $dat, \$RE );
my $graph_file = scigen::generate ($dat, "GRAPHVIZ", $RE, 0, 0);
open( VIZ, ">$viz_file" ) or die( "Can't open $viz_file for writing" );
print VIZ $graph_file;
close( VIZ );
system( "$program -Tps $viz_file > $eps_file.tmp; ps2epsi $eps_file.tmp $eps_file" ) and
die( "Can't run $program on $viz_file" );
if( `uname` =~ /Linux/ ) {
# fix bounding box of stupid linux's ps2epsi
my $bbline = `grep "BoundingBox" $eps_file.tmp | tail -1`;
chomp $bbline;
#print "Fixing box: $bbline\n";
system( "sed -e s/%%BoundingBox.*/\"$bbline\"/ -i $eps_file" );
}
if( !defined $filename ) {
system( "gv $eps_file" ) and
die( "Can't run $program on $viz_file" );
}
system( "rm -f $tmp_pre*" ) and die( "Couldn't rm" );