Question

In my java pojo XOM model , I have a static variable say count.I am executing 5 rules and it is increasing the count variable by one. So in the end of the execution I have value as count = 5 .

Now if I declare a non static variable count and increment its value for each rule execution,will the variable be initialized 5 times and it is non static.

In other words, Will 5 instances of my java XOM model class will be created if the object is not declared as static ?

Was it helpful?

Solution

Not sure I understood your question.
I you have a rule stating:
"then set myObj.count to myObj.count + 1;" then you will have as many count variables as myObj object.
So if you insert in the working memory ONE myObj then you will have only ONE myObj at the end and during execution.
Each rule will fire as many time as you have "matching" in the working memory.
So if you pass 5 myObj in the engine, you will have 5 myObj.count each having a final value of '5'.

If you are using static then it will be the same.
Except if you are using the engine in a stateful mode (which quite rare actually).
The BRMS is there to be a Decision Service and hence make a decision on the parameters passed in (and the rules of course).
I am not saying you cannot use it in a stateful mode but this use is rare.
If you do so, then yes the previous answer is correct and you will end up with an infinitely increasing value. Increasing by five at every run.

BTW: I never tried this because there are a whole set of attributes to get the number of rules that fired or not and time execution and so on...

To sum up, you have what you have in the working memory. Meaning that if you have ONE instance of an object the engine will never create more instance at runtime. That would lead to madness ( : : ) due to the RETEPlus algorithm. (which is not frequently used, as well)
The only way to create more instance of object is to do it yourself a trun time directly via the rules. E.g.: Add an message "hello world" to the messages of the report".
Hope it helps

OTHER TIPS

Static variables are merely class variables. Therefore, it will "persist" across rule invocations. In other words, the next time you run it, the count will be 10. If you are not using static, then it will be initialized once for each rule invocation, NOT once for each rule. So, in short, NO.

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