-1

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.

  • What do you want to generate those files from? – yole Feb 01 '16 at 10:28
  • i want own rule generation – RajeevMajumdar Feb 01 '16 at 10:38
  • Sorry, I don't understand. The .drl file is a program. Normally people don't generate programs, they write them in the IDE. If you do want to generate a program, you do this by writing text to a text file. You won't find much documentation or examples for generating programs, because it's not possible to provide such documentation in a general case: the process entirely depends on what you're generating the program from. – yole Feb 01 '16 at 10:42
  • i have some rules which is updated further time to time may be increased so i need a rule generation program. i go through drool and related links but i am not satisfied with that . – RajeevMajumdar Feb 01 '16 at 10:49
  • rule "Send an email offer to Potential Customers with credit limit lower or equal than $500" when $pc:PotentialCustomer( creditLimit <=500 ) then logger.info("\t==> Sending email to Potential Customer: " + $pc); // Here an email service will send the email... emailService.sendCreditCardOffer($pc); end – RajeevMajumdar Feb 01 '16 at 10:52
  • This example can be written and updated with a text editor. Unless you can provide more specific and detailed use cases and requirements, no other answer will be possible. And: edit your question, don't put code into comments. – laune Feb 01 '16 at 12:21

2 Answers2

3
// -------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 
1

Recents versions of Drools started to work in a way to programmatically define rules using a fluent API. I've used this API myself for some internal projects and it was flexible enough to meet my needs. The downsides of this API are:

  • It is not documented (you can find some tests in the code and that's pretty much it).
  • It is considered and internal API, so it could change in the future without backward compatibility.
  • Sometimes, the API was not "typed" enough. Some parts of a constraint had to be specified as chunks of Strings.

But there is a - probably better - alternative that you may want to consider. A DRL is nothing but a text file. If you want to programmatically generate some rules based on some data, you may use a template framework like String Template or Velocity to create DRL on-the-fly.

Hope it helps,

Esteban Aliverti
  • 6,259
  • 2
  • 19
  • 31