Question

i have written this code in jess but i keep gettin this error and i don't understand why?

(deffacts prices (price apple 125) (price chips 45) (price cola 110) (price egg 700))
(defrule createBill (buy ?n $?) => (assert (bill ?n 0)))
(deffunction getTotalPrice (?list)
    (bind ?result 0) (foreach ?product $?products 
        (price ?product ?price)
        (bind ?result (+ ?result ?price)))
    (return ?result))
(defrule calculate ?i<-(bill ?n $?) (buy ?n $?products)  =>  (retract ?i) (bind ?result (getTotalPrice $?products))(assert (bill ?n ?result)))
(reset)
(assert (buy yaser cola egg))
(run)

and i get this error ???

Jess reported an error in routine Funcall.execute
        while executing (price ?product ?price)
        while executing (foreach ?product $?products (price ?product ?price) (bind ?result (+ ?result ?price)))
        while executing defrule MAIN::calculate
        while executing (run).
  Message: Undefined function price.
  Program text: ( run )  at line 41.

The problem is Jess is looking for a function named price but i want to use the fact (price ... ...)
any help is good :) Yaser.

Was it helpful?

Solution

Actually the error message is pretty clear, I think; there's no function named price, but the expression (price ?product ?price) is interpreted as a call to a function by that name.

I understand what you want getTotalPrice to do, but I can't understand your implementation; I think you're mistaken about what the apply function does -- maybe you're confusing it with a similarly named function in some other language.

Anyway, the way to write getTotalPrice in Jess would be by using a query; see here to learn about those.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top