Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

java - how can i generate .drl file through programmatically .Any example would be helpful for me

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
// -------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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...