I am using Drools 6.1.0.FINAL and a stateless session to fire all the rules.
I have below 2 simple rules in my .drl file.
rule "Null Check"
when
$paymentHolder : PaymentHolder(pmtRequest.requestDetails == null
|| pmtRequest.requestDetails.paymentData == null
|| pmtRequest.requestDetails.paymentData.accountDetails == null
|| pmtRequest.requestDetails.paymentData.payments == null
|| pmtRequest.requestDetails.paymentData.payments.payment == null
|| pmtRequest.requestDetails.paymentData.payments.payment.get(0).tenderDetails == null
|| pmtRequest.requestDetails.paymentData.payments.payment.get(0).tenderDetails.paymentCardDetails == null
|| pmtRequest.requestDetails.paymentData.payments.payment.get(0).tenderDetails.paymentCardDetails.authorizationInfo == null)
then
System.out.println("Some Manadatory data is null ");
// populate error code and error message
List<ErrorHolder> errors = $paymentHolder.getErrors();
ErrorHolder erroHolder = new ErrorHolder();
errors.add(erroHolder);
erroHolder.setErrorCode(com.wdpr.payment.exception.ErrorCode.MANDATORY_DATA_MISSING);
// drools.halt();
// kcontext.getKieRuntime().halt();
end
rule "Amount is -ve"
when
$paymentHolder : PaymentHolder(errors.size() == 0)
$paymentHolder(pmtRequest.getRequestDetails()!.getPaymentData().getPayments().getPayment().get(0).getAmount().getAmount() < 0)
then
//System.out.println(paymentHolder.getProcessId());
System.out.println("Amount is -ve");
//throw new com.wdpr.payment.exception.PaymentPlatformException(com.wdpr.payment.exception.ErrorCode.DATA_RANGE_INVALID, "Data Range Invalid - Amount", null);
end
I want to do this: if the first rule is true then do not execute any other rules and exit from the .drl file.
I tried following:
Tried to throw a Runtime Exception inside Then block in first rule, But still it is going to second rule where it is failing because of NullPointerException.
Added drools.halt() and kcontext.getKieRuntime().halt() but did not work, still going to next rule.
So I added some error codes in my custom erroHolder in my first rule and checked the same condition in second rule so that it will not get executed but still it is getting executed and throwing NullPointerException.
If I remove the second rule then the first rule executes perfectly and prints the sysout. But when I run my .drl with this two rules it gives me below NPE
stacktrace
Exception in thread "main" java.lang.RuntimeException: Error while creating KieBase[Message [id=1, level=ERROR, path=com/my/payment/rules/validation.drl, line=40, column=0
text=[ERR 102] Line 40:53 mismatched input '!.' in rule "Amount is -ve"], Message [id=2, level=ERROR, path=com/my/payment/rules/validation.drl, line=0, column=0
text=Parser returned a null Package]]
at org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBase(KieContainerImpl.java:349)
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newStatelessKieSession(KieContainerImpl.java:540)
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newStatelessKieSession(KieContainerImpl.java:528)
at com.my.payment.workflow.DroolsRuleProcessor.runRules(DroolsRuleProcessor.java:58)
at com.my.payment.workflow.DroolsRuleProcessor.main(DroolsRuleProcessor.java:81)
My java code snippet
// some code
final StatelessKieSession kSession = this.kContainer.newStatelessKieSession(rulesSession);
// provide the necessary data and execute rules.
kSession.execute(paymentHolder);
Note: I do not want to use salient and activate-group