-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgantt_observer.pl
158 lines (127 loc) · 4.58 KB
/
gantt_observer.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
#!/bin/perl
use warnings;
use strict;
use Exelis::MNJ::Debug;
$Exelis::MNJ::Debug::DEBUG = 0;
package GanttProject::Observer;
sub receiveEvent {
warn "EventListener::receiveEvent not overriden to do anything useful";
}
package GanttProject::Subscription;
use Moose;
has observer_list => (is => 'ro', isa => 'HashRef[GanttProject::Observer]', default => sub { {} });
sub addObserver {
my $self = shift;
my $observer = shift;
$observer->isa('GanttProject::Observer') or die "$observer is not an Observer!\n";
$self->observer_list()->{$observer} = $observer;
}
sub delObserver {
my $self = shift;
my $observer = shift;
if (exists($self->observer_list()->{$observer})) {
delete $self->observer_list()->{$observer};
}
}
sub notifyObservers {
my $self = shift;
my $event = shift;
foreach my $observer (values %{$self->observer_list()}) {
eval { $observer->receiveEvent($event) };
warn $@ if ($@);
}
}
no Moose;
package GanttProject::Task;
use Moose;
use DateTime;
use DateTime::Duration;
extends "GanttProject::Subscription", "GanttProject::Observer";
has start_time => (is => 'rw', isa => 'DateTime', trigger => sub { ::debug_funccall(\@_); my $self = shift; if (!defined($self->duration()) || !defined($self->end_time())) { $self->determineCalc("start") } });
has end_time => (is => 'rw', isa => 'DateTime', trigger => sub { ::debug_funccall(\@_); my $self = shift; if (!defined($self->duration()) || !defined($self->start_time())) { $self->determineCalc("end") }});
has duration => (is => 'rw', isa => 'DateTime::Duration', trigger => sub { ::debug_funccall(\@_); my $self = shift; if (!defined($self->start_time()) || !defined($self->end_time())) { $self->determineCalc("duration") }});
has dependents => (is => 'rw', isa => 'ArrayRef[GanttProject::Task]', default => sub { [] }, traits => ['Array'], handles => { '_addDependent' => 'push' });
has depends_on => (is => 'rw', isa => 'ArrayRef[GanttProject::Task]', default => sub { [] }, traits => ['Array'], handles => { '_addDependsOn' => 'push' });
sub determineCalc {
::debug_funccall(\@_);
my $self = shift;
my $type = shift;
if ($type eq "start") {
if (defined($self->duration())) {
$self->calculateEnd();
} else {
$self->calculateDuration();
}
} elsif ($type eq "end") {
if (defined($self->duration())) {
$self->calculateStart();
} else {
$self->calculateDuration();
}
} elsif ($type eq "duration") {
if (defined($self->end_time())) {
$self->calculateStart();
} else {
$self->calculateEnd();
}
}
}
sub calculateStart {
::debug_funccall(\@_);
my $self = shift;
if (defined($self->end_time()) && defined($self->duration())) {
$self->start_time($self->end_time() - $self->duration());
}
}
sub calculateEnd {
::debug_funccall(\@_);
my $self = shift;
if (defined($self->start_time()) && defined($self->duration())) {
$self->end_time($self->start_time() + $self->duration());
}
}
sub calculateDuration {
::debug_funccall(\@_);
my $self = shift;
if (defined($self->start_time()) && defined($self->end_time())) {
$self->duration($self->end_time() - $self->start_time());
}
}
sub addDependent {
::debug_funccall(\@_);
my $self = shift;
my $task = shift;
$self->_addDependent($task);
}
sub addDependsOn {
::debug_funccall(\@_);
my $self = shift;
my $task = shift;
$self->_addDependsOn($task);
$self->start_time($task->end_time());
$task->addObserver($self);
}
sub display {
::debug_funccall(\@_);
my $self = shift;
use DateTime::Format::Duration;
use DateTime::Format::Strptime;
my $dtf = DateTime::Format::Strptime->new(pattern => "%H:%M");
my $dtfd = DateTime::Format::Duration->new(pattern => "%H:%M");
printf("%s <- %s -> %s\n", (defined($self->start_time())?$dtf->format_datetime($self->start_time()):"00:00"), (defined($self->duration())?$dtfd->format_duration($self->duration()):"00:00"), (defined($self->end_time())?$dtf->format_datetime($self->end_time()):"00:00"));
}
package main;
use DateTime;
use DateTime::Format::Duration;
use DateTime::Format::Strptime;
my $dtf = DateTime::Format::Strptime->new(pattern => "%H:%M");
my $dtfd = DateTime::Format::Duration->new(pattern => "%H:%M");
my $task1 = GanttProject::Task->new();
my $task2 = GanttProject::Task->new();
$task1->start_time($dtf->parse_datetime("12:00"));
$task1->duration($dtfd->parse_duration("0:30"));
$task2->addDependsOn($task1);
$task1->addDependent($task2);
$task2->duration($dtfd->parse_duration("0:30"));
$task1->display();
$task2->display();