rules icon indicating copy to clipboard operation
rules copied to clipboard

puzzled in Action Batches demo

Open brealisty opened this issue 5 years ago • 1 comments

https://github.com/jruizgit/rules/blob/master/docs/py/reference.md#action-batches

` with ruleset('expense'): # this rule will trigger as soon as three events match the condition @when_all(count(3), m.amount < 100) def approve(c): print('approved {0}'.format(c.m))

# this rule will be triggered when 'expense' is asserted batching at most two results       
@when_all(cap(2),
          c.expense << m.amount >= 100,
          c.approval << m.review == True)
def reject(c):
    print('rejected {0}'.format(c.m))

post_batch('expense', [{ 'amount': 10 }, { 'amount': 20 }, { 'amount': 100 }, { 'amount': 30 }, { 'amount': 200 }, { 'amount': 400 }]) assert_fact('expense', { 'review': True }) `

when I delete cap(2): rejected None rejected None rejected None after changing c.m to c._m, it will be ok: rejected {'expense': {'amount': 400}, 'approval': {'review': True}} rejected {'expense': {'amount': 200}, 'approval': {'review': True}} rejected {'expense': {'amount': 100}, 'approval': {'review': True}}

is this a inner mechanism? or something wrong?

brealisty avatar Jul 09 '20 06:07 brealisty

I am no expert on this library, just poked around a little:

This has to do with the fact that you are passing a context (c.expense), which is a Closure obj type. When the C engine sees this, returns a dictionary and it is stored in self._m member of that object. Alternatively, when it is an event, it happens that it is stored in a list, inside self.m member.

Why this happens exactly is faily complicated because requires diving into the C extension itself...

aijanai avatar Sep 04 '20 17:09 aijanai