Show Table of Contents
11.13. Process Fluent API
11.13.1. Using the Process Fluent API to Create Business Process
While it is recommended to define processes using the graphical editor or the underlying XML, you can also create a business process using the Process API directly. The most important process model elements are defined in the packages org.jbpm.workflow.core and org.jbpm.workflow.core.node.
Red Hat JBoss BPM Suite provides you a fluent API that allows you to easily construct processes in a readable manner using factories. You can then validate the process that you were constructing manually.
11.13.2. Process Fluent API Example
Here is an example of a basic process with only a script task:
RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.HelloWorld");
factory
// Header
.name("HelloWorldProcess")
.version("1.0")
.packageName("org.jbpm")
// Nodes
.startNode(1).name("Start").done()
.actionNode(2).name("Action")
.action("java", "System.out.println(\"Hello World\");").done()
.endNode(3).name("End").done()
// Connections
.connection(1, 2)
.connection(2, 3);
RuleFlowProcess process = factory.validate().getProcess();
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
Resource resource = ks.getResources().newByteArrayResource(
XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
resource.setSourcePath("helloworld.bpmn2");
kfs.write(resource);
ReleaseId releaseId = ks.newReleaseId("org.jbpm", "helloworld", "1.0");
kfs.generateAndWritePomXML(releaseId);
ks.newKieBuilder(kfs).buildAll();
ks.newKieContainer(releaseId).newKieSession().startProcess("org.jbpm.HelloWorld");
In this example, we first call the static
createProcess() method from the RuleFlowProcessFactory class. This method creates a new process and returns the RuleFlowProcessFactory that can be used to create the process.
A process consists of three parts:
- Header: The header section comprises global elements such as the name of the process, imports, and variables.In the above example, the header contains the name and version of the process and the package name.
- Nodes: The nodes section comprises all the different nodes that are part of the process.In the above example, nodes are added to the current process by calling the
startNode(),actionNode()andendNode()methods. These methods return a specificNodeFactorythat allows you to set the properties of that node. Once you have finished configuring that specific node, thedone()method returns you to the currentRuleFlowProcessFactoryso you can add more nodes, if necessary. - Connections: The connections section links the nodes to create a flow chart.In the above example, once you add all the nodes, you must connect them by creating connections between them. This can be done by calling the method
connection, which links the nodes.Finally, you can validate the generated process by calling thevalidate()method and retrieve the createdRuleFlowProcessobject.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.