rules icon indicating copy to clipboard operation
rules copied to clipboard

Poll state for existing facts

Open mpettis opened this issue 5 years ago • 4 comments

Is there a way to poll the facts of a session at a given point in time? That is, after the facts have been asserted or posted, can the session be queried for the facts that exist (including facts derived from rules that triggered)? The examples I've come across will print to stdout if a rule is triggered, but I'd like to be able to query and filter the session for facts I care about. I have worked with clara-rules, which has the concept of a query of the session, and I am wondering if there is an analog here. I am working in the python version of this.

http://www.clara-rules.org/docs/queries/

Thanks for this package, it is very helpful.

mpettis avatar Feb 10 '20 19:02 mpettis

(bump)

mpettis avatar Mar 12 '20 04:03 mpettis

Hi, currently the framework doesn't support querying for facts. I have started implementing the API. It will take me a couple more weeks to complete it.

jruizgit avatar Mar 12 '20 05:03 jruizgit

OK, good to know. Thank you again for the great work here.

mpettis avatar Mar 12 '20 16:03 mpettis

I have added support for querying facts. Please use version 2.0.22.

with ruleset('attributes'):
    @when_all(pri(3), m.amount < 300)
    def first_detect(c):
        print('attributes-> P3: {0}'.format(c.m.amount))
        # querying facts inside the action context
        print('attributes-> {0}'.format(c.get_facts()))
        
    @when_all(pri(2), m.amount < 200)
    def second_detect(c):
        print('attributes-> P2: {0}'.format(c.m.amount))
        
    @when_all(pri(1), m.amount < 100)
    def third_detect(c):
        print('attributes-> P1: {0}'.format(c.m.amount))
        
assert_fact('attributes', { 'amount': 50 })
assert_fact('attributes', { 'amount': 150 })
assert_fact('attributes', { 'amount': 250 })
# querying facts outside the action context
print(get_facts('attributes'))

jruizgit avatar Mar 15 '20 02:03 jruizgit