JuHong Jung
JuHong Jung

Reputation: 226

Jess pattern matching 'not' condition on rule

I'm playing on jess. I want to make rule that is not using '~'.

Here is my code.

(clear)
(reset)

(deftemplate person
    (slot name)
    (slot eyes)
    (slot hair))

(defrule person-without-brown-hair
    (person (name ?name) (hair ?hair&:(not brown)))
=>
    (printout t ?name " does not have brown hair." crlf))

(assert (person (name "Jeon") (eyes green) (hair brown)))
(assert (person (name "Park") (eyes blue) (hair red)))

(run)

What I expected was variable ?hair is not brown.

Upvotes: 0

Views: 154

Answers (1)

laune
laune

Reputation: 31290

Note that the function (not ...) negates its argument. TRUE is true, FALSE is false, and any other symbol is true.

To test whether a slot value is equal or not equal to some value, use (eq ...) or (neq ...) or (= ...) or (<> ...).

(defrule person-without-brown-hair
    (person (name ?name) (hair ?hair&:(neq ?hair brown)))
=>
    (printout t ?name " does not have brown hair." crlf))

Result:

Park does not have brown hair.
1

Direct matching is, of course, preferable.

Upvotes: 2

Related Questions