Camunda 8 BMPN Creation Programmatically with API

Hi Team,

Instead of fluent API, We need to create the shapes dynamically and later we need connect them, Trying to create in Camunda 8 something like below (following a link The new camunda BPMN model API | Camunda),

Kindly could you please help me how to do it in Camunda 8

public void testCreateProcess() {
  modelInstance = Bpmn.createEmptyModel();
  Definitions definitions = modelInstance.newInstance(Definitions.class);
  definitions.setTargetNamespace("https://camunda.org/examples");
  modelInstance.setDefinitions(definitions);

  Process process = modelInstance.newInstance(Process.class);
  definitions.addChildElement(process);

  StartEvent startEvent = modelInstance.newInstance(StartEvent.class);
  startEvent.setId("start");
  process.addChildElement(startEvent);

  UserTask userTask = modelInstance.newInstance(UserTask.class);
  userTask.setId("task");
  userTask.setName("User Task");
  process.addChildElement(userTask);

  SequenceFlow sequenceFlow = modelInstance.newInstance(SequenceFlow.class);
  sequenceFlow.setId("flow1");
  process.addChildElement(sequenceFlow);
  connect(sequenceFlow, startEvent, userTask);

  EndEvent endEvent = modelInstance.newInstance(EndEvent.class);
  endEvent.setId("end");
  process.addChildElement(endEvent);

  sequenceFlow = modelInstance.newInstance(SequenceFlow.class);
  sequenceFlow.setId("flow2");
  process.addChildElement(sequenceFlow);
  connect(sequenceFlow, userTask, endEvent);

  Bpmn.writeModelToFile(new File("target/new-process.bpmn"), modelInstance);
}

connect(SequenceFlow flow, FlowNode from, FlowNode to) {
  flow.setSource(from);
  from.getOutgoing().add(flow);
  flow.setTarget(to);
  to.getIncoming().add(flow);
}

This code generates the following BPMN 2.0 XML file.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<definitions targetNamespace="https://camunda.org/examples" xmlns="https://www.omg.org/spec/BPMN/20100524/MODEL">
  <process>
    <startEvent id="start">
      <outgoing>flow1</outgoing>
    </startEvent>
    <userTask id="task" name="User Task">
      <incoming>flow1</incoming>
      <outgoing>flow2</outgoing>
    </userTask>
    <sequenceFlow id="flow1" sourceRef="start" targetRef="task"/>
    <endEvent id="end">
      <incoming>flow2</incoming>
    </endEvent>
    <sequenceFlow id="flow2" sourceRef="task" targetRef="end"/>
  </process>
</definitions>

Hi @SAMBASIVARAO_Y,
The fluent API assists you to create BPMN models. The model that you create is not specific to any process engine. To make it executable, you’d need to add engine-specific extension elements. These are documented for each element in our documentation (e.g., Service tasks | Camunda Platform 8 Docs). However, if you want to do lots of configuration, this can be some effort (i.e., the fluent API does not have any methods for this out-of-the-box).

Hi @StephanHaarmann ,

With the FluentAPI, We can create the tasks one after another, But We need to create the list of tasks/nodes and later we need to have set of connections among them something like below.

Could you please let us know how we can do in Camunda 8?

Dashboard → { Accounts, Transactions, Budgets, Reports }
Accounts → { Dashboard, Transactions, Budgets, Reports }
Transactions → { Dashboard, Accounts, Budgets, Reports }
Budgets → { Dashboard, Accounts,Transactions, Reports }
Reports → { Dashboard, Accounts,Transactions,Budgets }

This looks like you want to connect everything to (almost) everything else.
Using the BPMN Model API, you can add arbitrary sequence flows between nodes.
Just repeat these steps:

  SequenceFlow sequenceFlow = modelInstance.newInstance(SequenceFlow.class);
  sequenceFlow.setId("flow1");
  process.addChildElement(sequenceFlow);
  connect(sequenceFlow, startEvent, userTask);

What do you mean by “create it in Camunda 8”?

Thanks @StephanHaarmann for quick turnaround, When we do as you suggested it is generating an ‘xml’ like below.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<definitions targetNamespace="https://camunda.org/examples" xmlns="https://www.omg.org/spec/BPMN/20100524/MODEL">
  <process>
    <startEvent id="start">
      <outgoing>flow1</outgoing>
    </startEvent>
    <userTask id="task" name="User Task">
      <incoming>flow1</incoming>
      <outgoing>flow2</outgoing>
    </userTask>
    <sequenceFlow id="flow1" sourceRef="start" targetRef="task"/>
    <endEvent id="end">
      <incoming>flow2</incoming>
    </endEvent>
    <sequenceFlow id="flow2" sourceRef="task" targetRef="end"/>
  </process>
</definitions>

When I’m opening that xml in the modeler, it is giving me an error.

Have you tried to verify the model?

Bpmn.validateModel(modelInstance);

This may give you some more details on what went wrong.
My guess is that the definitions should look more like this:

<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" targetNamespace="https://camunda.org/examples">

And the XML elements should subsequently be prefixed.