Wednesday, April 07, 2010

Goodbye Conflict Resolution Strategy, Hello Rule Dependency Meta Rules

I've added a new section to the Drools Language Enhancement ideas wiki page discussing meta-rules to help declaratively control rule ordering. Here is what I have so far, feedback welcome, you can find the original content here.

Rule Dependency Meta-Rule Language

When a terminal node is matched instead of adding the Activation to the agenda it inserts it into the WorkingMemory. We have a special builder that allows easy access to the contents.

All declarations are typed fields for the Activation fact, based on the "name" field. So the name field is mandatory. All FactHandles are available via an array accessor, which has type inference for the element being used. We also all bindings on the Activation fact to work this way too. Act is used for compactness, we'll allow that to be optionally user defined:
act1 : Act( someDeclaration == X, fact[0] == Y )
act2 : Act( someDeclaration.value > act1.someDeclaration.value )

Normal facts can also be matched. The RHS of the rule is side effect free, you cannot modify or insert facts; this allows the RHS to execute as soon as it's matched. What you can do is setup rule dependencies - where one rule blocks another:
act1.blockedBy( act2 ).until( Fired )
act1.blockedBy( act2 ).until( IsFalse )

We can even allow facts to block:
act1.blockedBy( someFact )

This means the act1 activation is blocked until a rule executes:
act1.unblockedBy( someFact )

We can probably add an override, something like:
act1.unblockAll()

Only when an Activation is no longer blocked will it be placed on the Agenda as normal.

If an activation on the agenda has not yet fired and something attempts to block it, it will be removed from the agenda until it is no longer blocked.

For this to be effective, especially for large systems, it will need to be combined with design time authoring help.

This work will be eventually be combined with further enhancements to help with parallel execution, in resulting conflicts, see the "Parellel Meta-Rule Language" heading.



9 comments:

  1. that looks really cool.
    It's a more declarative approach!

    ReplyDelete
  2. Effectively decoupling the conflict resolution from the engine. Cool - the rules governing conflict resolution are now implemented using the engine itself. Will the meta-rules be pluggable allowing people to implement their own conflict resolutions... not that I foresee the need, just adding to the flexibility and extensibility...

    ReplyDelete
  3. CRS will still be there and govern how the Agenda itself executes. But the meta rules provide a more declarative approach that works prior to the Agenda getting involved. It's similar in a way to ruleflow, in that it's just stopping activations reaching the Agenda. Just that ruleflow is procedural in nature and this will probably be declarative.

    Not sure what you mean by pluggable, the meta-rules will just be normal rules so you can write anything you want there.

    I think the hard bits will be getting regexp style matching on names, or even matching meta data. Allowing the Activations to match against groups of rules; as at that point you lose type safety, as different Activations could have different declarations.

    ReplyDelete
  4. I'm just evaluating Drools as some kind of calculation engine. What I'm suprised that it does not have conflict reslotuion based on dependencies between rules.
    It would be very useful in many places.

    ReplyDelete
  5. "What I'm suprised that it does not have conflict reslotuion based on dependencies between rules."

    This is not the normal, and not something the majority of commercial or open source production rule engines have. Most Production rule systems are based around some variation of salience+depth or MEA/LEX.

    We are exploring this idea, but it's very much a research item.

    ReplyDelete
  6. I understand that in many cases salience is important, becouse rules order is given.

    But I just look for some kind of calculation machine with advanced capblities for open source ERP where is not convinient to set up order of rules manulally.

    What is even worse I can not find way to set up silence for Order fact lower than OrderLine becouse it seems that for drools there is no conflict there as there are separate facts.

    Automatic ordering of facts would give spreadsheet like capablities.

    ReplyDelete
  7. "What is even worse I can not find way to set up silence for Order fact lower than OrderLine becouse it seems that for drools there is no conflict there as there are separate facts."

    I'm not sure what it is you are saying here, maybe you can rephrase it. Salience does not have to be a number, it can also be an expression on the bound facts, as long as it resolves to a number. So you could have salience determined by the price comparison of two items.

    "Automatic ordering of facts would give spreadsheet like capablities."
    See above, but there is also "ordering of rules" if you use sequential mode.

    ReplyDelete
  8. As an example of dynamic salience look at this drl, where we need to chose the correct rule with the lowest distance from the monster to the pacman:
    http://fisheye.jboss.org/browse/JBossRules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/pacman/monster.drl?r=HEAD

    ReplyDelete
  9. I wold like to have "live order". I change product and Drools reason new price and change it. Then it changes value netto, calculates tax and value brutto for line. It same for total order.
    It is easy to write rules for it. But I can not find way to say that value brutto rule should be calculated only on change of value netto or tax type.

    I get into infinte loop now.

    Is there way to say that rule should be fired on change of some property ?

    ReplyDelete