Skip to content

Commit

Permalink
[incubator-kie-issues-1111] Add capability to extends for code genera…
Browse files Browse the repository at this point in the history
…tion (#3507)

* [incubator-kie-issues-1111] Add capability to extends for code generation

* class loader corrections
  • Loading branch information
elguardian authored May 20, 2024
1 parent 2c4bdc9 commit 0c5b65f
Show file tree
Hide file tree
Showing 32 changed files with 934 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/
package org.jbpm.compiler.canonical;

import java.util.Map;

import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.workflow.core.node.CompositeContextNode;
import org.kie.api.definition.process.Node;
Expand All @@ -28,15 +27,15 @@

public abstract class AbstractCompositeNodeVisitor<T extends CompositeContextNode> extends AbstractNodeVisitor<T> {

protected Map<Class<?>, AbstractNodeVisitor<? extends Node>> nodesVisitors;
protected NodeVisitorBuilderService nodevisitorService;

public AbstractCompositeNodeVisitor(Map<Class<?>, AbstractNodeVisitor<? extends Node>> nodesVisitors) {
this.nodesVisitors = nodesVisitors;
public AbstractCompositeNodeVisitor(NodeVisitorBuilderService nodevisitorService) {
this.nodevisitorService = nodevisitorService;
}

protected <U extends Node> void visitNodes(String factoryField, U[] nodes, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
for (U node : nodes) {
AbstractNodeVisitor<U> visitor = (AbstractNodeVisitor<U>) nodesVisitors.get(node.getClass());
AbstractNodeVisitor<U> visitor = (AbstractNodeVisitor<U>) nodevisitorService.findNodeVisitor(node.getClass());
if (visitor == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
package org.jbpm.compiler.canonical;

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

import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.ruleflow.core.factory.CompositeContextNodeFactory;
import org.jbpm.workflow.core.node.CompositeContextNode;
import org.kie.api.definition.process.Node;

import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
Expand All @@ -34,8 +33,8 @@

public class CompositeContextNodeVisitor<T extends CompositeContextNode> extends AbstractCompositeNodeVisitor<T> {

public CompositeContextNodeVisitor(Map<Class<?>, AbstractNodeVisitor<? extends Node>> nodesVisitors) {
super(nodesVisitors);
public CompositeContextNodeVisitor(NodeVisitorBuilderService nodeVisitorService) {
super(nodeVisitorService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Stream;

import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.ruleflow.core.factory.DynamicNodeFactory;
import org.jbpm.workflow.core.node.DynamicNode;
import org.kie.api.definition.process.Node;

import com.github.javaparser.ast.expr.MethodCallExpr;

Expand All @@ -36,8 +35,8 @@

public class DynamicNodeVisitor extends CompositeContextNodeVisitor<DynamicNode> {

public DynamicNodeVisitor(Map<Class<?>, AbstractNodeVisitor<? extends Node>> nodesVisitors) {
super(nodesVisitors);
public DynamicNodeVisitor(NodeVisitorBuilderService nodeVisitorService) {
super(nodeVisitorService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Stream;

import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.ruleflow.core.factory.EventSubProcessNodeFactory;
import org.jbpm.workflow.core.node.EventSubProcessNode;
import org.kie.api.definition.process.Node;

import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
Expand All @@ -37,8 +36,8 @@

public class EventSubProcessNodeVisitor extends CompositeContextNodeVisitor<EventSubProcessNode> {

public EventSubProcessNodeVisitor(Map<Class<?>, AbstractNodeVisitor<? extends Node>> nodesVisitors) {
super(nodesVisitors);
public EventSubProcessNodeVisitor(NodeVisitorBuilderService nodeVisitorService) {
super(nodeVisitorService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.ruleflow.core.factory.ForEachNodeFactory;
import org.jbpm.workflow.core.Node;
Expand All @@ -45,8 +45,8 @@

public class ForEachNodeVisitor extends AbstractCompositeNodeVisitor<ForEachNode> {

public ForEachNodeVisitor(Map<Class<?>, AbstractNodeVisitor<? extends org.kie.api.definition.process.Node>> nodesVisitors) {
super(nodesVisitors);
public ForEachNodeVisitor(NodeVisitorBuilderService nodeVisitorService) {
super(nodeVisitorService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

public class HumanTaskNodeVisitor extends WorkItemNodeVisitor<HumanTaskNode> {

public HumanTaskNodeVisitor() {
super(null);
public HumanTaskNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.jbpm.compiler.canonical;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -28,6 +27,7 @@
import java.util.stream.Stream;

import org.jbpm.compiler.canonical.descriptors.ExpressionUtils;
import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.Context;
import org.jbpm.process.core.ContextContainer;
import org.jbpm.process.core.Work;
Expand All @@ -40,27 +40,7 @@
import org.jbpm.workflow.core.Node;
import org.jbpm.workflow.core.NodeContainer;
import org.jbpm.workflow.core.impl.ConnectionImpl;
import org.jbpm.workflow.core.node.ActionNode;
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.kie.api.definition.process.Connection;
import org.kie.api.definition.process.Process;
Expand Down Expand Up @@ -98,30 +78,10 @@ public class ProcessVisitor extends AbstractVisitor {

public static final String DEFAULT_VERSION = "1.0";

private Map<Class<?>, AbstractNodeVisitor<? extends org.kie.api.definition.process.Node>> nodesVisitors = new HashMap<>();
private NodeVisitorBuilderService nodeVisitorService;

public ProcessVisitor(ClassLoader contextClassLoader) {
this.nodesVisitors.put(StartNode.class, new StartNodeVisitor());
this.nodesVisitors.put(ActionNode.class, new ActionNodeVisitor());
this.nodesVisitors.put(EndNode.class, new EndNodeVisitor());
this.nodesVisitors.put(HumanTaskNode.class, new HumanTaskNodeVisitor());
this.nodesVisitors.put(WorkItemNode.class, new WorkItemNodeVisitor<>(contextClassLoader));
this.nodesVisitors.put(SubProcessNode.class, new LambdaSubProcessNodeVisitor());
this.nodesVisitors.put(Split.class, new SplitNodeVisitor());
this.nodesVisitors.put(Join.class, new JoinNodeVisitor());
this.nodesVisitors.put(FaultNode.class, new FaultNodeVisitor());
this.nodesVisitors.put(RuleSetNode.class, new RuleSetNodeVisitor(contextClassLoader));
this.nodesVisitors.put(BoundaryEventNode.class, new BoundaryEventNodeVisitor());
this.nodesVisitors.put(EventNode.class, new EventNodeVisitor());
this.nodesVisitors.put(ForEachNode.class, new ForEachNodeVisitor(nodesVisitors));
this.nodesVisitors.put(CompositeContextNode.class, new CompositeContextNodeVisitor<>(nodesVisitors));
this.nodesVisitors.put(EventSubProcessNode.class, new EventSubProcessNodeVisitor(nodesVisitors));
this.nodesVisitors.put(TimerNode.class, new TimerNodeVisitor());
this.nodesVisitors.put(MilestoneNode.class, new MilestoneNodeVisitor());
this.nodesVisitors.put(DynamicNode.class, new DynamicNodeVisitor(nodesVisitors));
this.nodesVisitors.put(StateNode.class, new StateNodeVisitor(nodesVisitors));
this.nodesVisitors.put(CatchLinkNode.class, new CatchLinkNodeVisitor());
this.nodesVisitors.put(ThrowLinkNode.class, new ThrowLinkNodeVisitor());
nodeVisitorService = new NodeVisitorBuilderService(contextClassLoader);
}

public void visitProcess(WorkflowProcess process, MethodDeclaration processMethod, ProcessMetaData metadata) {
Expand Down Expand Up @@ -221,14 +181,24 @@ private void visitHeader(WorkflowProcess process, BlockStmt body) {

private <U extends org.kie.api.definition.process.Node> void visitNodes(List<U> nodes, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
for (U node : nodes) {
AbstractNodeVisitor<U> visitor = (AbstractNodeVisitor<U>) nodesVisitors.get(node.getClass());
@SuppressWarnings("unchecked")
AbstractNodeVisitor<U> visitor = (AbstractNodeVisitor<U>) this.nodeVisitorService.findNodeVisitor(node.getClass());
if (visitor == null) {
throw new IllegalStateException("No visitor found for node " + node.getClass().getName());
}
visitor.visitNode(node, body, variableScope, metadata);
}
}

@SuppressWarnings("unchecked")
private String getFieldName(ContextContainer contextContainer) {
AbstractNodeVisitor visitor = null;
if (contextContainer instanceof CompositeNode) {
visitor = this.nodeVisitorService.findNodeVisitor(contextContainer.getClass());
}
return visitor != null ? visitor.getNodeId(((Node) contextContainer)) : FACTORY_FIELD_NAME;
}

private void visitConnections(org.kie.api.definition.process.Node[] nodes, BlockStmt body) {

List<Connection> connections = new ArrayList<>();
Expand Down Expand Up @@ -302,14 +272,6 @@ faultVariable.<Expression> map(StringLiteralExpr::new)
}
}

private String getFieldName(ContextContainer contextContainer) {
AbstractNodeVisitor visitor = null;
if (contextContainer instanceof CompositeNode) {
visitor = this.nodesVisitors.get(contextContainer.getClass());
}
return visitor != null ? visitor.getNodeId(((Node) contextContainer)) : FACTORY_FIELD_NAME;
}

private void visitSubExceptionScope(org.kie.api.definition.process.Node[] nodes, BlockStmt body) {
Stream.of(nodes)
.peek(node -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import java.util.Map;
import java.util.stream.Stream;

import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.ruleflow.core.factory.StateNodeFactory;
import org.jbpm.workflow.core.Constraint;
import org.jbpm.workflow.core.impl.ConnectionRef;
import org.jbpm.workflow.core.node.StateNode;
import org.kie.api.definition.process.Node;

import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
Expand All @@ -39,8 +39,8 @@

public class StateNodeVisitor extends CompositeContextNodeVisitor<StateNode> {

public StateNodeVisitor(Map<Class<?>, AbstractNodeVisitor<? extends Node>> nodesVisitors) {
super(nodesVisitors);
public StateNodeVisitor(NodeVisitorBuilderService nodeVisitorService) {
super(nodeVisitorService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.compiler.canonical.node;

import org.jbpm.compiler.canonical.AbstractNodeVisitor;
import org.jbpm.compiler.canonical.ActionNodeVisitor;
import org.jbpm.workflow.core.node.ActionNode;
import org.kie.api.definition.process.Node;

public class ActionNodeVisitorBuilder implements NodeVisitorBuilder {

@Override
public Class<?> type() {
return ActionNode.class;
}

@Override
public AbstractNodeVisitor<? extends Node> visitor(NodeVisitorBuilderService nodeVisitorService, ClassLoader classLoader) {
return new ActionNodeVisitor();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.compiler.canonical.node;

import org.jbpm.compiler.canonical.AbstractNodeVisitor;
import org.jbpm.compiler.canonical.BoundaryEventNodeVisitor;
import org.jbpm.workflow.core.node.BoundaryEventNode;
import org.kie.api.definition.process.Node;

public class BoundaryEventNodeVisitorBuilder implements NodeVisitorBuilder {

@Override
public Class<?> type() {
return BoundaryEventNode.class;
}

@Override
public AbstractNodeVisitor<? extends Node> visitor(NodeVisitorBuilderService nodeVisitorService, ClassLoader classLoader) {
return new BoundaryEventNodeVisitor();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.compiler.canonical.node;

import org.jbpm.compiler.canonical.AbstractNodeVisitor;
import org.jbpm.compiler.canonical.CatchLinkNodeVisitor;
import org.jbpm.workflow.core.node.CatchLinkNode;
import org.kie.api.definition.process.Node;

public class CatchLinkNodeVisitorBuilder implements NodeVisitorBuilder {

@Override
public Class<?> type() {
return CatchLinkNode.class;
}

@Override
public AbstractNodeVisitor<? extends Node> visitor(NodeVisitorBuilderService nodeVisitorService, ClassLoader classLoader) {
return new CatchLinkNodeVisitor();
}

}
Loading

0 comments on commit 0c5b65f

Please sign in to comment.