-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load rule set dynamically in KnowledgeEngine #35
Comments
There are several ways of doing this, but for recommending one we need to know more about your use case. What are you trying to accomplish by separating the rules from the engine declaration? |
I would like to add more flexibility in my program in adding all rules in a text or json file for example. |
You may find also interesting the multiple-engine joining method using mixings applied here: https://github.com/buguroo/pyknow/blob/develop/docs/talks/Sistemas%20Expertos%20en%20Python%20con%20PyKnow%20-%20PyConES%202017/Descuentos.ipynb |
Thank you very much for these resources ! |
Did you delete the comment after reopening the issue? |
Hello, def set_goal(args):
def goal(self):
self.declare(Fact("GOAL"))
print("GOAL REACHED")
return Rule(args)(goal)
class OtherTestEngine(KnowledgeEngine):
rule1 = set_goal(Fact("A"))
def __init__(self, goal):
super().__init__()
self.rule2 = set_goal(goal) test = OtherTestEngine(Fact("B"))
test.reset()
print(test.get_rules())
test.declare(Fact("A"))
print(test.facts)
test.run()
print(test.facts) output :
And, test.reset()
test.declare(Fact("B"))
print(test.facts)
test.run()
print(test.facts) output :
Do you have any suggestion about the way to fix that ? EDIT : i got a dirty way to fix that def solve_stuff(goal, facts):
class Engine(KnowledgeEngine):
rule1 = set_goal(goal)
@Rule(Fact("A"), Fact("B"))
def rule2(self):
self.declare(Fact("C"))
e = Engine()
e.reset()
for f in facts:
e.declare(f)
e.run()
return e
solution = solve_stuff(Fact("C"), [Fact("A"), Fact("B")]) output :
|
Hi @Bonzeye , thank you for reporting this, we lack some documentation about the behavior of the matcher and rule generation. The key point here is WHEN the matcher generates the RETE network, it is done ONLY on So, your code: def set_goal(args):
def goal(self):
self.declare(Fact("GOAL"))
print("GOAL REACHED")
return Rule(args)(goal)
class OtherTestEngine(KnowledgeEngine):
rule1 = set_goal(Fact("A"))
def __init__(self, goal):
super().__init__()
self.rule2 = set_goal(goal) will not work, but if you call class OtherTestEngine(KnowledgeEngine):
rule1 = set_goal(Fact("A"))
def __init__(self, goal):
self.rule2 = set_goal(goal)
super().__init__() # <-- AFTER the rule creation
Therefore, any code creating dynamic rules before There are several ways of doing this and we have to document about it. Additionally when we finish #25, the dynamic generation of rules will be less of a necessity. |
tyvm for the fast answer, it helped me a lot. |
Hi,
I am new to PyKnow and I am attempting to make a functionnality that will allow me to load the set of Rules from an external file.
I tried to use set_attr but without success the interpreter doesn't like the RULE decorator. Do you know a manner to do it properly ?
Thank you in advance for your time and consideration
The text was updated successfully, but these errors were encountered: