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-1439] UserTask Decouple codegen and interface from engine #3655

Merged
merged 18 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions addons/common/human-task-prediction/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
<artifactId>jbpm-deps-group-engine</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>jbpm-usertask</artifactId>
</dependency>


<!-- test dependencies -->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.kie.kogito.prediction.api;

import java.util.Optional;

import org.kie.kogito.internal.process.workitem.KogitoWorkItem;
import org.kie.kogito.internal.process.workitem.KogitoWorkItemHandler;
import org.kie.kogito.internal.process.workitem.KogitoWorkItemManager;
import org.kie.kogito.internal.process.workitem.WorkItemTransition;
import org.kie.kogito.process.workitems.impl.DefaultKogitoWorkItemHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PredictionAwareHumanTaskWorkItemHandler extends DefaultKogitoWorkItemHandler {

private static final Logger logger = LoggerFactory.getLogger(PredictionAwareHumanTaskWorkItemHandler.class);

private PredictionService predictionService;

public PredictionAwareHumanTaskWorkItemHandler(PredictionService predictionService) {
this.predictionService = predictionService;
}

public Optional<WorkItemTransition> activateWorkItemHandler(KogitoWorkItemManager manager, KogitoWorkItemHandler handler, KogitoWorkItem workItem, WorkItemTransition transition) {
PredictionOutcome outcome = predictionService.predict(workItem, workItem.getParameters());
logger.debug("Prediction service returned confidence level {} for work item {}", outcome.getConfidenceLevel(), workItem.getStringId());

if (outcome.isCertain()) {
workItem.setOutputs(outcome.getData());
logger.debug("Prediction service is certain (confidence level {}) on the outputs, completing work item {}", outcome.getConfidenceLevel(), workItem.getStringId());

return Optional.of(this.newTransition("skip", workItem.getPhaseStatus(), outcome.getData()));
} else if (outcome.isPresent()) {
logger.debug("Prediction service is NOT certain (confidence level {}) on the outputs, setting recommended outputs on work item {}",
outcome.getConfidenceLevel(),
workItem.getStringId());
workItem.setOutputs(outcome.getData());
}
return Optional.empty();
}

public Optional<WorkItemTransition> completeWorkItemHandler(KogitoWorkItemManager manager, KogitoWorkItemHandler handler, KogitoWorkItem workItem, WorkItemTransition transition) {
// upon actual transition train the data if it's completion phase
predictionService.train(workItem, workItem.getParameters(), transition.data());
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import java.util.concurrent.atomic.AtomicBoolean;

import org.drools.io.ClassPathResource;
import org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemHandler;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kie.kogito.Model;
import org.kie.kogito.auth.SecurityPolicy;
import org.kie.kogito.internal.process.workitem.Policy;
import org.kie.kogito.process.ProcessConfig;
import org.kie.kogito.process.ProcessInstance;
import org.kie.kogito.process.WorkItem;
Expand All @@ -38,7 +38,6 @@
import org.kie.kogito.process.impl.CachedWorkItemHandlerConfig;
import org.kie.kogito.process.impl.DefaultProcessEventListenerConfig;
import org.kie.kogito.process.impl.StaticProcessConfig;
import org.kie.kogito.process.workitem.Policy;
import org.kie.kogito.process.workitems.InternalKogitoWorkItem;
import org.kie.kogito.services.identity.StaticIdentityProvider;
import org.kie.kogito.services.uow.CollectingUnitOfWorkFactory;
Expand All @@ -50,7 +49,7 @@

public class PredictionAwareHumanTaskLifeCycleTest {

private Policy<?> securityPolicy = SecurityPolicy.of(new StaticIdentityProvider("john"));
private Policy securityPolicy = SecurityPolicy.of(new StaticIdentityProvider("john"));

private AtomicBoolean predictNow;
private List<String> trainedTasks;
Expand Down Expand Up @@ -88,7 +87,7 @@ public String getIdentifier() {
};

CachedWorkItemHandlerConfig wiConfig = new CachedWorkItemHandlerConfig();
wiConfig.register("Human Task", new HumanTaskWorkItemHandler(new PredictionAwareHumanTaskLifeCycle(predictionService)));
wiConfig.register("Human Task", new PredictionAwareHumanTaskWorkItemHandler(predictionService));
config = new StaticProcessConfig(wiConfig, new DefaultProcessEventListenerConfig(), new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.Set;

import org.kie.api.runtime.process.WorkItem;
import org.kie.kogito.internal.process.runtime.KogitoWorkItem;
import org.kie.kogito.internal.process.workitem.KogitoWorkItem;
import org.kie.kogito.prediction.api.PredictionOutcome;
import org.kie.kogito.prediction.api.PredictionService;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import java.util.Map;

import org.drools.io.ClassPathResource;
import org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemHandler;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kie.kogito.Model;
import org.kie.kogito.prediction.api.PredictionAwareHumanTaskLifeCycle;
import org.kie.kogito.prediction.api.PredictionAwareHumanTaskWorkItemHandler;
import org.kie.kogito.prediction.api.PredictionService;
import org.kie.kogito.process.ProcessConfig;
import org.kie.kogito.process.ProcessInstance;
Expand Down Expand Up @@ -64,7 +63,7 @@ public void configure() {

predictionService = new SmileRandomForest(configuration);
CachedWorkItemHandlerConfig wiConfig = new CachedWorkItemHandlerConfig();
wiConfig.register("Human Task", new HumanTaskWorkItemHandler(new PredictionAwareHumanTaskLifeCycle(predictionService)));
wiConfig.register("Human Task", new PredictionAwareHumanTaskWorkItemHandler(predictionService));
config = new StaticProcessConfig(wiConfig, new DefaultProcessEventListenerConfig(), new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));

for (int i = 0; i < 10; i++) {
Expand All @@ -77,7 +76,6 @@ public void configure() {

@Test
public void testUserTaskWithPredictionService() {

BpmnProcess process = (BpmnProcess) BpmnProcess.from(config, new ClassPathResource("BPMN2-UserTask.bpmn2")).get(0);
process.configure();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
import org.kie.kogito.process.WorkItem;
import org.kie.kogito.process.bpmn2.BpmnProcess;
import org.kie.kogito.process.bpmn2.BpmnVariables;
import org.kie.kogito.services.identity.StaticIdentityProvider;
import org.kie.kogito.process.impl.DefaultWorkItemHandlerConfig;
import org.kie.kogito.process.impl.StaticProcessConfig;
import org.kie.kogito.process.workitems.impl.DefaultKogitoWorkItemHandler;
import org.kie.kogito.uow.UnitOfWork;
import org.kie.kogito.uow.UnitOfWorkManager;

import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.entry;
Expand All @@ -59,10 +62,12 @@

class FileSystemProcessInstancesTest {

private SecurityPolicy securityPolicy = SecurityPolicy.of(new StaticIdentityProvider("john"));
private SecurityPolicy securityPolicy = SecurityPolicy.of("john", emptyList());

private BpmnProcess createProcess(String fileName) {
BpmnProcess process = BpmnProcess.from(new ClassPathResource(fileName)).get(0);
StaticProcessConfig config = new StaticProcessConfig();
((DefaultWorkItemHandlerConfig) config.workItemHandlers()).register("Human Task", new DefaultKogitoWorkItemHandler());
BpmnProcess process = BpmnProcess.from(config, new ClassPathResource(fileName)).get(0);
process.setProcessInstancesFactory(new FileSystemProcessInstancesFactory());
process.configure();
abort(process.instances());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@
import org.kie.kogito.process.WorkItem;
import org.kie.kogito.process.bpmn2.BpmnProcess;
import org.kie.kogito.process.bpmn2.BpmnVariables;
import org.kie.kogito.services.identity.StaticIdentityProvider;
import org.kie.kogito.process.impl.DefaultWorkItemHandlerConfig;
import org.kie.kogito.process.impl.StaticProcessConfig;
import org.kie.kogito.process.workitems.impl.DefaultKogitoWorkItemHandler;
import org.kie.kogito.testcontainers.KogitoInfinispanContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.entry;
Expand Down Expand Up @@ -82,7 +85,9 @@ void close() {

@Test
void testFindByIdReadMode() {
BpmnProcess process = BpmnProcess.from(new ClassPathResource("BPMN2-UserTask-Script.bpmn2")).get(0);
StaticProcessConfig config = new StaticProcessConfig();
((DefaultWorkItemHandlerConfig) config.workItemHandlers()).register("Human Task", new DefaultKogitoWorkItemHandler());
BpmnProcess process = BpmnProcess.from(config, new ClassPathResource("BPMN2-UserTask-Script.bpmn2")).get(0);
// workaround as BpmnProcess does not compile the scripts but just reads the xml
for (Node node : ((WorkflowProcess) process.get()).getNodes()) {
if (node instanceof ActionNode) {
Expand Down Expand Up @@ -126,7 +131,9 @@ void testFindByIdReadMode() {

@Test
void testValuesReadMode() {
BpmnProcess process = BpmnProcess.from(new ClassPathResource("BPMN2-UserTask.bpmn2")).get(0);
StaticProcessConfig config = new StaticProcessConfig();
((DefaultWorkItemHandlerConfig) config.workItemHandlers()).register("Human Task", new DefaultKogitoWorkItemHandler());
BpmnProcess process = BpmnProcess.from(config, new ClassPathResource("BPMN2-UserTask.bpmn2")).get(0);
process.setProcessInstancesFactory(new CacheProcessInstancesFactory(cacheManager));
process.configure();

Expand All @@ -143,7 +150,9 @@ void testValuesReadMode() {

@Test
void testBasicFlow() {
BpmnProcess process = BpmnProcess.from(new ClassPathResource("BPMN2-UserTask.bpmn2")).get(0);
StaticProcessConfig config = new StaticProcessConfig();
((DefaultWorkItemHandlerConfig) config.workItemHandlers()).register("Human Task", new DefaultKogitoWorkItemHandler());
BpmnProcess process = BpmnProcess.from(config, new ClassPathResource("BPMN2-UserTask.bpmn2")).get(0);
process.setProcessInstancesFactory(new CacheProcessInstancesFactory(cacheManager));
process.configure();

Expand All @@ -154,7 +163,7 @@ void testBasicFlow() {

assertOne(process.instances());

SecurityPolicy asJohn = SecurityPolicy.of(new StaticIdentityProvider("john"));
SecurityPolicy asJohn = SecurityPolicy.of("john", emptyList());

assertThat(getFirst(process.instances()).workItems(asJohn)).hasSize(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import org.kie.kogito.process.bpmn2.BpmnProcess;
import org.kie.kogito.process.bpmn2.BpmnVariables;
import org.kie.kogito.process.impl.AbstractProcessInstance;
import org.kie.kogito.process.impl.DefaultWorkItemHandlerConfig;
import org.kie.kogito.process.impl.StaticProcessConfig;
import org.kie.kogito.process.workitems.impl.DefaultKogitoWorkItemHandler;
import org.kie.kogito.testcontainers.KogitoInfinispanContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
Expand Down Expand Up @@ -71,7 +74,9 @@ void close() {
}

private BpmnProcess createProcess(String fileName) {
BpmnProcess process = BpmnProcess.from(new ClassPathResource(fileName)).get(0);
StaticProcessConfig config = new StaticProcessConfig();
((DefaultWorkItemHandlerConfig) config.workItemHandlers()).register("Human Task", new DefaultKogitoWorkItemHandler());
BpmnProcess process = BpmnProcess.from(config, new ClassPathResource(fileName)).get(0);
AbstractProcessInstancesFactory factory = mock(AbstractProcessInstancesFactory.class);
process.setProcessInstancesFactory(factory);
process.configure();
Expand All @@ -80,6 +85,8 @@ private BpmnProcess createProcess(String fileName) {

@Test
public void testBasic() {
StaticProcessConfig config = new StaticProcessConfig();
((DefaultWorkItemHandlerConfig) config.workItemHandlers()).register("Human Task", new DefaultKogitoWorkItemHandler());
BpmnProcess process = createProcess("BPMN2-UserTask.bpmn2");

CacheProcessInstances pi = new CacheProcessInstances(process, cacheManager, null, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@
import org.junit.jupiter.api.Test;
import org.kie.kogito.auth.IdentityProviders;
import org.kie.kogito.auth.SecurityPolicy;
import org.kie.kogito.internal.process.workitem.Policy;
import org.kie.kogito.persistence.jdbc.JDBCProcessInstances;
import org.kie.kogito.process.ProcessInstance;
import org.kie.kogito.process.WorkItem;
import org.kie.kogito.process.bpmn2.BpmnProcess;
import org.kie.kogito.process.bpmn2.BpmnProcessInstance;
import org.kie.kogito.process.bpmn2.BpmnVariables;
import org.kie.kogito.process.impl.DefaultWorkItemHandlerConfig;
import org.kie.kogito.process.impl.StaticProcessConfig;
import org.kie.kogito.process.workitems.impl.DefaultKogitoWorkItemHandler;
import org.testcontainers.containers.JdbcDatabaseContainer;

import static java.util.Collections.singletonMap;
Expand All @@ -56,7 +60,7 @@
abstract class AbstractProcessInstancesIT {

public static final String TEST_ID = "02ac3854-46ee-42b7-8b63-5186c9889d96";
public static SecurityPolicy securityPolicy = SecurityPolicy.of(IdentityProviders.of("john"));
public static Policy securityPolicy = SecurityPolicy.of(IdentityProviders.of("john"));

DataSource dataSource;

Expand All @@ -69,7 +73,9 @@ public static void initMigration(JdbcDatabaseContainer container, String dbKind)
}

public static BpmnProcess createProcess(TestProcessInstancesFactory factory, String fileName) {
BpmnProcess process = BpmnProcess.from(new ClassPathResource(fileName)).get(0);
StaticProcessConfig config = new StaticProcessConfig();
((DefaultWorkItemHandlerConfig) config.workItemHandlers()).register("Human Task", new DefaultKogitoWorkItemHandler());
BpmnProcess process = BpmnProcess.from(config, new ClassPathResource(fileName)).get(0);
process.setProcessInstancesFactory(factory);
process.configure();
abort(process.instances());
Expand Down
Loading
Loading