Search in sources :

Example 6 with Process

use of org.kie.kogito.process.Process in project kogito-runtimes by kiegroup.

the class ProcessEventDispatcher method startNewInstance.

private ProcessInstance<M> startNewInstance(String trigger, Object event) {
    if (modelConverter == null) {
        return null;
    }
    final String businessKey = businessKeyResolver.resolve(event).asString();
    final String fromNode = nodeIdResolver.resolve(event).asString();
    // keep reference with the caller starting the instance (usually the caller process instance)
    final String referenceId = referenceIdResolver.resolve(event).asString();
    final Object data = dataResolver.apply(event);
    // event correlation, extract if any, the workflow instance correlation
    final CompositeCorrelation correlation = instanceCorrelationResolver.map(r -> r.resolve(event)).orElse(null);
    LOGGER.info("Starting new process instance with signal '{}'", trigger);
    return processService.createProcessInstance(process, businessKey, modelConverter.apply(data), fromNode, trigger, referenceId, correlation);
}
Also used : ProcessService(org.kie.kogito.process.ProcessService) BUSINESS_KEY(org.kie.kogito.event.cloudevents.CloudEventExtensionConstants.BUSINESS_KEY) CompositeAttributeCorrelationResolver(org.kie.kogito.services.event.correlation.CompositeAttributeCorrelationResolver) LoggerFactory(org.slf4j.LoggerFactory) PROCESS_REFERENCE_ID(org.kie.kogito.event.cloudevents.CloudEventExtensionConstants.PROCESS_REFERENCE_ID) CompletableFuture(java.util.concurrent.CompletableFuture) UnaryOperator(java.util.function.UnaryOperator) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) CorrelationInstance(org.kie.kogito.correlation.CorrelationInstance) Process(org.kie.kogito.process.Process) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.slf4j.Logger) PROCESS_START_FROM_NODE(org.kie.kogito.event.cloudevents.CloudEventExtensionConstants.PROCESS_START_FROM_NODE) EventDispatcher(org.kie.kogito.event.EventDispatcher) Set(java.util.Set) CorrelationResolver(org.kie.kogito.correlation.CorrelationResolver) SimpleAttributeCorrelationResolver(org.kie.kogito.services.event.correlation.SimpleAttributeCorrelationResolver) CompositeCorrelation(org.kie.kogito.correlation.CompositeCorrelation) Objects(java.util.Objects) ProcessInstance(org.kie.kogito.process.ProcessInstance) Model(org.kie.kogito.Model) Optional(java.util.Optional) PROCESS_INSTANCE_ID(org.kie.kogito.event.cloudevents.CloudEventExtensionConstants.PROCESS_INSTANCE_ID) Objects.nonNull(java.util.Objects.nonNull) CompositeCorrelation(org.kie.kogito.correlation.CompositeCorrelation)

Example 7 with Process

use of org.kie.kogito.process.Process in project kogito-runtimes by kiegroup.

the class ProcessEventDispatcherTest method setup.

