Search code examples
javabpmnbusiness-process-managementkie

Generate BPMN using Java Library


Trying to generate a BPMN diagram, even a basic one with start event, end event and some user tasks. Is there any Java libraries or API's available that would help me achieve this. Have searched around a lot but could not find any suitable. Any help would be appreciated


Solution

  • If you need a Java lib, you can use the Camunda model builder API.

     public static void main(String[] args) {
    
            BpmnModelInstance modelInst;
            try {
            // File file = new File(ModelModifier.class.getClassLoader().getResource("process1.bpmn").toURI());
                File file = new File("./src/main/resources/process1.bpmn");
            //  modelInst = Bpmn.readModelFromFile(file);
                modelInst = Bpmn.createProcess()
                        .name("Twitter QA")
                        .executable()
                        .startEvent()
                        .userTask().id("ApproveTweet").name("Approve Tweet")
                        .exclusiveGateway().id("isApproved").name("Approved?")
                        .condition("approved", "#{approved}")
                        .serviceTask().id("sendTweet").name("Send tweet")
                        .endEvent().name("Tweet sent")
                        .moveToLastGateway()
                        // done();
                        // Gateway gateway = modelInst.getModelElementById("isApproved");
                        // gateway.builder()
                        .condition("Not approved", "#{!approved}")
                        .serviceTask().name("Send Rejection")
                        .endEvent().name("Tweet rejected").done();
    
                log.info("Flow Elements - Name : Id : Type Name");
                modelInst.getModelElementsByType(UserTask.class).forEach(e -> log.info("{} : {} : {}", e.getName(), e.getId(), e.getElementType().getTypeName()));
    
                Bpmn.writeModelToFile(file, modelInst);
    //        file.createNewFile("/tmp/testDiagram2.bpmn")
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    Here is a project with a few examples: https://github.com/rob2universe/bpmn-creator.

    If you are also comfortable with js then bpmn.js from bpmn.io would be preferable