Skip to content

Commit

Permalink
Adding new dynamic addon (#3611)
Browse files Browse the repository at this point in the history
* Adding new dynamic addon

* [Fix #3615] Moving common stuff to common addons
  • Loading branch information
fjtirado authored Aug 14, 2024
1 parent 7041f76 commit 86b0d62
Show file tree
Hide file tree
Showing 26 changed files with 983 additions and 27 deletions.
1 change: 1 addition & 0 deletions addons/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<module>rest-exception-handler</module>
<module>process-svg</module>
<module>process-management</module>
<module>process-dynamic</module>
<module>knative</module>
<module>kubernetes</module>
<module>kubernetes-service-catalog</module>
Expand Down
43 changes: 43 additions & 0 deletions addons/common/process-dynamic/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.kie</groupId>
<artifactId>kogito-addons-common-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>kie-addons-process-dynamic</artifactId>
<name>KIE Add-On Process Instance Dynamic Calls</name>
<description>Allow performing dynamic calls for existing process instances</description>
<properties>
<java.module.name>org.kie.process.dynamic</java.module.name>
</properties>
<dependencies>

<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-rest-workitem</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.process.dynamic;

import java.util.Map;

import org.kogito.workitem.rest.pathresolvers.DefaultPathParamResolver;

public class DynamicPathParamResolver extends DefaultPathParamResolver {

private String processInstanceId;

public DynamicPathParamResolver(String processInstanceId) {
this.processInstanceId = processInstanceId;
}

@Override
protected Object getParam(Map<String, Object> parameters, String key) {
return key.equals("processInstanceId") ? this.processInstanceId : super.getParam(parameters, key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.process.dynamic;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import org.drools.core.common.InternalKnowledgeRuntime;
import org.jbpm.process.instance.InternalProcessRuntime;
import org.jbpm.process.instance.KogitoProcessContextImpl;
import org.jbpm.process.instance.ProcessInstance;
import org.jbpm.util.ContextFactory;
import org.jbpm.workflow.core.impl.WorkflowProcessImpl;
import org.jbpm.workflow.instance.WorkflowProcessInstance;
import org.jbpm.workflow.instance.node.DynamicUtils;
import org.kie.kogito.Model;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.expr.ExpressionHandlerFactory;
import org.kie.kogito.process.impl.AbstractProcessInstance;
import org.kogito.workitem.rest.RestWorkItemHandler;

public class ProcessInstanceDynamicCallHelper {

private ProcessInstanceDynamicCallHelper() {
}

public static void executeRestCall(RestWorkItemHandler handler, Stream<Process<?>> processes, String processId, String processInstanceId, RestCallInfo input) {
Process<?> processDef = processes.filter(p -> p.id().equals(processId)).findAny().orElseThrow(() -> new IllegalArgumentException("Cannot find process " + processId));
WorkflowProcessInstance pi = (WorkflowProcessInstance) findProcessInstance(processDef, processInstanceId);
WorkflowProcessImpl process = (WorkflowProcessImpl) pi.getProcess();
if (!process.isDynamic()) {
process.setDynamic(true);
}
InternalKnowledgeRuntime runtime = pi.getKnowledgeRuntime();
InternalProcessRuntime.asKogitoProcessRuntime(runtime).getKogitoWorkItemManager().registerWorkItemHandler(RestWorkItemHandler.REST_TASK_TYPE, handler);
Map<String, Object> parameters = input.getArguments() == null ? new HashMap<>() : new HashMap<>(input.getArguments());
if (input.getHost() != null) {
parameters.put(RestWorkItemHandler.HOST, input.getHost());
}
if (input.getPort() != null) {
parameters.put(RestWorkItemHandler.PORT, input.getPort());
}
if (input.getMethod() != null) {
parameters.put(RestWorkItemHandler.METHOD, input.getMethod());
}
if (input.getEndpoint() != null) {
parameters.put(RestWorkItemHandler.URL, input.getEndpoint());
}
parameters.put(RestWorkItemHandler.PATH_PARAM_RESOLVER, new DynamicPathParamResolver(processInstanceId));
WorkItemHandlerResultHolder holder = new WorkItemHandlerResultHolder();
parameters.put(RestWorkItemHandler.RESULT_HANDLER, holder);
KogitoProcessContextImpl context = ContextFactory.fromItem(DynamicUtils.addDynamicWorkItem(pi, runtime, RestWorkItemHandler.REST_TASK_TYPE, parameters));
Model model = ((Model) processDef.createModel());
model.update(input.getOutputExpression() != null
? ExpressionHandlerFactory.get(input.getOutputExpressionLang(), input.getOutputExpression()).eval(holder.getResult(), Map.class, context)
: holder.getResult());
((AbstractProcessInstance) pi.unwrap()).updateVariablesPartially(model);
}

private static ProcessInstance findProcessInstance(Process<?> process, String processInstanceId) {
return process.instances()
.findById(processInstanceId).map(AbstractProcessInstance.class::cast)
.map(AbstractProcessInstance::internalGetProcessInstance)
.orElseThrow(() -> new IllegalArgumentException("Cannot find process instance " + processInstanceId + " for process " + process.id()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.process.dynamic;

import java.util.Map;

public class RestCallInfo {

private String endpoint;
private String host;
private Integer port;
private String method;
private Map<String, Object> arguments;
private String outputExpression;
private String outputExpressionLang = "jq";

public String getEndpoint() {
return endpoint;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public Integer getPort() {
return port;
}

public void setPort(Integer port) {
this.port = port;
}

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

public Map<String, Object> getArguments() {
return arguments;
}

public void setArguments(Map<String, Object> arguments) {
this.arguments = arguments;
}

public String getOutputExpression() {
return outputExpression;
}

public void setOutputExpression(String outputExpression) {
this.outputExpression = outputExpression;
}

public String getOutputExpressionLang() {
return outputExpressionLang;
}

public void setOutputExpressionLang(String outputExpressionLang) {
this.outputExpressionLang = outputExpressionLang;
}

@Override
public String toString() {
return "RestCallInfo [endpoint=" + endpoint + ", host=" + host + ", port=" + port + ", method=" + method
+ ", arguments=" + arguments + ", outputExpression=" + outputExpression + ", outputExpressionLang="
+ outputExpressionLang + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.process.dynamic;

import java.util.Map;

import org.kogito.workitem.rest.resulthandlers.RestWorkItemHandlerResult;

import io.vertx.mutiny.core.buffer.Buffer;
import io.vertx.mutiny.ext.web.client.HttpResponse;

public class WorkItemHandlerResultHolder implements RestWorkItemHandlerResult {

private Map<String, Object> result;

@Override
public Object apply(HttpResponse<Buffer> buffer, Class<?> clazz) {
result = buffer.bodyAsJson(Map.class);
return result;
}

public Map<String, Object> getResult() {
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public void setNodeInstanceContainer(KogitoNodeInstanceContainer nodeInstanceCon

@Override
public org.kie.api.definition.process.Node getNode() {
if (nodeId == null) {
return null;
}
try {
return ((org.jbpm.workflow.core.NodeContainer) this.nodeInstanceContainer.getNodeContainer()).internalGetNode(this.nodeId);
} catch (IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,12 @@ public List<NodeInstance> getNodeInstances(WorkflowElementIdentifier nodeId) {
}

public List<NodeInstance> getNodeInstances(WorkflowElementIdentifier nodeId, final List<NodeInstance> currentView) {
if (nodeId == null) {
return Collections.emptyList();
}
List<NodeInstance> result = new ArrayList<>();
for (final NodeInstance nodeInstance : currentView) {
if (nodeInstance.getNodeId().equals(nodeId)) {
if (nodeId.equals(nodeInstance.getNodeId())) {
result.add(nodeInstance);
}
}
Expand Down Expand Up @@ -712,7 +715,7 @@ public void signalEvent(String type, Object event) {
}
nodeInstance.trigger(null, Node.CONNECTION_DEFAULT_TYPE);
} else if (node instanceof CompositeNode) {
Optional<NodeInstance> instance = this.nodeInstances.stream().filter(ni -> ni.getNodeId().equals(node.getId())).findFirst();
Optional<NodeInstance> instance = this.nodeInstances.stream().filter(ni -> Objects.equals(ni.getNodeId(), node.getId())).findFirst();
instance.ifPresent(n -> ((CompositeNodeInstance) n).signalEvent(type, event));
}
}
Expand Down
Loading

0 comments on commit 86b0d62

Please sign in to comment.