# Flowable
###### tags: `Ideation` `tutorials`
Flowable is a fork of Activiti (registered trademark of Alfresco). Essentially is a business process engine written in Java, open-source ( Apache V2 license) with 2.7 stars on Github with steady support.
Docs : https://flowable.com/open-source/docs/oss-introduction/
Github: https://github.com/flowable/flowable-engine
Simply put, a Business Process is a set of tasks that, once completed in a defined order, accomplishes a defined objective. Each task in a Business Process has clearly defined inputs and outputs. These tasks may require human intervention or may be completely automated.
Let's suppose we have a simple process. We create process definitions as XML files using the BPMN XML standard (example below).
```XML
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
xmlns:flowable="http://flowable.org/bpmn"
typeLanguage="http://www.w3.org/2001/XMLSchema"
expressionLanguage="http://www.w3.org/1999/XPath"
targetNamespace="http://www.flowable.org/processdef">
<process id="holidayRequest" name="Holiday Request" isExecutable="true">
<startEvent id="startEvent"/>
<sequenceFlow sourceRef="startEvent" targetRef="approveTask"/>
<userTask id="approveTask" name="Approve or reject request" flowable:candidateGroups="managers"/>
<sequenceFlow sourceRef="approveTask" targetRef="decision"/>
<exclusiveGateway id="decision"/>
<sequenceFlow sourceRef="decision" targetRef="externalSystemCall">
<conditionExpression xsi:type="tFormalExpression">
<![CDATA[
${approved}
]]>
</conditionExpression>
</sequenceFlow>
<sequenceFlow sourceRef="decision" targetRef="sendRejectionMail">
<conditionExpression xsi:type="tFormalExpression">
<![CDATA[
${!approved}
]]>
</conditionExpression>
</sequenceFlow>
<serviceTask id="externalSystemCall" name="Enter holidays in external system"
flowable:class="CallExternalSystemDelegate"/>
<sequenceFlow sourceRef="externalSystemCall" targetRef="holidayApprovedTask"/>
<userTask id="holidayApprovedTask" name="Holiday approved"/>
<sequenceFlow sourceRef="holidayApprovedTask" targetRef="approveEnd"/>
<serviceTask id="sendRejectionMail" name="Send out rejection email"
flowable:class="SendRejectionMail"/>
<sequenceFlow sourceRef="sendRejectionMail" targetRef="rejectEnd"/>
<endEvent id="approveEnd"/>
<endEvent id="rejectEnd"/>
</process>
</definitions>
```
There are several elements that are standard XML stuff, while others are specific to BMPN.
* The entire process is wrapped in a tag called “process”, which in turn, is part of a tag called “definitions”
* A process consists of events, flows, tasks, and gateways
* An event is either a start event or an end event
* A flow (in this example, a sequence flow) connects other elements like events and tasks
* Tasks are where actual work is done; these can be “user tasks” or “service tasks”, among others
* A user task requires a human user to interact with the Flowable API and take action
* A service task represents an automatic task, which can be a call to a Java class or even an HTTP call
* A gateway executes based on the attribute “approved”; this is known as a process variable
While we can create process definition files in any text editor, this isn't always the most convenient way. Fortunately, though, Flowable also comes with user interface options to do so.
## Let's go for holidays?
To provide a clear picture let's build a very simple holiday request process. In Flowable terminology, tre this as a **process definition**. From a **process definition**, many **process instances** can be started. Think of the process definition as the blueprint for many executions of the process. In this particular case, the **process definition** defines the different steps involved in requesting holidays, while one **process instance** matches the request for a holiday by one particular employee.
A visualization with a brief explanation helps to make a solid idea:

- The circle on the left is called a start event. It’s the starting point of a process instance.
- The first rectangle is a user task. This is a step in the process that a human user has to perform. In this case, the manager needs to approve or reject the request.
- Depending on what the manager decides, the exclusive gateway (the diamond shape with the cross) will route the process instance to either the approval or the rejection path.
- If approved, we have to register the request in some external system, which is followed by a user task again for the original employee that notifies them of the decision. This could, of course, be replaced by an email.
- If rejected, an email is sent to the employee informing them of this
## Working with Flowable API
After defining the simple process in an XML file as per the BPMN 2.0 standard, we need a way to submit and run it. Flowable provides the Process Engine API to interact with Flowable Engines. Flowable is very flexible and offers several ways to deploy this API.
Given that Flowable is a Java API, we can include the process engine in any Java application by simply including the requisite JAR files. We can very well leverage Maven for managing these dependencies.
Moreover, Flowable comes with bundled REST APIs to interact with Flowable over HTTP. We can use these REST APIs to pretty much do anything otherwise possible through Flowable API.
Finally, Flowable has excellent support for integration with Spring and Spring Boot.
"One of the many reasons for choosing to use a process engine like Flowable is because it automatically stores audit data or historical data for all the process instances. This data allows the creation of rich reports that give insights into how the organization works, where the bottlenecks are, etc.
For example, suppose we want to show the duration of the process instance that we’ve been executing so far. To do this, we get the HistoryService from the ProcessEngine and create a query for historical activities. In the snippet below you can see we add some additional filtering:
- only the activities for one particular process instance
- only the activities that have finished "