您的位置:首页 > 编程语言 > Java开发

Drools与Spring集成 登录测试

2015-06-11 22:49 477 查看
Drools5.2.0.Final与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代码


<?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代码


<?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="com.jsptpd.rjy.zyj.pojo.Vip" />

<bean id="loginService" class="com.jsptpd.rjy.zyj.service.LoginServiceImpl" >

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

</bean>

</beans>

LoginTest.java

Java代码


package com.jsptpd.rjy.zyj.junit;

import org.drools.runtime.StatefulKnowledgeSession;

import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jsptpd.rjy.zyj.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");

}

}

LoginServiceImpl.java

Java代码


package com.jsptpd.rjy.zyj.service;

import org.drools.runtime.StatefulKnowledgeSession;

import org.drools.runtime.StatelessKnowledgeSession;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jsptpd.rjy.zyj.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

Java代码


#created on: 2011-10-24

package com.jsptpd.rjy.zyj.service

#list any import classes here.

import com.jsptpd.rjy.zyj.pojo.Vip;

import java.io.Console;

import java.util.Scanner;

import com.jsptpd.rjy.zyj.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

原文地址:/content/2605136.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: