Skip to main content

JBoss BRMS - Rules 語法與範例信用卡程式

今年年初我會多花一些時間翻譯以及多介紹BRMS的部分。

JBoss Rules 是JBoss BRMS 裡面專門管理商業邏輯的部分,像是客戶是否符合打折的條件啊,什麼條件下應該要將倉庫的貨品拍賣等等的...
以人類的腦子裡面,通常我們是怎麼判斷邏輯的呢?
通常都是向下面一樣,

也就是會先設想一些條件,再來判斷要做的事情。要寫一條JBoss 的規則邏輯很簡單。跟人類大腦判斷的基本上也差不多。基本上,大概就分成幾個部分,


大部份要做的事情都在When 與 then 就做掉了。
語法很簡單, 只要會一點程式設計的人都可以很快上手,大多是 ==. >=, <=...等等...

有一些設定與規則的fire 順序與方式比較有關係,所以特別寫在上面註記一下:


no-loop
預設值: false
類型: Boolean
設定 no-loop為true可以防止這個規則邏輯有任何變動,會觸發其它的規則,導致無窮迴圈。

lock-on-active
預設值: false
類型: Boolean
no loop 的加強版,因為會其他的rule會觸發結果,也會有無窮迴圈的狀況產生,
因此,在這個 ruleflow group 或是 agenda group 啓動時,設定的rule只有一次。

salience
預設值: 0
類型: integer
設定執行的順序,數字越大優先等級越高


以下我們來寫利用JBoss BRMS 一個簡單的Drools 程式,並且在BRMS 上面建立一個測試的策略,以後修改規則都可以拿這個策略去確認是否正確。

1. 安裝JBoss BRMS, 我使用比較簡單的standalone 版本,也就是brms-standalone-x 的版本,基本上就是解壓縮,然後去JBoss 裡面的EAP 把brms-users.properties 裡面的使用者設定打開即可。(以前寫過JBoss EAP 設定,這裡我就不再多說了..)

2. 建立JBoss Drools Project.. 這次要寫的是信用卡的自動核准規則。

選擇建立Drools Project


輸入名字CreditCardVIP,

 因為這次不需要Sample, 所以把選項拿掉


確認Runtime 環境有正確設定,按下Finish 即可。



像是開發普通的系統一樣,先建立幾個物件,在src/main/java 底下建立 Customer 客戶與 CreditCard 信用卡。與在src/main/rules 建立規則。


Customer.java
package com.redhat.creditcard;
public class Customer {

	private String name = "";
	private int creditpoint = 0;
	private boolean vip = false;
	private int age = 0;
	private int delayPayment=0;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getCreditpoint() {
		return creditpoint;
	}
	public void setCreditpoint(int creditpoint) {
		this.creditpoint = creditpoint;
	}
	public boolean isVip() {
		return vip;
	}
	public void setVip(boolean vip) {
		this.vip = vip;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getDelayPayment() {
		return delayPayment;
	}
	public void setDelayPayment(int delayPayment) {
		this.delayPayment = delayPayment;
	}
	
}
Creditcard.java
package com.redhat.creditcard;

public class Creditcard {

	private String name="";
	private String type="GENERAL";
	private int cashback = 0;
	private int specialDiscount = 0;
	private int creditpointToVIP=0;
	private int interestRate=0;
	private boolean isApprove=false;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getCashback() {
		return cashback;
	}
	public void setCashback(int cashback) {
		this.cashback = cashback;
	}
	public int getSpecialDiscount() {
		return specialDiscount;
	}
	public void setSpecialDiscount(int specialDiscount) {
		this.specialDiscount = specialDiscount;
	}
	public int getCreditpointToVIP() {
		return creditpointToVIP;
	}
	public void setCreditpointToVIP(int creditpointToVIP) {
		this.creditpointToVIP = creditpointToVIP;
	}
	public int getInterestRate() {
		return interestRate;
	}
	public void setInterestRate(int interestRate) {
		this.interestRate = interestRate;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public boolean isApprove() {
		return isApprove;
	}
	public void setApprove(boolean isApprove) {
		this.isApprove = isApprove;
	}
	
	
}
3. 分別建立兩格主要的規則檔案,一個專門放核可發放信用卡的規則,一個專門放退件的信用卡規則。

建立一個 Rule Resource

取名 approveApplication.drl ,記得不要忘記規則的package 名字也要設定喔!



然後寫入兩個Rule. 這應該淺顯易懂,大家自己研究一下吧。


另一個規則也是照同樣的方式建立。rejectApplication.drl. 




4. 設定JBoss BRMS 的主控台,到 http://IP:PORT/jboss-brms, 預設可以用admin/admin登入,左邊選單選擇 knowledge base, 然後選擇 create a new package 建立新的package.
com.redhat.creditcard.vip

5. 把Project 與 BRMS 系統整合。先選擇Guvnor Repository Exploring 的 perspective,

然後新增一個Guvnor 的connection候記得加上 admin


它會顯示目前BRMS上面有的package 等等..

