enhancement #ICM6TX when 表达式构造增加 percentage 关键字

This commit is contained in:
luoyi
2025-07-16 22:48:25 +08:00
parent 504a3c6361
commit 3fa913d58a
2 changed files with 47 additions and 5 deletions

View File

@@ -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<String> 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();

View File

@@ -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()));
}
}