bpm-engine-runtime 0.2.0

BPM engine runtime: dispatcher, executor, timers, and event handling
Documentation
//! GatewayEvaluator: evaluate gateway, return target node(s).

use std::collections::HashMap;

use bpm_engine_core::{EdgeCondition, Node, NodeId};

pub trait GatewayEvaluator {
    fn evaluate(&self, node: &Node, variables: &HashMap<String, String>) -> Vec<NodeId>;
}

pub struct ExclusiveGatewayEvaluator;

impl GatewayEvaluator for ExclusiveGatewayEvaluator {
    fn evaluate(&self, node: &Node, variables: &HashMap<String, String>) -> Vec<NodeId> {
        let mut default_target: Option<NodeId> = None;
        for edge in &node.outgoing_edges {
            match &edge.condition {
                None => return vec![edge.target],
                Some(EdgeCondition::Default) => default_target = Some(edge.target),
                Some(EdgeCondition::VariableEq { key, value }) => {
                    if variables.get(key) == Some(value) {
                        return vec![edge.target];
                    }
                }
                Some(EdgeCondition::Expression(expr)) => {
                    if super::el::eval_condition(expr, variables).unwrap_or(false) {
                        return vec![edge.target];
                    }
                }
            }
        }
        default_target.map(|t| vec![t]).unwrap_or_default()
    }
}