How to Simplify Complex Business Logic with CompileFlow Workflow Engine
Learn how to replace tangled if‑else code with a visual workflow engine using CompileFlow, from setting up the designer and BPM files to binding data, methods, conditions, and executing a sample chicken‑training process, while understanding the underlying principles and practical tips.
Hello everyone, today we'll introduce a practical enterprise development technique—workflow—using a fun example. Proper use of workflow can eliminate massive if‑else code, making projects easier to maintain and extend.
What is workflow?
A workflow is a series of tasks forming a process, similar to a factory production line where each worker performs their part after the steps are defined.
For example, a chicken‑training system requires each chicken to pass singing, dancing, rap, and basketball assessments and train for two and a half years before sale; otherwise, it must restart training.
The corresponding flowchart is shown below.
If you were to code this logic, you'd likely write a long series of
if ... else ...statements.
In enterprise development, business processes are often far more complex, making scattered if‑else statements impractical, especially with multiple developers.
To develop and maintain workflows efficiently, we typically use a
workflow enginethat provides visual drag‑and‑drop design, automatically generating business process code and reducing development cost, even for non‑programmers.
Popular workflow engines include Activiti and Flowable, but they have a learning curve. For this tutorial, we choose the lightweight CompileFlow engine.
Implementing the Chicken System with Workflow
1. Preparation
Download the CompileFlow demo project (a Maven + Spring Boot application) and open it in an IDE. The project contains many BPM files defining workflows.
The BPM files are XML, which can be complex to write manually.
Therefore, we install the
Compileflow Designerplugin for visual editing.
2. Create a BPM file
Create a new BPM file named
ji.bpmand switch to the visual editor to design the process flow.
In the visual editor, the left side shows
nodes. Each workflow must have a start and end node, and nodes can have different control rules.
After editing, the flowchart looks like this:
Now we need to bind data and code to make the flow executable.
3. Bind Data
Define the workflow
context(global variables). For this example, the input is a
Jiobject (the chicken) and the output is a boolean result.
Configure the context in the designer, setting
inOutTypeto
paramfor input and
resultfor output.
4. Bind Methods
Assign a method to each node, representing the action to perform (e.g.,
checkChang, success, failure).
Configure method parameters and return values using the context.
5. Bind Conditions
For decision nodes, set conditional expressions (yes/no) and priority to mimic if‑else order.
6. Execute the Process
Write a main method to create a
Jiinstance, set it in the context, and start the workflow using the engine.
Example code:
<code>public static void main(String[] args) {
// Input a chicken
Ji ji = new Ji();
ji.setName("cai");
ji.setCanChang(true);
ji.setCanTiao(true);
ji.setCanRap(true);
ji.setCanLanQiu(true);
ji.setPracticeYear(2.5D);
// BPM file location
String code = "bpm.ji";
// Set context (input parameters)
Map<String, Object> context = new HashMap<>();
context.put("ji", ji);
try {
// Execute workflow
ProcessEngine processEngine = ProcessEngineFactory.getProcessEngine();
Map<String, Object> result = processEngine.start(code, context);
// Get result
System.out.println(result.get("result"));
} catch (Exception e) {
e.printStackTrace();
}
}
</code>Running the program yields the assessment result, demonstrating the workflow engine usage.
While this simple example shows the basics, more complex scenarios (e.g.,
orderFulfillmentFlow.bpm) better illustrate the advantages of workflow engines, which are commonly used in ERP and OA systems.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.