Thomas Sundberg

October 11, 2012

JBoss Drools – a hello world example

Filed under: J2EE, Java, Maven — Tags: , , — Thomas Sundberg @ 11:58

This post is available at http://www.thinkcode.se/blog/2012/10/11/jboss-drools-a-hello-world-example

Advertisements

38 Comments »

  1. Excellent piece of work… Thanks

    Comment by nisarpro — March 20, 2013 @ 19:54

  2. very good !
    And crystal clear ! Thanks a lot for the blog. It really helped me a lot.
    Can you please post any complex examples with drools?
    Actually i need to develop an application where the user should be able to create new rules during the run time.
    thanks again

    Comment by Kazi Abid Azad — June 20, 2013 @ 14:26

  3. I have a problem because the syntax of rules does’t seem to work for me.
    In the ‘then’ section of a rule I have to use message getters and/or setters to get to the values instead of the property names.
    Can you explain why this happens? Also is there any way to make drools work as you with the property names and not the getters/setters?

    Comment by Marcin Lobocki — August 21, 2013 @ 12:24

    • Hi!

      I don’t really understand your problem. Could you create a minimal example that doesn’t work as you wish and send it?

      /Thomas

      Comment by Thomas Sundberg — August 21, 2013 @ 21:28

  4. Excellent Tutorial !!!
    I understood what a rule engine is by experimenting with your hello world example.
    Nice work and thanks a lot. 🙂

    Comment by Himesh — February 28, 2014 @ 07:27

  5. Good article. However I’m facing a weird issue in Intellij when I run the MessageTest.java class. org.eclipse.jdt.internal.compiler.CompilationResult.getProblems()[Lorg/eclipse/jdt/core/compiler/CategorizedProblem;
    Any idea how to resolve this?

    Comment by Arvind — March 19, 2014 @ 23:55

    • Not from the top of my head. I would need to see your code and try for my self.

      What happens when you remove test after test, possibly line by line, are you able to locate the offending statement? It is probably easier to understand the problem if you can locate the exact line that has the problem. It may even be obvious when you can trigger it at will.

      HTH
      Thomas

      Comment by Thomas Sundberg — March 20, 2014 @ 23:30

  6. Hey Thomas,

    Excellent article-helped me set up a test rig really easily. Just wanted to point out with the current pom on the Hello World I am getting a compilation failure

    1. static import declarations are not supported in -source 1.3
    2. error: annotations are not supported in -source 1.3

    I am running Maven 2.2.1 with Java version: 1.7.0_17

    Consulted http://maven.apache.org/general.html#Compiling-J2SE-5 & my pom looks like:

    http://pastebin.com/S3NxF3dy

    Comment by Nitin Jaswal — April 16, 2014 @ 20:43

    • Hi!

      I would like to suggest that you use a modern version of Maven. It will default to a Java level that supports annotations and static imports. Maven 2 is end of life 2014-02-18 according to http://maven.apache.org/docs/history.html.

      You could also say that the source and target should be 1.7 in the compiler settings.

      /Thomas

      Comment by Thomas Sundberg — April 16, 2014 @ 20:55

      • Copy that. Thanks for the tips!

        Comment by Nitin Jaswal — April 16, 2014 @ 22:03

  7. Thanks a lot Thomas.

    It helped me to start with Drools in style.

    Comment by Punya — June 9, 2014 @ 12:32

  8. Great tutorial : )

    Comment by Dongho Yoo — June 12, 2014 @ 08:33

  9. hi Thomas,

    result show only High value, not both of them.

    this result: Type: High value
    Exception in thread “main” java.lang.AssertionError:
    Expected: is
    got:

    at org.junit.Assert.assertThat(Assert.java:780)
    at org.junit.Assert.assertThat(Assert.java:738)
    at src.com.test.HelloWorldRuleTest.shouldFireAllRules(HelloWorldRuleTest.java:34)
    at src.com.test.Test.main(Test.java:11)

    how does show 2 type, type Hello and type Hight value?

    Comment by Hoàn Mỹ Nguyễn — June 13, 2014 @ 12:05

    • Hi!

      I really can’t say whats wrong. I can only see the Hamcrest error message that says that you didn’t get what you expected. I would have to assume that you have made a mistake when you copied the source code. I would suggest that you double check your source and possible find some error.

      That’s all can assist you with at the moment.

      Sorry
      Thomas

      Comment by Thomas Sundberg — June 13, 2014 @ 20:04

      • My code is same your code, not change.

        createHelloWorld(workingMemory);
        createHighValue(workingMemory);

        the workingMemory variable has overridden in createHighValue method. Output is only one type that is “High value”. How do I have output “Hello” and “High value”?

        thank you for your reply.
        hoanmy

        Comment by Hoàn Mỹ Nguyễn — June 15, 2014 @ 01:41

  10. Hi!

    I don’t understand what you mean by “the workingMemory variable has overridden in createHighValue method”. The variable isn’t overridden in my version of the method. I am adding things into the workingMemory variable. It is a case of an output variable.

    I executed my source code and got his output for the extended example:

    Type: High value value: 42
    Type: Hello value: 0

    Since I haven’t seen your source I can only assume that you have made some kind of mistake when you copied the source code.

    I would suggest that you create the extended example from scratch again. It is just a matter of creating a few files and copy some code. Should be pretty fast.

    /Thomas

    Comment by Thomas Sundberg — June 15, 2014 @ 10:38

  11. Hi This site is useful for my needs. Thank you – Rajesh R

    Comment by Rajesh R — October 27, 2014 @ 15:11

  12. Hi
    Have you tried getting your example working with 6-1-0-final release. There seem to be some major changes.

    Cheers
    Charlie

    Comment by Charlie Simms — February 17, 2015 @ 10:10

    • Hi!

      I haven’t tried. If you give it a try, please tell us the result.

      /Thomas

      Comment by Thomas Sundberg — February 17, 2015 @ 22:46

  13. very detailed and very clear explanation ! thanks to you it became more easy to understand…

    Comment by nilayc — March 5, 2015 @ 15:30

  14. I like the post. It has helped get me up to speed quickly. When running it, I did run into an issue: java.lang.NoClassDefFoundError: com/google/protobuf/MessageOrBuilder. After adding dependency compile ‘com.google.protobuf:protobuf-java:2.6.1’ to my Gradle build, all worked fine.

    Comment by wltiii — March 8, 2015 @ 21:26

  15. hi,

    thank you so much for this example
    i request if i have many files “.drl” how can I do
    i don’t think is best idea to do it in class java.

    thank you for your help

    Comment by zineb cherkaoui — March 11, 2015 @ 19:44

  16. System.out.println(“Type type); should be “Type ” + type ; ))

    Comment by cybuch — March 18, 2015 @ 00:37

    • It should indeed be so. Unfortunately, I can’t see where in the post I have made that mistake.

      Thomas

      Comment by Thomas Sundberg — March 18, 2015 @ 19:49

  17. i am looking for an API in drools to get the names of all the packages that i have created. This helps me to give user a flexibility of selecting the package from the list of packages in my application, so that only that particular package gets executed.
    i didn’t find anything. Please help me on this

    Comment by kiran — July 16, 2015 @ 16:24

    • Hi!

      I have no idea how to do that out of the box with Drools. I would probably not even try finding it in Drools. I would probaly implement it myself by scanning the class path and use some clever heuristics to report back the relevant packages.

      I haven’t done it so this is just an outline of my initial idea.

      /Thomas

      Comment by Thomas Sundberg — July 16, 2015 @ 19:08

  18. Thanks Thomas….
    i have one more question regarding the packageBuilder creation. I have drools application deployed with all the packages being created in the drools GUI. i have a url for one of the package
    as http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/AutoAction/LATEST.drl. i tried to modified your code instead reading the rules from the file system, since i already have rules deployed. i tried to below code

    PackageBuilder packageBuilder = new PackageBuilder();

    InputStream input = new URL(“http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/AutoAction/LATEST.drl”).openStream();
    Reader reader = new InputStreamReader(input);
    packageBuilder.addPackageFromDrl(reader);
    org.drools.core.rule.Package rulesPackage = packageBuilder.getPackage();
    I am getting the null for packageBuilder.getPackage();

    Do i need to pass any other information into PackageBuilder constructor the connect to my rules application?
    Please let me know how can i achieve this.?

    Comment by kiran — July 16, 2015 @ 21:26

    • Hi!

      I don’t know what you need to change. You ask if you need to pass any other information into the constructor for PackageBuilder and that is possible. I think that Stackoverflow might be a better place to ask.

      /Thomas

      Comment by Thomas Sundberg — July 17, 2015 @ 06:52

  19. Thanks A LOT!!!!

    Comment by Oleg — February 16, 2016 @ 16:27


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: