Receiving notification of Drools logical retraction events
I have a knowledge base for performing validation of my data model. Modification events from the UI are posted asynchronously to a separate thread that updates the know开发者_开发知识库ledge base and fires the rules. Validation errors trigger a logical insert of an error object. I can collect these and post events asynchronously back to the UI thread. However, to make it easier to keep the UI up-to-date, I also want to post an event when the user fixes an error – i.e. when an error object is retracted from the knowledge base.
I have two ideas for how to do this, neither of which I like:
I could listen to working memory events from procedural code, but that would violate the encapsulation of the validation functionality within the knowledge base.Alternately, I could insert a flag object paired with my logical insertion of an error object and write a rule that detects un-paired flags, retracts them, and fires the "error fixed" event.
Is there a clean and simple way to activate a rule based on the logical retraction of an error object as described above?
Self-answering so that I can link to this later and find out if there is a better way to do it.
Here's the approach I wound up taking:
When a validation rule is triggered, insertLogical an object with a unique id representing the validation error (e.g. ValidationMessage).
ValidationMessage has a property "flagged", which defaults to false.
Define a rule that triggers on existence of unmarked ValidationMessages. In the RHS, mark the message and make an onAssert call to a global event handler. Insert a second object (e.g. ValidationMessageFlag) with the same id as the ValidationMessage.
Define a rule that triggers on existence of a ValidationMessageFlag, when no corresponding ValidationMessage (with the same id exists). In the RHS, call onRetract in the global event handler. Retract the ValidationMessageFlag.
精彩评论