Drools engine causes NPE when an attempt is made to remove a fact which caused an exception during insertion into Working Memory
Issue
- If users insert a (corrupt)fact that causes an exception when it is inserted into
Working Memory, users might want to remove this fact fromWorking Memory. When it is attempted to remove the fact, anNPEis thrown byDrools engine. How to solve this kind ofNPE? - To give a background on the issue, while a user tries to insert some facts into
Working Memoryin theLHSof a rule (as shown below) the user is expecting/or assuming that some ofRuntimeExceptionmight be thrown during the process of fact's insertion. Users might suspect that these facts can stay in theWorking Memoryand are corrupted in nature, so they need to be removed. However, in real life example users might not be sure of which facts (among the facts which is inserted) can cause this issue. So, users might want to adopt a generalized approach to effectively remove thecorruptedfacts fromWorking Memory. Here is a simple example which can mock such a situation.
Rule where the corrupted fact is getting inserted.
rule "LHS exception rule"
when
$fact1 : SimpleFact(code == "xyz")
$fact2 : SimpleFact() from
SimpleFactGenerate.generateSimpleFactsThrowingException()
then
System.out.println("Executing the rule still works... ");
end
Fact creation which throws exception.
public class SimpleFactGenerate {
public static List<SimpleFact> generateSimpleFactsThrowingException() {
throw new RuntimeException("RuntimeException is thrown while creating the fact...");
}
...
}
Exception thrown while executing the rule.
2014-10-24 23:13:50,731 (main) [DEBUG -
org.drools.core.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:200)]
Starting Engine in PHREAK mode
java.lang.NullPointerException
at
org.drools.core.phreak.RuleNetworkEvaluator.deleteLeftChild(RuleNetworkEvaluator.java:754)
at
org.drools.core.phreak.PhreakFromNode.doLeftDeletes(PhreakFromNode.java:252)
at org.drools.core.phreak.PhreakFromNode.doNode(PhreakFromNode.java:41)
at
org.drools.core.phreak.RuleNetworkEvaluator.innerEval(RuleNetworkEvaluator.java:348)
at
org.drools.core.phreak.RuleNetworkEvaluator.outerEval(RuleNetworkEvaluator.java:161)
at
org.drools.core.phreak.RuleNetworkEvaluator.evaluateNetwork(RuleNetworkEvaluator.java:116)
at
org.drools.core.phreak.RuleExecutor.reEvaluateNetwork(RuleExecutor.java:193)
at
org.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:68)
at
org.drools.core.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:936)
at
org.drools.core.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1201)
at
org.drools.core.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:952)
at
org.drools.core.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:926)
at
org.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:256)
at
...
Environment
- Red Hat JBoss BRMS (BRMS)
- 6.0.x
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
