-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGantt.pm
221 lines (150 loc) · 4.32 KB
/
Gantt.pm
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package Gantt;
use strict;
use XML::Simple;
use Data::Dumper;
BEGIN {
use Exporter qw();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
@ISA = qw( Exporter );
$VERSION = 0.1;
@EXPORT_OK = qw();
@EXPORT = qw();
}
use vars @EXPORT_OK;
use vars qw();
my %marked = ();
my $Debug = 1;
sub new
{
my $self = {};
$self->{ Project } = undef;
$self->{ Start } = undef;
return bless $self;
}
# Obtains a Directed Acyclic Graph from project description
# It also finds and returns the first task in the project
# (ATTENTION: this task must be is unique)
sub project_to_dag
{
my $self = shift;
my $start;
foreach my $task ( keys %{ $self->{ Project }->{task} } ) {
if (not (defined $self->{Project}->{task}->{$task}->{dependency})) {
$start = $task;
} else {
foreach my $t ( @{$self->{Project}->{task}->{$task}->{dependency}} ) {
push @{ $self->{ Project }->{task}->{$t}->{adjacent}}, $task;
}
}
}
$self->{Start} = $start;
print Dumper( $self->{ Project } ) if $Debug;
}
# Topological Sort
# see "Data Structures and Algorithms", A.V.Aho, J.E.Hopcroft, J.D.Ullman, p. 221
sub topsort
{
my $self = shift;
my $task = shift;
my $pred = shift;
$marked{ $task } = 1;
if ( $pred ) {
print STDERR "PRED: $pred = " . Dumper($self->{ Project }->{task}->{$pred}) if $Debug;
$self->{ Project }->{task}->{$task}->{from_start} =
$self->{ Project }->{task}->{$pred}->{from_start} +
$self->{ Project }->{task}->{$pred}->{duration};
}
if (defined @{ $self->{ Project }->{task}->{$task}->{adjacent}} ) {
my @sorted_tasks = sort {
$self->{Project}->{task}->{$b}->{duration} <=>
$self->{Project}->{task}->{$a}->{duration}
} @{ $self->{ Project }->{task}->{$task}->{adjacent}};
foreach my $t ( @sorted_tasks ) {
if (not $marked{ $t }) {
$self->topsort( $t, $task );
}
}
}
}
sub load_project
{
my $self = shift;
my $project_file_name = shift;
$self->{ Project } = XMLin( $project_file_name, forcearray => 1)
|| die "Can't load project file - $! ";
$self->project_to_dag( $self->{ Project } );
$self->topsort( $self->{ Start });
}
sub length
{
my $self = shift;
my @l = sort { $b <=> $a } map {
$_->{from_start} + $_->{duration}
} values %{ $self->{ Project }->{task}};
return $l[0];
}
sub print_as_text
{
my $self = shift;
my $l = $self->length();
print Dumper( $self->{ Project } ) if $Debug;
print "$self->{ Project }->{ name }\n";
print ' ' x (20+7);
for my $i ( 1..$l ) {
print ($i % 5 ? '-' : '+');
}
print "\n\n";
foreach my $task ( keys %{ $self->{ Project }->{task} } ) {
printf "%-20s [%3d] ", $task, $self->{ Project }->{task}->{$task}->{duration};
print '-' x $self->{ Project }->{task}->{$task}->{from_start} .
'#' x $self->{ Project }->{task}->{$task}->{duration} . "\n";
}
print "Total days: $l\n\n";
}
"That's all, folks";
END {
}
=head1 AUTHOR
Stefano Rodighiero, ([email protected])
=cut
__END__
=head1 NAME
Gantt - Simple module to produce Gantt diagram from
XML project descriptions.
=head1 EXAMPLE
use Gantt;
my $g = new Gantt();
$g->load_project( './project.xml' );
$g->print_as_text;
Here a simple XML project description in the form expected
by this module
<project name="Program development">
<task name="Define Specifics" duration="5">
</task>
<task name="Analysis" duration="10">
<dependency>Define Specifics</dependency>
</task>
<task name="Search documentation" duration="3">
<dependency>Define Specifics</dependency>
</task>
<task name="Write code" duration="7">
<dependency>Analysis</dependency>
<dependency>Search documentation</dependency>
</task>
<task name="Write documentation" duration="5">
<dependency>Write code</dependency>
</task>
<task name="Test" duration="5">
<dependency>Write code</dependency>
</task>
<task name="Release" duration="2">
<dependency>Test</dependency>
<dependency>Write documentation</dependency>
</task>
</project>
=head1 WHAT I WOULD LIKE TO DO
=item * HTML output
=item * GraphViz output
=item * Graphic output using GD
=item * Tk interface to produce XML project description
=cut