【开源项目】CompileFlow流程引擎

CompileFlow源码解析

源码地址

https://gitee.com/mirrors/compileflow

项目介绍

compileflow是一个非常轻量、高性能、可集成、可扩展的流程引擎。

compileflow Process引擎是淘宝工作流TBBPM引擎之一,是专注于纯内存执行,无状态的流程引擎,通过将流程文件转换生成java代码编译执行,简洁高效。当前是阿里业务中台交易等多个核心系统的流程引擎。

compileflow能让开发人员通过流程编辑器设计自己的业务流程,将复杂的业务逻辑可视化,为业务设计人员与开发工程师架起了一座桥梁。

入门使用

  1. 在资源目录下添加资源文件bpm/ktvExample.bpm。
<?xml version="1.0" encoding="UTF-8" ?>
<bpm code="bpm.ktv.ktvExample" name="ktv example" type="process" description="ktv example">
  <var name="price" description="支付价格" dataType="java.lang.Integer" inOutType="return"/>
  <var name="totalPrice" description="实付价" dataType="java.lang.Integer" inOutType="inner"/>
  <var name="pList" description="人员" dataType="java.util.List&lt;java.lang.String&gt;" inOutType="param"/>
  <end id="11" name="结束" g="101,549,30,30"/>
  <autoTask id="12" name="付款" g="72,469,88,48">
    <transition g=":-15,20" to="11"/>
    <action type="java">
      <actionHandle clazz="com.charles.test.KtvService" method="payMoney">
        <var name="p1" description="价格" dataType="java.lang.Integer" contextVarName="price" defaultValue=""
             inOutType="param"/>
      </actionHandle>
    </action>
  </autoTask>
  <scriptTask id="9" name="原价" g="132,389,88,48">
    <transition g=":-15,20" to="12"/>
    <action type="ql">
      <actionHandle expression="price*1">
        <var name="price" description="价格" dataType="java.lang.Integer" contextVarName="totalPrice"
             defaultValue="" inOutType="param"/>
        <var name="price" description="价格" dataType="java.lang.Integer" contextVarName="price" defaultValue=""
             inOutType="return"/>
      </actionHandle>
    </action>
  </scriptTask>
  <loopProcess id="13" name="循环节点" g="20,75,198,190" collectionVarName="pList" variableName="p" indexVarName="i"
               variableClass="java.lang.String" startNodeId="13-1" endNodeId="13-1">
    <transition g=":-15,20" to="8"/>
    <autoTask id="13-1" name="每人唱一首歌" g="70,95,88,48">
      <action type="spring-bean">
        <actionHandle bean="ktvService" clazz="com.charles.test.KtvService" method="sing">
          <var name="p1" description="" dataType="java.lang.String" contextVarName="p" defaultValue=""
               inOutType="param"/>
        </actionHandle>
      </action>
    </autoTask>
  </loopProcess>
  <decision id="8" name="计算费用" g="72,309,88,48">
    <transition expression="" name="不超过400" priority="1" g=":-15,20" to="9"/>
    <transition expression="totalPrice&gt;=400" name="超过400" priority="10" g=":-15,20" to="10"/>
    <action type="java">
      <actionHandle clazz="com.charles.test.MockJavaClazz" method="calPrice">
        <var name="p1" description="人数" dataType="java.lang.Integer" contextVarName="pList.size()"
             defaultValue="" inOutType="param"/>
        <var name="p2" description="价格" dataType="java.lang.Integer" contextVarName="totalPrice" defaultValue=""
             inOutType="return"/>
      </actionHandle>
    </action>
  </decision>
  <start id="1" name="开始" g="105,17,30,30">
    <transition g=":-15,20" to="13"/>
  </start>
  <note id="14" g="273,82,93,55" comment="外框为循环节点" visible="true">
    <transition g=":-15,20" to="13"/>
  </note>
  <scriptTask id="10" name="9折优惠" g="12,389,88,48">
    <transition g=":-15,20" to="12"/>
    <action type="ql">
      <actionHandle expression="(round(price*0.9,0)).intValue()">
        <var name="price" description="价格" dataType="java.lang.Integer" contextVarName="totalPrice"
             defaultValue="" inOutType="param"/>
        <var name="price" description="价格" dataType="java.lang.Integer" contextVarName="price" defaultValue=""
             inOutType="return"/>
      </actionHandle>
    </action>
  </scriptTask>
</bpm>
  1. 执行该流程引擎
    @Test
    public void testProcessEngine() {
        final String code = "bpm.ktvExample";

        final Map<String, Object> context = new HashMap<>();
        final List<String> pList = new ArrayList<>();
        pList.add("wuxiang");
        pList.add("xuan");
        pList.add("yusu");
        context.put("pList", pList);

        final ProcessEngine<TbbpmModel> processEngine = ProcessEngineFactory.getProcessEngine();

        final TbbpmModel tbbpmModel = processEngine.load(code);
        final OutputStream outputStream = TbbpmModelConverter.getInstance().convertToStream(tbbpmModel);
        System.out.println(outputStream);
        System.out.println(processEngine.getTestCode(code));

        processEngine.preCompile(code);

        System.out.println(processEngine.execute(code, context));
    }

源码解析

  1. 该项目的核心主要是将xml文件编译成可运行的java文件。
  2. AbstractProcessEngine#load是将xml文件编译成java对象。
  3. AbstractProcessEngine#getCompiledRuntime根据对象生成对应的javaCode,并写入到指定路径下,然后进行编译。编译使用的组件是org.eclipse.jdt.internal.compiler.Compiler来编译的,FlowClassLoader指定路径进行类加载。
  4. AbstractProcessRuntime#executeProcessInstance。执行流程,根据class来生成对应的实例,执行execute方法。
  5. xml的规则。节点类型有scriptTask,start,end,loopProcess,decision,autoTask。节点流转是根据transition的to属性。autoTask和scriptTask都有actionHandle,用来执行java和script语句。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值