Checking a object property in LHS

拟墨画扇 提交于 2019-12-12 05:06:57

问题


I need to check the existence of a value for an o'bject's property in a LHS.

(defrule check-property
    ?room <- (object (is-a ROOM))
    (integerp (send ?room get-property))   ; #1
    =>
    (printout ?*debug-print* "Room " ?room " has property"  crlf))

But it seems to me that #1 is not valuated in LHS. Instead if I put it in RHS, it returns TRUE. Where am I wrong?

Thx, Nic


回答1:


Use the test conditional element to evaluate an expression in the LHS of a rule:

(defrule check-property
    ?room <- (object (is-a ROOM))
    (test (integerp (send ?room get-property)))
    =>
    (printout ?*debug-print* "Room " ?room " has property"  crlf))

It's better to explicitly retrieve the slot value by matching it rather than using the slot accessor as this will cause the condition to be reevaluated whenever the slot value changes:

(defrule check-property
    ?room <- (object (is-a ROOM)
                     (property ?property))
    (test (integerp ?property))
    =>
    (printout ?*debug-print* "Room " ?room " has property"  crlf))


来源:https://stackoverflow.com/questions/37121490/checking-a-object-property-in-lhs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!