jruizgit / rules

Durable Rules Engine
MIT License
1.14k stars 209 forks source link

Clear Ruleset & c #379

Open sabyasachi-basu-i opened 3 years ago

sabyasachi-basu-i commented 3 years ago

I built a basic flask app, to run the rules using API, the first issue I faced was when I was trying to run, the ruleset does not clear for every call. Resulting in an error. I saw #262 already open. Will await for an update on that.

The second problem I faced was while testing https://github.com/jruizgit/rules/blob/master/docs/py/reference.md#events Let's say my first API call is to compare below two post:

post('risk', {'t': 'purchase', 'location': 'CA'})
post('risk', {'t': 'purchase', 'location': 'US'})

After that I send another two post to test the same rule, example:

post('risk', {'t': 'purchase', 'location': 'US'})
post('risk', {'t': 'purchase', 'location': 'US'})

this will create problem, since in the condition:

 @when_all(c.first << m.t == 'purchase' , c.second << m.location != c.first.location)

c.first is still CA but since I made the second API call my expectation is the condition should assign c.first with US and c.second to US thereby (my custom rule - No Fraud detected)

My rule code is below for reference:

from durable.lang import *

def event(body):
    with ruleset('risk'):
        @when_all(c.first << m.t == 'purchase' , c.second << m.location != c.first.location)
        # this condition is to identify actual fraud cases
        def fraud(c):
            print('Fraud detected -> {0}, {1}'.format(c.first.location, c.second.location))
            c.s.result = ('Fraud detected -> {0}, {1}'.format(c.first.location, c.second.location))

        @when_all(c.first << m.t == 'purchase', c.second << m.location == c.first << m.location)
        # this condition is to identify No fraud cases
        def fraudno(c):
            print('No Fraud detected -> {0}, {1}'.format(c.first.location, c.second.location))
            c.s.result = ('No Fraud detected -> {0}, {1}'.format(c.first.location, c.second.location))

I maybe doing something wrong, since, I'm very new to this.

Thanks