本文介绍了我如何通过编程方式生成.drl文件。任何示例对我都将有所帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多站点,但找不到有关.drl文件生成的任何特定相关示例。 .drl文件生成方面的文档也不值得。

I searched lots of sites but unable to find any specific related example on .drl file generation . Documentation is also not worthy about .drl file generation.

推荐答案

// -------package section-------
PackageDescr pkg=new PackageDescr();
pkg.setName("com.demo.model");

// -------import section here-------
ImportDescr importEntry1= new ImportDescr();
importEntry1.setTarget("com.demo.model.Purchase");
pkg.addImport(importEntry1);
ImportDescr importEntry2= new ImportDescr();
importEntry2.setTarget("com.demo.model.PotentialCustomer");
pkg.addImport(importEntry2);

ImportDescr importEntry3= new ImportDescr();
importEntry3.setTarget("com.demo.model.PaymentMethod");
pkg.addImport(importEntry3);

//-------global section here-------
GlobalDescr globalEntry=new GlobalDescr();
globalEntry.setType("org.slf4j.Logger");
globalEntry.setIdentifier("logger");
pkg.addGlobal(globalEntry);

//------- rule section here
RuleDescr ruleEntry=new RuleDescr();
ruleEntry.setName("Identify potential customers");

// ------- lhs starts here ------- 
AndDescr lhs=new AndDescr();

//-------  pattern starts here ------- 
PatternDescr patternEntry1=new PatternDescr();
patternEntry1.setIdentifier("$p");
patternEntry1.setObjectType("Purchase");

//------- ExprConstraint starts here ------- 
 ExprConstraintDescr ecd1=new ExprConstraintDescr();
 ecd1.setExpression("paymentMethod");
 ExprConstraintDescr ecd2=new ExprConstraintDescr();
 ecd2.setExpression("PaymentMethod.CASH");
//-------  Added exprConstraint into relational expr------- 
    RelationalExprDescr red1=new RelationalExprDescr("==",false, null, ecd1, ecd2);

    ExprConstraintDescr ecd3=new ExprConstraintDescr();
    ecd3.setExpression("subTotal");
    ExprConstraintDescr ecd4=new ExprConstraintDescr();
    ecd4.setExpression("300");
    RelationalExprDescr red2=new RelationalExprDescr(">",false, null, ecd3, ecd4);


patternEntry1.addConstraint(red1);
patternEntry1.addConstraint(red2);
lhs.addDescr(patternEntry1);

NotDescr notDescr=new NotDescr();
notDescr.setText("not");


PatternDescr pattDescr1=new PatternDescr();
pattDescr1.setObjectType("PotentialCustomer");

ExprConstraintDescr ecd11=new ExprConstraintDescr();
ecd11.setExpression("customerName");
ExprConstraintDescr ecd12=new ExprConstraintDescr();
ecd12.setExpression("$p.getCustomerName()");
RelationalExprDescr red11=new RelationalExprDescr("==",false, null, ecd11,ecd12);
pattDescr1.addConstraint(red11);
notDescr.addDescr(pattDescr1);
lhs.addDescr(notDescr);


ruleEntry.setLhs(lhs);

pkg.addRule(ruleEntry);
String drl = new DrlDumper().dump( pkg );

 // here drl is in form of String 

这篇关于我如何通过编程方式生成.drl文件。任何示例对我都将有所帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 02:03