From d9ebc78754dc0c94c37bbb3c1f6bd4049a9ea29d Mon Sep 17 00:00:00 2001 From: luoyi <972849752@qq.com> Date: Wed, 19 Jul 2023 11:19:08 +0800 Subject: [PATCH] =?UTF-8?q?bug=20#I7KY2N=20=E9=80=BB=E8=BE=91=E8=BF=90?= =?UTF-8?q?=E7=AE=97=E5=A2=9E=E5=8A=A0=E7=9F=AD=E8=B7=AF=E6=95=88=E6=9E=9C?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E5=A4=9A=E4=BD=99=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../element/condition/AndOrCondition.java | 39 ++++++++++++------- .../BooleanOptELSpringbootTest.java | 11 +++++- .../src/test/resources/booleanOpt/flow.xml | 5 +++ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/AndOrCondition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/AndOrCondition.java index 3c70b6b8..129a93da 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/AndOrCondition.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/AndOrCondition.java @@ -1,7 +1,6 @@ package com.yomahub.liteflow.flow.element.condition; import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.util.BooleanUtil; import cn.hutool.core.util.StrUtil; import com.yomahub.liteflow.enums.ConditionTypeEnum; import com.yomahub.liteflow.exception.AndOrConditionException; @@ -11,7 +10,9 @@ import com.yomahub.liteflow.log.LFLog; import com.yomahub.liteflow.log.LFLoggerManager; import com.yomahub.liteflow.slot.DataBus; import com.yomahub.liteflow.slot.Slot; + import java.util.List; +import java.util.function.Predicate; public class AndOrCondition extends Condition { @@ -23,21 +24,10 @@ public class AndOrCondition extends Condition { public void executeCondition(Integer slotIndex) throws Exception { List itemList = this.getItem(); - if (CollUtil.isEmpty(itemList)){ throw new AndOrConditionException("boolean item list is null"); } - boolean[] booleanArray = new boolean[itemList.size()]; - - for (int i = 0; i < itemList.size(); i++) { - Executable item = itemList.get(i); - item.setCurrChainId(this.getCurrChainId()); - item.execute(slotIndex); - booleanArray[i] = item.getItemResultMetaValue(slotIndex); - LOG.info("the result of boolean component [{}] is [{}]", item.getId(), booleanArray[i]); - } - BooleanConditionTypeEnum booleanConditionType = this.getBooleanConditionType(); Slot slot = DataBus.getSlot(slotIndex); @@ -45,16 +35,37 @@ public class AndOrCondition extends Condition { String resultKey = StrUtil.format("{}_{}",this.getClass().getName(),this.hashCode()); switch (booleanConditionType) { case AND: - slot.setAndOrResult(resultKey, BooleanUtil.and(booleanArray)); + slot.setAndOrResult(resultKey, itemList.stream().allMatch(new AndOrConditionPredicate(slotIndex))); break; case OR: - slot.setAndOrResult(resultKey, BooleanUtil.or(booleanArray)); + slot.setAndOrResult(resultKey, itemList.stream().anyMatch(new AndOrConditionPredicate(slotIndex))); break; default: throw new AndOrConditionException("condition type must be 'AND' or 'OR'"); } } + private class AndOrConditionPredicate implements Predicate { + + private final Integer slotIndex; + + public AndOrConditionPredicate(Integer slotIndex) { + this.slotIndex = slotIndex; + } + + @Override + public boolean test(Executable condition) { + try { + condition.setCurrChainId(getCurrChainId()); + condition.execute(slotIndex); + return condition.getItemResultMetaValue(slotIndex); + } catch (Exception e) { + throw new AndOrConditionException(e.getMessage()); + } + } + + } + @Override @SuppressWarnings("unchecked") diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/booleanOpt/BooleanOptELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/booleanOpt/BooleanOptELSpringbootTest.java index 0e7eb64c..8d354796 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/booleanOpt/BooleanOptELSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/booleanOpt/BooleanOptELSpringbootTest.java @@ -39,7 +39,7 @@ public class BooleanOptELSpringbootTest extends BaseTest { public void testBooleanOpt2() throws Exception { LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg"); Assertions.assertTrue(response.isSuccess()); - Assertions.assertEquals("x1==>x2==>x3==>a", response.getExecuteStepStr()); + Assertions.assertEquals("x1==>a", response.getExecuteStepStr()); } // IF情况下AND+NOT @@ -64,4 +64,13 @@ public class BooleanOptELSpringbootTest extends BaseTest { Assertions.assertTrue(response.isSuccess()); Assertions.assertEquals("w1==>w2==>a==>bk==>w1==>w2==>a==>bk==>w1==>w2==>a==>bk==>w1==>w2==>a==>bk", response.getExecuteStepStr()); } + + // AND + NOT 实现短路效果 + @Test + public void testBooleanOpt6() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain6", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("x1==>b", response.getExecuteStepStr()); + } + } diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/booleanOpt/flow.xml b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/booleanOpt/flow.xml index 8898e245..b215310e 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/booleanOpt/flow.xml +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/booleanOpt/flow.xml @@ -25,4 +25,9 @@ WHILE(AND(w1, NOT(w2))).DO(a).BREAK(bk); + + + IF(AND(NOT(x1), x2, x3), a, b); + + \ No newline at end of file