Salahuddin
Salahuddin

Reputation: 1719

Compare symbol variable to many values

I'd like to compare a variable that holds a symbol to many values, so that if any of them matches the variable, the CE is satisfied. Here is a minimal example:

(defrule compare-students
  ?x <- (Student (FirstName ?n))
  (or (eq ?n John) (eq ?n Beter) (eq ?n Sarah))
  =>
  (modify ?x (SecondName ?n)))

When I compile the constructs file to c code, I got something like this:

Defining defrule: compare-students +j+j+j
=j=j+j+j
=j=j+j+j

Is this the right way to do that?

Thanks

Upvotes: 0

Views: 97

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

Preferably use this:

(defrule compare-students
  ?x <- (Student (FirstName ?n&John | Beter | Sarah)
                 (SecondName ~?n))
  =>
  (modify ?x (SecondName ?n)))

Or alternately this:

(defrule compare-students
  ?x <- (Student (FirstName ?n)
                 (SecondName ~?n))
  (test (or (eq ?n John) (eq ?n Beter) (eq ?n Sarah)))
  =>
  (modify ?x (SecondName ?n)))

The first uses pattern matching constraints for brevity and the second uses the test conditional element (CE) to indicate that the following syntax is a function call to be evaluated, not a pattern to be matched. In your original rule, you don't use the test CE, so the "or" in that rule is an "or" conditional element. It will attempt to match eq facts rather than make a function call.

Upvotes: 1

Related Questions