Jess pattern matching 'not' condition on rule

154 views Asked by At

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.

1

There are 1 answers

0
laune On BEST ANSWER

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.