Question

What is the use of physical packages and package declaration in Drools.

For eg,

I have a rule, Myrule.drl in the physical package com.mycompany

As I understand the package declaration in drools doesn't depend on the actual physical package the file lies in.

So I can give in Myrule.drl as

package com.sample;

rule "my rule" when then end

Can somebody help me to understand what is the relation between the physical package/folder that the drl file lies and the package declaration in drl file?

Was it helpful?

Solution

Your understanding is correct. There is no relation between the package declaration in your DRL and the place where the DRL file is. All the Java classes generated by either the RHS of the rules or the type declarations are going to be placed by Drools' Compiler in the package defined in the DRL file where they are. This allow you to have rules or declare types with same name but in different packages.

Hope it helps,

OTHER TIPS

Why don't you just read the documentation, Drools "Expert" manual, Section 7.5?

A package is a collection of rules and other related constructs, such as imports and globals. The package members are typically related to each other - perhaps HR rules, for instance. A package represents a namespace, which ideally is kept unique for a given grouping of rules. The package name itself is the namespace, and is not related to files or folders in any way.

There's more...

Can somebody help me to understand what is the relation between the physical package/folder that the drl file lies and the package declaration in drl file?

There is no relationship (directly). You have to think in terms of what happens when you run the rules engine. When you run your application, these rules are turned into virtual objects placed in the declared namespace. So, for example:

DRL snippet

package com.mystuff

import java.util.logging.Logger;
import java.util.logging.Level;

rule "Hello World"
    when
       some condition...
    then
       Logger.log(Level.INFO, "Hello World");
end

When this rule is activated, given that you have logging turned on to the right levels, you will notice an entry similar to this one:

INFO   com.mystuff.Rule_HelloWorld592399015 defaultConsequence Hello World

proving that the rule was turned into a virtual object (you won't find it as a .class file in your project) that it's located in the com.mystuff namespace as Estaban Aliverti mentioned on this post.

This is not that important if you are only operating with a single rule file. But, as you can imagine, if you have multiple files with similar rules, they will be resolved by the namespace included in the DRL file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top