Skip to content

Commit

Permalink
pass Tcontext to execute action
Browse files Browse the repository at this point in the history
  • Loading branch information
stackdump committed Sep 29, 2024
1 parent 6d764fb commit 55e5460
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pflow-metamodel"
version = "0.4.1"
version = "0.5.0"
edition = "2021"
description = "Declarative Petri-nets using a rust DSL"
license = "MIT"
Expand Down
14 changes: 9 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub mod zblob;
/// The `model` encapsulates the `PetriNet` and `Vasm` objects into a single `Model` object.
pub mod model;

/// The `display` module contains the `ImageBuilder` and `ImageOutput` traits for rendering Petri-nets as SVG.
mod display;

pub use crate::model::*;
Expand Down Expand Up @@ -283,7 +284,7 @@ pub trait Process<TContext> {
/// Runs the main loop of the state machine
fn run_impl(&self, action: Option<&str>, seq: Option<u64>, event_log: Vec<Event<TContext>>) -> Vec<Event<TContext>>;
/// Process an action and return the resulting event
fn process_action(&self, action: &str, seq: u64) -> Option<Event<TContext>>;
fn process_action(&self, action: &str, seq: u64, ctx: TContext) -> Option<Event<TContext>>;
/// Get the next action to be executed
fn next_action(&self) -> Vec<String>;
/// Execute an action and return the resulting event
Expand Down Expand Up @@ -401,7 +402,7 @@ mod tests {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
struct Context {
#[allow(unused)]
pub msg: String,
Expand Down Expand Up @@ -433,7 +434,8 @@ mod tests {
let mut current_seq = seq.unwrap_or(0) + 1;

while let Some(ref action) = current_action {
if let Some(transaction) = self.process_action(action, current_seq) {
let context = event_log.last().expect("last event").data.clone();
if let Some(transaction) = self.process_action(action, current_seq, context) {
event_log.push(transaction);
} else {
break;
Expand All @@ -458,17 +460,19 @@ mod tests {
/// # Panics
///
/// Panics if the lock fails
fn process_action(&self, action: &str, seq: u64) -> Option<Event<Context>> {
fn process_action(&self, action: &str, seq: u64, ctx: Context) -> Option<Event<Context>> {
let mut state = self.state.lock().expect("lock failed");
let res = self.model.vm.transform(&state, action, 1);
let mut data = ctx.clone();
data.msg = format!("completed! #{seq}: {action}");

if res.is_ok() {
*state = res.output;
let evt = Event {
action: action.to_string(),
seq,
state: state.clone(),
data: Context { msg: format!("completed! #{seq}: {action}") },
data,
};
let transaction = self.execute_action(evt);

Expand Down

0 comments on commit 55e5460

Please sign in to comment.