0

我已经在jess中编写了这段代码,但我一直遇到这个错误,我不明白为什么?

(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)

我得到这个错误???

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.

问题是Jess正在寻找一个名为 price 的函数,但我想使用(price ... ...)
任何帮助都是好的事实 :) Yaser

4

1 回答 1

0

实际上,我认为错误消息很清楚;没有名为 的函数price,但表达式(price ?product ?price)被解释为对该名称的函数的调用。

我明白你想做getTotalPrice什么,但我不明白你的实现;我认为您误解了该apply函数的作用-也许您将它与其他语言中类似名称的函数混淆了。

无论如何,getTotalPrice用 Jess 编写的方法是使用查询;看到这里了解这些。

于 2013-11-08T23:14:11.690 回答