Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Aug 1, 2024
1 parent 2ad6b53 commit e135d06
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public interface ProcessInstance<T> {
* @param trigger name of the trigger that will indicate what start node to trigger
* @param referenceId optional reference id that points to a another component triggering this instance
*/
void trigger(String trigger, String referenceId, Map<String, Object> payload);
void trigger(String trigger, String referenceId, Object payload);

/**
* Starts process instance with trigger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.jbpm.process.instance;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -35,8 +34,6 @@
import org.jbpm.process.core.timer.DateTimeUtils;
import org.jbpm.process.core.timer.Timer;
import org.jbpm.ruleflow.core.RuleFlowProcess;
import org.jbpm.workflow.core.impl.DataAssociation;
import org.jbpm.workflow.core.impl.NodeIoHelper;
import org.jbpm.workflow.core.node.EventTrigger;
import org.jbpm.workflow.core.node.StartNode;
import org.jbpm.workflow.core.node.Trigger;
Expand Down Expand Up @@ -66,8 +63,6 @@
import org.kie.kogito.signal.SignalManager;
import org.kie.kogito.uow.UnitOfWorkManager;

import static org.jbpm.ruleflow.core.Metadata.TRIGGER_MAPPING_INPUT;

public class LightProcessRuntime extends AbstractProcessRuntime {
private ProcessRuntimeContext runtimeContext;
private final InternalKnowledgeRuntime knowledgeRuntime;
Expand Down Expand Up @@ -263,7 +258,7 @@ private void initProcessEventListener(Process process) {
type = eventTypeFilter.getType();
}
}
StartProcessEventListener listener = new StartProcessEventListener(startNode, trigger, process.getId(), filters);
StartProcessEventListener listener = new StartProcessEventListener(process.getId(), filters);
signalManager.addEventListener(type, listener);
ruleFlowProcess.getRuntimeMetaData().put("StartProcessEventType", type);
ruleFlowProcess.getRuntimeMetaData().put("StartProcessEventListener", listener);
Expand All @@ -278,12 +273,8 @@ private void initProcessEventListener(Process process) {
private class StartProcessEventListener implements EventListener {
private String processId;
private List<EventFilter> eventFilters;
private Trigger trigger;
private StartNode startNode;

public StartProcessEventListener(StartNode startNode, Trigger trigger, String processId, List<EventFilter> eventFilters) {
this.startNode = startNode;
this.trigger = trigger;
public StartProcessEventListener(String processId, List<EventFilter> eventFilters) {
this.processId = processId;
this.eventFilters = eventFilters;
}
Expand All @@ -300,31 +291,11 @@ public void signalEvent(String type, Object event) {
return;
}
}
Map<String, Object> outputSet = new HashMap<>();
for (Map.Entry<String, String> entry : trigger.getInMappings().entrySet()) {
outputSet.put(entry.getKey(), entry.getKey());
}
// data association needs to be corrected as it is not input mapping but output mapping
boolean eventFound = false;
for (DataAssociation dataAssociation : trigger.getInAssociations()) {
if ("event".equals(dataAssociation.getSources().get(0).getLabel())) {
eventFound = true;
}
}

if (!eventFound && !trigger.getInAssociations().isEmpty()) {
String inputLabel = (String) startNode.getMetaData(TRIGGER_MAPPING_INPUT);
outputSet.put(inputLabel, event);
} else {
outputSet.put("event", event);
}

Map<String, Object> parameters = NodeIoHelper.processOutputs(trigger.getInAssociations(), key -> outputSet.get(key));

UnitOfWorkExecutor.executeInUnitOfWork(unitOfWorkManager, () -> {
org.kie.kogito.process.Process<? extends Model> process = getApplication().get(Processes.class).processById(processId);
org.kie.kogito.process.ProcessInstance<?> pi = process.createInstance(process.createModel());
pi.trigger(type, type, parameters);
pi.trigger(type, type, event);
return null;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.jbpm.process.instance;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -42,25 +41,19 @@
import org.jbpm.process.instance.event.KogitoProcessEventSupportImpl;
import org.jbpm.process.instance.impl.DefaultProcessInstanceManagerFactory;
import org.jbpm.ruleflow.core.RuleFlowProcess;
import org.jbpm.workflow.core.impl.DataAssociation;
import org.jbpm.workflow.core.impl.NodeIoHelper;
import org.jbpm.workflow.core.node.EventTrigger;
import org.jbpm.workflow.core.node.StartNode;
import org.jbpm.workflow.core.node.Trigger;
import org.kie.api.KieBase;
import org.kie.api.command.ExecutableCommand;
import org.kie.api.definition.process.Node;
import org.kie.api.definition.process.Process;
import org.kie.api.event.rule.DefaultAgendaEventListener;
import org.kie.api.event.rule.MatchCreatedEvent;
import org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent;
import org.kie.api.runtime.Context;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.process.EventListener;
import org.kie.api.runtime.process.ProcessInstance;
import org.kie.api.runtime.process.WorkItemManager;
import org.kie.api.runtime.rule.AgendaFilter;
import org.kie.internal.command.RegistryContext;
import org.kie.internal.process.CorrelationKey;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import org.kie.kogito.Application;
Expand All @@ -78,8 +71,6 @@
import org.kie.kogito.signal.SignalManager;
import org.kie.kogito.uow.UnitOfWorkManager;

import static org.jbpm.ruleflow.core.Metadata.TRIGGER_MAPPING_INPUT;

public class ProcessRuntimeImpl extends AbstractProcessRuntime {

private InternalKnowledgeRuntime kruntime;
Expand Down Expand Up @@ -301,7 +292,7 @@ private void initProcessEventListener(Process process) {
type = ((EventTypeFilter) filter).getType();
}
}
StartProcessEventListener listener = new StartProcessEventListener(startNode, trigger, process.getId(), filters);
StartProcessEventListener listener = new StartProcessEventListener(process.getId(), filters);
signalManager.addEventListener(type, listener);
((RuleFlowProcess) process).getRuntimeMetaData().put("StartProcessEventType", type);
((RuleFlowProcess) process).getRuntimeMetaData().put("StartProcessEventListener", listener);
Expand Down Expand Up @@ -476,14 +467,10 @@ private class StartProcessEventListener implements EventListener {

private String processId;
private List<EventFilter> eventFilters;
private StartNode startNode;
private Trigger trigger;

public StartProcessEventListener(StartNode startNode, Trigger trigger, String processId, List<EventFilter> eventFilters) {
public StartProcessEventListener(String processId, List<EventFilter> eventFilters) {
this.processId = processId;
this.eventFilters = eventFilters;
this.trigger = trigger;
this.startNode = startNode;
}

@Override
Expand All @@ -498,53 +485,10 @@ public void signalEvent(final String type, Object event) {
return;
}
}
Map<String, Object> outputSet = new HashMap<>();
for (Map.Entry<String, String> entry : trigger.getInMappings().entrySet()) {
outputSet.put(entry.getKey(), entry.getKey());
}

// data association needs to be corrected as it is not input mapping but output mapping
boolean eventFound = false;
for (DataAssociation dataAssociation : trigger.getInAssociations()) {
if ("event".equals(dataAssociation.getSources().get(0).getLabel())) {
eventFound = true;
}
}

if (!eventFound && !trigger.getInAssociations().isEmpty()) {
String inputLabel = (String) startNode.getMetaData(TRIGGER_MAPPING_INPUT);
outputSet.put(inputLabel, event);
} else {
outputSet.put("event", event);
}

Map<String, Object> parameters = NodeIoHelper.processOutputs(trigger.getInAssociations(), key -> outputSet.get(key));
startProcessWithParamsAndTrigger(processId, parameters, type);

}
}

private class StartProcessWithTypeCommand implements ExecutableCommand<Void> {

private static final long serialVersionUID = -8890906804846111698L;

private String processId;
private Map<String, Object> params;
private String type;

private StartProcessWithTypeCommand(String processId, Map<String, Object> params, String type) {
this.processId = processId;
this.params = params;
this.type = type;
}

@Override
public Void execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
((ProcessRuntimeImpl) ((InternalKnowledgeRuntime) ksession).getProcessRuntime()).startProcess(processId,
params, type);
String processInstanceId = kogitoProcessRuntime.createProcessInstance(processId, null).getId();
kogitoProcessRuntime.triggerProcessInstance(processInstanceId, type, event, null);

return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,18 @@ public void start(String trigger) {
this.start(trigger, null);
}

public void start(String trigger, Object payload) {
@Override
public void start(String trigger, Object data) {
synchronized (this) {
if (getState() != KogitoProcessInstance.STATE_PENDING) {
throw new IllegalArgumentException("A process instance can only be started once");
}
setState(KogitoProcessInstance.STATE_ACTIVE);
internalStart(trigger, payload);
internalStart(trigger, data);
}
}

protected abstract void internalStart(String trigger, Object payload);
protected abstract void internalStart(String trigger, Object data);

@Override
public void disconnect() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.jbpm.process.core.context.exception.CompensationScope;
Expand Down Expand Up @@ -113,55 +111,35 @@ public static List<Node> getEndNodes(Node[] nodes) {
return endNodes;
}

public StartNode getStart(String trigger, Function<String, Object> varResolver) {
public StartNode getStart(String trigger, Object payload) {
Node[] nodes = getNodes();

for (int i = 0; i < nodes.length; i++) {
if (nodes[i] instanceof StartNode) {

StartNode start = ((StartNode) nodes[i]);
if (nodes[i] instanceof StartNode start) {
// no events
if (trigger == null && ((start.getTriggers() == null || start.getTriggers().isEmpty()) && start.getTimer() == null)) {
return start;
}

// return start node that is not event based node
if (trigger == null && ((start.getTriggers() == null
|| start.getTriggers().isEmpty())
&& start.getTimer() == null)) {
// there is a timer
if (start.getTimer() != null && "timer".equals(trigger)) {
return start;
} else {
if (start.getTriggers() != null) {
for (Trigger t : start.getTriggers()) {
if (t instanceof EventTrigger) {
EventTrigger eventTrigger = (EventTrigger) t;
Map<String, String> mappings = eventTrigger.getInMappings();
Object event = null;
if (varResolver != null) {
switch (mappings.size()) {
case 0:
event = null;
break;
case 1:
event = varResolver.apply(mappings.values().iterator().next());
break;
default:
event = varResolver.apply("event");
break;
}
}
}

for (EventFilter filter : eventTrigger.getEventFilters()) {
if (filter.acceptsEvent(trigger, event, varResolver)) {
return start;
}
// there is an event trigger
if (start.getTriggers() != null) {
for (Trigger t : start.getTriggers()) {
if (t instanceof EventTrigger eventTrigger) {
for (EventFilter filter : eventTrigger.getEventFilters()) {
if (filter.acceptsEvent(trigger, payload, key -> null)) {
return start;
}
} else if (t instanceof ConstraintTrigger && "conditional".equals(trigger)) {
return start;
}
}
} else if (start.getTimer() != null) {

if ("timer".equals(trigger)) {
} else if (t instanceof ConstraintTrigger && "conditional".equals(trigger)) {
return start;
}
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public RuleFlowProcess getRuleFlowProcess() {

@Override
public void internalStart(String trigger, Object payload) {
StartNode startNode = getRuleFlowProcess().getStart(trigger, varName -> getVariable(varName));
StartNode startNode = getRuleFlowProcess().getStart(trigger, payload);
if (startNode != null) {
if (Metadata.EVENT_TYPE_NONE.equals(startNode.getMetaData(Metadata.EVENT_TYPE))) {
getNodeInstance(startNode).trigger(null, null);
Expand All @@ -52,4 +52,5 @@ public void internalStart(String trigger, Object payload) {
List<Node> autoStartNodes = getRuleFlowProcess().getAutoStartNodes();
autoStartNodes.forEach(autoStartNode -> signalEvent(autoStartNode.getName(), null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ public void signalEvent(String type, Object event) {
if (triggerTime == null) {
triggerTime = new Date();
}

String variableName = (String) getStartNode().getMetaData(TRIGGER_MAPPING_INPUT);
if (variableName != null) {
Map<String, Object> outputSet = Collections.singletonMap(variableName, event);
NodeIoHelper.processOutputs(this, key -> outputSet.get(key), varName -> this.getVariable(varName));
if (variableName == null) {
variableName = "event";
}
Map<String, Object> outputSet = Collections.singletonMap(variableName, event);
NodeIoHelper.processOutputs(this, key -> outputSet.get(key), varName -> this.getVariable(varName));
triggerCompleted();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public void start(String trigger, String referenceId) {
}

@Override
public void trigger(String trigger, String referenceId, Map<String, Object> payload) {
public void trigger(String trigger, String referenceId, Object payload) {
if (this.status != KogitoProcessInstance.STATE_PENDING) {
throw new IllegalStateException("Impossible to start process instance that already has started");
}
Expand Down
Loading

0 comments on commit e135d06

Please sign in to comment.