edit this page - page history - about editing

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 GeneratedElements 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.
Categories: Drools | Code Snippets
edit this page - what links to here? - page history - top
Last edited by jevon jevon 119 months ago