
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 BusinessLayer;
/**
* Sample Struts action with Pseudocode
* 使用偽代碼的Struts行為示例
*/
public class SampleStrutsAction extends Action{
/**
* Standard Struts doPerfom method
* 標準的Struts doPerform方法
*/
public ActionForward doPerform(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws InvalidEntryPointException {
//Local Variables
//本地變量
StockOffer userOffer =null;
//Get any previous values from the session
//從session取得以前的數據
userOffer=(StockOffer)request.getSession()
.getAttribute("PREVIOUS_STOCK_OFFER");
//create this object if it is null
//如為null則創(chuàng )建新對象
if (null==userOffer){
userOffer = new StockOffer();
}
//Update with the incoming values
//用上送的數據更新
//These values match those on the form
//這些數據是與form中的數據相對應的
userOffer.setStockName(request.
getParameterValue("STOCK_NAME"));
userOffer.setStockPrice(request
.getParameterValue("STOCK_PRICE"));
userOffer.setStockQuantity(request
.getParameterValue("STOCK_QTY"));
//Reset the output value
//重置輸出數據
userOffer.setRecommendPurchase(null);
//Call the Business Layer
//調用商業(yè)層
BusinessLayer
.evaluateStockPurchase(userOffer);
//Forward to the appropriate page
//轉向合適的頁(yè)面
if ("YES".equals(
testOffer.getRecommendPurchase()){
return mapping.findForward("YES_WEB_PAGE");
}
//otherwise default to the no page
//否則指向無(wú)此頁(yè)面
return mapping.findForward("NO_WEB_PAGE");
}
}
/**
* Defines a Data Access Object - a non data
* source specific way of obtaining data.
* 定義一個(gè)數據存取對象-一種非數據源獲取數據的方法
*/
public interface StockNameDao {
/**
* Get a list of stock names for the application
* @return String[] array of stock names
* 得到一個(gè)股票名字的列表
* 返回股票名稱(chēng)的String[]數組
*/
public String [] getStockNames();
/**
* Check if our stock is on the list
* 檢查股票是否在列表中
* @param stockName
* @return
*/
public boolean isOnStockList(String stockName);
}
And here‘s the DaoImplementation:
這是DaoImplementation:
/**
* Concrete Definition of a Data Access Object
* 數據存取對象的具體定義
*/
public class DaoImplementation
implements StockNameDao {
/**
* Constructor with package level access only
* to encourage use of factory method
* 這里的構造器只是讓你使用工廠(chǎng)(factory)方法
*/
DaoImplementation(){}
/**
* Get a list of stock names for the app.
* This is a hard coded sample
* normally we would get this from
* a database or other datasource.
* 得到一個(gè)股票名字的列表,這只是一個(gè)硬編碼的例子,一般來(lái)
* 說(shuō)我們應該從數據庫或其它數據源取得數據
* @return String[] array of stock names
*/
public String[] getStockNames() {
String[] stockNames=
{"XYZ","ABC","MEGACORP","SOMEOTHERCOMPANY"};
return stockNames;
}
/**
* Check if our stock is on the list
* 檢查我們的股票是否在列表中
* @param stockName
* @return true / false as appropriate
*/
public boolean isOnStockList(String stockName){
//Get our list of stocks
//獲取股票列表
String stockList[] = getStockNames();
//Loop and see if our stock is on it
// done this way for clarity . not speed!
//循環(huán)看股票是否存在,這樣做是為了清晰不是速度!
for (int a=0; a<stockList.length;a++){
if(stockList[a].equals(stockName)){
return true;
}
}
//Default return value
return false;
}
}
package net.firstpartners.rp;
/**
* Factory Method to get the Data Access Object.
* Normally we could replace this with a
* framework like Spring or Hibernate
* 得到數據存取對象的工廠(chǎng)方法,通常我們可以將它替換為像Spring或
* Hibernatte這樣的框架
*/
public class DaoFactory {
/**
* Get the stock name Dao
* This sample is hardcoded - in reality
* we would make this configurable / cache
* instances of the Dao as appropriate
* 得到股票名字的Dao,這個(gè)例子是硬編碼的-實(shí)際上我們可以讓它成為
* 可配的,緩存的合適的Dao對象。
* @return an instance of StockNameDao
*/
public static StockNameDao getStockDao(){
return new DaoImplementation();
}
}
<?xml version="1.0"?>
<rule-set name="BusinessRulesSample"
xmlns="http://drools.org/rules"
xmlns:java="http://drools.org/semantics/java"
xmlns:xs="
http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="
http://drools.org/rules rules.xsd
http://drools.org/semantics/java java.xsd">
<!-- Import the Java Objects that
we refer to in our rules -->
<!-- 導入規則中使用的對象 -->
<java:import>
java.lang.Object
</java:import>
<java:import>
java.lang.String
</java:import>
<java:import>
net.firstpartners.rp.StockOffer
</java:import>
<java:import>
net.firstpartners.rp.DaoFactory
</java:import>
<java:import>
net.firstpartners.rp.StockNameDao
</java:import>
<!-- Application Data not associated -->
<!-- with any particular rule -->
<!-- In this case it‘s our factory -->
<!-- object which gives us back -->
<!-- a handle to whatever Dao (Data -->
<!-- access object) that we need -->
<!-- 沒(méi)有和任何規則聯(lián)系的應用數據,這里是我們的工廠(chǎng)對象,-->
<!—它向我們提供向后的操作,告訴我們什么Dao 是我們需要的。-->
<application-data
identifier="daoFactory">DaoFactory
</application-data>
<!-- A Java (Utility) function -->
<!-- 一個(gè)Java方法 -->
<!-- we reference in our rules -->
<!-- 在我們的規則中打印跟蹤信息 -->
<java:functions>
public void printStock(
net.firstpartners.rp.StockOffer stock)
{
System.out.println(
"Name:"+stock.getStockName()
+" Price: "+stock.getStockPrice()
+" BUY:"+stock.getRecommendPurchase());
}
</java:functions>
<!-- Check for XYZ Corp-->
<!-- 檢查XYZ公司 -->
<rule name="XYZCorp" salience="-1">
<!-- Parameters we can pass into-->
<!-- the business rule -->
<!-- 可以傳入規則中的參數 -->
<parameter identifier="stockOffer">
<class>StockOffer</class>
</parameter">
<!-- Conditions that must be met for -->
<!-- business rule to fire -->
<!-- 激活規則必須滿(mǎn)足的條件 -->
<java:condition>
stockOffer.getStockName().equals("XYZ")
</java:condition>
<java:condition>
stockOffer.getRecommendPurchase() == null
</java:condition>
<java:condition>
stockOffer.getStockPrice() > 10
</java:condition>
<!-- What happens when the business -->
<!-- rule is activated -->
<!-- 規則激活后執行的步驟 -->
<java:consequence>
stockOffer.setRecommendPurchase(
StockOffer.NO);
printStock(stockOffer);
</java:consequence>
</rule>
<!-- Ensure that negative prices -->
<!-- are not accepted -->
<!-- 確定負數不被接受 -->
<rule name="Stock Price Not Negative">
<!-- Parameters we can pass into the -->
<!-- business rule -->
<!-- 可以傳入規則中的參數 -->
<parameter identifier="stockOffer">
<class>StockOffer</class>
</parameter>
<!-- Conditions for rule to fire -->
<!-- 激活規則必須滿(mǎn)足的條件 -->
<java:condition>
stockOffer.getStockPrice() < 0
</java:condition>
<!--When rule is activated then ... -->
<!-- 規則激活后執行的步驟 -->
<java:consequence>
stockOffer.setRecommendPurchase
(StockOffer.NO);
printStock(stockOffer);
</java:consequence>
</rule>
<!-- Check for Negative Prices-->
<!-- 尋找低價(jià) -->
<rule name="Stock Price Low Enough">
<!-- Parameters for the rule -->
<!-- 可以傳入規則中的參數 -->
<parameter identifier="stockOffer">
<class>StockOffer</class>
</parameter>
<!-- Now uses Dao to get stock list -->
<!-- 現在使用Dao獲取股票列表 -->
<java:condition>
daoFactory.getStockDao().isOnStockList(
stockOffer.getStockName())
</java:condition>
<java:condition>
stockOffer.getRecommendPurchase() == null
</java:condition>
<java:condition>
stockOffer.getStockPrice() < 100
</java:condition>
<!-- When rule is activated do this -->
<!-- 規則激活后執行的步驟 -->
<java:consequence>
stockOffer.setRecommendPurchase(
StockOffer.YES);
printStock(stockOffer);
</java:consequence>
</rule>
</rule-set>
聯(lián)系客服