Skip to content

Commit

Permalink
doc:readme:example
Browse files Browse the repository at this point in the history
  • Loading branch information
galenseilis committed Nov 6, 2024
1 parent e39bdbb commit 0f3583a
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions desru/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,58 @@ fn main() {
}
```

### Clock

Inspired by the simple clock example in the SimPy documentation.


```rust
use desru::{Event, EventScheduler};

fn clock(scheduler: &mut EventScheduler, name: String, tick: f64) {
// Function to handle the clock's actions and schedule the next tick
fn action(scheduler: &mut EventScheduler, name: String, tick: f64) {
// Print the name of the clock and the current simulation time
println!("{}: {}", name, scheduler.current_time);

// Schedule the next tick of the clock
let next_time = scheduler.current_time + tick;
let event = Event::new(
next_time,
Some(Box::new(move |scheduler: &mut EventScheduler| {
action(scheduler, name.clone(), tick);
None
})),
None,
);
scheduler.schedule(event);
}

// Schedule the first event for the clock at time 0
scheduler.schedule(Event::new(
0.0,
Some(Box::new(move |scheduler: &mut EventScheduler| {
action(scheduler, name.clone(), tick);
None
})),
None,
));
}

fn main() {
// Initialize the event scheduler
let mut scheduler = EventScheduler::new();

// Schedule initial clock processes
clock(&mut scheduler, "fast".to_string(), 0.5);
clock(&mut scheduler, "slow".to_string(), 1.0);

// Run the scheduler until the maximum simulation time
scheduler.run_until_max_time(2.0);
}
```


### Simple Car Process

This example replicates the classic SimPy Car simulation, where a car alternates between parking and driving.
Expand Down

0 comments on commit 0f3583a

Please sign in to comment.