Binding multiple variables in Drools LHS throws NPE - drools

I have searched but can't find why this simple code will fail in Drools (core 5.2.1.Final and compiler 5.2.0.Final).
rule "name"
no-loop true
when
$offer: Offer(properties != null && properties.size() > 0, $properties : properties)
$invalidProperty: ContextualizedOfferProperty(name == null || "".equals(name)) from $properties
then
...
end
I get the following exception:
java.lang.NullPointerException
at org.drools.reteoo.FromNode.<init>(FromNode.java:87)
at org.drools.reteoo.builder.FromBuilder.build(FromBuilder.java:41)
at org.drools.reteoo.builder.PatternBuilder.attachPattern(PatternBuilder.java:122)
at org.drools.reteoo.builder.PatternBuilder.build(PatternBuilder.java:76)
at org.drools.reteoo.builder.GroupElementBuilder$AndBuilder.build(GroupElementBuilder.java:126)
at org.drools.reteoo.builder.GroupElementBuilder.build(GroupElementBuilder.java:73)
at org.drools.reteoo.builder.ReteooRuleBuilder.addSubRule(ReteooRuleBuilder.java:152)
at org.drools.reteoo.builder.ReteooRuleBuilder.addRule(ReteooRuleBuilder.java:123)
at org.drools.reteoo.ReteooBuilder.addRule(ReteooBuilder.java:110)
at org.drools.reteoo.ReteooRuleBase.addRule(ReteooRuleBase.java:419)
at org.drools.common.AbstractRuleBase.addRule(AbstractRuleBase.java:814)
at org.drools.common.AbstractRuleBase.addPackages(AbstractRuleBase.java:555)
at org.drools.reteoo.ReteooRuleBase.addPackages(ReteooRuleBase.java:436)
at org.drools.impl.KnowledgeBaseImpl.addKnowledgePackages(KnowledgeBaseImpl.java:149
If I remove the second binding, it passes correctly. I have tried many different, simple bindings, it always throws if I have two bindings, one referencing the other...
Basically, I want the rule to fire for each ContextualizedOfferProperty that has no name for any Offer having at least one property.
Any idea?
Thanks,
John

It looks like a bug. Have you tried version 5.3.1? It fixes several compilation problems like this.

Related

Drools filtering stream within DRL

I would like to create a rule with lamba expression like below:
when
rule_id: String() from "784acba8-32e5-41de-bd73-04f9ce2bfaff"
$: DroolsEventWrapper(Arrays.stream("testing".split("")).filter(element->(element!=null && element.contains("e"))).findFirst().isPresent())
then
System.out.println("Qualified for "+rule_id);
end
just a simple check "testing" will be given as a parameter also; however when I use it drl. It gave error like:
mismatched input '.' in rule "784acba8-32e5-41de-bd73-04f9ce2bfaff" in pattern
When I check the location of that dot; it belongs to filter(element->(element!=null && element.contains("e"))) when I omit it, it is working.
I am using the latest version of Drools: 7.55.0.Final. I found some tickets which say that lambda expressions somehow buggy at previous versions but not the latest ones.
Do I missing something, or is there any way to run this within DRL ?

Boolean flags handing in Drools conditions

We are working on a monitoring application in which we follow the processing of a task in a set of applications.
We have a set of drools rules matching our needs but we have some performance issues (we may have easily up to 50k objects in session).
We are looking for best pactices
This question is about bloolean flag usage.
We are working to remove most of org.drools.core.rule.constraint.MvelConstraint: Exception jitting: ... warns.
We have often such warn on boolean flags.
for example in:
rule "PropagateDeprecation"
when
$parent:BaseChainStep( $parent.Deprecated )
$child:BaseChainStep( $parent.Id == $child.Parent, !$child.Deprecated )
then
modify($child){
setDeprecated(true)
}
end
we have warn on both $parent.Deprecated and !$child.Deprecated.
We would like to understand why there is such warn on boolean flags.
We would like to know also the impacts of the warn on composed conditions.
For example in:
rule "App1_TriggerExpected"
when $chainStep:App1ChainStep(
HasChain
, HasParent
, !$chainStep.Deprecated
, Status in ("error", "closed")
, Places != null
, Analysis != null)
then
..
end
if we have the warn on the first condition HasChain, how is resolved the when clause ?
Does other conditions are evaluated too (with iteration on all App1ChainStep objects) or some "index" are still used to help ?
If its matter, we are using flags as boolean (and not Boolean) to ensure false value as default.
Edit:
The problem may be linked to extended classes. In our use case we have something like:
declare BaseChainStep
parent : GUID
deprecated : boolean
end
declare App1ChainStep extends BaseChainStep
// specific App1 fields
end
BaseChainStep fields may be manipulated in rules using App1ChainStep objects or BaseChainStep objects.
rule "deprecateApp1"
when $app1:App1ChainStep( BusinessLogicCondition )
then
modify($app1) {
setDeprecated(true)
}
end
Then the deprecated flag is propagated to App1 children using "PropagateDeprecation" rule.
Boolean flag causing warn are declared in BaseChainStep class.
Although you are deviating from the conventional way of accessing attributes, this should not trigger the warning you have reported. I can't reproduce this using 6.3.0. You should add (a) the Drools version (b) the Java code for a class BaseChainStep with which the problem can be reproduced with the rule as shown.
This is another (much simpler) way of writing rules combining boolean attributes:
rule bool1
when
X( big, fast )
then
System.out.println( "big fast X" );
end
You may even use boolean operators:
rule bool2
when
X( big && ! fast )
then
System.out.println( "big slow X" );
end
Note the simple use of field name, assuming conventional naming, e.g., big for the field, isBig and setBig for the accessors.

drools rule for fact with list values and multiple conditions

i am attempting to write a rule that will compare a fact list values to a multiple condition rule
rule: only valid legislations
when
fact:Fact (legislationList == ('L1' && 'L2') OR 'L3')
then
fact.printMessage();
end
ie. if fact.legislationList={'L1', 'L2') then fire
if fact.legislationList={'L1', 'L4') then dont fire
i looked at the 'from' clause but wasnt clear how that would solve my issue.
is there some voodoo method/solution that will help me?
thanks
-lp
Assuming that legislationList is a List<String>, then you could use Drools' 'contains' operator:
rule: only valid legislations
when
fact:Fact ((legislationList contains "L1" && legislationList contains "L2") || legislationList contains "L3" )
then
fact.printMessage();
end
You can abbreviate the syntax a bit, but I wanted to be explicit.
Hope it helps,

Reuse of conditions (when) in Drool DSL statements

Is it possible to reuse a when/condition statement into another when/condition statement in a DSL file?
For example, I have two conditions:
[condition][]The client is invalid = Client( name == null || email == null )
[condition][]All the clients are invalid = forall( Client( name == null || email == null ) )
Note that the second condition just diff the first for the forall command, but the statement inside is equals. In these case, I want to reuse the first condition into the second.
Is it possible? How?
Thank you.
even the most recent version of drools will only let you substitute values into a template from pojo's or a corresponding map as per their documentation here.
This won't work for your use case though.
Since drool files are simply text files, there is nothing preventing you from considering a more powerful templating toolkit.
Possibilities include Apache Velocity, ANTLR or Scala!

Drools: Variables can not be used inside bindings

I get the error
Variables can not be used inside bindings
on the following Drools-Rule Code
rule "minGapsBetweenAppointments"
when
$leftAssignment : AppointmentRequest(feasibleAppointment != null)
$totalValue : Number( ) from accumulate(
AppointmentRequest(feasibleAppointment != null,
$leftAssignment.requestId != requestId,
$quality : this.getOccupiedSurroundingsValue($leftAssignment)),
sum( $quality )
) // ERROR LINE
then
scoreHolder.addSoftConstraintMatch(kcontext, $totalValue.intValue());
end
Although i found this post from another question, it's not helping me much, as I need to call function getOccupiedSurroundingsValue for all other AppointmentRequests, as they're related.
Any help appreciated.
That code should work. There's nothing wrong with it as far as I can see.
Double check if you it's exactly the same as the code you're executing it. I use similar code in my examples and those work.
If it's OK, then it might be a bug in Drools Expert.
There are 2 ways to proceed:
The easy way: repost this question on the drools mailing list, maybe Edson or Wolfgang sees something I don't. Post a jira if no one sees a user-mistake.
The fastest way to solve your problem: create a new test in MiscTest that proves your case and submit it as pull request. Either you find out what you're doing wrong or you prove without a shadow of a doubt there's a bug we need to fix asap :)