Come ottenere un'attivazione della regola per chiamare una funzione Python, usando PYCLIPS
-
12-11-2019 - |
Domanda
Sto sperimentando con Pyclips e voglio integrarlo strettamente con Python, in modo che quando una regola è attivata, chiama una funzione Python.
Ecco cosa ho finora:
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 )
""")
.
Tuttavia, quando asserisco il fatto "animale-è anatra", la mia funzione Python non viene chiamata:
>>> clips.Assert("(animal-is duck)")
<Fact 'f-0': fact object at 0x7fe4cb323720>
>>> clips.Run()
0
.
Cosa sto facendo male?
Soluzione
C'è una staffa malificata che chiude la regola troppo presto lasciando fuori il 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
.
Se si desidera verificare che il addf
restituisse effettivamente 42, il risultato potrebbe essere vincolante e stampato:
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
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow