Is there any API in drools to create the drl files dynamically by just passing values?
Asked Answered
S

6

12

I know how to create DRL files inside KIE workbench by using all the methods. But what my problem is without using the KIE workbench, can we create the .drl file by using our required values.If any possibility is there please suggest me. Same way suggest me any API is regarding to that. Thanks in advance.

Saxhorn answered 17/12, 2014 at 9:52 Comment(3)
They don't actually create DRL, but you should read the documentation on decision tables and templates.Garlan
Hey @Garlan thanks for you quick reply. Could you please provide me the link of documentation.Thanks.Saxhorn
That's not enough for a question to be answered with any reasonable precision.Hesperidin
S
8

You can use Drools Fluent API. Try below sample code :

package com.sample;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import org.drools.lang.DrlDumper;
import org.drools.lang.api.DescrFactory;
import org.drools.lang.descr.PackageDescr;

@SuppressWarnings("restriction")
public class Drl_Creator {
    public static void main(String str[]){
        PackageDescr pkg = DescrFactory.newPackage()
                   .name("org.drools.example")
                   .newRule().name("Xyz")
                       .attribute("ruleflow-grou","bla")
                   .lhs()
                       .and()
                           .pattern("Foo").id( "$foo", false ).constraint("bar==baz").constraint("x>y").end()
                           .not().pattern("Bar").constraint("a+b==c").end().end()
                       .end()
                   .end()
                   .rhs( "System.out.println();" ).end()
                   .getDescr();
        DrlDumper dumper=new DrlDumper();
        String drl=dumper.dump(pkg);
        System.out.print(drl);
        try{
            // create new file
            File file = new File("src/main/rules/test.drl");
            file.createNewFile();
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(drl);
            // close connection
            bw.close();
            System.out.println("File Created Successfully");
         }catch(Exception e){
             System.out.println(e);
         }
    }
}
Sanguinaria answered 6/10, 2016 at 7:19 Comment(1)
if I need to add import class or package statement after package name, how would I add? thanksMattins
S
6

I interpret your question in two different ways.

1. Is it possible to write rules for Drools without using the KIE workbench?

Yes, it should support importing rules so all you need to do is open up a text editor and start typing. The rules are written as text using a fairly simple syntax that you can figure out in about 1-2 hours of reading. I do not know what your environment looks like but there should be a mechanism to parse and import a new rule. All rules you write will start out in a text editor looking like this:

rule "<name>"
    <attribute>
when
    <conditional element>
then
    <action>
end

You will add to the conditions and actions. Of course you will have to know what conditions you can create which is limited to your environment and likewise for the actions.

2. Is it possible to create rules and use them programatically through some sort of API?

Yes, I do it all of the time for the processing we do using the Java API. We have 2 types of rules that we use, static and dynamic. The static ones have pre-canned conditions and are written to perform the same comparisons (LHS) over and over and performing the same actions each time the conditions are met (RHS). The dynamic ones are created on the fly based upon a minimalistic set of object types and comparisons (LHS) specified by the user when they are created. The actions (RHS) are pre-canned but are selected for use depending on the need for the overall rule use. The entire rule is created as text then passed to the Drools parser before being added to the list of rules to evaluate.

Hope this helps.

Statesmanship answered 23/12, 2014 at 15:32 Comment(2)
can you please add more details about option 2? Like which library to use for "create rule as text then passed to the Drools parser before being added to the list of rules to evaluate."Ambrosial
These lines do what you ask. sorry couldn't get it to format for me.<br/> <code> KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder( ); kb.add( ResourceFactory.newByteArrayResource( ruleset.getBytes( ) ), ResourceType.DRL ); </code>Statesmanship
T
4

Another option is to use the "descr" APIs, starting from the factory:

org.drools.compiler.lang.api.DescrFactory

These APIs build the Drools AST, which can be passed directly to the compiler, bypassing the parser. The AST can also be used to recreate DRL, using the helper class org.drools.compiler.lang.DrlDumper

Toxicology answered 23/12, 2014 at 17:30 Comment(0)
G
2

The standard tools don't produce DRL files. Instead they encourage you to have templates which are applied to your data at runtime.

You should take a look at the documentation on Decision Tables (specially structured spreadsheets):

http://docs.jboss.org/drools/release/6.0.1.Final/drools-docs/html_single/#d0e4221

... and Rule Templates:

http://docs.jboss.org/drools/release/6.0.1.Final/drools-docs/html_single/#d0e4969

Garlan answered 17/12, 2014 at 10:29 Comment(1)
Thanks @Garlan for the answer.But the requirement what I am looking for is different from this scenario.You have given me the right thing to implement rules by decision tables. May be my requirement is not good enough. Thanks once again. :)Saxhorn
R
1

Even i have used the same implementation that @apandey846 suggested. I would just like to add one more thing: If you want to import the required classes, you can do it as follows:

               PackageDescr pkg = DescrFactory.newPackage()                        
               .newImport("classname").target().end()
               .name("org.drools.example")
               .newRule().name("Xyz")
                   .attribute("ruleflow-grou","bla")
               .lhs()
                   .and()
                       .pattern("Foo").id( "$foo", false ).constraint("bar==baz").constraint("x>y").end()
                       .not().pattern("Bar").constraint("a+b==c").end().end()
                   .end()
               .end()
               .rhs( "System.out.println();" ).end()
               .getDescr();

To add multiple conditions in the LHS, you can do:

               pattern("eval").constraint("condition1").end().
               pattern("eval").constraint("condition2").end().
               pattern("eval").constraint("condition3").end().

Hope it helps.. :)

Ridiculous answered 18/4, 2018 at 11:10 Comment(0)
M
0

Decesion tables have worked for me alternatively you could try using the new Drools workbench.

I have used the DrlDescr dump method but it is Not updating the drl file, Does anybody have any idea why?

Code:- pkg1.addRule(rules); System.out.println(dDump.dump(pkg1));

Macadam answered 13/7, 2015 at 6:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.