 選擇我們要的com.redhat.creditcard.vip 為要儲存的package.


 按下Finish完成後,可以回到專案,按下滑鼠右鍵,就會有個Guvnor 的選項出現,選擇Add... 加入BRMS.




6. 將Model Fact 也就是我們建立的Pojo 物件們放入BRMS 中。
一樣到Knowledge Base 下,選擇 Upload POJO module jar,

取名為 CreditCardFactModel


選擇物件的jar 檔,按下Upload 更新上去
7. 需要Build Package, 請到com.redhat.creditcard.vip 下的Edit 去按下Build Package的按鈕。


8. 建立 Test Scenario.
到Knowledge Base 下,選擇New Test Scenario

在Name 的地方寫下名字,以及選擇Package 的名字。

建立Test Scenario 的一些需要帶入的值。建立完如果規則寫的正確,按下Run Scenario 就會有100%的綠色燈。


以下為其他剩下的Test Scenario。




注意一下,再跑完Run Scenario 後,在Call Method 上會告訴你,這個case 到底跑了幾個rule, 與執行時間,非常有參考價值。


9. 到左邊選單的QA, 會發現剛剛建立的所有Test Scenario,以後若有修改,可以直接來這裡跑所有的測試結果。方便多拉!


以上。



Comments

Popular posts from this blog

Fuse Integration Service - Setup JBDS and create first quickstart application

Before we go and start creating our first application, I want to show you how to setup your JBoss Developer Studio, create a small application from the quickstart example and then running it on Fuse Integration Service.

I am using JBoss Developer Studio version 9, you can find it here.
After download the

jboss-devstudio-9.0.0.GA-installer-eap.jar
double-click it, and start installing with default values.

After successful installation, we will need install the plugins for Fuse, on JBoss Central view, select software update, select enable early access.


And select JBoss Fuse Development for the plugin,


Click on install, and we are all set to go!

First thing first, we want to create a Fuse project to deploy on the base of Fuse Integration Service, which is OpenShift. If you have not installed it, please go back to my previous post for instructions. So on your JBDS, right click and start creating the project. Select new, maven project, if you have installed the plugin correctly, you should …

Red Hat JBoss Fuse/A-MQ - Fuse and A-MQ Version 6.3 GA is released!

Fuse and A-MQ 6.3 GA has just went out. Maybe, you would think this is just only a minor version release why should I care? Hold your thoughts on that! Because they have done a lot of improvements and also added many new features into this release.

Besides various bug fixes and making sure Fuse Fabric is much more stable. There are two major change in this version update:

New Tooling in JBoss Developer Studio (JBDS) 9.1 GA. Newer Apache Camel version – Camel v2.17. I was really impressed by the work put in to make developing Camel application much simpler. First is the installation of tooling itself. Now it has a all-in-one installer so you don't need to worry about which plugins you need to check. See the videos below to see the new "Getting Started" of Fuse 6.3.



And If you notice from the above video, the presentation of camel route in JBDS has also updated. It fixed some of the miss representation of logic and making it easier to read.

Old Camel Route
New Camel Route
On …

RHTE - Supercharge your integration services

Red Hat Tech Exchange has taken place in Vietnam, Ho Chi Minh city two weeks ago, it is a great event held by Red Hat in Asia Pacific Region. It is open to all Red Hat partners who are interested in learning what Red Hat is doing recently, see what the trend of the open source world, basically it is a great event to share your knowledge and experience, to meet other enthusiastic people.

I am very fortunate to talk in this great event, to talk about the things I have been working on and even discuss it with many. Also got lots of great ideas too. So here are the slide.

My first talk was with Thomas Qvarnström about how to handle large size data in JBoss Fuse and how JBoss Data Grid can help in the situation.

Here is the agenda of the talk, we will be talk about this in the up coming webinar on 24th Sept.

Integration often involves storing, retrieving, and transforming data. Using a traditional database in your integration is likely to becomes a bottleneck that is expensive and hard to …