Spring + Activiti + Drools整合的请假例子(转)

Spring + Activiti + Drools整合的请假例子



如果请假总天数大于等于3天,则需要总经理审批,否则不需要总经理审批


如果当次请假小于3天,则请假总天数等于当次请假天数+2
否则,请假总天数等于当次请假次数+5
 

其中,总的请假次数的计算逻辑交给drools处理

新建maven项目,目录结构如下:

一:加入maven依赖:

[html] view plain copy

  1. <properties>  
  2.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  3.     <activiti.version>5.17.0</activiti.version>  
  4.     <drools.version>5.6.0.Final</drools.version>  
  5. </properties>  
  6.   
  7. <dependencies>  
  8.     <dependency>  
  9.         <groupId>org.drools</groupId>  
  10.         <artifactId>drools-core</artifactId>  
  11.         <version>${drools.version}</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.         <groupId>org.drools</groupId>  
  15.         <artifactId>drools-compiler</artifactId>  
  16.         <version>${drools.version}</version>  
  17.     </dependency>  
  18.     <dependency>  
  19.         <groupId>org.activiti</groupId>  
  20.         <artifactId>activiti-engine</artifactId>  
  21.         <version>${activiti.version}</version>  
  22.         <scope>provided</scope>  
  23.     </dependency>  
  24.     <dependency>  
  25.         <groupId>org.activiti</groupId>  
  26.         <artifactId>activiti-bpmn-layout</artifactId>  
  27.         <version>${activiti.version}</version>  
  28.         <scope>provided</scope>  
  29.     </dependency>  
  30.     <dependency>  
  31.         <groupId>org.activiti</groupId>  
  32.         <artifactId>activiti-spring</artifactId>  
  33.         <version>${activiti.version}</version>  
  34.         <scope>provided</scope>  
  35.     </dependency>  
  36.     <dependency>  
  37.         <groupId>org.postgresql</groupId>  
  38.         <artifactId>postgresql</artifactId>  
  39.         <version>9.4-1201-jdbc41</version>  
  40.     </dependency>  
  41. </dependencies>  
 
  1. <properties>

  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    扫描二维码关注公众号,回复: 4350734 查看本文章
  3. <activiti.version>5.17.0</activiti.version>

  4. <drools.version>5.6.0.Final</drools.version>

  5. </properties>

  6.  
  7. <dependencies>

  8. <dependency>

  9. <groupId>org.drools</groupId>

  10. <artifactId>drools-core</artifactId>

  11. <version>${drools.version}</version>

  12. </dependency>

  13. <dependency>

  14. <groupId>org.drools</groupId>

  15. <artifactId>drools-compiler</artifactId>

  16. <version>${drools.version}</version>

  17. </dependency>

  18. <dependency>

  19. <groupId>org.activiti</groupId>

  20. <artifactId>activiti-engine</artifactId>

  21. <version>${activiti.version}</version>

  22. <scope>provided</scope>

  23. </dependency>

  24. <dependency>

  25. <groupId>org.activiti</groupId>

  26. <artifactId>activiti-bpmn-layout</artifactId>

  27. <version>${activiti.version}</version>

  28. <scope>provided</scope>

  29. </dependency>

  30. <dependency>

  31. <groupId>org.activiti</groupId>

  32. <artifactId>activiti-spring</artifactId>

  33. <version>${activiti.version}</version>

  34. <scope>provided</scope>

  35. </dependency>

  36. <dependency>

  37. <groupId>org.postgresql</groupId>

  38. <artifactId>postgresql</artifactId>

  39. <version>9.4-1201-jdbc41</version>

  40. </dependency>

  41. </dependencies>


drools貌似不能用6.x版本的

