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

业务规则是这样的(没有实际意义,只是做demo演示)


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


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


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


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

Spring + Activiti + Drools整合的请假例子_第1张图片

一:加入maven依赖:


	UTF-8
	5.17.0
	5.6.0.Final



	
		org.drools
		drools-core
		${drools.version}
	
	
		org.drools
		drools-compiler
		${drools.version}
	
	
		org.activiti
		activiti-engine
		${activiti.version}
		provided
	
	
		org.activiti
		activiti-bpmn-layout
		${activiti.version}
		provided
	
	
		org.activiti
		activiti-spring
		${activiti.version}
		provided
	
	
		org.postgresql
		postgresql
		9.4-1201-jdbc41
	

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


spring配置文件:




	
		
		
		
		
	

	
		
	

	
		
		
		
		
			 
	              
	         
		
		
	

	
		
	

	
	
	
	
	



ppleave.bpmn



	
	
		
		
		
		
		
		
		
		
		
		
		
			= 10}]]>
		
		
			
		
		
		
	


流程图如下:

Spring + Activiti + Drools整合的请假例子_第2张图片


drools规则文件:product.drl

package com.product;
import com.lala.bean.Leave;

rule "leave1"
    when
    	u : Leave(day < 3);
    then
    	u.setTotal(u.getDay() + 2);
end

rule "leave2"
    when
    	u : Leave(day >= 3);
    then
    	u.setTotal(u.getDay() + 5);
end

Fact对象:

package com.lala.bean;

import java.io.Serializable;

public class Leave implements Serializable
{
	private static final long serialVersionUID = 1L;
	private String name;
	private Integer day; 			//当前请假天数
	private Integer total = 0;		//总共请假天数
	public Leave(String name, Integer day)
	{
		this.name = name;
		this.day = day;
	}
	public Integer getDay() 
	{
		return day;
	}
	public void setDay(Integer day)
	{
		this.day = day;
	}	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getTotal() {
		return total;
	}
	public void setTotal(Integer total) {
		this.total = total;
	}
	public String toString()
	{
		return "[name="+name+",day="+day+",total="+total+"]";
	}
}


package com.lala.service;

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

public class DroolsService implements JavaDelegate
{
	public void execute(DelegateExecution execution) throws Exception
	{	
		System.out.println("++++++++++++++++++++++++++++++++++++++");
		System.out.println(execution.getVariable("reason"));
		System.out.println("++++++++++++++++++++++++++++++++++++++");
	}
}

测试:

package com.lala.spring;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lala.bean.Leave;

public class Main 
{
	static void run(ApplicationContext context) throws Exception
	{
    	RepositoryService repositoryService = (RepositoryService)context.getBean("repositoryService");
    	
    	/**
    	 * 注意这里:必须要把drl文件一起deploy
    	 */
    	DeploymentBuilder deploy = repositoryService.createDeployment();
    	deploy.addClasspathResource("rule/product.drl");
    	deploy.addClasspathResource("bpmn/ppleave.bpmn");
    	deploy.deploy();
    	
    	RuntimeService runtimeService = (RuntimeService)context.getBean("runtimeService");
    	
    	ProcessInstance pi = runtimeService.startProcessInstanceByKey("leave");
    	
    	TaskService taskService = (TaskService)context.getBean("taskService");
    	
    	Map vars = new HashMap();  
        vars.put("leave", new Leave("白展堂", 12));
    	
        /**
         * 当前任务
         */
    	List tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
    	for(Task task : tasks)
    	{
    		System.out.println(task.getId() + " , " + task.getName());
    		taskService.complete(task.getId(), vars);
    	}
    	
    	/**
    	 * 下一步任务
    	 */
    	tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
    	for(Task task : tasks)
    	{
    		System.out.println(task.getId() + " , " + task.getName());
    	}
	}
    public static void main( String[] args )throws Exception
    {
    	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    	run(context);
    	context.close();
    }
}

运行结果:

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

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



否则,输出结果:

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

你可能感兴趣的:(框架技术)