From 5e655b55309f2829aca9c7eb069cbb8f516a38b2 Mon Sep 17 00:00:00 2001 From: wangli <274027703@qq.com> Date: Tue, 16 Jul 2024 14:34:46 +0800 Subject: [PATCH] =?UTF-8?q?fix=20-=20=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=9C=BA=E6=99=AF=E4=B8=8B=EF=BC=8C=E5=B7=B2=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E7=9A=84=E5=AE=9E=E4=BE=8B=EF=BC=8C=E6=9F=A5=E5=AE=9E=E4=BE=8B?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BC=9A=E5=AD=98=E5=9C=A8NPE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ParallelMultiInstanceLoopVariableType.java | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 workflow-engine-core/src/main/java/org/flowable/engine/impl/variable/ParallelMultiInstanceLoopVariableType.java diff --git a/workflow-engine-core/src/main/java/org/flowable/engine/impl/variable/ParallelMultiInstanceLoopVariableType.java b/workflow-engine-core/src/main/java/org/flowable/engine/impl/variable/ParallelMultiInstanceLoopVariableType.java new file mode 100644 index 000000000..80f11888a --- /dev/null +++ b/workflow-engine-core/src/main/java/org/flowable/engine/impl/variable/ParallelMultiInstanceLoopVariableType.java @@ -0,0 +1,109 @@ +/* Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.flowable.engine.impl.variable; + +import org.flowable.bpmn.model.BoundaryEvent; +import org.flowable.common.engine.impl.interceptor.CommandContext; +import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; +import org.flowable.engine.impl.persistence.entity.ExecutionEntity; +import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager; +import org.flowable.engine.impl.util.CommandContextUtil; +import org.flowable.variable.api.types.ValueFields; +import org.flowable.variable.api.types.VariableType; + +import java.util.List; + +/** + * @author Filip Hrisafov + */ +public class ParallelMultiInstanceLoopVariableType implements VariableType { + + public static final String TYPE_NAME = "bpmnParallelMultiInstanceCompleted"; + protected static final String NUMBER_OF_INSTANCES = "nrOfInstances"; + + protected final ProcessEngineConfigurationImpl processEngineConfiguration; + + public ParallelMultiInstanceLoopVariableType(ProcessEngineConfigurationImpl processEngineConfiguration) { + this.processEngineConfiguration = processEngineConfiguration; + } + + @Override + public String getTypeName() { + return TYPE_NAME; + } + + @Override + public boolean isCachable() { + return false; + } + + @Override + public boolean isAbleToStore(Object value) { + return value instanceof ParallelMultiInstanceLoopVariable; + } + + @Override + public void setValue(Object value, ValueFields valueFields) { + if (value instanceof ParallelMultiInstanceLoopVariable) { + valueFields.setTextValue(((ParallelMultiInstanceLoopVariable) value).getExecutionId()); + valueFields.setTextValue2(((ParallelMultiInstanceLoopVariable) value).getType()); + } else { + valueFields.setTextValue(null); + valueFields.setTextValue2(null); + } + } + + @Override + public Object getValue(ValueFields valueFields) { + CommandContext commandContext = CommandContextUtil.getCommandContext(); + if (commandContext != null) { + return getValue(valueFields, commandContext); + } else { + return processEngineConfiguration.getCommandExecutor() + .execute(context -> getValue(valueFields, context)); + } + } + + protected Object getValue(ValueFields valueFields, CommandContext commandContext) { + String multiInstanceRootId = valueFields.getTextValue(); + String type = valueFields.getTextValue2(); + + ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext); + ExecutionEntityManager executionEntityManager = processEngineConfiguration.getExecutionEntityManager(); + ExecutionEntity multiInstanceRootExecution = executionEntityManager.findById(multiInstanceRootId); + if (multiInstanceRootExecution == null) { + /** + * @see https://github.com/flowable/flowable-engine/blob/main/modules/flowable-engine/src/main/java/org/flowable/engine/impl/variable/ParallelMultiInstanceLoopVariableType.java + */ + return 0; + } + List childExecutions = multiInstanceRootExecution.getExecutions(); + int nrOfActiveInstances = (int) childExecutions.stream().filter(execution -> execution.isActive() + && !(execution.getCurrentFlowElement() instanceof BoundaryEvent)).count(); + if (ParallelMultiInstanceLoopVariable.COMPLETED_INSTANCES.equals(type)) { + Object nrOfInstancesValue = multiInstanceRootExecution.getVariable(NUMBER_OF_INSTANCES); + int nrOfInstances = (Integer) (nrOfInstancesValue != null ? nrOfInstancesValue : 0); + return nrOfInstances - nrOfActiveInstances; + } else if (ParallelMultiInstanceLoopVariable.ACTIVE_INSTANCES.equals(type)) { + return nrOfActiveInstances; + } else { + //TODO maybe throw exception + return 0; + } + } + + @Override + public boolean isReadOnly() { + return true; + } +}