spring配置文件:

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation="  
  5.        http://www.springframework.org/schema/beans   
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd">  
  7.   
  8.     <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
  9.         <property name="driverClass" value="org.postgresql.Driver" />  
  10.         <property name="url" value="jdbc:postgresql://127.0.0.1/activiti17" />  
  11.         <property name="username" value="admin" />  
  12.         <property name="password" value="root" />  
  13.     </bean>  
  14.   
  15.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  16.         <property name="dataSource" ref="dataSource" />  
  17.     </bean>  
  18.   
  19.     <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
  20.         <property name="dataSource" ref="dataSource" />  
  21.         <property name="transactionManager" ref="transactionManager" />  
  22.         <property name="databaseSchemaUpdate" value="true" />  
  23.         <property name="customPostDeployers">  
  24.             <list>   
  25.                   <bean class="org.activiti.engine.impl.rules.RulesDeployer" />  
  26.             </list>   
  27.         </property>  
  28.         <!--  
  29.             <property name="deploymentResources" value="classpath*:/bpmn/*.bpmn" />   
  30.          -->  
  31.     </bean>  
  32.   
  33.     <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
  34.         <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
  35.     </bean>  
  36.   
  37.     <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
  38.     <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
  39.     <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
  40.     <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
  41.     <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />  
  42.   
  43. </beans>  
 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xsi:schemaLocation="

  5. http://www.springframework.org/schema/beans

  6. http://www.springframework.org/schema/beans/spring-beans.xsd">

  7.  
  8. <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">

  9. <property name="driverClass" value="org.postgresql.Driver" />

  10. <property name="url" value="jdbc:postgresql://127.0.0.1/activiti17" />

  11. <property name="username" value="admin" />

  12. <property name="password" value="root" />

  13. </bean>

  14.  
  15. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  16. <property name="dataSource" ref="dataSource" />

  17. </bean>

  18.  
  19. <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">

  20. <property name="dataSource" ref="dataSource" />

  21. <property name="transactionManager" ref="transactionManager" />

  22. <property name="databaseSchemaUpdate" value="true" />

  23. <property name="customPostDeployers">

  24. <list>

  25. <bean class="org.activiti.engine.impl.rules.RulesDeployer" />

  26. </list>

  27. </property>

  28. <!--

  29. <property name="deploymentResources" value="classpath*:/bpmn/*.bpmn" />

  30. -->

  31. </bean>

  32.  
  33. <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">

  34. <property name="processEngineConfiguration" ref="processEngineConfiguration" />

  35. </bean>

  36.  
  37. <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />

  38. <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />

  39. <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />

  40. <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />

  41. <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

  42.  
  43. </beans>


