Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Drawing states...
[simgrid.git] / tools / MSG_visualization / trace2fig.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use Data::Dumper;
4 use XFig;
5
6 my($grid_Y_size)=225; # xfig 
7 my($grid_X_size)=225; # xfig 
8
9 sub read_cat {
10     my(%Cat);
11     my($filename)=@_;
12     my($line);
13
14     open INPUT, $filename;
15
16     while (defined($line=<INPUT>)) {
17         chomp $line;
18         if($line =~ /^7\s/) {
19             my($event,$date,$id,$type,$father,@name) = split(/\s+/,$line);
20
21             $Cat{$id}{name}="@name ";
22             $Cat{$id}{name}=~s/\"//g;
23             $Cat{$id}{father}=$father;
24             $Cat{$id}{type}=$type;
25             $Cat{$id}{date}=$date;
26         }
27     }
28     close INPUT;
29     return \%Cat;
30 }
31
32
33 sub read_event {
34     my($filename,$Cat)=@_;
35     my($line);
36
37     open INPUT, $filename;
38
39     while (defined($line=<INPUT>)) {
40         chomp $line;
41         if($line =~ /^11\s/) {
42             my($event,$date,$type,$id,$state) = split(/\s+/,$line);
43             push @{$$Cat{$id}{state}}, [$date,$state];
44         }
45         if($line =~ /^12\s/) {
46             my($event,$date,$type,$id) = split(/\s+/,$line);
47             push @{$$Cat{$id}{state}}, [$date];
48         }
49     }
50     close INPUT;
51 }
52
53
54 sub build_cat_tree {
55     my($root,$Cat)=@_;
56     my(@childs)=();
57     my($cat);
58
59     foreach $cat (keys %$Cat) {
60         if($$Cat{$cat}{father} eq $root) {
61             push @childs, build_cat_tree($cat,$Cat);
62         }
63 #       print "$$Cat{$cat}{name}\t\t $Cat{$cat}{father}\n";
64     }
65     
66     return [$root,@childs];
67 }
68
69 sub build_cat_list {
70     my($tree,$cat_list)=@_;
71     my($root) = shift @$tree;
72     my($u);
73     
74     push @$cat_list,$root;
75
76     foreach $u (@$tree) {
77         build_cat_list($u,$cat_list);
78     }
79     unshift @$tree, $root;
80 }
81
82
83 sub set_cat_position {
84     my($Cat,$cat_list)=@_;
85     my($i)=0;
86     my($cat);
87     foreach $cat (@$cat_list) {
88         $$Cat{$cat}{Y_min} = $i;
89         $$Cat{$cat}{Y_max} = $i+1;
90         $i++;
91     }
92 }
93
94 sub create_fig {
95     my($filename)=shift;
96     my($fig)=new XFig;
97     $fig->{object} = 'compound'; # Compound
98     $fig->{elements} = [];
99     $fig->{version} = 3.2;
100     $fig->{orientation}   = 'Landscape';
101     $fig->{justification} = 'Center';
102     $fig->{units}         = 'Metric';
103     $fig->{papersize}     = 'A4';
104     $fig->{magnification} = '100.00';
105     $fig->{multiplepage}  = 'Single';
106     $fig->{transparent}   = '-2';
107     $fig->{resolution}    = '1200';
108     $fig->{coordsystem}   = '2';
109     $fig->{filename}   = $filename;
110     return $fig;
111 }
112
113 sub draw_cat {
114     my($fig,$Cat)=@_;
115     my($cat,$e);
116     foreach $cat (keys %$Cat) {
117         next unless (defined($$Cat{$cat}{Y_min}) && 
118                      defined($$Cat{$cat}{Y_max}));
119         my($text) = new XFig ('text');
120         $text->{'text'} = $cat;
121         $text->{'y'} = ($$Cat{$cat}{Y_min}+$$Cat{$cat}{Y_max})/2*$grid_Y_size+68;
122         $fig->add ($text);
123     }
124     foreach $cat (keys %$Cat) {
125         next unless (defined($$Cat{$cat}{Y_min}) && 
126                      defined($$Cat{$cat}{Y_max}));
127         my(@states)=();
128         my($e);
129         foreach $e (@{$$Cat{$cat}{state}}) {
130             my($new_date,$state) = ($$e[0],$$e[1]);
131             if(defined($state)) {
132                 push @states, $e;
133             } else {
134                 my($old_event) = pop @states;
135                 my($old_date) = $$old_event[0];
136                 $state = $$old_event[1];
137                 
138                 my($line) = new XFig ('polyline');
139
140                 $line->{'subtype'} = 1;  # line
141                 $line->{'points'} = [ [$old_date*$grid_X_size, $$Cat{$cat}{Y_min}*$grid_Y_size],
142                                       [$new_date*$grid_X_size, $$Cat{$cat}{Y_min}*$grid_Y_size],
143                                       [$new_date*$grid_X_size, $$Cat{$cat}{Y_max}*$grid_Y_size],
144                                       [$old_date*$grid_X_size, $$Cat{$cat}{Y_max}*$grid_Y_size] ];
145                 $line->{'areafill'} = 20;
146                 if($state eq "S") {
147                     $line->{'fillcolor'} = 1;
148                 } elsif ($state eq "E") {
149                     $line->{'fillcolor'} = 2;
150                 } elsif ($state eq "B") {
151                     $line->{'fillcolor'} = 3;
152                 } elsif ($state eq "C") {
153                     $line->{'fillcolor'} = 4;
154                 }
155                 $fig->add ($line);
156             }
157         }
158     }
159 }
160
161 sub main {
162     my($Cat) = read_cat($ARGV[0]);
163     my($cat_tree)=build_cat_tree("0",$Cat);
164     read_event($ARGV[0],$Cat);
165 #    print Dumper($cat_tree);
166 #    print Dumper($Cat);
167     my($cat_list)=[];
168     build_cat_list($cat_tree,$cat_list);
169     shift @$cat_list;
170     shift @$cat_list;
171     print "@$cat_list \n";
172
173     set_cat_position($Cat,$cat_list);
174     
175     my($fig)=create_fig("toto.fig");
176     draw_cat($fig,$Cat);
177     $fig->writefile ();
178     system "fig2dev -L pdf toto.fig toto.pdf";
179 }
180
181 main;