Frage

Ich experimentiere damit PyClips und ich möchte es eng in Python integrieren, sodass bei Aktivierung einer Regel eine Python-Funktion aufgerufen wird.

Folgendes habe ich bisher:

import clips

def addf(a, b):
    return a + b

clips.RegisterPythonFunction(addf)

clips.Build("""
(defrule duck
  (animal-is duck)
  =>
  (assert (sound-is quack))
  (printout t "it’s a duck" crlf))
  (python-call addf 40 2 )
""")

Wenn ich jedoch die Tatsache behaupte, dass „Tier eine Ente ist“, wird meine Python-Funktion NICHT aufgerufen:

>>> clips.Assert("(animal-is duck)")
<Fact 'f-0': fact object at 0x7fe4cb323720>
>>> clips.Run()
0

Was mache ich falsch?

War es hilfreich?

Lösung

Es gibt eine falsch platzierte Klammer, die die Regel zu früh schließt und das weglässt python-call:

clips.Build("""
(defrule duck
  (animal-is duck)
  =>
  (assert (sound-is quack))
  (printout t "it's a duck" crlf))
  (python-call addf 40 2 )       ^
""")                      ^      |
                          |   this one
                          |
                      should go here

Wenn Sie überprüfen möchten, ob die addf tatsächlich 42 zurückgegeben, konnte das Ergebnis gebunden und ausgedruckt werden:

clips.Build("""
(defrule duck
  (animal-is duck)
  =>
  (assert (sound-is quack))
  (printout t \"it's a duck\" crlf)
  (bind ?tot (python-call addf 40 2 ))
  (printout t ?tot crlf))
""")


clips.Assert("(animal-is duck)")
clips.Run()
t = clips.StdoutStream.Read()
print t
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top