Spring + Activiti + drools integration of leave examples

Source: Internet
Author: User
Tags cdata vars

Business rules are like this (no real meaning, just do demo demo)


If the total number of days of absence is equal to 3 days, general manager approval is required, otherwise the general manager approval is not required


If the time of absence is less than 3 days, the total number of days leave is equal to the number of days of leave +2
Otherwise, the total number of days leave is equal to the number of times to leave +5


The calculation logic of the total number of leave is given to drools processing


Create a new MAVEN project with the following directory structure:


One: Join maven dependencies:

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>< Activiti.version>5.17.0</activiti.version><drools.version>5.6.0.final</drools.version> </properties><dependencies><dependency><groupId>org.drools</groupId>< Artifactid>drools-core</artifactid><version>${drools.version}</version></dependency ><dependency><groupid>org.drools</groupid><artifactid>drools-compiler</ Artifactid><version>${drools.version}</version></dependency><dependency><groupid >org.activiti</groupid><artifactid>activiti-engine</artifactid><version>${ activiti.version}</version><scope>provided</scope></dependency><dependency>< Groupid>org.activiti</groupid><artifactid>activiti-bpmn-layout</artifactid><version >${activiti.version}</version><scope>provided</scope></dependency><dependency><groupid>org.activiti</groupid>< Artifactid>activiti-spring</artifactid><version>${activiti.version}</version><scope >provided</scope></dependency><dependency><groupId>org.postgresql</groupId> <artifactid>postgresql</artifactid><version>9.4-1201-jdbc41</version></dependency ></dependencies>

Drools seems unable to use the 6.x version of


Spring configuration file:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "><bean id=" DataSource "class=" Org.springframework.jdbc.datasource.SimpleDriverDataSource "><property name=" Driverclass "value=" Org.postgresql.Driver "/><property name=" url "value=" jdbc:postgresql://127.0.0.1/activiti17 "/>< Property name= "username" value= "admin"/><property name= "password" value= "root"/></bean><bean id= " TransactionManager "class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager "><property Name= "DataSource" ref= "DataSource"/></bean><bean id= "processengineconfiguration" class= " Org.activiti.spring.SpringProcessEngineConfiguration "><property name=" DataSource "ref=" DataSource "/> <property name= "TransactIonmanager "ref=" TransactionManager "/><property name=" Databaseschemaupdate "value=" true "/><property Name= "Custompostdeployers" ><list> <bean class= "Org.activiti.engine.impl.rules.RulesDeployer"/&G        T </list> </property><!--<property name= "deploymentresources" value= "CLASSPATH*:/BPMN/*.BPMN"/ >--></bean><bean id= "processengine" class= "Org.activiti.spring.ProcessEngineFactoryBean" >< Property Name= "Processengineconfiguration" ref= "processengineconfiguration"/></bean><bean id= " Repositoryservice "factory-bean=" Processengine "factory-method=" Getrepositoryservice "/><bean id=" Runtimeservice "factory-bean=" Processengine "factory-method=" Getruntimeservice "/><bean id=" TaskService " Factory-bean= "Processengine" factory-method= "Gettaskservice"/><bean id= "Historyservice" factory-bean= " Processengine "factory-method=" Gethistoryservice "/><bean id=" Managementservice "factory-beAn= "Processengine" factory-method= "Getmanagementservice"/></beans> 

Ppleave.bpmn

<?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: Activiti= "HTTP://ACTIVITI.ORG/BPMN" 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 "typelanguage="/HTTP/ Www.w3.org/2001/XMLSchema "expressionlanguage=" Http://www.w3.org/1999/XPath "targetnamespace="/HTTP/ Www.activiti.org/test "><process id=" Leave "name=" Leave Approval "isexecutable=" true "><startevent id=" startevent1 "Name=" Start "></startevent><endevent id=" Endevent1 "name=" End "></endevent><usertask id=" Usertask1 "name=" department manager approval "></usertask><businessruletask id=" Businessruletask1 "name=" Days Judging "Activiti: rulevariablesinput= "${leave}" activiti:rules= "Leave1,leave2" activiti:resultvariable= "Reason" ></ Businessruletask><serviCetask id= "Servicetask1" name= "Get Variable" activiti:class= "Com.lala.service.DroolsService" ></serviceTask>< Usertask id= "Usertask2" name= "HR Approval" ></usertask><sequenceflow id= "Flow1" sourceref= "Startevent1" targetref= "Usertask1" ></sequenceflow><sequenceflow id= "Flow2" sourceref= "Usertask1" targetref= " Businessruletask1 "></sequenceflow><sequenceflow id=" flow3 "sourceref=" Businessruletask1 "targetref=" Servicetask1 "></sequenceflow><usertask id=" Usertask3 "Name=" general manager approval "></userTask>< Sequenceflow id= "flow4" sourceref= "Servicetask1" targetref= "Usertask3" ><conditionexpression xsi:type= " Tformalexpression "><! [Cdata[${reason[0].total >= 10}]]></conditionexpression></sequenceflow><sequenceflow id=] Flow5 "sourceref=" Servicetask1 "targetref=" Usertask2 "><conditionexpression xsi:type=" Tformalexpression "> <! [Cdata[${reason[0].total < 10}]]></conditionexpression></sequenceflow><seqUenceflow id= "flow6" sourceref= "Usertask3" targetref= "Usertask2" ></sequenceflow><sequenceflow id= " Flow7 "sourceref=" Usertask2 "targetref=" Endevent1 "></sequenceFlow></process></definitions>


The flowchart is as follows:



Drools Rule file: PRODUCT.DRL

Package Com.product;import com.lala.bean.leave;rule ' leave1 '    when    u:leave (Day < 3);    Then    u.settotal (U.getday () + 2), endrule "Leave2"    when    u:leave (Day >= 3);    Then    u.settotal (U.getday () + 5); end

Fact object:

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; The current day of leave private Integer total = 0;//number of days to leave 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 ("++++++++++++++++++++++++++++++++++++++");}}

Test:

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");    /** * Note here: You must deploy the DRL file together/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<string, object> vars = new hashmap<string, object> ();            Vars.put ("Leave", New Leave ("Bai Zhantong", 12)); /** * Current Task */list<task> tasks = Taskservice.createtaskquery (). Processinstanceid (Pi.getid ()). List    ();    for (Task task:tasks) {System.out.println (Task.getid () + "," + task.getname ());    Taskservice.complete (Task.getid (), VARs);    }/** * Next task * * 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 CLASSP    Athxmlapplicationcontext ("Applicationcontext.xml");    Run (context); Context.cloSE (); }}

Operation Result:

When the number of days to leave is less than 3 days, the output:

5009, department manager approval
++++++++++++++++++++++++++++++++++++++
[[Name= Bai Zhantong, day=2,total=4]]
++++++++++++++++++++++++++++++++++++++
5019, HR approval



Otherwise, the output:

7509, department manager approval
++++++++++++++++++++++++++++++++++++++
[[Name= Bai Zhantong, day=22,total=27]]
++++++++++++++++++++++++++++++++++++++
7519, general manager approval

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring + Activiti + drools integration of leave examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.