nemonik/Intellect

Issue in detecting instance variables in fact checking for "while"

Opened this issue · 0 comments

@nemonik
@scotte
@dicato
Hi,

when rule is not working properly for me
Variables i am passing to fact is being detected

helloworld.policy

import logging
from helloworld import Check
first_sum = 0
second_sum = 0


location = ["China", "Malaysia"]

rule "restrict who are under 18":
        agenda-group "test_d"
        when:
                $check := Check(age < 18)
        then:
                print($check)
                log("{0} is with age {1} from {2}, no access allowed".format($check.name, $check.age, $check.country),
                    "example", logging.DEBUG)
                #$check.getDetails()

rule "restrict who are from restricted countries":
        agenda-group "test_d"
        when:
                $check := Check(country in location)
        then:
                print($check)
                log("{0} is with age {1} from {2}, no access allowed".format($check.name, $check.age, $check.country),
                    "example", logging.DEBUG)
                #$check.getDetails()

helloworld.py

# import ipdb;ipdb.set_trace()
import sys, logging
from intellect.Intellect import Intellect
from intellect.Intellect import Callable

class MyIntellect(Intellect):
        pass

class Check():
    def __init__(self, name, age, country) :
        self.name = name
        self.age = age
        self.country = country
    @Callable
    def getDetails(self):
        print("{0} is with {1} and from {2}".format(name, age, country))

if __name__ == "__main__" :
    # set up logging for the example
    logger = logging.getLogger('example')
    logger.setLevel(logging.DEBUG)
    consoleHandler = logging.StreamHandler(stream=sys.stdout)
    consoleHandler.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s%(message)s'))
    logger.addHandler(consoleHandler)

    # training with rules
    myIntellect = MyIntellect()
    policyHelloWorld = myIntellect.learn(Intellect.local_file_uri("helloworld.policy"))
    logging.getLogger("example").debug("Asking the engine to learn a BuyOrder for {0} sheep.".format(Check))

    # age, country input from user
    name = raw_input("Enter your name:")
    age = int(input("Enter your age:"))
    country = raw_input("Enter your country:")
    print(name, age,country)
    myIntellect.learn(Check(name, age, country))

    # executing the rules
    myIntellect.reason(["test_d"])

O/P:
image

But when i am initializing variables "age", "country" in helloworld.policy,
then fact checking in while is taking these values instead of taking from actual instance attributes

helloworld.policy

import logging
from helloworld import Check
first_sum = 0
second_sum = 0

age=0
country=""
location = ["China", "Malaysia"]

rule "restrict who are under 18":
        agenda-group "test_d"
        when:
                $check := Check(age < 18)
        then:
                print($check)
                log("{0} is with age {1} from {2}, no access allowed".format($check.name, $check.age, $check.country),
                    "example", logging.DEBUG)
                #$check.getDetails()

rule "restrict who are from restricted countries":
        agenda-group "test_d"
        when:
                $check := Check(country in location)
        then:
                print($check)
                log("{0} is with age {1} from {2}, no access allowed".format($check.name, $check.age, $check.country),
                    "example", logging.DEBUG)
                #$check.getDetails()

O/P:
image

In above output, inside "while", age=0 nd country="" are being treated which makes rule1 True and rule2 False

But in examples given in offical git doc()
instance attributes are directly used in "while" checking

image

Where its going wrong ?