基于Drools 规则引擎的信用卡申请系统 之 后台代码

 

Beans设计,很简单都是基本的setter / getter方法

 

package com.chen.beans;

public class CardInfo {
 // 该类存放信用卡信息
 private String bankName;

 private int value;

 private User user;

 public String getBankName() {
  return bankName;
 }

 public void setBankName(String bankName) {
  this.bankName = bankName;
 }

 public User getUser() {
  return user;
 }

 public void setUser(User user) {
  this.user = user;
 }

 public int getValue() {
  return value;
 }

 public void setValue(int value) {
  this.value = value;
 }

}

package com.chen.beans;

public class User {
 
    private String username;
   
    private String degree;
   
    private String hasCar;
   
    private String hasHouse;
   
    private String hasCredit;
   
    private float salary;
   
    //是否通过
    private boolean approve=true;// 默认通过
   
    private CardInfo cardInfo;
   
   
 public String getDegree() {
  return degree;
 }

 public void setDegree(String degree) {
  this.degree = degree;
 }

 

 public String getHasCar() {
  return hasCar;
 }

 public void setHasCar(String hasCar) {
  this.hasCar = hasCar;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public boolean isApprove() {
  return approve;
 }

 public void setApprove(boolean approve) {
  this.approve = approve;
 }

 public CardInfo getCardInfo() {
  return cardInfo;
 }

 public void setCardInfo(CardInfo cardInfo) {
  this.cardInfo = cardInfo;
 }

 public String getHasCredit() {
  return hasCredit;
 }

 public void setHasCredit(String hasCredit) {
  this.hasCredit = hasCredit;
 }

 public String getHasHouse() {
  return hasHouse;
 }

 public void setHasHouse(String hasHouse) {
  this.hasHouse = hasHouse;
 }

 public float getSalary() {
  return salary;
 }

 public void setSalary(float salary) {
  this.salary = salary;
 }
}

 

Part II 使用Drools进行推理

 

package com.chen.drools;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.agent.RuleAgent;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;


public class WorkingMemoryHelper {
 private RuleBase ruleBase;

 private WorkingMemory workingMemory;

 public RuleBase getRuleBase(){
  
  //方法一 RuleAgent的部署方式,如果不行,再采取第一种方式
/*  RuleAgent agent=RuleAgent.newRuleAgent("/deploy2.properties"); //一个属性文件
  
  ruleBase=agent.getRuleBase();*/
  
  //方法选择二 package方法
  Reader resource=new InputStreamReader(WorkingMemoryHelper.class.getResourceAsStream("/rules.drl"));
  
  PackageBuilder builder=new PackageBuilder();
  try {
   builder.addPackageFromDrl(resource);
  } catch (DroolsParserException e) {

   e.printStackTrace();
  } catch (IOException e) {

   e.printStackTrace();
  }
 
  
  //第二个drl 开始
        resource=new InputStreamReader(WorkingMemoryHelper.class.getResourceAsStream("/payment.drl"));
  try {
   builder.addPackageFromDrl(resource);
  } catch (DroolsParserException e1) {
   
   e1.printStackTrace();
  } catch (IOException e1) {
   
   e1.printStackTrace();
  }
 
  //第二个drl结束
  
  //添加一个规则流
  //开始
  resource=new InputStreamReader(WorkingMemoryHelper.class.getResourceAsStream("/MyFlow.rfm"));
  builder.addRuleFlow(resource);
  //结束
  
  Package pkg=builder.getPackage();
  
  RuleBase ruleBase=RuleBaseFactory.newRuleBase();
  try {
   ruleBase.addPackage(pkg);
  } catch (Exception e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
 
  
  return ruleBase;
 }

 public WorkingMemory getWorkingMemory()  {
  
  if(ruleBase==null)
  {
   ruleBase=getRuleBase();
  }
  workingMemory=ruleBase.newStatefulSession();
  
  return workingMemory;
 }

}

 

后台处理之:Struts 2,如下清单:

 

package com.chen.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.drools.WorkingMemory;

import com.chen.beans.CardInfo;
import com.chen.beans.User;
import com.chen.drools.WorkingMemoryHelper;
import com.opensymphony.xwork2.ActionSupport;

public class Apply extends ActionSupport {

 /**
  *
  */

 private boolean success;

 private String msg;

 private static final long serialVersionUID = 4042346926789559143L;

 public User user = new User();
 CardInfo card=new CardInfo();
 
 //定义工作引擎
 private WorkingMemory workingMemory;
 
 //Helper 方法二
 WorkingMemoryHelper wh=new WorkingMemoryHelper();
 
 //方法一
 //WMHelper hp=new WMHelper();

 public String execute() throws Exception {

  HttpServletRequest request = ServletActionContext.getRequest();
       
  //用户名:
  user.setUsername(request.getParameter("username"));

  //学历:
  user.setDegree(request.getParameter("degree"));
        //车子
  user.setHasCar(request.getParameter("hasCar"));
  //房子
  user.setHasHouse(request.getParameter("hasHouse"));
  
  //有信用卡
  user.setHasCredit(request.getParameter("hasCredit"));
  //收入
  user.setSalary(Float.parseFloat(request.getParameter("salary")));
  
    
  System.out.println(user.getUsername());
       
      
  //危险开始
  //取得workingMemory
  workingMemory=wh.getWorkingMemory();
  
  //将事实插入workingMemory
  workingMemory.insert(user);
  workingMemory.insert(card);
  
  //激活规则流
  workingMemory.startProcess("myflow");
  
  //激活规则
  workingMemory.fireAllRules();
  
  //关闭
  //workingMemory.clearAgenda();
  
  //危险结束
       
  //从规则引擎返回信息 判断是否申请成功
  
  //加入信用卡信息
  System.out.println("测试payment"+card.getBankName());
  
  if (user.isApprove()==true) {
   success = true;
   msg = user.getUsername()+"成功申请了"+card.getBankName()+card.getValue();
  } else {
   success = false;
   msg = "申请失败";

  }

  return this.SUCCESS;

 }

 public boolean isSuccess() {
  return success;
 }

 public void setSuccess(boolean success) {
  this.success = success;
 }

 public String getMsg() {
  return msg;
 }

 public void setMsg(String msg) {
  this.msg = msg;
 }
}

 

你可能感兴趣的:(Drools)