ppleave.bpmn

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  4.     xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"  
  5.     xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"  
  6.     typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"  
  7.     targetNamespace="http://www.activiti.org/test">  
  8.       
  9.     <process id="leave" name="请假审批" isExecutable="true">  
  10.         <startEvent id="startevent1" name="Start"></startEvent>  
  11.         <endEvent id="endevent1" name="End"></endEvent>  
  12.         <userTask id="usertask1" name="部门经理审批"></userTask>  
  13.         <businessRuleTask id="businessruletask1" name="天数判断" activiti:ruleVariablesInput="${leave}" activiti:rules="leave1,leave2" activiti:resultVariable="reason"></businessRuleTask>  
  14.         <serviceTask id="servicetask1" name="获取变量" activiti:class="com.lala.service.DroolsService"></serviceTask>  
  15.         <userTask id="usertask2" name="HR审批"></userTask>  
  16.         <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>  
  17.         <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="businessruletask1"></sequenceFlow>  
  18.         <sequenceFlow id="flow3" sourceRef="businessruletask1" targetRef="servicetask1"></sequenceFlow>  
  19.         <userTask id="usertask3" name="总经理审批"></userTask>  
  20.         <sequenceFlow id="flow4" sourceRef="servicetask1" targetRef="usertask3">  
  21.             <conditionExpression xsi:type="tFormalExpression"><![CDATA[${reason[0].total >= 10}]]></conditionExpression>  
  22.         </sequenceFlow>  
  23.         <sequenceFlow id="flow5" sourceRef="servicetask1" targetRef="usertask2">  
  24.             <conditionExpression xsi:type="tFormalExpression"><![CDATA[${reason[0].total < 10}]]></conditionExpression>  
  25.         </sequenceFlow>  
  26.         <sequenceFlow id="flow6" sourceRef="usertask3" targetRef="usertask2"></sequenceFlow>  
  27.         <sequenceFlow id="flow7" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>  
  28.     </process>  
  29. </definitions>  
 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

  4. xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"

  5. xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"

  6. typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"

  7. targetNamespace="http://www.activiti.org/test">

  8.  
  9. <process id="leave" name="请假审批" isExecutable="true">

  10. <startEvent id="startevent1" name="Start"></startEvent>

  11. <endEvent id="endevent1" name="End"></endEvent>

  12. <userTask id="usertask1" name="部门经理审批"></userTask>

  13. <businessRuleTask id="businessruletask1" name="天数判断" activiti:ruleVariablesInput="${leave}" activiti:rules="leave1,leave2" activiti:resultVariable="reason"></businessRuleTask>

  14. <serviceTask id="servicetask1" name="获取变量" activiti:class="com.lala.service.DroolsService"></serviceTask>

  15. <userTask id="usertask2" name="HR审批"></userTask>

  16. <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>

  17. <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="businessruletask1"></sequenceFlow>

  18. <sequenceFlow id="flow3" sourceRef="businessruletask1" targetRef="servicetask1"></sequenceFlow>

  19. <userTask id="usertask3" name="总经理审批"></userTask>

  20. <sequenceFlow id="flow4" sourceRef="servicetask1" targetRef="usertask3">

  21. <conditionExpression xsi:type="tFormalExpression"><![CDATA[${reason[0].total >= 10}]]></conditionExpression>

  22. </sequenceFlow>

  23. <sequenceFlow id="flow5" sourceRef="servicetask1" targetRef="usertask2">

  24. <conditionExpression xsi:type="tFormalExpression"><![CDATA[${reason[0].total < 10}]]></conditionExpression>

  25. </sequenceFlow>

  26. <sequenceFlow id="flow6" sourceRef="usertask3" targetRef="usertask2"></sequenceFlow>

  27. <sequenceFlow id="flow7" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>

  28. </process>

  29. </definitions>

流程图如下:

drools规则文件:product.drl

[plain] view plain copy

  1. package com.product;  
  2. import com.lala.bean.Leave;  
  3.   
  4. rule "leave1"  
  5.     when  
  6.         u : Leave(day < 3);  
  7.     then  
  8.         u.setTotal(u.getDay() + 2);  
  9. end  
  10.   
  11. rule "leave2"  
  12.     when  
  13.         u : Leave(day >= 3);  
  14.     then  
  15.         u.setTotal(u.getDay() + 5);  
  16. end  
 
  1. package com.product;

  2. import com.lala.bean.Leave;

  3.  
  4. rule "leave1"

  5. when

  6. u : Leave(day < 3);

  7. then

  8. u.setTotal(u.getDay() + 2);

  9. end

  10.  
  11. rule "leave2"

  12. when

  13. u : Leave(day >= 3);

  14. then

  15. u.setTotal(u.getDay() + 5);

  16. end


Fact对象:

