如何使用Drools5和Spring3进行集成详解编程语言

如何使用Drools5和Spring3进行集成详解编程语言

在drools5.2,有一个jar包:drools-spring-5.2.0.Final.jar,其中定义了在spring中应用的drools的扩展。通过这些扩展,可以直接在spring的配置文件中,配置knowledgebase、session等bean,从而在spring配置的程序中直接应用。drools-spring-5.2.0.Final.jar在droolsjbpm-integration-distribution-5.2.0.Final/binaries文件夹下。


一.配置文件:beans.xml

<?xml version=”1.0″ encoding=”UTF-8″?>  

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

    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:context=”http://www.springframework.org/schema/context”  

    xmlns:aop=”http://www.springframework.org/schema/aop” xmlns:tx=”http://www.springframework.org/schema/tx”  

    xmlns:p=”http://www.springframework.org/schema/p”  

    xsi:schemaLocation=”http://www.springframework.org/schema/beans     

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     

        http://www.springframework.org/schema/context     

         http://www.springframework.org/schema/context/spring-context-3.0.xsd     

        http://www.springframework.org/schema/tx     

        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     

        http://www.springframework.org/schema/aop      

         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd”>   

     <import resource=”classpath:com/jsptpd/rjy/zyj/drools/beans-drools.xml”/>  

</beans>  


二.配置文件:beans-drools.xml

<?xml version=”1.0″ encoding=”UTF-8″?>  

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

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

       xmlns:drools=”http://drools.org/schema/drools-spring”   

       xmlns:camel=”http://camel.apache.org/schema/spring”  

       xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  

                           http://drools.org/schema/drools-spring http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd  

                           http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd”>  

  <drools:kbase id=”kbase1″>  

     <drools:resources>  

          <!–不是<drools:resource type=”DRL” source=”classpath:com/jsptpd/rjy/zyj/service/Login.drl”/> –>  

         <drools:resource type=”DRL” source=”classpath:Login.drl”/>  

     </drools:resources>  

  </drools:kbase>  

  <drools:ksession id=”ksession1″ type=”stateful” kbase=”kbase1″/>  

   <bean id=”vip” class=”net.itxm.pojo.Vip” />  

   <bean id=”loginService” class=”net.itxm.service.LoginServiceImpl” >  

        <property name=”vip” ref=”vip” />  

   </bean>  

</beans>  


三.java代码示例文件:LoginTest.java

package net.itxm.junit;  

import org.drools.runtime.StatefulKnowledgeSession;  

import org.junit.Test;  

import org.springframework.context.support.ClassPathXmlApplicationContext;  

import net.itxm.service.LoginServiceImpl;  

public class LoginTest {  

    @Test  

    public void testLogin(){  

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( “beans.xml” );  

        LoginServiceImpl loginServiceImpl= (LoginServiceImpl) context.getBean( “loginService” );  

        StatefulKnowledgeSession kstateless = (StatefulKnowledgeSession) context.getBean( “ksession1” );  

        loginServiceImpl.checkLogin(kstateless);  

        System.out.println(“aa”);  

    }  

}  


四.java代码示例文件:LoginServiceImpl.java

package net.itxm.service;  

import org.drools.runtime.StatefulKnowledgeSession;  

import org.drools.runtime.StatelessKnowledgeSession;  

import org.springframework.context.support.ClassPathXmlApplicationContext;  

import net.itxm.pojo.Vip;  

public class LoginServiceImpl {  

        private Vip vip;  

        public Vip getVip() {  

            return vip;  

        }  

        public void setVip(Vip vip) {  

            this.vip = vip;  

        }  

        public void checkLogin(StatefulKnowledgeSession kstateless ){  

            System.out.println(“s”);  

            kstateless.insert(vip);  

            kstateless.fireAllRules();  

            kstateless.dispose();  

            System.out.println(“e”);  

        }  

        public static boolean checkDB(String name,String password){  

            //实际可以到数据库匹配  

            return name.trim().equals(“jack”)&&password.trim().equals(“123”);  

        }  

}  


五.规则文件:Login.drl

package net.itxm.service  

#list any import classes here.  

import net.itxm.pojo.Vip;  

import java.io.Console;  

import java.util.Scanner;  

import net.itxm.service.LoginServiceImpl  

#declare any global variables here  

rule “vip初次登录”  

    salience 100  

    when  

        $vip:Vip((name==null||name==””)&&  

                 (password==null||password==””) )  

    then  

        String tempName;  

        String tempPassword;  

        Console console=System.console();  

        Scanner scanner = new Scanner(System.in);  

        System.out.print(“请输入用户名: “);     

        tempName=(console!=null?console.readLine():scanner.nextLine());  

        System.out.print(“请输入密码: “);  

        tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());  

        $vip.setName(tempName.trim());  

        $vip.setPassword(tempPassword.trim());  

        update($vip);  

end  

rule “没有输入密码”  

    salience  90  

    when  

       $vip:Vip((name!=null&&name!=””)&&  

                 (password==null||password==””),$name:name)  

    then  

        String tempPassword=””;  

        Console console=System.console();  

        Scanner scanner = new Scanner(System.in);  

        System.out.print(“请输入密码: “);  

        tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());  

        $vip.setPassword(tempPassword.trim());  

        update($vip);  

end  

rule “没有输入用户名”  

    salience  90  

    when  

       $vip:Vip((name==null||name==””)&&  

                 (password!=null&&password!=””),$password:password )  

    then  

        String tempName=””;  

        Scanner scanner = new Scanner(System.in);  

        System.out.print(“请输入用户名: “);     

        tempName=scanner.nextLine();  

        $vip.setName(tempName.trim());  

        update($vip);  

end  

rule “输入正确的用户名和密码”  

    salience  80  

    when  

       $vip:Vip((name!=null&&name!=””),  

                 (password!=null&&password!=””),LoginServiceImpl.checkDB(name,password) )  

    then  

        System.out.print(” 欢迎 !!!”+$vip.getName());   

end  

rule “输入错误的用户名和密码”  

    salience  80  

    when  

       $vip:Vip((name!=null&&name!=””),  

                 (password!=null&&password!=””),!LoginServiceImpl.checkDB(name,password) )  

    then  

        System.out.print(” 输入错误用户名或密码,请重新输入 !!!/n”);      

        $vip.setName(“”);  

        $vip.setPassword(“”);  

        update($vip);  

end  

如何使用Drools5和Spring3进行集成详解编程语言

转载请注明来源网站:blog.ytso.com谢谢!

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/14626.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论