Skip to content

Commit

Permalink
[incubator-kie-issues-1195] Process Variable support multiple metadat…
Browse files Browse the repository at this point in the history
…a fields
  • Loading branch information
elguardian committed May 28, 2024
1 parent a4e5345 commit 6f7e454
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import org.kie.api.definition.process.NodeContainer;
import org.kie.api.definition.process.WorkflowElementIdentifier;
import org.kie.kogito.internal.process.runtime.KogitoNode;
import org.kie.kogito.internal.utils.KogitoTags;

import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.ClassExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.NullLiteralExpr;
Expand Down Expand Up @@ -102,13 +102,28 @@ protected void visitVariableScope(String field, VariableScope variableScope, Blo
if (!visitedVariables.add(variable.getName())) {
continue;
}
String tags = (String) variable.getMetaData(KogitoTags.VARIABLE_TAGS);

Map<String, Object> metaData = variable.getMetaData();
NodeList<Expression> parameters = new NodeList<>();
for (Map.Entry<String, Object> entry : metaData.entrySet().stream().filter(e -> e.getValue() != null).toList()) {
parameters.add(new StringLiteralExpr(entry.getKey()));
parameters.add(new StringLiteralExpr(entry.getValue().toString()));
}

Expression metadataExpression = new FieldAccessExpr(new NameExpr(Map.class.getPackage().getName()), Map.class.getSimpleName());
metadataExpression = new MethodCallExpr(metadataExpression, "of", parameters);
Object defaultValue = variable.getValue();
body.tryAddImportToParentCompilationUnit(variable.getType().getClass());
body.addStatement(getFactoryMethod(field, METHOD_VARIABLE, new StringLiteralExpr(variable.getName()),
new MethodCallExpr(DataTypeResolver.class.getName() + ".fromClass", new ClassExpr(parseClassOrInterfaceType(variable.getType().getStringType()).removeTypeArguments())),
defaultValue != null ? new StringLiteralExpr(defaultValue.toString()) : new NullLiteralExpr(), new StringLiteralExpr(KogitoTags.VARIABLE_TAGS),
tags != null ? new StringLiteralExpr(tags) : new NullLiteralExpr()));

NodeList<Expression> variableFactoryParameters = new NodeList<>();
variableFactoryParameters.add(new StringLiteralExpr(variable.getName()));
variableFactoryParameters
.add(new MethodCallExpr(DataTypeResolver.class.getName() + ".fromClass", new ClassExpr(parseClassOrInterfaceType(variable.getType().getStringType()).removeTypeArguments())));
if (defaultValue != null) {
variableFactoryParameters.add(new StringLiteralExpr(defaultValue.toString()));
}
variableFactoryParameters.add(metadataExpression);
body.addStatement(getFactoryMethod(field, METHOD_VARIABLE, variableFactoryParameters.stream().toArray(Expression[]::new)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.jbpm.compiler.canonical;

import java.util.Collections;

import org.jbpm.process.core.datatype.impl.type.IntegerDataType;
import org.jbpm.process.core.datatype.impl.type.ObjectDataType;
import org.jbpm.process.core.datatype.impl.type.StringDataType;
Expand Down Expand Up @@ -130,8 +132,8 @@ public void testScriptVariablewithDefaultValue() {
RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("demo.orders");
factory
.variable("order", new ObjectDataType("com.myspace.demo.Order"))
.variable("approver", new StringDataType(), "john", "customTags", null)
.variable("age", new IntegerDataType(), "1", "customTags", null)
.variable("approver", new StringDataType(), "john", Collections.singletonMap("customTags", null))
.variable("age", new IntegerDataType(), "1", Collections.singletonMap("customTags", null))
.name("orders")
.packageName("com.myspace.demo")
.dynamic(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.jbpm.ruleflow.core;

import java.util.Map;

import org.jbpm.process.core.Context;
import org.jbpm.process.core.ContextContainer;
import org.jbpm.process.core.context.exception.ActionExceptionHandler;
Expand Down Expand Up @@ -237,11 +239,11 @@ public T errorExceptionHandler(String signalType, String faultCode, String fault

public abstract T variable(String name, DataType type);

public abstract T variable(String name, DataType type, Object value);
public abstract T variable(String name, DataType type, Map<String, Object> metadata);

public abstract T variable(String name, DataType type, String metaDataName, Object metaDataValue);
public abstract T variable(String name, DataType type, Object value);

public abstract T variable(String name, DataType type, Object value, String metaDataName, Object metaDataValue);
public abstract T variable(String name, DataType type, Object value, Map<String, Object> metadata);

private <S extends Context> S getScope(String scopeType, Class<S> scopeClass) {
ContextContainer contextContainer = (ContextContainer) getNodeContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -200,29 +201,30 @@ public RuleFlowProcessFactory variable(String name, Class<?> clazz) {

@Override
public RuleFlowProcessFactory variable(String name, DataType type) {
return variable(name, type, null);
return variable(name, type, Collections.emptyMap());
}

@Override
public RuleFlowProcessFactory variable(String name, DataType type, Object value) {
return variable(name, type, value, null, null);
return variable(name, type, value, Collections.emptyMap());
}

@Override
public RuleFlowProcessFactory variable(String name, DataType type, String metaDataName, Object metaDataValue) {
return variable(name, type, null, metaDataName, metaDataValue);
public RuleFlowProcessFactory variable(String name, DataType type, Map<String, Object> metadata) {
return this.variable(name, type, null, metadata);
}

@Override
public RuleFlowProcessFactory variable(String name, DataType type, Object value, String metaDataName, Object metaDataValue) {

public RuleFlowProcessFactory variable(String name, DataType type, Object value, Map<String, Object> metadata) {
Variable variable = new Variable();
variable.setName(name);
variable.setType(type);
variable.setValue(type.verifyDataType(value) ? value : type.readValue((String) value));
if (metaDataName != null && metaDataValue != null) {
variable.setMetaData(metaDataName, metaDataValue);

for (Map.Entry<String, Object> entry : metadata.entrySet()) {
variable.setMetaData(entry.getKey(), entry.getValue());
}

getRuleFlowProcess().getVariableScope().getVariables().add(variable);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.jbpm.ruleflow.core.factory;

import java.util.Collections;
import java.util.Map;

import org.jbpm.process.core.context.variable.Variable;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.process.core.datatype.DataType;
Expand Down Expand Up @@ -53,28 +56,28 @@ public T timeout(String timeout) {

@Override
public T variable(String name, DataType type) {
return variable(name, type, null);
return variable(name, type, Collections.emptyMap());
}

@Override
public T variable(String name, DataType type, Object value) {
return variable(name, type, value, null, null);
return variable(name, type, value, Collections.emptyMap());
}

@Override
public T variable(String name, DataType type, String metaDataName, Object metaDataValue) {
return variable(name, type, null, metaDataName, metaDataValue);
public T variable(String name, DataType type, Map<String, Object> metaData) {
return variable(name, type, null, metaData);
}

@Override
public T variable(String name, DataType type, Object value, String metaDataName, Object metaDataValue) {
public T variable(String name, DataType type, Object value, Map<String, Object> metadata) {
Variable variable = new Variable();
variable.setName(name);
variable.setType(type);
variable.setValue(value);
VariableScope variableScope = (VariableScope) getCompositeNode().getDefaultContext(VariableScope.VARIABLE_SCOPE);
if (metaDataName != null && metaDataValue != null) {
variable.setMetaData(metaDataName, metaDataValue);
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
variable.setMetaData(entry.getKey(), entry.getValue());
}
if (variableScope == null) {
variableScope = new VariableScope();
Expand Down
20 changes: 20 additions & 0 deletions jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/VariableTagsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.drools.io.ClassPathResource;
import org.jbpm.bpmn2.objects.TestWorkItemHandler;
import org.jbpm.bpmn2.tags.ApprovalWithRequiredVariableTagsProcess;
import org.jbpm.process.core.context.variable.Variable;
import org.jbpm.ruleflow.core.RuleFlowProcess;
import org.jbpm.test.utils.ProcessTestHelper;
import org.junit.jupiter.api.Test;
import org.kie.api.event.process.ProcessVariableChangedEvent;
import org.kie.kogito.Application;
import org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener;
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;
import org.kie.kogito.internal.process.runtime.KogitoWorkItem;
Expand All @@ -36,11 +42,25 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.entry;
import static org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ABORTED;
import static org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ACTIVE;

public class VariableTagsTest extends JbpmBpmn2TestCase {

@Test
public void testVariableMultipleMetadata() throws Exception {
Application app = ProcessTestHelper.newApplication();
ApprovalWithRequiredVariableTagsProcess processDefinition = (ApprovalWithRequiredVariableTagsProcess) ApprovalWithRequiredVariableTagsProcess.newProcess(app);

RuleFlowProcess processEngine = (RuleFlowProcess) processDefinition.process();
Optional<Variable> var = processEngine.getVariableScope().getVariables().stream().filter(e -> "approver".equals(e.getName())).findAny();
assertThat(var).isPresent();
assertThat(var.get().getMetaData()).hasSize(3);
assertThat(var.get().getMetaData()).containsExactly(entry("approver", "approver"), entry("customTags", "required"), entry("ItemSubjectRef", "ItemDefinition_9"));

}

@Test
public void testProcessWithMissingRequiredVariable() throws Exception {
kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/tags/BPMN2-ApprovalWithRequiredVariableTags.bpmn2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ protected final MakeNodeResult filterAndMergeNode(RuleFlowNodeContainerFactory<?
boolean shouldMerge, FilterableNodeSupplier nodeSupplier) {

if (isTempVariable(actionVarName)) {
embeddedSubProcess.variable(actionVarName, new ObjectDataType(JsonNode.class.getCanonicalName()), KogitoTags.VARIABLE_TAGS, KogitoTags.INTERNAL_TAG);
embeddedSubProcess.variable(actionVarName, new ObjectDataType(JsonNode.class.getCanonicalName()), Map.of(KogitoTags.VARIABLE_TAGS, KogitoTags.INTERNAL_TAG));
}
NodeFactory<?, ?> startNode, currentNode;
if (fromStateExpr != null) {
Expand Down

0 comments on commit 6f7e454

Please sign in to comment.