Drools学习 入门实例
?http://blog.sina.com.cn/s/blog_67d6d5df0100jyqk.html
在Eclipse新建Drools工程就可以導(dǎo)入自帶的例子進(jìn)行學(xué)習(xí)。
??? 接下來(lái):
??? 如此,就可以對(duì)實(shí)例進(jìn)行學(xué)習(xí)了。
?下面只是參照HelloWorld寫(xiě)的一個(gè)小例子,當(dāng)做筆記。
???? 設(shè)計(jì)場(chǎng)景是銀行借貸,當(dāng)借貸款額小于1000時(shí),直接借出,否則,需交由總經(jīng)理處理。
???? 1 新建工程,命名:drools,導(dǎo)入Drools相關(guān)jar包。
???? 2 新建Package,命名:aaron.loan,在此包中建立class文件:pojo.java pojo.java package aaron.loan; public class pojo {
??? int moneycount; public int getMoneycount() {
????? return moneycount;
??? } public void setMoneycount(int moneycount) {
????? this.moneycount = moneycount;
??? }
???
??? }
???? 3 新建規(guī)則文件:DecisionRule.drl Decision.drl import aaron.loan.pojo;
??? rule "A"
????? when
???????? ?$m:pojo(moneycount<1000);
????? then
????????? System.out.println("Done!");
??? end rule "B"
??????when
????????? $m:pojo(moneycount>=1000);
????? then
????????? System.out.println("Pass it to the management!");
??? end ??? 4 在aaron.loan包中新建class文件:deploy.java??? deploy.java package aaron.loan; import org.drools.KnowledgeBase;
??? import org.drools.KnowledgeBaseFactory;
??? import org.drools.builder.KnowledgeBuilder;
??? import org.drools.builder.KnowledgeBuilderError;
??? import org.drools.builder.KnowledgeBuilderErrors;
??? import org.drools.builder.KnowledgeBuilderFactory;
??? import org.drools.builder.ResourceType;
??? import org.drools.io.ResourceFactory;
??? import org.drools.logger.KnowledgeRuntimeLogger;
??? import org.drools.logger.KnowledgeRuntimeLoggerFactory;
??? import org.drools.runtime.StatefulKnowledgeSession;
??? public class deploy {
???? ?public void loanrool(int moneycount) throws Exception {
?????? // TODO Auto-generated method stub
???????????? pojo p=new pojo();
????????? try {
???????????? ?KnowledgeBase kbase = readKnowledgeBase();
????????????? StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
????????????? KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory
?????????????? ?.newFileLogger(ksession, "test");
?????????
???????????? p.setMoneycount(moneycount);
???????????? ksession.insert(p);
???????????? ksession.fireAllRules();
?????????????System.out.print("moneycount:"+p.getMoneycount());
???????????? logger.close();
???
????????? } catch (Throwable t) {
????? t.printStackTrace();
??? }
??
?? } private KnowledgeBase readKnowledgeBase() throws Exception {
??????? ?KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
?????????? .newKnowledgeBuilder();
??????? ?kbuilder.add(ResourceFactory.newClassPathResource("DecisionRule.drl"),?ResourceType.DRL);
???????? KnowledgeBuilderErrors errors = kbuilder.getErrors();
?????????? ?if (errors.size() > 0) {
??????????????? ?for (KnowledgeBuilderError error : errors) {
???????????????????? ?System.err.println(error);
????????????????????????????}
?????????throw new IllegalArgumentException("Could not parse knowledge.");
?????????? }
???????? ?KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
?????????? kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
??????????? return kbase;
??????? ?} }? 5 建立測(cè)試文件:test.java test.java package aaron.loan;
??? public class test {
????? ?public static void main(String[] args) throws Exception {
?????????? // TODO Auto-generated method stub
??????????? ?deploy dp=new deploy();
?????????????pojo pj=new pojo();
???????????? pj.setMoneycount(900);?? //此處900是任意設(shè)的值。
???????????? int mc=pj.getMoneycount();
???????????? dp.loanrool(mc);?????
?????? } } 運(yùn)行test.java文件,控制臺(tái)結(jié)果如下:
??? Done!
??? moneycount:900
???
????到此,Drools小實(shí)例就運(yùn)行成功了。
?
another drl example:
#ident? "%W%"
#created on: Nov 4, 2011
package rules.com.citi.gcd.ptrs
#list any import classes here.
import com.citi.apollo.fmk.limit2.LimitRequest;
import java.util.List;
import java.util.ArrayList;
import com.citi.apollo.fmk.limit.PortfolioLimitResult;
import com.citi.apollo.fmk.limit.PortfolioLimitStatus;
import com.citi.apollo.fmk.util.aggregation.Aggregate;
import com.citi.apollo.fmk.limit.LimitAggregate;
import com.citi.apollo.fmk.limit.LimitRange;
# functions
import function com.citi.apollo.fmk.limit2.Limit2Util.setDefaultResult;
import function com.citi.apollo.fmk.limit2.Limit2Util.totalOf;
function void calcSomething(LimitRequest req, Double value, Double total) {
???List<Aggregate> l = new ArrayList<Aggregate>();
???LimitAggregate a = new LimitAggregate(new Aggregate(value, total));
???a.setPercentValue(total/total);
???l.add(a);
???req.result.addAggregates(l);
???req.result.setStatus(PortfolioLimitStatus.PASS);
???
???LimitRange lr = new LimitRange(req.getLimit().getMinValue(),
????????????????????????????????? req.getLimit().getMaxValue(),
????????????????????????????????? req.getLimit().getMinPerValue(),
????????????????????????????????? req.getLimit().getMaxPerValue());
???lr.setExcessConcentration(false);????????????????????????????????
???req.result.setLimitRange(lr);?
}
?
#declare any global variables here
rule "Commitment"
?no-loop true
?when
??$req : LimitRequest ( limit.limitName == "Commitment" )
?then
??double total = totalOf($req, "commitmentamount");
??calcSomething($req, total, total);
??setDefaultResult($req, total, total);?
end
?
?
總結(jié)
以上是生活随笔為你收集整理的Drools学习 入门实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Happy Holidays and S
- 下一篇: Drools 规则语言详解(上)