@BeforeEach
void setup() {
    application = mock(Application.class);
    when(application.unitOfWorkManager()).thenReturn(new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));
    process = mock(Process.class);
    processInstances = mock(ProcessInstances.class);
    processInstance = mock(ProcessInstance.class);
    when(processInstance.id()).thenReturn("1");
    when(process.instances()).thenReturn(processInstances);
    correlationService = spy(new DefaultCorrelationService());
    when(process.correlations()).thenReturn(correlationService);
    when(processInstances.findById(Mockito.anyString())).thenReturn(Optional.empty());
    when(processInstances.findById("1")).thenReturn(Optional.of(processInstance));
    processService = mock(ProcessService.class);
    when(processService.createProcessInstance(eq(process), any(), any(), any(), any(), any(), any())).thenReturn(processInstance);
    when(processService.signalProcessInstance(eq(process), any(), any(), any())).thenReturn(Optional.of(mock(DummyModel.class)));
    executor = Executors.newSingleThreadExecutor();
}
Also used : ProcessInstances(org.kie.kogito.process.ProcessInstances) DefaultCorrelationService(org.kie.kogito.services.event.correlation.DefaultCorrelationService) CollectingUnitOfWorkFactory(org.kie.kogito.services.uow.CollectingUnitOfWorkFactory) Process(org.kie.kogito.process.Process) ProcessInstance(org.kie.kogito.process.ProcessInstance) ProcessService(org.kie.kogito.process.ProcessService) Application(org.kie.kogito.Application) DefaultUnitOfWorkManager(org.kie.kogito.services.uow.DefaultUnitOfWorkManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with Process

use of org.kie.kogito.process.Process in project kogito-runtimes by kiegroup.

the class ProcessEventDispatcherTest method testSigCloudEventWithCorrelation.

@Test
void testSigCloudEventWithCorrelation() throws Exception {
    String userId = "userId";
    String name = "name";
    String userValue = UUID.randomUUID().toString();
    String nameValue = UUID.randomUUID().toString();
    SimpleCorrelation<String> userCorrelation = new SimpleCorrelation<>(userId, userValue);
    SimpleCorrelation<String> nameCorrelation = new SimpleCorrelation<>(name, nameValue);
    Set<Correlation<?>> correlations = Set.of(userCorrelation, nameCorrelation);
    CompositeCorrelation compositeCorrelation = new CompositeCorrelation(correlations);
    correlationService.create(compositeCorrelation, "1");
    EventDispatcher<DummyModel> dispatcher = new ProcessEventDispatcher<>(process, modelConverter().get(), processService, executor, correlations.stream().map(Correlation::getKey).collect(Collectors.toSet()), AbstractMessageConsumer::cloudEventResolver);
    DummyCloudEvent event = new DummyCloudEvent(new DummyEvent("pepe"), DUMMY_TOPIC, "source");
    event.addExtensionAttribute(userId, userValue);
    event.addExtensionAttribute(name, nameValue);
    ProcessInstance<DummyModel> instance = dispatcher.dispatch(DUMMY_TOPIC, event).toCompletableFuture().get();
    ArgumentCaptor<String> signal = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> processInstanceId = ArgumentCaptor.forClass(String.class);
    verify(correlationService).find(compositeCorrelation);
    verify(processService).signalProcessInstance(Mockito.any(Process.class), processInstanceId.capture(), Mockito.any(Object.class), signal.capture());
    assertEquals("Message-" + DUMMY_TOPIC, signal.getValue());
    assertEquals("1", processInstanceId.getValue());
    assertEquals(instance, processInstance);
}
Also used : Correlation(org.kie.kogito.correlation.Correlation) CompositeCorrelation(org.kie.kogito.correlation.CompositeCorrelation) SimpleCorrelation(org.kie.kogito.correlation.SimpleCorrelation) SimpleCorrelation(org.kie.kogito.correlation.SimpleCorrelation) Process(org.kie.kogito.process.Process) DummyCloudEvent(org.kie.kogito.services.event.DummyCloudEvent) DummyModel(org.kie.kogito.services.event.DummyModel) DummyEvent(org.kie.kogito.services.event.DummyEvent) CompositeCorrelation(org.kie.kogito.correlation.CompositeCorrelation) Test(org.junit.jupiter.api.Test)

Example 9 with Process

use of org.kie.kogito.process.Process in project kogito-runtimes by kiegroup.

the class HumanTaskServiceImpl method update.

@Override
public ExtendedDataContext update(LocalId id, DataContext dataContext) {
    ExtendedDataContext edc = dataContext.as(ExtendedDataContext.class);
    TaskMetaDataContext mdc = edc.meta().as(TaskMetaDataContext.class);
    SecurityPolicy securityPolicy = convertPolicyObject(mdc.policy());
    TaskInstanceId taskInstanceId = ProcessIdParser.select(id, TaskInstanceId.class);
    ProcessInstanceId instanceId = taskInstanceId.taskId().processInstanceId();
    Process<MappableToModel<Model>> process = parseProcess(instanceId.processId());
    String taskInstanceIdString = taskInstanceId.taskInstanceId();
    String processInstanceIdString = instanceId.processInstanceId();
    Map<String, Object> map = dataContext.as(MapDataContext.class).toMap();
    Map<String, Object> result = UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> process.instances().findById(processInstanceIdString).map(pi -> {
        pi.updateWorkItem(taskInstanceIdString, wi -> HumanTaskHelper.updateContent(wi, map), securityPolicy);
        return pi.variables().toModel();
    })).orElseThrow().toMap();
    return ExtendedDataContext.ofData(MapDataContext.of(result));
}
Also used : org.kie.kogito.process(org.kie.kogito.process) HumanTaskNode(org.jbpm.workflow.core.node.HumanTaskNode) Collection(java.util.Collection) Policy(org.kie.kogito.incubation.processes.services.contexts.Policy) MappableToModel(org.kie.kogito.MappableToModel) HumanTaskHelper(org.jbpm.process.instance.impl.humantask.HumanTaskHelper) SecurityPolicy(org.kie.kogito.auth.SecurityPolicy) HumanTaskService(org.kie.kogito.incubation.processes.services.humantask.HumanTaskService) UnitOfWorkExecutor(org.kie.kogito.services.uow.UnitOfWorkExecutor) Application(org.kie.kogito.Application) KogitoNode(org.kie.kogito.internal.process.runtime.KogitoNode) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) org.kie.kogito.incubation.processes(org.kie.kogito.incubation.processes) List(java.util.List) Model(org.kie.kogito.Model) org.kie.kogito.incubation.common(org.kie.kogito.incubation.common) IdentityProviders(org.kie.kogito.auth.IdentityProviders) Map(java.util.Map) Process(org.kie.kogito.process.Process) TaskMetaDataContext(org.kie.kogito.incubation.processes.services.contexts.TaskMetaDataContext) TaskMetaDataContext(org.kie.kogito.incubation.processes.services.contexts.TaskMetaDataContext) MappableToModel(org.kie.kogito.MappableToModel) SecurityPolicy(org.kie.kogito.auth.SecurityPolicy)

