Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Aug 1, 2024
1 parent 0b40e99 commit 2ad6b53
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ interface Provider {
*/
KogitoProcessInstance startProcess(String processId);

/**
* Start a new process instance. The process (definition) that should
* be used is referenced by the given process id. Parameters can be passed
* to the process instance (as name-value pairs), and these will be set
* as variables of the process instance.
*
* @param processId the id of the process that should be started
* @param trigger
* @param parameters the process variables that should be set when starting the process instance
* @return the <code>ProcessInstance</code> that represents the instance of the process that was started
*/
KogitoProcessInstance triggerProcessInstance(String processInstanceId, String trigger, Object payload, AgendaFilter agendaFilter);

/**
* Start a new process instance. The process (definition) that should
* be used is referenced by the given process id. Parameters can be passed
* to the process instance (as name-value pairs), and these will be set
* as variables of the process instance.
*
* @param processId the id of the process that should be started
* @param parameters the process variables that should be set when starting the process instance
* @return the <code>ProcessInstance</code> that represents the instance of the process that was started
*/
KogitoProcessInstance startProcess(String processId, String trigger, Map<String, Object> parameters);

/**
* Start a new process instance. The process (definition) that should
* be used is referenced by the given process id. Parameters can be passed
Expand Down Expand Up @@ -218,4 +243,5 @@ interface Provider {

@Deprecated
KieSession getKieSession();

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public interface ProcessInstance<T> {
*/
void start();

/**
* Starts process instance with trigger
*
* @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);

/**
* Starts process instance with trigger
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,14 @@ public TimerService getTimerService() {
public Application getApplication() {
return processRuntime.getApplication();
}

@Override
public KogitoProcessInstance triggerProcessInstance(String processInstanceId, String trigger, Object payload, AgendaFilter agendaFilter) {
return processRuntime.getKogitoProcessRuntime().triggerProcessInstance(processInstanceId, trigger, payload, agendaFilter);
}

@Override
public KogitoProcessInstance startProcess(String processId, String trigger, Map<String, Object> parameters) {
return processRuntime.getKogitoProcessRuntime().startProcess(processId, parameters, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
*/
package org.jbpm.process.instance;

import java.util.Map;

import org.drools.core.common.InternalKnowledgeRuntime;
import org.drools.core.common.InternalWorkingMemory;
import org.kie.api.runtime.process.ProcessRuntime;
import org.kie.kogito.Application;
import org.kie.kogito.internal.process.event.KogitoProcessEventSupport;
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;
import org.kie.kogito.internal.process.runtime.KogitoProcessRuntime;
import org.kie.kogito.jobs.JobsService;
import org.kie.kogito.signal.SignalManager;
Expand Down Expand Up @@ -54,4 +57,6 @@ static KogitoProcessRuntime asKogitoProcessRuntime(ProcessRuntime kogitoProcessR
// this line is used only for legacy tests
return ((KogitoProcessRuntime.Provider) ((InternalWorkingMemory) kogitoProcessRuntimeProvider).getProcessRuntime()).getKogitoProcessRuntime();
}

KogitoProcessInstance startProcess(String processId, String trigger, Map<String, Object> parameters);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public KogitoProcessInstance startProcess(String processId, Map<String, Object>
return (KogitoProcessInstance) delegate.startProcess(processId, parameters);
}

@Override
public KogitoProcessInstance startProcess(String processId, String trigger, Map<String, Object> parameters) {
return (KogitoProcessInstance) delegate.startProcess(processId, trigger, parameters);
}

@Override
public KogitoProcessInstance createProcessInstance(String processId, Map<String, Object> parameters) {
return (KogitoProcessInstance) delegate.createProcessInstance(processId, parameters);
Expand Down Expand Up @@ -157,10 +162,22 @@ public KogitoProcessInstance startProcess(String processId, Map<String, Object>
return null;
}

public KogitoProcessInstance startProcessInstance(String processInstanceId, String trigger, AgendaFilter agendaFilter) {
@Override
public KogitoProcessInstance triggerProcessInstance(String processInstanceId, String trigger, Object payload, AgendaFilter agendaFilter) {
KogitoProcessInstance processInstance = getProcessInstance(processInstanceId);
org.jbpm.process.instance.ProcessInstance jbpmProcessInstance = (org.jbpm.process.instance.ProcessInstance) processInstance;

jbpmProcessInstance.configureTimers();
delegate.getProcessEventSupport().fireBeforeProcessStarted(processInstance, delegate.getInternalKieRuntime());
jbpmProcessInstance.setAgendaFilter(agendaFilter);
jbpmProcessInstance.start(trigger, payload);
delegate.getProcessEventSupport().fireAfterProcessStarted(processInstance, delegate.getInternalKieRuntime());
return jbpmProcessInstance;
}

public KogitoProcessInstance startProcessInstance(String processInstanceId, String trigger, AgendaFilter agendaFilter) {
KogitoProcessInstance processInstance = getProcessInstance(processInstanceId);
org.jbpm.process.instance.ProcessInstance jbpmProcessInstance = (org.jbpm.process.instance.ProcessInstance) processInstance;
jbpmProcessInstance.configureTimers();
delegate.getProcessEventSupport().fireBeforeProcessStarted(processInstance, delegate.getInternalKieRuntime());
jbpmProcessInstance.setAgendaFilter(agendaFilter);
Expand All @@ -173,4 +190,5 @@ public KogitoProcessInstance startProcessInstance(String processInstanceId, Stri
public Application getApplication() {
return delegate.getApplication();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.kie.internal.process.CorrelationKey;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import org.kie.kogito.Application;
import org.kie.kogito.Model;
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;
import org.kie.kogito.internal.process.runtime.KogitoWorkItemManager;
import org.kie.kogito.jobs.DurationExpirationTime;
Expand All @@ -61,6 +62,7 @@
import org.kie.kogito.jobs.ProcessJobDescription;
import org.kie.kogito.process.Processes;
import org.kie.kogito.services.jobs.impl.InMemoryJobService;
import org.kie.kogito.services.uow.UnitOfWorkExecutor;
import org.kie.kogito.signal.SignalManager;
import org.kie.kogito.uow.UnitOfWorkManager;

Expand Down Expand Up @@ -132,6 +134,11 @@ public ProcessInstance startProcess(String processId, Map<String, Object> parame
return startProcess(processId, parameters, trigger, null);
}

@Override
public KogitoProcessInstance startProcess(String processId, String trigger, Map<String, Object> parameters) {
return (KogitoProcessInstance) startProcess(processId, parameters, trigger, null);
}

@Override
public ProcessInstance startProcess(String processId, AgendaFilter agendaFilter) {
return startProcess(processId, null, null, agendaFilter);
Expand Down Expand Up @@ -287,7 +294,7 @@ public String[] getEventTypes() {
}

@Override
public void signalEvent(final String type, Object event) {
public void signalEvent(String type, Object event) {
for (EventFilter filter : eventFilters) {
if (!filter.acceptsEvent(type, event, varName -> null)) {
return;
Expand All @@ -313,7 +320,14 @@ public void signalEvent(final String type, Object event) {
}

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

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);
return null;
});

}
}

Expand Down Expand Up @@ -519,4 +533,5 @@ public ProcessInstance startProcessFromNodeIds(String s, CorrelationKey correlat
public ProcessInstance getProcessInstance(CorrelationKey correlationKey) {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public interface ProcessInstance extends KogitoProcessInstance,

void start();

void start(String trigger);
default void start(String trigger) {
this.start(trigger, null);
}

void start(String trigger, Object payload);

String getOutcome();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public ProcessInstance startProcess(String processId, Map<String, Object> parame
return kogitoProcessRuntime.startProcess(processId, parameters, trigger, null);
}

@Override
public KogitoProcessInstance startProcess(String processId, String trigger, Map<String, Object> parameters) {
return kogitoProcessRuntime.startProcess(processId, parameters, trigger, null);
}

@Override
public ProcessInstance startProcess(String processId, AgendaFilter agendaFilter) {
return kogitoProcessRuntime.startProcess(processId, null, null, agendaFilter);
Expand Down Expand Up @@ -558,4 +563,5 @@ public void internalExecute(ReteEvaluator reteEvaluator) {
signalEvent(type, event);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,20 @@ public void start() {

@Override
public void start(String trigger) {
this.start(trigger, null);
}

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

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

@Override
public void disconnect() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.jbpm.process.core.context.swimlane.SwimlaneContext;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.process.core.event.EventFilter;
import org.jbpm.process.core.validation.impl.ProcessValidationErrorImpl;
import org.jbpm.workflow.core.impl.NodeContainerImpl;
import org.jbpm.workflow.core.impl.WorkflowProcessImpl;
import org.jbpm.workflow.core.node.ConstraintTrigger;
Expand All @@ -42,7 +41,6 @@
import org.jbpm.workflow.core.node.Trigger;
import org.kie.api.definition.process.Node;
import org.kie.api.definition.process.NodeContainer;
import org.kie.kogito.process.validation.ValidationException;

public class RuleFlowProcess extends WorkflowProcessImpl {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import java.util.List;

import org.jbpm.ruleflow.core.Metadata;
import org.jbpm.ruleflow.core.RuleFlowProcess;
import org.jbpm.workflow.core.node.StartNode;
import org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl;
import org.jbpm.workflow.instance.node.StartNodeInstance;
import org.kie.api.definition.process.Node;

public class RuleFlowProcessInstance extends WorkflowProcessInstanceImpl {
Expand All @@ -33,10 +35,15 @@ public RuleFlowProcess getRuleFlowProcess() {
return (RuleFlowProcess) getProcess();
}

public void internalStart(String trigger) {
@Override
public void internalStart(String trigger, Object payload) {
StartNode startNode = getRuleFlowProcess().getStart(trigger, varName -> getVariable(varName));
if (startNode != null) {
getNodeInstance(startNode).trigger(null, null);
if (Metadata.EVENT_TYPE_NONE.equals(startNode.getMetaData(Metadata.EVENT_TYPE))) {
getNodeInstance(startNode).trigger(null, null);
} else {
((StartNodeInstance) getNodeInstance(startNode)).signalEvent(trigger, payload);
}
} else if (!getRuleFlowProcess().isDynamic()) {
throw new IllegalArgumentException("There is no start node that matches the trigger " + (trigger == null ? "none" : trigger));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public void start() {
}

@Override
public void start(String trigger) {
public void start(String trigger, Object payload) {
synchronized (this) {
setStartDate(new Date());
registerExternalEventNodeListeners();
Expand All @@ -515,7 +515,7 @@ public void start(String trigger) {
}
}
}
super.start(trigger);
super.start(trigger, payload);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,28 @@ public void start(String trigger, String referenceId) {
start(trigger, referenceId, Collections.emptyMap());
}

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

getProcessRuntime().getProcessInstanceManager().setLock(((MutableProcessInstances<T>) process.instances()).lock());
getProcessRuntime().getProcessInstanceManager().addProcessInstance(this.processInstance);
this.id = processInstance.getStringId();
addCompletionEventListener();
addToUnitOfWork(pi -> ((MutableProcessInstances<T>) process.instances()).create(id, this));
KogitoProcessInstance kogitoProcessInstance = getProcessRuntime().getKogitoProcessRuntime().triggerProcessInstance(this.id, trigger, payload, null);
if (kogitoProcessInstance.getState() != STATE_ABORTED && kogitoProcessInstance.getState() != STATE_COMPLETED) {
addToUnitOfWork(pi -> ((MutableProcessInstances<T>) process.instances()).update(pi.id(), pi));
}
unbind(variables, kogitoProcessInstance.getVariables());
if (this.processInstance != null) {
this.status = this.processInstance.getState();
}
}

@Override
public void start(String trigger, String referenceId, Map<String, List<String>> headers) {
if (this.status != KogitoProcessInstance.STATE_PENDING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,6 @@ public void beforeProcessStarted(ProcessStartedEvent event) {
definition.send(Sig.of("startSignal"));

assertThat(list).hasSize(1);
System.out.println("Process has started " + list.get(list.size() - 1));
definition.instances().stream().forEach(System.out::println);
for (String processInstanceId : list) {
MultipleStartEventProcessDifferentPathsProcess p =
(MultipleStartEventProcessDifferentPathsProcess) app.get(Processes.class).processByProcessInstanceId(processInstanceId).get();
Expand All @@ -460,7 +458,7 @@ public void beforeProcessStarted(ProcessStartedEvent event) {
assertThat(pi.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED);

assertThat(tracked.tracked())
.anyMatch(ProcessTestHelper.triggered("StartSignal"))
.anyMatch(ProcessTestHelper.left("StartSignal"))
.anyMatch(ProcessTestHelper.triggered("Script 3"))
.anyMatch(ProcessTestHelper.triggered("User task"))
.anyMatch(ProcessTestHelper.triggered("End"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Application newApplication() {
return new StaticApplication(new StaticConfig(null, staticConfig), bpmnProcesses);
}

public static void registerProcessEventListener(Application app, KogitoProcessEventListener ...kogitoProcessEventListeners) {
public static void registerProcessEventListener(Application app, KogitoProcessEventListener... kogitoProcessEventListeners) {
for (KogitoProcessEventListener kogitoProcessEventListener : kogitoProcessEventListeners) {
((DefaultProcessEventListenerConfig) app.config().get(ProcessConfig.class).processEventListeners()).register(kogitoProcessEventListener);
}
Expand Down

0 comments on commit 2ad6b53

Please sign in to comment.