Skip to content

Commit

Permalink
[incubator-kie-issues-1108] Add capability to create node instances (…
Browse files Browse the repository at this point in the history
…node states) from the nodes (apache#3482)
  • Loading branch information
elguardian authored and rgdoliveira committed May 7, 2024
1 parent 111c75f commit 92c33c5
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
package org.jbpm.workflow.instance.impl;

import org.jbpm.workflow.core.node.SubProcessNode;
import org.jbpm.workflow.instance.impl.factory.DefaultNodeInstanceFactory;
import org.jbpm.workflow.instance.node.LambdaSubProcessNodeInstance;

public class CodegenNodeInstanceFactoryRegistry extends NodeInstanceFactoryRegistry {

@Override
protected NodeInstanceFactory get(Class<?> clazz) {
if (SubProcessNode.class == clazz) {
return factory(LambdaSubProcessNodeInstance::new);
return new DefaultNodeInstanceFactory(SubProcessNode.class, LambdaSubProcessNodeInstance::new);
}
return super.get(clazz);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

public interface NodeInstanceFactory {

Class<? extends Node> forClass();

NodeInstance getNodeInstance(Node node, WorkflowProcessInstance processInstance, NodeInstanceContainer nodeInstanceContainer);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.jbpm.workflow.instance.impl;

import java.util.List;

public interface NodeInstanceFactoryProvider {

List<NodeInstanceFactory> provide();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,17 @@
*/
package org.jbpm.workflow.instance.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.ServiceLoader;

import org.jbpm.workflow.core.node.ActionNode;
import org.jbpm.workflow.core.node.AsyncEventNode;
import org.jbpm.workflow.core.node.AsyncEventNodeInstance;
import org.jbpm.workflow.core.node.BoundaryEventNode;
import org.jbpm.workflow.core.node.CatchLinkNode;
import org.jbpm.workflow.core.node.CompositeContextNode;
import org.jbpm.workflow.core.node.CompositeNode;
import org.jbpm.workflow.core.node.DynamicNode;
import org.jbpm.workflow.core.node.EndNode;
import org.jbpm.workflow.core.node.EventNode;
import org.jbpm.workflow.core.node.EventSubProcessNode;
import org.jbpm.workflow.core.node.FaultNode;
import org.jbpm.workflow.core.node.ForEachNode;
import org.jbpm.workflow.core.node.HumanTaskNode;
import org.jbpm.workflow.core.node.Join;
import org.jbpm.workflow.core.node.MilestoneNode;
import org.jbpm.workflow.core.node.RuleSetNode;
import org.jbpm.workflow.core.node.Split;
import org.jbpm.workflow.core.node.StartNode;
import org.jbpm.workflow.core.node.StateNode;
import org.jbpm.workflow.core.node.SubProcessNode;
import org.jbpm.workflow.core.node.ThrowLinkNode;
import org.jbpm.workflow.core.node.TimerNode;
import org.jbpm.workflow.core.node.WorkItemNode;
import org.jbpm.workflow.instance.WorkflowProcessInstance;
import org.jbpm.workflow.instance.node.ActionNodeInstance;
import org.jbpm.workflow.instance.node.BoundaryEventNodeInstance;
import org.jbpm.workflow.instance.node.CatchLinkNodeInstance;
import org.jbpm.workflow.instance.node.CompositeContextNodeInstance;
import org.jbpm.workflow.instance.node.CompositeNodeInstance;
import org.jbpm.workflow.instance.node.DynamicNodeInstance;
import org.jbpm.workflow.instance.node.EndNodeInstance;
import org.jbpm.workflow.instance.node.EventNodeInstance;
import org.jbpm.workflow.instance.node.EventSubProcessNodeInstance;
import org.jbpm.workflow.instance.node.FaultNodeInstance;
import org.jbpm.workflow.instance.node.ForEachNodeInstance;
import org.jbpm.workflow.instance.node.HumanTaskNodeInstance;
import org.jbpm.workflow.instance.node.JoinInstance;
import org.jbpm.workflow.instance.node.MilestoneNodeInstance;
import org.jbpm.workflow.instance.node.RuleSetNodeInstance;
import org.jbpm.workflow.instance.node.SplitInstance;
import org.jbpm.workflow.instance.node.StartNodeInstance;
import org.jbpm.workflow.instance.node.StateNodeInstance;
import org.jbpm.workflow.instance.node.SubProcessNodeInstance;
import org.jbpm.workflow.instance.node.ThrowLinkNodeInstance;
import org.jbpm.workflow.instance.node.TimerNodeInstance;
import org.jbpm.workflow.instance.node.WorkItemNodeInstance;
import org.jbpm.util.JbpmClassLoaderUtil;
import org.kie.api.definition.process.Node;
import org.kie.api.runtime.Environment;
import org.kie.api.runtime.process.NodeInstance;
import org.kie.api.runtime.process.NodeInstanceContainer;
import org.kie.kogito.internal.process.runtime.KogitoNodeInstanceContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class NodeInstanceFactoryRegistry {
private static final Logger LOGGER = LoggerFactory.getLogger(NodeInstanceFactoryRegistry.class);
private static final NodeInstanceFactoryRegistry INSTANCE = new NodeInstanceFactoryRegistry();
Expand All @@ -96,11 +46,26 @@ public static NodeInstanceFactoryRegistry getInstance(Environment environment) {

protected NodeInstanceFactoryRegistry() {
this.registry = new HashMap<>();
initRegistry();
}

private void initRegistry() {
ServiceLoader.load(NodeInstanceFactoryProvider.class, JbpmClassLoaderUtil.findClassLoader())
.stream()
.map(ServiceLoader.Provider::get)
.map(NodeInstanceFactoryProvider::provide)
.flatMap(Collection::stream)
.forEach(this::register);
}

private void register(NodeInstanceFactory nodeInstanceFactory) {
LOGGER.debug("registering new node instance factory for {} set by {}", nodeInstanceFactory.forClass(), nodeInstanceFactory.getClass().getCanonicalName());
this.registry.put(nodeInstanceFactory.forClass(), nodeInstanceFactory);
}

public void register(Class<? extends Node> cls,
NodeInstanceFactory factory) {
this.registry.put(cls, factory);
public void register(Class<? extends Node> forClass, NodeInstanceFactory nodeInstanceFactory) {
LOGGER.debug("override new node instance factory for {} set by {}", forClass, nodeInstanceFactory.getClass().getCanonicalName());
this.registry.put(forClass, nodeInstanceFactory);
}

public NodeInstanceFactory getProcessNodeInstanceFactory(Node node) {
Expand All @@ -112,111 +77,12 @@ public NodeInstanceFactory getProcessNodeInstanceFactory(Node node) {
}
clazz = clazz.getSuperclass();
}
LOGGER.debug("node instance factory not found for node {}", node.getClass().getName());
return null;
}

protected NodeInstanceFactory get(Class<?> clazz) {
// hard wired nodes:
if (RuleSetNode.class == clazz) {
return factory(RuleSetNodeInstance::new);
}
if (Split.class == clazz) {
return factory(SplitInstance::new);
}
if (Join.class == clazz) {
return factoryOnce(JoinInstance::new);
}
if (StartNode.class == clazz) {
return factory(StartNodeInstance::new);
}
if (EndNode.class == clazz) {
return factory(EndNodeInstance::new);
}
if (MilestoneNode.class == clazz) {
return factory(MilestoneNodeInstance::new);
}
if (SubProcessNode.class == clazz) {
return factory(SubProcessNodeInstance::new);
}
if (ActionNode.class == clazz) {
return factory(ActionNodeInstance::new);
}
if (WorkItemNode.class == clazz) {
return factory(WorkItemNodeInstance::new);
}
if (TimerNode.class == clazz) {
return factory(TimerNodeInstance::new);
}
if (FaultNode.class == clazz) {
return factory(FaultNodeInstance::new);
}
if (EventSubProcessNode.class == clazz) {
return factory(EventSubProcessNodeInstance::new);
}
if (CompositeNode.class == clazz) {
return factory(CompositeNodeInstance::new);
}
if (CompositeContextNode.class == clazz) {
return factory(CompositeContextNodeInstance::new);
}
if (HumanTaskNode.class == clazz) {
return factory(HumanTaskNodeInstance::new);
}
if (ForEachNode.class == clazz) {
return factory(ForEachNodeInstance::new);
}
if (EventNode.class == clazz) {
return factory(EventNodeInstance::new);
}
if (StateNode.class == clazz) {
return factory(StateNodeInstance::new);
}
if (DynamicNode.class == clazz) {
return factory(DynamicNodeInstance::new);
}
if (BoundaryEventNode.class == clazz) {
return factory(BoundaryEventNodeInstance::new);
}
if (CatchLinkNode.class == clazz) {
return factory(
CatchLinkNodeInstance::new);
}
if (ThrowLinkNode.class == clazz) {
return factory(
ThrowLinkNodeInstance::new);
}
if (AsyncEventNode.class == clazz) {
return factory(
AsyncEventNodeInstance::new);
}

return this.registry.get(clazz);
}

protected NodeInstanceFactory factoryOnce(Supplier<NodeInstanceImpl> supplier) {
return (node, processInstance, nodeInstanceContainer) -> {
NodeInstance result = ((org.jbpm.workflow.instance.NodeInstanceContainer) nodeInstanceContainer).getFirstNodeInstance(node.getId());
if (result != null) {
return result;
} else {
LOGGER.debug("creating node {} with identifier {}", node, node.getId());
return createInstance(supplier.get(), node, processInstance, nodeInstanceContainer);
}
};
}

protected NodeInstanceFactory factory(Supplier<NodeInstanceImpl> supplier) {
return (node, processInstance, nodeInstanceContainer) -> createInstance(supplier.get(), node, processInstance, nodeInstanceContainer);
}

private static NodeInstance createInstance(NodeInstanceImpl nodeInstance, Node node, WorkflowProcessInstance processInstance, NodeInstanceContainer nodeInstanceContainer) {
nodeInstance.setNodeId(node.getId());
nodeInstance.setNodeInstanceContainer((KogitoNodeInstanceContainer) nodeInstanceContainer);
nodeInstance.setProcessInstance(processInstance);
nodeInstance.setMetaData(CUSTOM_ASYNC, node.getMetaData().get(CUSTOM_ASYNC));

int level = ((org.jbpm.workflow.instance.NodeInstanceContainer) nodeInstanceContainer).getLevelForNode(node.getUniqueId());
nodeInstance.setLevel(level);
return nodeInstance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.jbpm.workflow.instance.impl.factory;

import org.jbpm.workflow.instance.WorkflowProcessInstance;
import org.jbpm.workflow.instance.impl.NodeInstanceFactory;
import org.jbpm.workflow.instance.impl.NodeInstanceImpl;
import org.kie.api.definition.process.Node;
import org.kie.api.runtime.process.NodeInstance;
import org.kie.api.runtime.process.NodeInstanceContainer;
import org.kie.kogito.internal.process.runtime.KogitoNodeInstanceContainer;

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

public abstract class AbstractNodeInstanceFactory implements NodeInstanceFactory {

protected NodeInstance createInstance(NodeInstanceImpl nodeInstance, Node node, WorkflowProcessInstance processInstance, NodeInstanceContainer nodeInstanceContainer) {
nodeInstance.setNodeId(node.getId());
nodeInstance.setNodeInstanceContainer((KogitoNodeInstanceContainer) nodeInstanceContainer);
nodeInstance.setProcessInstance(processInstance);
nodeInstance.setMetaData(CUSTOM_ASYNC, node.getMetaData().get(CUSTOM_ASYNC));

int level = ((org.jbpm.workflow.instance.NodeInstanceContainer) nodeInstanceContainer).getLevelForNode(node.getUniqueId());
nodeInstance.setLevel(level);
return nodeInstance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.jbpm.workflow.instance.impl.factory;

import java.util.function.Supplier;

import org.jbpm.workflow.core.impl.NodeImpl;
import org.jbpm.workflow.instance.WorkflowProcessInstance;
import org.jbpm.workflow.instance.impl.NodeInstanceImpl;
import org.kie.api.definition.process.Node;
import org.kie.api.runtime.process.NodeInstance;
import org.kie.api.runtime.process.NodeInstanceContainer;

public class DefaultNodeInstanceFactory extends AbstractNodeInstanceFactory {

private Class<? extends NodeImpl> nodeDefinition;
private Supplier<NodeInstanceImpl> nodeInstanceSupplier;

@Override
public Class<? extends Node> forClass() {
return nodeDefinition;
}

public DefaultNodeInstanceFactory(Class<? extends NodeImpl> nodeDefinition, Supplier<NodeInstanceImpl> supplier) {
this.nodeDefinition = nodeDefinition;
this.nodeInstanceSupplier = supplier;
}

@Override
public NodeInstance getNodeInstance(Node node, WorkflowProcessInstance processInstance, NodeInstanceContainer nodeInstanceContainer) {
return createInstance(nodeInstanceSupplier.get(), node, processInstance, nodeInstanceContainer);
}

}
Loading

0 comments on commit 92c33c5

Please sign in to comment.