Example 10 with Process

use of org.kie.kogito.process.Process in project kogito-runtimes by kiegroup.

the class ProcessEventDispatcherTest method testStringSigCloudEvent.

@Test
void testStringSigCloudEvent() throws Exception {
    EventDispatcher<DummyModel> dispatcher = new ProcessEventDispatcher<>(process, null, processService, executor, null, AbstractMessageConsumer::cloudEventResolver);
    ProcessInstance<DummyModel> instance = dispatcher.dispatch(DUMMY_TOPIC, new StringCloudEvent("pepe", DUMMY_TOPIC, "source", "1")).toCompletableFuture().get();
    ArgumentCaptor<String> signal = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> processInstanceId = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Object> signalObject = ArgumentCaptor.forClass(Object.class);
    verify(processService, times(1)).signalProcessInstance(Mockito.any(Process.class), processInstanceId.capture(), signalObject.capture(), signal.capture());
    assertEquals("Message-" + DUMMY_TOPIC, signal.getValue());
    assertEquals("pepe", signalObject.getValue());
    assertEquals("1", processInstanceId.getValue());
    assertEquals(instance, processInstance);
}
Also used : DummyModel(org.kie.kogito.services.event.DummyModel) StringCloudEvent(org.kie.kogito.services.event.StringCloudEvent) Process(org.kie.kogito.process.Process) Test(org.junit.jupiter.api.Test)

Aggregations

Process (org.kie.kogito.process.Process)13 Test (org.junit.jupiter.api.Test)7 Application (org.kie.kogito.Application)7 Model (org.kie.kogito.Model)7 ProcessInstance (org.kie.kogito.process.ProcessInstance)7 Collection (java.util.Collection)5 List (java.util.List)5 Map (java.util.Map)5 Objects (java.util.Objects)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 SecurityPolicy (org.kie.kogito.auth.SecurityPolicy)4 WorkItem (org.kie.kogito.process.WorkItem)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Function (java.util.function.Function)3 HumanTaskNode (org.jbpm.workflow.core.node.HumanTaskNode)3 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)3 MappableToModel (org.kie.kogito.MappableToModel)3