Files
osiris/examples/engine/08_flow.rhai
Timur Gordon 87c556df7a wip
2025-10-29 16:52:33 +01:00

91 lines
2.7 KiB
Plaintext

// Example 8: Flow Template and Instance Management
print("=== Flow Example ===\n");
// Get a context to work with
print("1. Getting context...");
let ctx = get_context(["alice", "bob"]);
print(" ✓ Context created: " + ctx.context_id());
// Create a registration flow template
print("\n2. Creating registration flow template...");
let registration_flow = new_flow()
.name("registration_flow")
.description("User Registration Flow with KYC");
registration_flow.add_step("registration", "User PK Registration");
registration_flow.add_step("kyc", "KYC Verification");
registration_flow.add_step("email", "Email Verification");
print(" ✓ Created registration flow template");
print(" Template: " + registration_flow.get_name());
print(" Description: " + registration_flow.get_description());
// Save the template
print("\n3. Saving flow template to context...");
let template_id = ctx.save(registration_flow.build());
print(" ✓ Saved template with ID: " + template_id);
// Create a flow instance for a user
print("\n4. Creating flow instance for user...");
let user_id = "user_123";
let flow_instance = new_flow_instance(
"registration_flow_user_123",
"registration_flow",
user_id
);
// Initialize steps (in real scenario, would get from template)
flow_instance.start();
print(" ✓ Created and started flow instance");
print(" Instance name: " + flow_instance.get_name());
print(" Template: " + flow_instance.get_template_name());
print(" Entity ID: " + flow_instance.get_entity_id());
print(" Status: " + flow_instance.get_status());
// Save the instance
print("\n5. Saving flow instance to context...");
let instance_id = ctx.save(flow_instance);
print(" ✓ Saved instance with ID: " + instance_id);
// Simulate completing steps
print("\n6. Simulating step completion...");
// Complete registration step
flow_instance.complete_step("registration");
print(" ✓ Completed 'registration' step");
// Save updated instance
ctx.save(flow_instance);
// Complete KYC step
flow_instance.complete_step("kyc");
print(" ✓ Completed 'kyc' step");
// Save updated instance
ctx.save(flow_instance);
// Complete email step
flow_instance.complete_step("email");
print(" ✓ Completed 'email' step");
// Save final instance
ctx.save(flow_instance);
print(" Final status: " + flow_instance.get_status());
// List all flow templates
print("\n7. Listing flow templates in context...");
let template_ids = ctx.list("flow_templates");
print(" ✓ Found " + template_ids.len() + " flow templates");
// List all flow instances
print("\n8. Listing flow instances in context...");
let instance_ids = ctx.list("flow_instances");
print(" ✓ Found " + instance_ids.len() + " flow instances");
print("\n=== Flow Example Complete ===");