[java] view plain copy

  1. package com.lala.bean;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Leave implements Serializable  
  6. {  
  7.     private static final long serialVersionUID = 1L;  
  8.     private String name;  
  9.     private Integer day;            //当前请假天数  
  10.     private Integer total = 0;      //总共请假天数  
  11.     public Leave(String name, Integer day)  
  12.     {  
  13.         this.name = name;  
  14.         this.day = day;  
  15.     }  
  16.     public Integer getDay()   
  17.     {  
  18.         return day;  
  19.     }  
  20.     public void setDay(Integer day)  
  21.     {  
  22.         this.day = day;  
  23.     }     
  24.     public String getName() {  
  25.         return name;  
  26.     }  
  27.     public void setName(String name) {  
  28.         this.name = name;  
  29.     }  
  30.     public Integer getTotal() {  
  31.         return total;  
  32.     }  
  33.     public void setTotal(Integer total) {  
  34.         this.total = total;  
  35.     }  
  36.     public String toString()  
  37.     {  
  38.         return "[name="+name+",day="+day+",total="+total+"]";  
  39.     }  
  40. }  
 
  1. package com.lala.bean;

  2.  
  3. import java.io.Serializable;

  4.  
  5. public class Leave implements Serializable

  6. {

  7. private static final long serialVersionUID = 1L;

  8. private String name;

  9. private Integer day; //当前请假天数

  10. private Integer total = 0; //总共请假天数

  11. public Leave(String name, Integer day)

  12. {

  13. this.name = name;

  14. this.day = day;

  15. }

  16. public Integer getDay()

  17. {

  18. return day;

  19. }

  20. public void setDay(Integer day)

  21. {

  22. this.day = day;

  23. }

  24. public String getName() {

  25. return name;

  26. }

  27. public void setName(String name) {

  28. this.name = name;

  29. }

  30. public Integer getTotal() {

  31. return total;

  32. }

  33. public void setTotal(Integer total) {

  34. this.total = total;

  35. }

  36. public String toString()

  37. {

  38. return "[name="+name+",day="+day+",total="+total+"]";

  39. }

  40. }

[java] view plain copy

  1. package com.lala.service;  
  2.   
  3. import org.activiti.engine.delegate.DelegateExecution;  
  4. import org.activiti.engine.delegate.JavaDelegate;  
  5.   
  6. public class DroolsService implements JavaDelegate  
  7. {  
  8.     public void execute(DelegateExecution execution) throws Exception  
  9.     {     
  10.         System.out.println("++++++++++++++++++++++++++++++++++++++");  
  11.         System.out.println(execution.getVariable("reason"));  
  12.         System.out.println("++++++++++++++++++++++++++++++++++++++");  
  13.     }  
  14. }  
 
  1. package com.lala.service;

  2.  
  3. import org.activiti.engine.delegate.DelegateExecution;

  4. import org.activiti.engine.delegate.JavaDelegate;

  5.  
  6. public class DroolsService implements JavaDelegate

  7. {

  8. public void execute(DelegateExecution execution) throws Exception

  9. {

  10. System.out.println("++++++++++++++++++++++++++++++++++++++");

  11. System.out.println(execution.getVariable("reason"));

  12. System.out.println("++++++++++++++++++++++++++++++++++++++");

  13. }

  14. }


测试:

