-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdining_philosophers.rs
403 lines (372 loc) · 16.5 KB
/
dining_philosophers.rs
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use bevy::input::common_conditions::input_just_pressed;
use bevy::prelude::*;
use bevy::window::WindowResolution;
use bevy_ascii_terminal::{
AutoCamera, Border, Terminal, TerminalBundle, TerminalPlugin, TileFormatter,
};
use petnat::{NetId, PetriNet, PetriNetPlugin, Place, Token, Trans, W};
enum DiningPhils {}
const LEFT: bool = true;
const RIGHT: bool = false;
enum ForkClean<const LR: bool> {}
enum ForkTaken<const LR: bool, const N: usize> {}
enum ForkDirty<const LR: bool, const N: usize> {}
enum Eating<const N: usize> {}
enum Thinking<const N: usize> {}
enum Take<const LR: bool, const N: usize> {}
enum Wash<const LR: bool, const N: usize> {}
enum Eat<const N: usize> {}
enum Finish<const N: usize> {}
impl NetId for DiningPhils {}
impl<const LR: bool> Place<DiningPhils> for ForkClean<LR> {}
impl<const LR: bool, const N: usize> Place<DiningPhils> for ForkTaken<LR, N> {}
impl<const LR: bool, const N: usize> Place<DiningPhils> for ForkDirty<LR, N> {}
impl<const N: usize> Place<DiningPhils> for Eating<N> {}
impl<const N: usize> Place<DiningPhils> for Thinking<N> {}
impl<const LR: bool, const N: usize> Trans<DiningPhils> for Take<LR, N> {}
impl<const LR: bool, const N: usize> Trans<DiningPhils> for Wash<LR, N> {}
impl<const N: usize> Trans<DiningPhils> for Eat<N> {}
impl<const N: usize> Trans<DiningPhils> for Finish<N> {}
fn add_philosopher<const N: usize>(net: PetriNet<DiningPhils>) -> PetriNet<DiningPhils> {
net.add_place::<ForkTaken<LEFT, N>>()
.add_place::<ForkTaken<RIGHT, N>>()
.add_place::<ForkDirty<LEFT, N>>()
.add_place::<ForkDirty<RIGHT, N>>()
.add_place::<Eating<N>>()
.add_place::<Thinking<N>>()
.add_trans::<Take<LEFT, N>, (ForkClean<LEFT>, W<1>), (ForkTaken<LEFT, N>, W<1>)>()
.add_trans::<Take<RIGHT, N>, (ForkClean<RIGHT>, W<1>), (ForkTaken<RIGHT, N>, W<1>)>()
.add_trans::<Wash<LEFT, N>, (ForkDirty<LEFT, N>, W<1>), (ForkClean<LEFT>, W<1>)>()
.add_trans::<Wash<RIGHT, N>, (ForkDirty<RIGHT, N>, W<1>), (ForkClean<RIGHT>, W<1>)>()
.add_trans::<Eat<N>, (
(Thinking<N>, W<1>),
(ForkTaken<LEFT, N>, W<1>),
(ForkTaken<RIGHT, N>, W<1>),
), (Eating<N>, W<1>)>()
.add_trans::<Finish<N>, (Eating<N>, W<1>), (
(Thinking<N>, W<1>),
(ForkDirty<LEFT, N>, W<1>),
(ForkDirty<RIGHT, N>, W<1>),
)>()
}
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resolution: WindowResolution::new(640.0, 640.0),
title: "Dining Philosophers".to_string(),
resizable: LEFT,
..default()
}),
..default()
}))
.add_plugins(TerminalPlugin)
.insert_resource(ClearColor(Color::BLACK))
.add_plugins(PetriNetPlugin::<DiningPhils> {
build: |net| {
net.add_place::<ForkClean<LEFT>>()
.add_place::<ForkClean<RIGHT>>()
.compose(add_philosopher::<0>)
.compose(add_philosopher::<1>)
},
})
.add_systems(Startup, spawn_terminal)
.add_systems(
PostStartup,
(
(
spawn_token,
apply_deferred,
(init_forks, init_philosopher::<0>, init_philosopher::<1>),
)
.chain(),
(show_manual, show_net),
),
)
.add_systems(
PreUpdate,
(
despawn_token,
spawn_token,
apply_deferred,
(init_forks, init_philosopher::<0>, init_philosopher::<1>),
)
.chain()
.run_if(input_just_pressed(KeyCode::G)),
)
.add_systems(
Update,
(
// In this setup, all transitions are fired manually via player input.
take_fork::<LEFT, 0>.run_if(input_just_pressed(KeyCode::Q)),
take_fork::<RIGHT, 0>.run_if(input_just_pressed(KeyCode::E)),
wash_fork::<LEFT, 0>.run_if(input_just_pressed(KeyCode::A)),
wash_fork::<RIGHT, 0>.run_if(input_just_pressed(KeyCode::D)),
eat::<0>.run_if(input_just_pressed(KeyCode::W)),
finish::<0>.run_if(input_just_pressed(KeyCode::S)),
take_fork::<LEFT, 1>.run_if(input_just_pressed(KeyCode::U)),
take_fork::<RIGHT, 1>.run_if(input_just_pressed(KeyCode::O)),
wash_fork::<LEFT, 1>.run_if(input_just_pressed(KeyCode::J)),
wash_fork::<RIGHT, 1>.run_if(input_just_pressed(KeyCode::L)),
eat::<1>.run_if(input_just_pressed(KeyCode::I)),
finish::<1>.run_if(input_just_pressed(KeyCode::K)),
)
.chain(),
)
.add_systems(
PostUpdate,
(
show_clean_fork::<LEFT>,
show_clean_fork::<RIGHT>,
show_taken_fork::<LEFT, 0>,
show_taken_fork::<LEFT, 1>,
show_taken_fork::<RIGHT, 0>,
show_taken_fork::<RIGHT, 1>,
show_dirty_fork::<LEFT, 0>,
show_dirty_fork::<LEFT, 1>,
show_dirty_fork::<RIGHT, 0>,
show_dirty_fork::<RIGHT, 1>,
show_philosopher::<0>,
show_philosopher::<1>,
)
.chain(),
)
.run();
}
const TERM_SIZE: [usize; 2] = [50, 50];
fn spawn_terminal(mut commands: Commands) {
let term = Terminal::new(TERM_SIZE).with_border(Border::single_line());
commands.spawn((TerminalBundle::from(term), AutoCamera));
}
fn spawn_token(mut commands: Commands, net: Res<PetriNet<DiningPhils>>) {
// A token can represent the current state of some game object, or some sort of resource.
// In this example, the token represents the state of the dinner, capturing both the state
// of the forks, and the state of the philosophers.
let token = net.spawn_token();
commands.spawn(token);
info!("Spawning a token...");
}
fn init_forks(net: Res<PetriNet<DiningPhils>>, mut tokens: Query<&mut Token<DiningPhils>>) {
let mut token = tokens.single_mut();
net.mark::<ForkClean<LEFT>>(&mut token, 1);
net.mark::<ForkClean<RIGHT>>(&mut token, 1);
}
fn init_philosopher<const N: usize>(
net: Res<PetriNet<DiningPhils>>,
mut tokens: Query<&mut Token<DiningPhils>>,
) {
let mut token = tokens.single_mut();
net.mark::<Thinking<N>>(&mut token, 1);
}
const fn side<const LR: bool>() -> &'static str {
match LR {
LEFT => "left",
RIGHT => "right",
}
}
fn take_fork<const LR: bool, const N: usize>(
net: Res<PetriNet<DiningPhils>>,
mut tokens: Query<&mut Token<DiningPhils>>,
) {
let mut token = tokens.single_mut();
if let Ok(()) = net.fire::<Take<LR, N>>(token.bypass_change_detection()) {
token.set_changed();
info!("Philosopher {N} took the {} fork.", side::<LR>());
} else {
warn!("Philosopher {N} cannot take the {} fork.", side::<LR>());
}
}
fn wash_fork<const LR: bool, const N: usize>(
net: Res<PetriNet<DiningPhils>>,
mut tokens: Query<&mut Token<DiningPhils>>,
) {
let mut token = tokens.single_mut();
if let Ok(()) = net.fire::<Wash<LR, N>>(token.bypass_change_detection()) {
token.set_changed();
info!("Philosopher {N} washed the {} fork.", side::<LR>());
} else {
warn!("Philosopher {N} cannot wash the {} fork.", side::<LR>());
}
}
fn eat<const N: usize>(
net: Res<PetriNet<DiningPhils>>,
mut tokens: Query<&mut Token<DiningPhils>>,
) {
let mut token = tokens.single_mut();
if let Ok(()) = net.fire::<Eat<N>>(token.bypass_change_detection()) {
token.set_changed();
info!("Philosopher {N} started eating.");
} else if net.marks::<Eating<N>>(&token) > 0 {
warn!("Philosopher {N} is already eating.");
} else {
warn!("Philosopher {N} cannot eat without both clean forks.");
}
}
fn finish<const N: usize>(
net: Res<PetriNet<DiningPhils>>,
mut tokens: Query<&mut Token<DiningPhils>>,
) {
let mut token = tokens.single_mut();
if let Ok(()) = net.fire::<Finish<N>>(token.bypass_change_detection()) {
token.set_changed();
info!("Philosopher {N} finished eating.");
} else {
warn!("Philosopher {N} cannot finish eating.");
}
}
fn despawn_token(mut commands: Commands, tokens: Query<Entity, With<Token<DiningPhils>>>) {
let token = tokens.single();
commands.entity(token).despawn();
info!("Despawning the token...");
}
#[rustfmt::skip]
fn show_manual(mut terms: Query<&mut Terminal>) {
let mut term = terms.single_mut();
let ys = &mut (0..TERM_SIZE[1]).rev().take(19);
let mut y = || ys.next().unwrap();
term.put_string([0, y()], " DINING PHILOSOPHERS ");
term.put_string([0, y()], " ");
term.put_string([0, y()], " Controls ");
term.put_string([0, y()], " ");
term.put_string([0, y()], " Philosopher 0 Philosopher 1 ");
term.put_string([0, y()], " ");
term.put_string([0, y()], " <Q> take left fork <U> take left fork ");
term.put_string([0, y()], " ");
term.put_string([0, y()], " <E> take right fork <O> take right fork ");
term.put_string([0, y()], " ");
term.put_string([0, y()], " <W> start eating <I> start eating ");
term.put_string([0, y()], " ");
term.put_string([0, y()], " <S> finish eating <K> finish eating ");
term.put_string([0, y()], " ");
term.put_string([0, y()], " <A> wash left fork <J> wash left fork ");
term.put_string([0, y()], " ");
term.put_string([0, y()], " <D> wash right fork <L> wash right fork ");
term.put_string([0, y()], " ");
term.put_string([0, y()], "--------------------------------------------------");
}
#[rustfmt::skip]
fn show_net(mut terms: Query<&mut Terminal>) {
let mut term = terms.single_mut();
let ys = &mut (0..TERM_SIZE[1]).rev().skip(20);
let mut y = || ys.next().unwrap();
term.put_string([0, y()], r" WASH FINISH WASH ");
term.put_string([0, y()], r" ");
term.put_string([0, y()], r" +---[#]<---( )<-----[#]----->( )--->[#]---+ ");
term.put_string([0, y()], r" | dirty ^ \ dirty | ");
term.put_string([0, y()], r" | / \ | ");
term.put_string([0, y()], r" | / v | ");
term.put_string([0, y()], r" | ( ) 0 ( ) | ");
term.put_string([0, y()], r" | eating^ /thinking | ");
term.put_string([0, y()], r" | TAKE \ / TAKE | ");
term.put_string([0, y()], r" | \ v | ");
term.put_string([0, y()], r" | [#]--->( )----->[#]<-----( )<---[#] | ");
term.put_string([0, y()], r" | ^ taken taken ^ | ");
term.put_string([0, y()], r" | / EAT \ | ");
term.put_string([0, y()], r" V/ \V ");
term.put_string([0, y()], r" L( )clean clean( )R ");
term.put_string([0, y()], r" ^\ /^ ");
term.put_string([0, y()], r" | \ EAT / | ");
term.put_string([0, y()], r" | v v | ");
term.put_string([0, y()], r" | [#]--->( )----->[#]<-----( )<---[#] | ");
term.put_string([0, y()], r" | taken / ^ taken | ");
term.put_string([0, y()], r" | TAKE / \ TAKE | ");
term.put_string([0, y()], r" | v \ | ");
term.put_string([0, y()], r" | ( ) 1 ( ) | ");
term.put_string([0, y()], r" | eating\ ^thinking | ");
term.put_string([0, y()], r" | \ / | ");
term.put_string([0, y()], r" | v / | ");
term.put_string([0, y()], r" +---[#]<---( )<-----[#]----->( )--->[#]---+ ");
term.put_string([0, y()], r" dirty dirty ");
term.put_string([0, y()], r" WASH FINISH WASH ");
}
const W: usize = TERM_SIZE[0];
const H: usize = TERM_SIZE[1] - 20;
const ROWS: [usize; 7] = [H - 3, H - 7, H - 11, H / 2, 11, 7, 3];
const COLS: [usize; 9] = [3, 8, 15, 19, W / 2 - 1, W - 21, W - 17, W - 10, W - 5];
fn show_place<P: Place<DiningPhils>>(
net: &PetriNet<DiningPhils>,
token: &Token<DiningPhils>,
term: &mut Terminal,
[x, y]: [usize; 2],
) {
if net.marks::<P>(token) > 0 {
term.put_char([x, y], '*'.fg(Color::YELLOW));
term.put_char([x - 1, y], '('.fg(Color::YELLOW));
term.put_char([x + 1, y], ')'.fg(Color::YELLOW));
} else {
term.clear_box([x, y], [1, 1]);
term.put_char([x - 1, y], '('.fg(Color::WHITE));
term.put_char([x + 1, y], ')'.fg(Color::WHITE));
}
}
fn show_trans<T: Trans<DiningPhils>>(
net: &PetriNet<DiningPhils>,
token: &Token<DiningPhils>,
term: &mut Terminal,
[x, y]: [usize; 2],
) {
if net.enabled::<T>(token) {
term.put_char([x, y], '#'.fg(Color::GREEN));
term.put_char([x - 1, y], '['.fg(Color::GREEN));
term.put_char([x + 1, y], ']'.fg(Color::GREEN));
} else {
term.clear_box([x, y], [1, 1]);
term.put_char([x - 1, y], '['.fg(Color::WHITE));
term.put_char([x + 1, y], ']'.fg(Color::WHITE));
}
}
fn show_clean_fork<const LR: bool>(
net: Res<PetriNet<DiningPhils>>,
tokens: Query<&Token<DiningPhils>, Changed<Token<DiningPhils>>>,
mut terms: Query<&mut Terminal>,
) {
if let Ok(token) = tokens.get_single() {
let mut term = terms.single_mut();
let x = if LR { COLS[0] } else { COLS[8] };
show_place::<ForkClean<LR>>(&net, token, &mut term, [x, ROWS[3]]);
}
}
fn show_taken_fork<const LR: bool, const N: usize>(
net: Res<PetriNet<DiningPhils>>,
tokens: Query<&Token<DiningPhils>, Changed<Token<DiningPhils>>>,
mut terms: Query<&mut Terminal>,
) {
let mut term = terms.single_mut();
if let Ok(token) = tokens.get_single() {
let x = if LR { COLS[2] } else { COLS[6] };
let y = if N == 0 { ROWS[2] } else { ROWS[4] };
show_place::<ForkTaken<LR, N>>(&net, token, &mut term, [x, y]);
let x = if LR { COLS[1] } else { COLS[7] };
show_trans::<Take<LR, N>>(&net, token, &mut term, [x, y]);
}
}
fn show_dirty_fork<const LR: bool, const N: usize>(
net: Res<PetriNet<DiningPhils>>,
tokens: Query<&Token<DiningPhils>, Changed<Token<DiningPhils>>>,
mut terms: Query<&mut Terminal>,
) {
let mut term = terms.single_mut();
if let Ok(token) = tokens.get_single() {
let x = if LR { COLS[2] } else { COLS[6] };
let y = if N == 0 { ROWS[0] } else { ROWS[6] };
show_place::<ForkDirty<LR, N>>(&net, token, &mut term, [x, y]);
let x = if LR { COLS[1] } else { COLS[7] };
show_trans::<Wash<LR, N>>(&net, token, &mut term, [x, y]);
}
}
fn show_philosopher<const N: usize>(
net: Res<PetriNet<DiningPhils>>,
tokens: Query<&Token<DiningPhils>, Changed<Token<DiningPhils>>>,
mut terms: Query<&mut Terminal>,
) {
let mut term = terms.single_mut();
if let Ok(token) = tokens.get_single() {
let y = if N == 0 { ROWS[1] } else { ROWS[5] };
show_place::<Eating<N>>(&net, token, &mut term, [COLS[3], y]);
show_place::<Thinking<N>>(&net, token, &mut term, [COLS[5], y]);
let y = if N == 0 { ROWS[2] } else { ROWS[4] };
show_trans::<Eat<N>>(&net, token, &mut term, [COLS[4], y]);
let y = if N == 0 { ROWS[0] } else { ROWS[6] };
show_trans::<Finish<N>>(&net, token, &mut term, [COLS[4], y]);
}
}