Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-issues#1497] Using the getSerializableNodeInstances inside ProtobufProcessInstanceWriter #3707

Merged
merged 15 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.jbpm.workflow.core.Node;
import org.jbpm.workflow.core.node.CompositeNode;
Expand All @@ -54,7 +56,6 @@

/**
* Runtime counterpart of a composite node.
*
*/
public class CompositeNodeInstance extends StateBasedNodeInstance implements NodeInstanceContainer, EventNodeInstanceInterface, EventBasedNodeInstanceInterface {

Expand Down Expand Up @@ -204,7 +205,12 @@ public void removeNodeInstance(final NodeInstance nodeInstance) {

@Override
public Collection<org.kie.api.runtime.process.NodeInstance> getNodeInstances() {
return new ArrayList<>(getNodeInstances(false));
return Collections.unmodifiableCollection(nodeInstances);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change not required

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

}

@Override
public Collection<org.kie.api.runtime.process.NodeInstance> getSerializableNodeInstances() {
return nodeInstances.stream().filter(this::isSerializable).collect(Collectors.toUnmodifiableList());
}

@Override
Expand Down Expand Up @@ -473,4 +479,24 @@ public Map<String, Integer> getIterationLevels() {
return iterationLevels;
}

/**
* Return a Set of classes that are not serializable
* Every subclass should override it, if needed, to avoid polluting the parent one (this) with children details
*
* @return
*/
protected Set<Class<? extends org.kie.api.runtime.process.NodeInstance>> getNotSerializableClasses() {
return Collections.emptySet();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required.

}

/**
* Check if the given <code>org.kie.api.runtime.process.NodeInstance</code> is serializable.
*
* @param toCheck
* @return
*/
private boolean isSerializable(org.kie.api.runtime.process.NodeInstance toCheck) {
return !getNotSerializableClasses().contains(toCheck.getClass());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.jbpm.process.core.ContextContainer;
import org.jbpm.process.core.context.variable.VariableScope;
Expand Down Expand Up @@ -58,6 +59,8 @@
public class ForEachNodeInstance extends CompositeContextNodeInstance {

private static final long serialVersionUID = 510L;
private static final Set<Class<? extends org.kie.api.runtime.process.NodeInstance>> NOT_SERIALIZABLE_CLASSES = Set.of(ForEachJoinNodeInstance.class); // using Arrays.asList to allow multiple exclusions

public static final String TEMP_OUTPUT_VAR = "foreach_output";

private int totalInstances;
Expand Down Expand Up @@ -350,6 +353,11 @@ public int getLevelForNode(String uniqueID) {
return 1;
}

@Override
protected Set<Class<? extends org.kie.api.runtime.process.NodeInstance>> getNotSerializableClasses() {
return NOT_SERIALIZABLE_CLASSES;
}

private class ForEachNodeInstanceResolverFactory extends NodeInstanceResolverFactory {

private static final long serialVersionUID = -8856846610671009685L;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.jbpm.workflow.instance.node;

import java.util.Collection;
import java.util.Set;

import org.junit.jupiter.api.Test;
import org.kie.api.runtime.process.NodeInstance;

import static org.assertj.core.api.Assertions.assertThat;

class ForEachNodeInstanceTest {

@Test
void getNodeInstances() {
ForEachNodeInstance toTest = new ForEachNodeInstance();
CompositeNodeInstance compositeNodeInstance = new CompositeNodeInstance();
toTest.addNodeInstance(compositeNodeInstance);
Collection<NodeInstance> nodeInstances = toTest.getNodeInstances();
assertThat(nodeInstances)
.isNotNull()
.hasSize(1)
.contains(compositeNodeInstance);
ForEachNodeInstance.ForEachJoinNodeInstance forEachJoinNodeInstance = toTest.new ForEachJoinNodeInstance();
toTest.addNodeInstance(forEachJoinNodeInstance);
nodeInstances = toTest.getNodeInstances();
assertThat(nodeInstances)
.isNotNull()
.hasSize(2)
.contains(compositeNodeInstance, forEachJoinNodeInstance);
}

@Test
void getSerializableNodeInstances() {
ForEachNodeInstance toTest = new ForEachNodeInstance();
CompositeNodeInstance compositeNodeInstance = new CompositeNodeInstance();
toTest.addNodeInstance(compositeNodeInstance);
Collection<NodeInstance> serializableNodeInstances = toTest.getSerializableNodeInstances();
assertThat(serializableNodeInstances)
.isNotNull()
.hasSize(1)
.contains(compositeNodeInstance);
ForEachNodeInstance.ForEachJoinNodeInstance forEachJoinNodeInstance = toTest.new ForEachJoinNodeInstance();
toTest.addNodeInstance(forEachJoinNodeInstance);
serializableNodeInstances = toTest.getSerializableNodeInstances();
assertThat(serializableNodeInstances)
.isNotNull()
.hasSize(1)
.contains(compositeNodeInstance);
}

@Test
void getNotSerializableClasses() {
Set<Class<? extends NodeInstance>> retrieved = new ForEachNodeInstance().getNotSerializableClasses();
assertThat(retrieved).isNotNull()
.hasSize(1)
.allMatch(cls -> cls.equals(ForEachNodeInstance.ForEachJoinNodeInstance.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public FieldDescriptor getContextField(GeneratedMessageV3.Builder<?> builder) {
}

private <T extends NodeInstanceContainer & ContextInstanceContainer & ContextableInstance> WorkflowContext buildWorkflowContext(T nodeInstance) {
List<NodeInstance> nodeInstances = new ArrayList<>(nodeInstance.getNodeInstances());
List<NodeInstance> nodeInstances = new ArrayList<>(nodeInstance.getSerializableNodeInstances());
List<ContextInstance> exclusiveGroupInstances = nodeInstance.getContextInstances(ExclusiveGroup.EXCLUSIVE_GROUP);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) nodeInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
List<Map.Entry<String, Object>> variables = (variableScopeInstance != null) ? new ArrayList<>(variableScopeInstance.getVariables().entrySet()) : Collections.emptyList();
Expand Down
Loading