[java] view plain copy

  1. package com.lala.spring;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.activiti.engine.RepositoryService;  
  8. import org.activiti.engine.RuntimeService;  
  9. import org.activiti.engine.TaskService;  
  10. import org.activiti.engine.repository.DeploymentBuilder;  
  11. import org.activiti.engine.runtime.ProcessInstance;  
  12. import org.activiti.engine.task.Task;  
  13. import org.springframework.context.ApplicationContext;  
  14. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  15.   
  16. import com.lala.bean.Leave;  
  17.   
  18. public class Main   
  19. {  
  20.     static void run(ApplicationContext context) throws Exception  
  21.     {  
  22.         RepositoryService repositoryService = (RepositoryService)context.getBean("repositoryService");  
  23.           
  24.         /** 
  25.          * 注意这里:必须要把drl文件一起deploy 
  26.          */  
  27.         DeploymentBuilder deploy = repositoryService.createDeployment();  
  28.         deploy.addClasspathResource("rule/product.drl");  
  29.         deploy.addClasspathResource("bpmn/ppleave.bpmn");  
  30.         deploy.deploy();  
  31.           
  32.         RuntimeService runtimeService = (RuntimeService)context.getBean("runtimeService");  
  33.           
  34.         ProcessInstance pi = runtimeService.startProcessInstanceByKey("leave");  
  35.           
  36.         TaskService taskService = (TaskService)context.getBean("taskService");  
  37.           
  38.         Map<String, Object> vars = new HashMap<String, Object>();    
  39.         vars.put("leave", new Leave("白展堂", 12));  
  40.           
  41.         /** 
  42.          * 当前任务 
  43.          */  
  44.         List<Task> tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();  
  45.         for(Task task : tasks)  
  46.         {  
  47.             System.out.println(task.getId() + " , " + task.getName());  
  48.             taskService.complete(task.getId(), vars);  
  49.         }  
  50.           
  51.         /** 
  52.          * 下一步任务 
  53.          */  
  54.         tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();  
  55.         for(Task task : tasks)  
  56.         {  
  57.             System.out.println(task.getId() + " , " + task.getName());  
  58.         }  
  59.     }  
  60.     public static void main( String[] args )throws Exception  
  61.     {  
  62.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  63.         run(context);  
  64.         context.close();  
  65.     }  
  66. }  
 
  1. package com.lala.spring;

  2.  
  3. import java.util.HashMap;

  4. import java.util.List;

  5. import java.util.Map;

  6.  
  7. import org.activiti.engine.RepositoryService;

  8. import org.activiti.engine.RuntimeService;

  9. import org.activiti.engine.TaskService;

  10. import org.activiti.engine.repository.DeploymentBuilder;

  11. import org.activiti.engine.runtime.ProcessInstance;

  12. import org.activiti.engine.task.Task;

  13. import org.springframework.context.ApplicationContext;

  14. import org.springframework.context.support.ClassPathXmlApplicationContext;

  15.  
  16. import com.lala.bean.Leave;

  17.  
  18. public class Main

  19. {

  20. static void run(ApplicationContext context) throws Exception

  21. {

  22. RepositoryService repositoryService = (RepositoryService)context.getBean("repositoryService");

  23.  
  24. /**

  25. * 注意这里:必须要把drl文件一起deploy

  26. */

  27. DeploymentBuilder deploy = repositoryService.createDeployment();

  28. deploy.addClasspathResource("rule/product.drl");

  29. deploy.addClasspathResource("bpmn/ppleave.bpmn");

  30. deploy.deploy();

  31.  
  32. RuntimeService runtimeService = (RuntimeService)context.getBean("runtimeService");

  33.  
  34. ProcessInstance pi = runtimeService.startProcessInstanceByKey("leave");

  35.  
  36. TaskService taskService = (TaskService)context.getBean("taskService");

  37.  
  38. Map<String, Object> vars = new HashMap<String, Object>();

  39. vars.put("leave", new Leave("白展堂", 12));

  40.  
  41. /**

  42. * 当前任务

  43. */

  44. List<Task> tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();

  45. for(Task task : tasks)

  46. {

  47. System.out.println(task.getId() + " , " + task.getName());

  48. taskService.complete(task.getId(), vars);

  49. }

  50.  
  51. /**

  52. * 下一步任务

  53. */

  54. tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();

  55. for(Task task : tasks)

  56. {

  57. System.out.println(task.getId() + " , " + task.getName());

  58. }

  59. }

  60. public static void main( String[] args )throws Exception

  61. {

  62. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

  63. run(context);

  64. context.close();

  65. }

  66. }


运行结果:

请假天数小于3天时,输出结果:

5009 , 部门经理审批
++++++++++++++++++++++++++++++++++++++
[[name=白展堂,day=2,total=4]]
++++++++++++++++++++++++++++++++++++++
5019 , HR审批

否则,输出结果:

7509 , 部门经理审批
++++++++++++++++++++++++++++++++++++++
[[name=白展堂,day=22,total=27]]
++++++++++++++++++++++++++++++++++++++
7519 , 总经理审批

猜你喜欢

转载自blog.csdn.net/rainyear/article/details/83016656
今日推荐