tests: remove remaining old-style attributes declarations
[nit.git] / tests / bench_netsim.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2005-2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 class Node
18 redef fun to_s: String
19 do
20 return _name
21 end
22 var name: String = "noname"
23 end
24
25 class WakeUpNode
26 super Node
27 fun wake_up is abstract
28 # Wake up the node
29 fun wake_up_in(d: Int)
30 # Ask to be weaked up in `d' time units
31 do
32 _scheduler.add_event(self, d)
33 end
34 var scheduler: Scheduler
35 init do end
36 end
37
38 class NodeSource
39 super Node
40 var nexts: nullable ArraySet[NodeSink] = null
41 fun attach(n: NodeSink)
42 # Add the sink `n' the the connected nodes
43 # Do nothing if `n' is already connected
44 do
45 # Create the collection if needed
46 if _nexts == null then
47 _nexts = new ArraySet[NodeSink]
48 end
49 _nexts.add(n)
50 end
51
52 fun detach(n: NodeSink)
53 # Remove the sink `n' from the connected nodes
54 # Do nothing if `n' is not connected
55 do
56 _nexts.remove(n)
57 end
58
59 protected fun send
60 # Notify the sinks by calling the recieve(1) method of each sink
61 do
62 if _nexts == null then
63 return
64 end
65 for n in _nexts.as(not null) do
66 n.recieve(self)
67 end
68 end
69 end
70
71 class NodeSink
72 super Node
73 fun recieve(n: NodeSource) is abstract
74 # the `n' has emeted a signal
75 end
76
77 #
78
79 class Scheduler
80 var time_list: Array[Couple[Int, WakeUpNode]] = new Array[Couple[Int, WakeUpNode]]
81 var time: Int = 0 # What time is it ?
82 fun add_event(n: WakeUpNode, d: Int)
83 # The node `n' whant to be weaked up in `d' time units
84 do
85 var entry = new Couple[Int, WakeUpNode](d+_time, n)
86 _time_list.add(entry)
87 end
88
89 fun next_event: nullable WakeUpNode
90 # Get the
91 do
92 if _time_list.is_empty then
93 return null
94 end
95
96 # Get the next entry
97 var entry = _time_list.first
98 for e in _time_list do
99 if e.first < entry.first then
100 entry = e
101 end
102 end
103 _time_list.remove(entry)
104
105 _time = entry.first
106 return entry.second
107 end
108
109 fun run_for(time_limit: Int)
110 do
111 loop
112 var node = next_event
113 if _time > time_limit then
114 print("Time limit.")
115 return
116 end
117 if node == null then
118 print("No more event.")
119 return
120 end
121 node.wake_up
122 end
123 end
124
125 init
126 do
127 end
128 end
129
130 #
131
132 class BeepSource
133 super NodeSource
134 super WakeUpNode
135 redef fun wake_up
136 do
137 send
138 wake_up_in(_delay)
139 end
140 var delay: Int
141 fun start
142 do
143 wake_up_in(_delay)
144 end
145
146 init(name: String, s: Scheduler, delay: Int)
147 do
148 _name = name
149 _scheduler = s
150 _delay = delay
151 end
152 end
153
154 class CountSink
155 super NodeSink
156 var count: Int = 0
157 redef fun recieve(n: NodeSource)
158 do
159 count = count + 1
160 end
161 end
162
163 class SimpleCountSink
164 super CountSink
165
166 init(name: String)
167 do
168 _name = name
169 end
170 end
171
172 class NodeAlternate
173 super NodeSink
174 super NodeSource
175 var last: nullable NodeSource
176 redef fun recieve(n: NodeSource)
177 do
178 if n != _last then
179 _last = n
180 send
181 end
182 end
183
184 init(name: String)
185 do
186 _name = name
187 end
188 end
189
190 class NodeEat
191 super CountSink
192 super NodeSource
193 var limit: Int
194 redef fun recieve(n: NodeSource)
195 do
196 var c = count + 1
197 if c >= _limit then
198 count = 0
199 send
200 else
201 count = c
202 end
203 end
204
205 init(name: String, limit: Int)
206 do
207 _name = name
208 _limit = limit
209 end
210 end
211
212 class NodeDelay
213 super NodeSource
214 super NodeSink
215 super WakeUpNode
216 var delay: Int
217 redef fun recieve(n: NodeSource)
218 do
219 wake_up_in(_delay)
220 end
221 redef fun wake_up
222 do
223 send
224 end
225
226 init(name: String, scheduler: Scheduler, delay: Int)
227 do
228 _name = name
229 _scheduler = scheduler
230 _delay = delay
231 end
232 end
233
234 #
235
236 var s = new Scheduler
237
238 var b1 = new BeepSource("Beep 1", s, 10)
239 var b2 = new BeepSource("Beep 2", s, 7)
240 var a1 = new NodeAlternate("Alternate 1")
241 var a2 = new NodeAlternate("Alternate 2")
242 var a3 = new NodeAlternate("Alternate 3")
243 var d1 = new NodeDelay("Delay 1", s, 2)
244 var e1 = new NodeEat("Eat 1", 5)
245 var c1 = new SimpleCountSink("Count 1")
246 b1.attach(a1)
247 b1.attach(a3)
248
249 b2.attach(a1)
250 b2.attach(a2)
251
252 b2.attach(a3)
253
254 a1.attach(e1)
255 a1.attach(a2)
256
257 a2.attach(a2)
258 a2.attach(a3)
259
260 a3.attach(e1)
261 a3.attach(d1)
262
263 d1.attach(e1)
264
265 e1.attach(c1)
266 e1.attach(a1)
267
268 b1.start
269 b2.start
270
271 var nb = 100000
272 if not args.is_empty then
273 nb = args.first.to_i
274 end
275
276 s.run_for(nb)
277 print(c1.count)
278