initial commit
This commit is contained in:
162
core/engine/examples/flow/example.rs
Normal file
162
core/engine/examples/flow/example.rs
Normal file
@@ -0,0 +1,162 @@
|
||||
use engine::mock_db::create_mock_db;
|
||||
use engine::{create_heromodels_engine, eval_file};
|
||||
use heromodels::models::flow::{Flow, FlowStep, SignatureRequirement};
|
||||
use heromodels_core::Model;
|
||||
use rhai::Scope;
|
||||
use std::path::Path;
|
||||
|
||||
mod mock;
|
||||
use mock::seed_flow_data;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Flow Rhai Example");
|
||||
println!("=================");
|
||||
|
||||
// Create a mock database
|
||||
let db = create_mock_db();
|
||||
|
||||
// Seed the database with initial data
|
||||
seed_flow_data(db.clone());
|
||||
|
||||
// Create the Rhai engine with all modules registered
|
||||
let engine = create_heromodels_engine(db.clone());
|
||||
|
||||
// Get the path to the script
|
||||
let script_path = Path::new(file!())
|
||||
.parent()
|
||||
.unwrap()
|
||||
.join("flow_script.rhai");
|
||||
|
||||
println!("\nRunning script: {}", script_path.display());
|
||||
println!("---------------------");
|
||||
|
||||
// Run the script
|
||||
match eval_file(&engine, &script_path.to_string_lossy()) {
|
||||
Ok(result) => {
|
||||
if !result.is_unit() {
|
||||
println!("\nScript returned: {:?}", result);
|
||||
}
|
||||
println!("\nScript executed successfully!");
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("\nError running script: {}", err);
|
||||
return Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
err.to_string(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
// Demonstrate direct Rust interaction with the Rhai-exposed flow functionality
|
||||
println!("\nDirect Rust interaction with Rhai-exposed flow functionality");
|
||||
println!("----------------------------------------------------------");
|
||||
|
||||
// Create a new scope
|
||||
let mut scope = Scope::new();
|
||||
|
||||
// Create a new flow using the Rhai function
|
||||
let result = engine.eval::<Flow>("new_flow(0, \"Direct Rust Flow\")");
|
||||
match result {
|
||||
Ok(mut flow) => {
|
||||
println!(
|
||||
"Created flow from Rust: {} (ID: {})",
|
||||
flow.name,
|
||||
flow.get_id()
|
||||
);
|
||||
|
||||
// Set flow status using the builder pattern
|
||||
flow = flow.status("active".to_string());
|
||||
println!("Set flow status to: {}", flow.status);
|
||||
|
||||
// Create a new flow step using the Rhai function
|
||||
let result = engine.eval::<FlowStep>("new_flow_step(0, 1)");
|
||||
|
||||
match result {
|
||||
Ok(mut step) => {
|
||||
println!(
|
||||
"Created flow step from Rust: Step Order {} (ID: {})",
|
||||
step.step_order,
|
||||
step.get_id()
|
||||
);
|
||||
|
||||
// Set step description
|
||||
step = step.description("Direct Rust Step".to_string());
|
||||
println!(
|
||||
"Set step description to: {}",
|
||||
step.description
|
||||
.clone()
|
||||
.unwrap_or_else(|| "None".to_string())
|
||||
);
|
||||
|
||||
// Create a signature requirement using the Rhai function
|
||||
let result = engine.eval::<SignatureRequirement>(
|
||||
"new_signature_requirement(0, 1, \"Direct Rust Signer\", \"Please sign this document\")"
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(req) => {
|
||||
println!(
|
||||
"Created signature requirement from Rust: Public Key {} (ID: {})",
|
||||
req.public_key,
|
||||
req.get_id()
|
||||
);
|
||||
|
||||
// Add the step to the flow using the builder pattern
|
||||
flow = flow.add_step(step);
|
||||
println!(
|
||||
"Added step to flow. Flow now has {} steps",
|
||||
flow.steps.len()
|
||||
);
|
||||
|
||||
// Save the flow to the database using the Rhai function
|
||||
let save_flow_script = "fn save_it(f) { return db::save_flow(f); }";
|
||||
let save_flow_ast = engine.compile(save_flow_script).unwrap();
|
||||
let result = engine.call_fn::<Flow>(
|
||||
&mut scope,
|
||||
&save_flow_ast,
|
||||
"save_it",
|
||||
(flow,),
|
||||
);
|
||||
match result {
|
||||
Ok(saved_flow) => {
|
||||
println!(
|
||||
"Saved flow to database with ID: {}",
|
||||
saved_flow.get_id()
|
||||
);
|
||||
}
|
||||
Err(err) => eprintln!("Error saving flow: {}", err),
|
||||
}
|
||||
|
||||
// Save the signature requirement to the database using the Rhai function
|
||||
let save_req_script =
|
||||
"fn save_it(r) { return db::save_signature_requirement(r); }";
|
||||
let save_req_ast = engine.compile(save_req_script).unwrap();
|
||||
let result = engine.call_fn::<SignatureRequirement>(
|
||||
&mut scope,
|
||||
&save_req_ast,
|
||||
"save_it",
|
||||
(req,),
|
||||
);
|
||||
match result {
|
||||
Ok(saved_req) => {
|
||||
println!(
|
||||
"Saved signature requirement to database with ID: {}",
|
||||
saved_req.get_id()
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error saving signature requirement: {}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => eprintln!("Error creating signature requirement: {}", err),
|
||||
}
|
||||
}
|
||||
Err(err) => eprintln!("Error creating flow step: {}", err),
|
||||
}
|
||||
}
|
||||
Err(err) => eprintln!("Error creating flow: {}", err),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
111
core/engine/examples/flow/flow_script.rhai
Normal file
111
core/engine/examples/flow/flow_script.rhai
Normal file
@@ -0,0 +1,111 @@
|
||||
// flow_script.rhai
|
||||
// Example Rhai script for working with Flow models
|
||||
|
||||
// Constants for Flow status
|
||||
const STATUS_DRAFT = "draft";
|
||||
const STATUS_ACTIVE = "active";
|
||||
const STATUS_COMPLETED = "completed";
|
||||
const STATUS_CANCELLED = "cancelled";
|
||||
|
||||
// Create a new flow using builder pattern
|
||||
let my_flow = new_flow(0, "flow-123");
|
||||
name(my_flow, "Document Approval Flow");
|
||||
status(my_flow, STATUS_DRAFT);
|
||||
|
||||
print(`Created flow: ${get_flow_name(my_flow)} (ID: ${get_flow_id(my_flow)})`);
|
||||
print(`Status: ${get_flow_status(my_flow)}`);
|
||||
|
||||
// Create flow steps using builder pattern
|
||||
let step1 = new_flow_step(0, 1);
|
||||
description(step1, "Initial review by legal team");
|
||||
status(step1, STATUS_DRAFT);
|
||||
|
||||
let step2 = new_flow_step(0, 2);
|
||||
description(step2, "Approval by department head");
|
||||
status(step2, STATUS_DRAFT);
|
||||
|
||||
let step3 = new_flow_step(0, 3);
|
||||
description(step3, "Final signature by CEO");
|
||||
status(step3, STATUS_DRAFT);
|
||||
|
||||
// Create signature requirements using builder pattern
|
||||
let req1 = new_signature_requirement(0, get_flow_step_id(step1), "legal@example.com", "Please review this document");
|
||||
signed_by(req1, "Legal Team");
|
||||
status(req1, STATUS_DRAFT);
|
||||
|
||||
let req2 = new_signature_requirement(0, get_flow_step_id(step2), "dept@example.com", "Department approval needed");
|
||||
signed_by(req2, "Department Head");
|
||||
status(req2, STATUS_DRAFT);
|
||||
|
||||
let req3 = new_signature_requirement(0, get_flow_step_id(step3), "ceo@example.com", "Final approval required");
|
||||
signed_by(req3, "CEO");
|
||||
status(req3, STATUS_DRAFT);
|
||||
|
||||
print(`Created flow steps with signature requirements`);
|
||||
|
||||
// Add steps to the flow
|
||||
let flow_with_steps = my_flow;
|
||||
add_step(flow_with_steps, step1);
|
||||
add_step(flow_with_steps, step2);
|
||||
add_step(flow_with_steps, step3);
|
||||
|
||||
print(`Added steps to flow. Flow now has ${get_flow_steps(flow_with_steps).len()} steps`);
|
||||
|
||||
// Activate the flow
|
||||
let active_flow = flow_with_steps;
|
||||
status(active_flow, STATUS_ACTIVE);
|
||||
print(`Updated flow status to: ${get_flow_status(active_flow)}`);
|
||||
|
||||
// Save the flow to the database
|
||||
let saved_flow = db::save_flow(active_flow);
|
||||
print(`Flow saved to database with ID: ${get_flow_id(saved_flow)}`);
|
||||
|
||||
// Save signature requirements to the database
|
||||
let saved_req1 = db::save_signature_requirement(req1);
|
||||
let saved_req2 = db::save_signature_requirement(req2);
|
||||
let saved_req3 = db::save_signature_requirement(req3);
|
||||
print(`Signature requirements saved to database with IDs: ${get_signature_requirement_id(saved_req1)}, ${get_signature_requirement_id(saved_req2)}, ${get_signature_requirement_id(saved_req3)}`);
|
||||
|
||||
// Retrieve the flow from the database
|
||||
let retrieved_flow = db::get_flow_by_id(get_flow_id(saved_flow));
|
||||
print(`Retrieved flow: ${get_flow_name(retrieved_flow)}`);
|
||||
print(`It has ${get_flow_steps(retrieved_flow).len()} steps`);
|
||||
|
||||
// Complete the flow
|
||||
let completed_flow = retrieved_flow;
|
||||
status(completed_flow, STATUS_COMPLETED);
|
||||
print(`Updated retrieved flow status to: ${get_flow_status(completed_flow)}`);
|
||||
|
||||
// Save the updated flow
|
||||
db::save_flow(completed_flow);
|
||||
print("Updated flow saved to database");
|
||||
|
||||
// List all flows in the database
|
||||
let all_flows = db::list_flows();
|
||||
print("\nListing all flows in database:");
|
||||
let flow_count = 0;
|
||||
for flow in all_flows {
|
||||
print(` - Flow: ${get_flow_name(flow)} (ID: ${get_flow_id(flow)})`);
|
||||
flow_count += 1;
|
||||
}
|
||||
print(`Total flows: ${flow_count}`);
|
||||
|
||||
// List all signature requirements
|
||||
let all_reqs = db::list_signature_requirements();
|
||||
print("\nListing all signature requirements in database:");
|
||||
let req_count = 0;
|
||||
for req in all_reqs {
|
||||
print(` - Requirement for step ${get_signature_requirement_flow_step_id(req)} (ID: ${get_signature_requirement_id(req)})`);
|
||||
req_count += 1;
|
||||
}
|
||||
print(`Total signature requirements: ${req_count}`);
|
||||
|
||||
// Clean up - delete the flow
|
||||
db::delete_flow(get_flow_id(completed_flow));
|
||||
print(`Deleted flow with ID: ${get_flow_id(completed_flow)}`);
|
||||
|
||||
// Clean up - delete signature requirements
|
||||
db::delete_signature_requirement(get_signature_requirement_id(saved_req1));
|
||||
db::delete_signature_requirement(get_signature_requirement_id(saved_req2));
|
||||
db::delete_signature_requirement(get_signature_requirement_id(saved_req3));
|
||||
print("Deleted all signature requirements");
|
65
core/engine/examples/flow/mock.rs
Normal file
65
core/engine/examples/flow/mock.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use heromodels::db::hero::OurDB;
|
||||
use heromodels::db::{Collection, Db};
|
||||
use heromodels::models::flow::{Flow, FlowStep, SignatureRequirement};
|
||||
use heromodels_core::Model;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Seed the mock database with flow data
|
||||
#[cfg(feature = "flow")]
|
||||
pub fn seed_flow_data(db: Arc<OurDB>) {
|
||||
// Create a flow
|
||||
let flow = Flow::new(None, "Onboarding Flow".to_string())
|
||||
.description("New employee onboarding process".to_string())
|
||||
.status("active".to_string());
|
||||
|
||||
// Create a signature requirement first
|
||||
let sig_req = SignatureRequirement::new(
|
||||
None,
|
||||
1,
|
||||
"hr_manager_pubkey".to_string(),
|
||||
"Please sign the employment contract".to_string(),
|
||||
);
|
||||
let (sig_req_id, saved_sig_req) = db
|
||||
.collection::<SignatureRequirement>()
|
||||
.expect("Failed to get SignatureRequirement collection")
|
||||
.set(&sig_req)
|
||||
.expect("Failed to store signature requirement");
|
||||
|
||||
// Create a flow step and add the signature requirement
|
||||
let step = FlowStep::new(None, 1)
|
||||
.description("Complete HR paperwork".to_string())
|
||||
.add_signature_requirement(sig_req_id);
|
||||
|
||||
let (step_id, saved_step) = db
|
||||
.collection::<FlowStep>()
|
||||
.expect("Failed to get FlowStep collection")
|
||||
.set(&step)
|
||||
.expect("Failed to store flow step");
|
||||
|
||||
// Add the step to the flow
|
||||
let flow_with_step = flow.add_step(step_id);
|
||||
|
||||
// Store the flow
|
||||
let (_flow_id, saved_flow) = db
|
||||
.collection::<Flow>()
|
||||
.expect("Failed to get Flow collection")
|
||||
.set(&flow_with_step)
|
||||
.expect("Failed to store flow");
|
||||
|
||||
println!("Mock database seeded with flow data:");
|
||||
println!(
|
||||
" - Added flow: {} (ID: {})",
|
||||
saved_flow.name,
|
||||
saved_flow.get_id()
|
||||
);
|
||||
println!(
|
||||
" - Added step with order: {} (ID: {})",
|
||||
saved_step.step_order,
|
||||
saved_step.get_id()
|
||||
);
|
||||
println!(
|
||||
" - Added signature requirement for: {} (ID: {})",
|
||||
saved_sig_req.public_key,
|
||||
saved_sig_req.get_id()
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user