I think Richard Kasperowski wrote:

> how would I find the two automobiles that have the same model but 
> different years?
> 
> This rule almost does what I want:
> 
> (defrule find-different-years
>      (automobile (model ?model-1) (year ?year-1))
>      (automobile (model ?model-2) (year ?year-2))
>      (test (eq ?model-1 ?model-2))
>      (test (not (eq ?year-1 ?year-2)))


First, note the following two general rules:
        1) Don't use "test" if you don't need to, and
        2) direct matching is more efficient than function calls.

Therefore, I'd recomment rewriting your rule above as:

 (defrule find-different-years
      (automobile (model ?model) (year ?year-1))
      (automobile (model ?model) (year ?year-2&~?year-1))

Note that using "?model" to match both facts means that both facts
must contain the same slot value. This rule says exactly the same
thing as the original, but does a lot less work during pattern
matching. 

Now, as written, the rule matches any time the two years are
different, which means that any pair will match twice: 1,2 and 2,1 are
both valid matches. To remove the duplicates, just pick one specific
ordering. Since "year" is numeric, this is easy here:

 (defrule find-different-years
      (automobile (model ?model) (year ?year-1))
      (automobile (model ?model) (year ?year-2&:(> ?year-1 ?year-2)))

i.e., the rule matches only one of the two possible orderings, when
?year-1 comes after ?year-2.

---------------------------------------------------------
Ernest Friedman-Hill  
Distributed Systems Research        Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [EMAIL PROTECTED]
Livermore, CA 94550         http://herzberg.ca.sandia.gov

--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------

Reply via email to