| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Simulation::DiscreteEvent; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
19014
|
use Moose; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use Module::Load; |
|
5
|
|
|
|
|
|
|
use Simulation::DiscreteEvent::Event; |
|
6
|
|
|
|
|
|
|
use namespace::clean -except => ['meta']; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Simulation::DiscreteEvent - module for discrete-event simulation |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use Simulation::DiscreteEvent; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
This module implements library for discrete-event simulation. Currently it is |
|
21
|
|
|
|
|
|
|
beta quality, I do not plan any backward incompatible changes in interface, |
|
22
|
|
|
|
|
|
|
but everything may happen. Please see L<Simulation::DiscreteEvent::Cookbook> |
|
23
|
|
|
|
|
|
|
for information about using this module. |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head2 new |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Creates simulation object. |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=cut |
|
32
|
|
|
|
|
|
|
sub BUILD { |
|
33
|
|
|
|
|
|
|
my $self = shift; |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$self->_time(0); |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head2 $self->time |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Returns current model time. |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
has time => ( reader => 'time', writer => '_time', isa => 'Num' ); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
has _servers => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } ); |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
has _events => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } ); |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head2 $self->schedule($time, $server, $event[, $message]) |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Schedule event at I<$time> for I<$server>. I<$event> is a string that |
|
53
|
|
|
|
|
|
|
defines event type. I<$message> is a message that will be passed to I<$server>'s |
|
54
|
|
|
|
|
|
|
event handler. |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=cut |
|
57
|
|
|
|
|
|
|
sub schedule { |
|
58
|
|
|
|
|
|
|
my ($self, $time, $server, $event_type, $message) = @_; |
|
59
|
|
|
|
|
|
|
die "Can't schedule event in the past" if $time < $self->time; |
|
60
|
|
|
|
|
|
|
my $event = Simulation::DiscreteEvent::Event->new( |
|
61
|
|
|
|
|
|
|
time => $time, |
|
62
|
|
|
|
|
|
|
server => $server, |
|
63
|
|
|
|
|
|
|
type => $event_type, |
|
64
|
|
|
|
|
|
|
message => $message |
|
65
|
|
|
|
|
|
|
); |
|
66
|
|
|
|
|
|
|
my $i=0; |
|
67
|
|
|
|
|
|
|
for (@{$self->_events}) { |
|
68
|
|
|
|
|
|
|
last if $_->time > $time; |
|
69
|
|
|
|
|
|
|
$i++; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
splice @{$self->_events}, $i, 0, $event; |
|
72
|
|
|
|
|
|
|
1; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 $self->send($server, $event[, $message]) |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Schedule I<$event> for I<$server> to happen right now. |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
|
80
|
|
|
|
|
|
|
sub send { |
|
81
|
|
|
|
|
|
|
my $self = shift; |
|
82
|
|
|
|
|
|
|
$self->schedule($self->time, @_); |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 $self->add($server_class, %parameters) |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Will create new object of class I<$server_class> and add it to model. |
|
88
|
|
|
|
|
|
|
I<%parameters> are passed to the object constructor. Returns reference to the |
|
89
|
|
|
|
|
|
|
created object. |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |
|
92
|
|
|
|
|
|
|
sub add { |
|
93
|
|
|
|
|
|
|
my $self = shift; |
|
94
|
|
|
|
|
|
|
my $server_class = shift; |
|
95
|
|
|
|
|
|
|
{ |
|
96
|
|
|
|
|
|
|
no strict 'refs'; |
|
97
|
|
|
|
|
|
|
load $server_class unless @{"${server_class}::ISA"}; |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
my $srv = $server_class->new( model => $self, @_ ); |
|
100
|
|
|
|
|
|
|
push @{$self->_servers}, $srv; |
|
101
|
|
|
|
|
|
|
return $srv; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 $self->run([$stop_time]) |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Start simulation. You should schedule at least one event before run simulation. |
|
107
|
|
|
|
|
|
|
Simulation will be finished at I<$stop_time> if specified, or when there will |
|
108
|
|
|
|
|
|
|
be no more events scheduled for execution. |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |
|
111
|
|
|
|
|
|
|
sub run { |
|
112
|
|
|
|
|
|
|
my $self = shift; |
|
113
|
|
|
|
|
|
|
my $stop_time = shift; |
|
114
|
|
|
|
|
|
|
my $counter; |
|
115
|
|
|
|
|
|
|
while ( my $event = shift @{ $self->_events } ) { |
|
116
|
|
|
|
|
|
|
if ( $stop_time && $stop_time < $event->time ) { |
|
117
|
|
|
|
|
|
|
unshift @{ $self->_events }, $event; |
|
118
|
|
|
|
|
|
|
$self->_time($stop_time); |
|
119
|
|
|
|
|
|
|
last; |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
$self->_time( $event->time ); |
|
122
|
|
|
|
|
|
|
$event->handle; |
|
123
|
|
|
|
|
|
|
$counter++; |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
$counter; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 $self->step |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Handles one event from the events queue. |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=cut |
|
133
|
|
|
|
|
|
|
sub step { |
|
134
|
|
|
|
|
|
|
my $self = shift; |
|
135
|
|
|
|
|
|
|
my $event = shift @{ $self->_events }; |
|
136
|
|
|
|
|
|
|
return unless $event; |
|
137
|
|
|
|
|
|
|
$self->_time( $event->time ); |
|
138
|
|
|
|
|
|
|
$event->handle; |
|
139
|
|
|
|
|
|
|
1; |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
1; |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
__END__ |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 AUTHOR |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Pavel Shaydo, C<< <zwon at cpan.org> >> |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 BUGS |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Please report any bugs or feature requests to C<bug-simulation-discreteevent at rt.cpan.org>, or through |
|
153
|
|
|
|
|
|
|
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Simulation-DiscreteEvent>. I will be notified, and then you'll |
|
154
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 SUPPORT |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
perldoc Simulation::DiscreteEvent |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Project's git repository can be accessed at |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
http://github.com/trinitum/perl-Simulation-DiscreteEvent |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
You can also look for information at: |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=over 4 |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Simulation-DiscreteEvent> |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
L<http://annocpan.org/dist/Simulation-DiscreteEvent> |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
L<http://cpanratings.perl.org/d/Simulation-DiscreteEvent> |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item * Search CPAN |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
L<http://search.cpan.org/dist/Simulation-DiscreteEvent/> |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=back |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Copyright 2010 Pavel Shaydo. |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
195
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
|
196
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |
|
202
|
|
|
|
|
|
|
|