Struts + Jboss Drools 规则引擎

xiaoxiao2026-03-25  3

                                  Struts+Jboss Drools规则引擎:一个Stock小小例子

    第一篇写在JavaEye的东西,很粗糙!

 

   

1、从try输入信息,Struts Action控制其跳转,Drools规则引擎执行规则之后将信息提供给Struts Action。

1.1 BusinessLayer类

代码如下:

package com.stock;

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

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

//加载Drools规则库 //评估该股票即可

public class BusinessLayer {       //定义规则文件字符串  public final static String file="Business.drl";  public static RuleBase businessRule;  public static RuleBase rr;    private static RuleBase readRules() throws Exception {   //读入规则文件   Reader source = new InputStreamReader(BusinessLayer.class     .getResourceAsStream(file));

  PackageBuilder builder = new PackageBuilder();

  builder.addPackageFromDrl(source);

  Package pkg = builder.getPackage();

  RuleBase ruleBase = RuleBaseFactory.newRuleBase();

  ruleBase.addPackage(pkg);

  return ruleBase;

 }    public static void evaluateStockPurchase(UserOffer stockToBuy)  {      try {    businessRule=readRules();   } catch (Exception e) {    // TODO 自动生成 catch 块    e.printStackTrace();   }          WorkingMemory workingMemory=businessRule.newStatefulSession();   workingMemory.insert(stockToBuy);      //我忘记激活规则了   System.out.println("激活规则");   System.out.println("-----------");      workingMemory.fireAllRules();  }    public static void ee()  {   try {    rr=readRules();   } catch (Exception e) {    // TODO 自动生成 catch 块    e.printStackTrace();   }      WorkingMemory work=rr.newStatefulSession();   work.insert(new UserOffer());   work.fireAllRules();   //该处代码仅供JUnit测试使用!

 } }

1.2 UserOffer类

 

package com.stock;

public class UserOffer {  private final static String Yes = "Yes";

 private final static String No = "No";

 private String stockName;

 private String stockPrice;

 private String stockQuantity;

 private String commendPurchase;

 public String getCommendPurchase() {   return commendPurchase;  }

 public void setCommendPurchase(String commendPurchase) {   this.commendPurchase = commendPurchase;  }

 public String getStockName() {   return stockName;  }

 public void setStockName(String stockName) {   this.stockName = stockName;  }

 public String getStockPrice() {   return stockPrice;  }

 public void setStockPrice(String stockPrice) {   this.stockPrice = stockPrice;  }

 public String getStockQuantity() {   return stockQuantity;  }

 public void setStockQuantity(String stockQuantity) {   this.stockQuantity = stockQuantity;  } }

3、Struts Action

/*  * Generated by MyEclipse Struts  * Template path: templates/java/JavaClass.vtl  */ package com.stock.dao.action;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;

import com.stock.BusinessLayer; import com.stock.UserOffer; import com.stock.dao.form.TryForm;

/**  * MyEclipse Struts  * Creation date: 05-13-2009  *  * XDoclet definition:  * @struts.action path="/try" name="tryForm" input="/form/try.jsp" scope="request" validate="true"  */ public class TryAction extends Action {  /*   * Generated Methods   */

 /**   * Method execute   * @param mapping   * @param form   * @param request   * @param response   * @return ActionForward   */  public ActionForward execute(ActionMapping mapping, ActionForm form,    HttpServletRequest request, HttpServletResponse response) {   TryForm tryForm = (TryForm) form;// TODO Auto-generated method stub

  UserOffer user = null;

  if (null == user) {    user = new UserOffer();   }

  //设置user的数值   //从Form获得   user.setStockName(request.getParameter("stockname"));

  //调用商业逻辑   BusinessLayer.evaluateStockPurchase(user);              //查看执行方法   BusinessLayer.ee();      if ("Yes".equals(user.getCommendPurchase())) {    return mapping.findForward("yes");   } else {    return mapping.findForward("no");   }

 } }

 

1.4 JBoss Drools 规则文件

#created on: 2009-5-13 package com.stock;

#list any import classes here. import com.stock.UserOffer;

#declare any global variables here

 

rule "Your First Rule"    when   #conditions   $s:UserOffer( stockName=="ABC")  then   #actions   #返回名字为ABC公司的名称 ,利用Action来控制   $s.setCommendPurchase("Yes");   System.out.println("规则引擎执行!");    end

rule "new rule"  when     then   System.out.println("Hello Drools,测试规则引擎用例!"); end

 

测试结果:

 

 

 

 

 

输入:ABC

 

 

 

 

 

   

 

相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-5046251.html

最新回复(0)