From 3fa913d58a3cda03a2e176bd87a0b4ebbe04bb41 Mon Sep 17 00:00:00 2001 From: luoyi <972849752@qq.com> Date: Wed, 16 Jul 2025 22:48:25 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#ICM6TX=20when=20=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F=E6=9E=84=E9=80=A0=E5=A2=9E=E5=8A=A0=20percen?= =?UTF-8?q?tage=20=E5=85=B3=E9=94=AE=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/builder/el/WhenELWrapper.java | 33 ++++++++++++++++--- .../test/builder/WhenELBuilderTest.java | 19 +++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/liteflow-el-builder/src/main/java/com/yomahub/liteflow/builder/el/WhenELWrapper.java b/liteflow-el-builder/src/main/java/com/yomahub/liteflow/builder/el/WhenELWrapper.java index 34384e087..9e3d47935 100644 --- a/liteflow-el-builder/src/main/java/com/yomahub/liteflow/builder/el/WhenELWrapper.java +++ b/liteflow-el-builder/src/main/java/com/yomahub/liteflow/builder/el/WhenELWrapper.java @@ -1,9 +1,14 @@ package com.yomahub.liteflow.builder.el; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.StrUtil; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; /** * 并行组件 @@ -23,6 +28,7 @@ public class WhenELWrapper extends ELWrapper { private boolean ignoreError; private String customThreadExecutor; private final List mustExecuteList; + private Double percentage; public WhenELWrapper(ELWrapper... elWrappers) { this.addWrapper(elWrappers); @@ -41,6 +47,11 @@ public class WhenELWrapper extends ELWrapper { return this; } + public WhenELWrapper percentage(double percentage) { + this.percentage = percentage; + return this; + } + public WhenELWrapper ignoreError(boolean ignoreError) { this.ignoreError = ignoreError; return this; @@ -110,6 +121,18 @@ public class WhenELWrapper extends ELWrapper { @Override protected String toEL(Integer depth, StringBuilder paramContext) { + + // 互斥检查:确保三个属性中最多只有一个被设置 + long count = Stream.of( + this.any ? 1 : 0, + CollectionUtil.isNotEmpty(this.mustExecuteList) ? 1 : 0, + ObjUtil.isNotNull(this.percentage) ? 1 : 0 + ).filter(num -> num > 0).count(); + + if (count > 1) { + throw new IllegalArgumentException("Properties 'any', 'must', and 'percentage' are mutually exclusive. Only one can be set at a time."); + } + Integer sonDepth = depth == null ? null : depth + 1; StringBuilder sb = new StringBuilder(); @@ -139,10 +162,6 @@ public class WhenELWrapper extends ELWrapper { sb.append(StrUtil.format(".threadPool(\"{}\")", customThreadExecutor)); } if(CollectionUtil.isNotEmpty(mustExecuteList)){ - // 校验must 语义与 any语义冲突 - if (this.any){ - throw new IllegalArgumentException("'.must()' and '.any()' can use in when component at the same time!"); - } // 处理must子表达式输出 sb.append(".must("); for(int i = 0; i < mustExecuteList.size(); i++){ @@ -154,6 +173,10 @@ public class WhenELWrapper extends ELWrapper { sb.append(")"); } + if (ObjUtil.isNotNull(percentage)){ + sb.append(StrUtil.format(".percentage({})", percentage)); + } + // 处理公共属性输出 processWrapperProperty(sb, paramContext); return sb.toString(); diff --git a/liteflow-testcase-el/liteflow-testcase-el-builder/src/test/java/com/yomahub/liteflow/test/builder/WhenELBuilderTest.java b/liteflow-testcase-el/liteflow-testcase-el-builder/src/test/java/com/yomahub/liteflow/test/builder/WhenELBuilderTest.java index 1f8b1623f..f8293ed46 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-builder/src/test/java/com/yomahub/liteflow/test/builder/WhenELBuilderTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-builder/src/test/java/com/yomahub/liteflow/test/builder/WhenELBuilderTest.java @@ -210,4 +210,23 @@ public class WhenELBuilderTest extends BaseTest { WhenELWrapper el = ELBus.when(ELBus.node("a"), ELBus.node("b"), ELBus.node("c")).customThreadExecutor("com.yomahub.liteflow.test.builder.customTreadExecutor.CustomThreadExecutor1"); Assertions.assertTrue(LiteFlowChainELBuilder.validate(el.toEL())); } + + // 测试互斥属性设置 + @Test + public void testWHEN1(){ + WhenELWrapper el = ELBus.when(ELBus.node("a"), ELBus.node("b")).percentage(0.66).any(true); + WhenELWrapper el2 = ELBus.when(ELBus.node("a"), ELBus.node("b")).must("a").any(true); + WhenELWrapper el3 = ELBus.when(ELBus.node("a"), ELBus.node("b")).must("a").percentage(0.55); + Assertions.assertThrowsExactly(IllegalArgumentException.class, el::toEL); + Assertions.assertThrowsExactly(IllegalArgumentException.class, el2::toEL); + Assertions.assertThrowsExactly(IllegalArgumentException.class, el3::toEL); + } + + // 测试 percentage 属性 + @Test + public void testWHEN2(){ + WhenELWrapper el = ELBus.when(ELBus.node("a"), ELBus.node("b")).percentage(0.66); + Assertions.assertTrue(LiteFlowChainELBuilder.validate(el.toEL())); + } + }