[jboss-jira] [JBoss JIRA] (DROOLS-365) Rule attribute for adding inverse of consequence as rule condition
Davide Sottara (JIRA)
jira-events at lists.jboss.org
Sat Dec 7 17:58:06 EST 2013
[ https://issues.jboss.org/browse/DROOLS-365?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12929351#comment-12929351 ]
Davide Sottara edited comment on DROOLS-365 at 12/7/13 5:56 PM:
----------------------------------------------------------------
I see your use case. Here is another possible way to do it. If you like it, we may work on getting it into master by default.
We need the notion of "Undercutting", borrowed from non-monotonic logic. This is the core idea, to be expanded on as needed.
Imagine you have two rules potentially triggered by the same object(s), and you want one to take precedence over the other.
Use an annotation like @Undercuts in the first, passing the name of the other rule as an argument.
{code}
rule "handle all Bars of type second"
@Undercuts( others )
when
$bar : Bar( type == 'second', $total : total )
then
System.out.println( "Second bars " + $total );
end
rule "others"
when
$bar : Bar( $total : total )
then
System.out.println( "Other bars " + $total );
end
{code}
In the main, enable the declarative agenda:
{code}
knowledgeBaseConfiguration.setOption( DeclarativeAgendaOption.ENABLED );
{code}
And finally add a "utility" rule. This rule is generic, but its purpose is to enable the desired functionality:
{code}
rule "Undercut"
@activationListener('direct')
when
$m : Match( $handles : factHandles )
$v : Match( rule.name == $m.Undercuts, factHandles == $handles )
then
System.out.println( "Activation of rule " + $m.getRule().getName() + " overrides " + $v.getRule().getName() + " for tuple " + $handles );
kcontext.cancelMatch( $v );
end
{code}
Let me know if this would be enough for your concrete use case.
Important : I fixed a bug while creating the test, so it won't work until my PR is merged on Monday
Davide
was (Author: dsotty):
I see your use case. Here is another possible way to do it. If you like it, we may work on getting it into master by default.
We need the notion of "Undercutting", borrowed from non-monotonic logic. This is the core idea, to be expanded on as needed.
Imagine you have two rules potentially triggered by the same object(s), and you want one to take precedence over the other.
Use an annotation like @Undercuts in the first, passing the name of the other rule as an argument.
{code}
rule "handle all Bars of type second"
@Undercuts( others )
when
$bar : Bar( type == 'second', $total : total )
then
System.out.println( "Second bars " + $total );
end
rule "others"
when
$bar : Bar( $total : total )
then
System.out.println( "Other bars " + $total );
end
{code}
In the main, enable the declarative agenda:
{code}
knowledgeBaseConfiguration.setOption( DeclarativeAgendaOption.ENABLED );
{code}
And finally add a "utility" rule. This rule is generic, but its purpose is to enable the desired functionality:
{code}
rule "Undercut"
@activationListener('direct')
when
$m : Match( $handles : factHandles )
$v : Match( rule.name == $m.Undercuts, factHandles == $handles )
then
System.out.println( "Activation of rule " + $m.getRule().getName() + " overrides " + $v.getRule().getName() + " for tuple " + $handles );
kcontext.cancelMatch( $v );
end
{code}
Let me know if this would be enough for your concrete use case.
Davide
> Rule attribute for adding inverse of consequence as rule condition
> ------------------------------------------------------------------
>
> Key: DROOLS-365
> URL: https://issues.jboss.org/browse/DROOLS-365
> Project: Drools
> Issue Type: Feature Request
> Security Level: Public(Everyone can see)
> Affects Versions: 5.5.0.Final
> Reporter: Adar Dembo
> Assignee: Mark Proctor
>
> I have quite a few rules that look like this:
> {noformat}
> rule "if foo then bar"
> when
> Foo($a : a, $b : b)
> not Bar(a == $a, b == $b)
> then
> insert(new Bar($a, $b));
> end
> {noformat}
> This is a fairly common pattern for me: a rule should not activate if the facts generated by its consequence were already asserted. It's a pattern I use extensively to generate linear (i.e. non-repeating) workflows.
> Ideally I'd be able to express it as follows:
> {noformat}
> rule "if foo then bar"
> condition-on-inverse-of-consequence
> when
> Foo($a : a, $b : b)
> then
> insert(new Bar($a, $b));
> end
> {noformat}
> And {{condition-on-inverse-of-consequence}} would cause Drools to generate the right kind of condition, which would be the inverse of the fact changes made by the consequence.
> I think {{lock-on-active}} is similar but harsher: it prevents the rule from firing a second time altogether, regardless of input. At least, that's based on my reading of the docs. In my example, that would be equivalent to {{not Bar()}} condition in the rule, which is stricter than {{not Bar(a == $a, b == $b)}}.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the jboss-jira
mailing list