I tried to implement a recursive version of the factorial example from the
manual. Here is the code:
(do-backward-chaining factorial)
(defquery rec-factorial
(declare (variables ?x))
(factorial ?x ?r)
)
(defrule do-factorial
(need-factorial ?x&:(> ?x 1) ?)
=>
(bind ?res (run-query rec-factorial (- ?x 1)))
(bind ?token (call ?res next))
(bind ?fact (call ?token fact 1))
(bind ?slot (fact-slot-value ?fact __d))
(bind ?r (nth$ ?slot 2))
(assert (factorial ?x (* ?x ?r)))
)
(defrule do-factorial1
(need-factorial ?x&0|1 ?)
=>
(assert (factorial ?x 1))
)
This way I could get a factorial value by calling query
"rec-factorial". But when I run the query it does not recurse deeper and
throws exception (NoSuchElement - see below), obviously because somewhat
it stops firing next rules. I think this simple example should work. Also
calling functions from the head of backchaining rules should be.
Please explain me why these things don't work or where I was
mistaken.
Thank you
Frivolt Gyorgy
Jess reported an error in routine call
while executing (call ?res next)
while executing (bind ?token (call ?res next))
while executing defrule MAIN::do-factorial
while executing (run).
Message: Called method threw an exception.
Program text: ( run ) at line 4.
Nested exception is:
null
java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java:423)
at java.lang.reflect.Method.invoke(Native Method)
at jess.Call.call(ReflectFunctions.java:398)
at jess.FunctionHolder.call(FunctionHolder.java:37)
at jess.Funcall.execute(Funcall.java:270)
at jess.FuncallValue.resolveValue(FuncallValue.java:33)
at jess.Bind.call(Funcall.java:773)
at jess.FunctionHolder.call(FunctionHolder.java:37)
at jess.Funcall.execute(Funcall.java:270)
at jess.Defrule.fire(Defrule.java:224)
at jess.Activation.fire(Activation.java:62)
at jess.Agenda.run(Agenda.java:193)
at jess.Agenda.run(Agenda.java:172)
at jess.Rete.run(Rete.java:979)
at jess.HaltEtc.call(Funcall.java:1468)
at jess.FunctionHolder.call(FunctionHolder.java:37)
at jess.Funcall.execute(Funcall.java:270)
at jess.Jesp.parseAndExecuteFuncall(Jesp.java:1596)
at jess.Jesp.parseSexp(Jesp.java:183)
at jess.Jesp.parse(Jesp.java:62)
at jess.Main.execute(Main.java:123)
at jess.Main.main(Main.java:23)
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------