Skip to main content

matrixcode_core/workflow/executors/
condition.rs

1//! Condition Executor
2//!
3//! 条件判断执行器,使用 rule_engine 进行条件判断和分支选择。
4
5use anyhow::Result;
6use async_trait::async_trait;
7
8use crate::workflow::context::WorkflowContext;
9use crate::workflow::def::NodeDef;
10use crate::workflow::rule_engine::evaluate_expression;
11use crate::workflow::template::TemplateRenderer;
12use super::node_executor::NodeExecutor;
13
14/// 条件执行器
15///
16/// 使用 rule_engine 进行条件判断和分支选择。
17pub struct ConditionExecutor {
18    /// 模板渲染器
19    template_renderer: TemplateRenderer,
20}
21
22impl ConditionExecutor {
23    /// 创建新的条件执行器
24    pub fn new() -> Self {
25        Self {
26            template_renderer: TemplateRenderer::new(),
27        }
28    }
29}
30
31impl Default for ConditionExecutor {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37#[async_trait]
38impl NodeExecutor for ConditionExecutor {
39    async fn execute(
40        &self,
41        node: &NodeDef,
42        context: &mut WorkflowContext,
43    ) -> Result<serde_json::Value> {
44        // 条件节点必须有分支定义
45        let branches = node.branches.as_ref()
46            .ok_or_else(|| anyhow::anyhow!("Condition node '{}' has no branches", node.id))?;
47
48        // 遍历所有分支,找到匹配的
49        for branch in branches {
50            // 渲染条件表达式
51            let rendered_condition = self.template_renderer
52                .render(&branch.condition, &context.variables)?;
53
54            // 评估条件
55            let passed = evaluate_expression(&rendered_condition, &context.variables)?;
56
57            if passed {
58                // 找到匹配分支,返回目标节点
59                return Ok(serde_json::json!({
60                    "matched_branch": branch.name,
61                    "target": branch.target,
62                    "condition": branch.condition,
63                }));
64            }
65        }
66
67        // 没有匹配的分支
68        Ok(serde_json::json!({
69            "matched": false,
70            "branches_checked": branches.len(),
71        }))
72    }
73
74    fn name(&self) -> &str {
75        "condition_executor"
76    }
77}