buguroo / pyknow

PyKnow: Expert Systems for Python
GNU Lesser General Public License v3.0
470 stars 141 forks source link

Declare new fact in RHS function? #49

Open radiantone opened 5 years ago

radiantone commented 5 years ago

Hi I want to declare new facts in rule RHS. Is there an example of this?

Thanks!

8ball030 commented 5 years ago

Hi,

Please see;

''' from pyknow import *

class Greetings(KnowledgeEngine): @DefFacts() def _initial_action(self): yield Fact(action="greet")

@Rule(Fact(action='greet'),
      NOT(Fact(name=W())))
def ask_name(self):
    self.declare(Fact(name=input("What's your name? ")))

@Rule(Fact(action='greet'),
      NOT(Fact(location=W())))
def ask_location(self):
    self.declare(Fact(location=input("Where are you? ")))

@Rule(Fact(action='greet'),
      Fact(name=MATCH.name),
      Fact(location=MATCH.location))
def greet(self, name, location):
    print("Hi %s! How is the weather in %s?" % (name, location))

engine = Greetings() engine.reset() # Prepare the engine for the execution. engine.run() # Run it! '''

From the documentation :)

I think this is what you are after?