Nicola Ben
Nicola Ben

Reputation: 11367

Checking a object property in LHS

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

Upvotes: 0

Views: 112

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

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

Upvotes: 2

Related Questions