What exactly are the semantics for or() in 'when'

150 views
Skip to first unread message

Kent McPhee

unread,
Aug 30, 2013, 7:38:49 PM8/30/13
to no...@googlegroups.com
I have a rule that uses or() in a when clause like this:

rule d42 {
  when {
    or(
       v1 : Value v1.id =~ /f39/,
       v2 : Value v2.id =~ /f117/
    );
    r : Result;
  } then {
    r.push("r42");
  }
}

If I assert two Value instances with ids 'f39' and 'f117', the rule ends up firing twice.

I can sort of see why the rule would fire twice. But, in keeping with inclusive-or semantics, I think it's equally valid to expect it to fire once -- at least that's what I expected while I was typing it in.

Being a newbie to rules-based programming, is there some subtle rules/logic thing that I'm missing here?  Is there a way to get the above rule to fire just once when both facts are asserted?

Steven Devijver

unread,
Aug 31, 2013, 4:37:56 AM8/31/13
to no...@googlegroups.com
If there are two Values the rule will always fire twice. Why do you want it to fire only once?

Kent McPhee

unread,
Aug 31, 2013, 3:01:46 PM8/31/13
to no...@googlegroups.com
Correct me if I'm wrong , but I think you are saying that the semantics of or(A,B) is "fire if A" and then later on also "fire if B",  not my initial expectation of "fire once if either A or B or both A and B".   So I believed that the semantics for the rules evaluation is "for each rule, check its 'when' clause against the pool of facts and if the conditions in the clause are met, then fire the rule" -- but it seems that you are saying the semantics are somewhat different wherein rules are being looked at multiple times?

I want the or() thing to act as an 'inclusive or' because I don't want to emit r42 twice when both sides of the or() expression are true. 

Steven Devijver

unread,
Aug 31, 2013, 3:07:56 PM8/31/13
to no...@googlegroups.com
Every fact will be evaluated against every rule. That is until a fact is retracted. If there are two Value facts they will both be evaluated.

Kent McPhee

unread,
Aug 31, 2013, 3:43:17 PM8/31/13
to no...@googlegroups.com
I seem to have fallen into a trap that catches many new rule authors.  The Drools documentation on "Conditional Element or" explains the situation.


dynagent

unread,
Oct 19, 2013, 5:24:18 AM10/19/13
to no...@googlegroups.com
Thinking in semantics web aproach, the rule classifies individuals of some class. In semantic aproach "A OR B" means "A subset JOIN to B subset" of individuals, and "AND" means "subset INTERSECTION". So if two individuals satisfies the OR restriction, both of them should "pass or be classified".

Other logic would be "to exists A OR B", but nools doesnt support EXISTS

Alfonso from DynaGent Software semantics ERP system


dynagent

unread,
Oct 19, 2013, 6:26:00 AM10/19/13
to no...@googlegroups.com
The correct word is UNION, "A OR B" means "A subset UNION B subset"
Reply all
Reply to author
Forward
0 new messages