Adding the source rule to elements inserted by Drools
In Drools, if you are inserting/updating lots of elements in the working memory, it’s possible to keep track of which rules have been modifying the elements, by adding a listener to the workflow.
workingMemory.addEventListener(new DefaultWorkingMemoryEventListener() {
@Override
public void objectInserted(ObjectInsertedEvent event) {
if (event.getObject() instanceof GeneratedElement) {
GeneratedElement e = (GeneratedElement) event.getObject();
e.setGeneratedRule(event.getPropagationContext().getRuleOrigin().getName());
}
}
});
In this code, all GeneratedElement_s will have an additional parameter, called _generatedRule, which stores the name of the rule. There are lots of other properties you can get from a given Rule. Likewise, there are additional listener methods for retracting and updating objects.
You can also see this in action.