If you want to consider only enabled instances,

(defrule CP::enabled-example-rule
    "Enabled My-Template with the greatest index1 and
among those enabled with the smallest index2."
    (My-Template (id ?id) (enabled TRUE) (index1 ?index1) (index2 ?index2))
    (not (My-Template (enabled TRUE)
                      (index1 ?another-index1:(< ?index1 ?another-index1))))
    (not (My-Template (enabled TRUE) (index1 ?index1)
                      (index1 ?another-index2:(< ?index2 ?another-index2))))
    =>
    (printout t "Enabled My-Template id=" ?id
              " with greatest index1=" ?index1
              " and smallest index2=" ?index2
              " with greatest index." crlf))

If you want to only fire the maximal instance when it is enabled,

(defrule CP::example-rule-when-enabled
    "Enabled My-Template with the greatest index1 and
among those enabled with the smallest index2."
    (My-Template (id ?id) (enabled TRUE) (index1 ?index1) (index2 ?index2))
    (not (My-Template (index1 ?another-index1:(< ?index1 ?another-index1))))
    (not (My-Template (index1 ?index1)
                      (index1 ?another-index2:(< ?index2 ?another-index2))))
    =>
    (printout t "My-Template id=" ?id
              " with greatest index1=" ?index1
              " and smallest index2=" ?index2
              " with greatest index is enabled." crlf))

Bob Kirby

At 08:57 AM 10/20/2007, Nicolas Fortin wrote:

>Hello everybody,
>
>I have just added a bit more complexity to the previous rule and now I have 
>the following template:
>
>(deftemplate MAIN::My-Template
>    (slot id (type INTEGER))
>    (slot enabled (default FALSE))
>    (slot index1 (type INTEGER))
>    (slot index2 (type INTEGER)))
>
>What I want to known is among the enabled My-Template with the greatest 
>index1, what are those with the smallest index2.
>
>For example, with the following facts:
>
>(assert (MAIN::My-Template 
>        (id 1) (enabled TRUE) (index1 1) (index2 2)))
>(assert (MAIN::My-Template 
>        (id 2) (enabled TRUE) (index1 1) (index2 1)))
>(assert (MAIN::My-Template 
>        (id 3) (enabled TRUE) (index1 0) (index2 0)))
>
>Facts id 1, 2 and 3 are enabled. Only facts id 1 and 2 have the greatest 
>index1. But among these facts, it is id 2 that has the smallest index2. So id 
>2 is the fact that I want.
>
>Another example, with the following facts:
>
>(assert (MAIN::My-Template 
>        (id 1) (enabled FALSE) (index1 1) (index2 2)))
>(assert (MAIN::My-Template 
>        (id 2) (enabled FALSE) (index1 1) (index2 1)))
>(assert (MAIN::My-Template 
>        (id 3) (enabled TRUE) (index1 0) (index2 0)))
>
>Facts id 3 is the only fact that is enabled, and there is no other enabled 
>fact with greatest index1 and smallest index2. So id 3 is the fact that I want.
>
>Is it possible to do that with only one rule?
>
>Thank you.
>
>Nicolas
>
>> From: [EMAIL PROTECTED]
>> To: jess-users@sandia.gov
>> Subject: RE: JESS: How can I write this rule?
>> Date: Thu, 18 Oct 2007 16:59:19 -0400
>>
>>
>> Hello Ernest,
>>
>>> There's a subtle difference between the solutions Jason and Wolfgang
>>> posted; you're basically using Jason's, but apparently you want
>>> Wolfgang's.
>>>
>>>
>>> Jason's and yours will fire for facts for which no single other fact
>>> has *both* a higher index1 and a lower index2. You're using a single
>>> "not" pattern, which restricts these two conditions to applying to a
>>> single other facts.
>>>
>>> Wolfgang's will for facts for which *no other fact* has a higher
>>> index1, and *no other fact* has a higher index2. He uses two separate
>>> "not" patterns to describe these two conditions, so each is
>>> considered separately.
>>>
>>> Make sense?
>>
>> It makes a lot of sense. Thank you for your explanation. In fact, I did not 
>> read Wolfgang's solution enough carefully. Sorry Wolfgang.
>>
>> Best regards,
>>
>> Nicolas.
>>
>>
>> On Oct 18, 2007, at 8:30 AM, Nicolas Fortin wrote:
>>
>>>>
>>>> Hello everybody and specially to Jason and Wolfgang. Thank you for
>>>> your help guys.
>>>>
>>>> First, concerning what you said Jason, I don't want to hunt fact
>>>> with the highest index1 and lowest index2 among all instances of an
>>>> arbitrary number of fact template (e.g. My-Template, Queue, etc.).
>>>> I did a typo (damn copy and paste), you should have seen only My-
>>>> Template. So forget the Queue template.
>>>>
>>>> Second, I have already done an example as you said guys, but what
>>>> puzzled me is that the result is not really what I expected. So
>>>> let's say I have:
>>>>
>>>> (deftemplate MAIN::My-Template
>>>> (slot id (type INTEGER))
>>>> (slot index1 (type INTEGER))
>>>> (slot index2 (type INTEGER)))
>>>>
>>>> (reset)
>>>>
>>>>
>>>> (assert (MAIN::My-Template
>>>> (id 1) (index1 1) (index2 0)))
>>>> (assert (MAIN::My-Template
>>>> (id 2) (index1 1) (index2 0)))
>>>> (assert (MAIN::My-Template
>>>> (id 3) (index1 0) (index2 0)))
>>>>
>>>>
>>>> (defrule MAIN::greatest-one-smallest-two
>>>> "Find the My-Template with greatest index1 and smallest index2"
>>>> (MAIN::My-Template
>>>> (id ?id1)
>>>> (index1 ?index1_1)
>>>> (index2 ?index2_1))
>>>> (not (MAIN::My-Template
>>>> (id ~?id1)
>>>> (index1 ?index1_2&:(= ?index1_2 ?index1_1))
>>>> (index2 ?index2_2&:(< ?index2_2 ?index2_1 ))))
>>>> =
>>>> (printout t
>>>> "No My-Template fact has both a larger index1 and a smallest index2
>>>> than fact # " ?id1 crlf)
>>>> (printout t
>>>> " index1: " ?index1_1 " index2: " ?index2_1 crlf))
>>>>
>>>> (run)
>>>>
>>>> The output is not what I want, since the third fact (id 3)
>>>> activates the rule. As far as I can understand it should not,
>>>> because its index1 slot is not the greatest. In this example, I
>>>> would be expected that only the first and the second facts activate
>>>> this rule, since they both have the greatest index1 and the
>>>> smallest index2. Maybe I am wrong, but it seems for me that Jess is
>>>> performing a *or* rather than an *and* in this example. What is wrong?
>>>>
>>>> Thanks again.
>>>>
>>>> Nicolas
>>>>
>>>>
>>>>
>>>> Date: Thu, 18 Oct 2007 02:56:18 -0400From:
>>>> [EMAIL PROTECTED]: [EMAIL PROTECTED]: Re: JESS:
>>>> How can I write this rule?
>>>> Hi NIcolas,
>>>> A rule like this seems to work for me:(defrule greatest-one-
>>>> smallest-two "Find the My-Template with greatest index1 and
>>>> smallest index2" (MAIN::My-Template (id ?id1)(index1 ?i1_1)
>>>> (index2 ?i1_2)) (not (MAIN::My-Template
>>>> (id ~?id1)
>>>> (index1 ?i2_1&:(= ?i2_1 ?i1_1))
>>>> (index2 ?i2_2&:(< ?i2_2 ?i1_2 )))) = (printout t
>>>> "No My-Template fact has both a larger index1 and a smallest
>>>> index2 than fact # " ?id1 crlf) (printout t
>>>> " index1: " ?i1_1 " index2: " ?i1_2 crlf))It seems a bit
>>>> more complicated if you want to hunt for a particular fact with the
>>>> highest index1 and lowest index2 among all instances of an
>>>> arbitrary number of fact templates ( i.e., my-template, queue,
>>>> etc.) that share these two slots in common
>>>
>>> ---------------------------------------------------------
>>> Ernest Friedman-Hill
>>> Informatics & Decision Sciences Phone: (925) 294-2154
>>> Sandia National Labs FAX: (925) 294-2234
>>> PO Box 969, MS 9012 [EMAIL PROTECTED]
>>> Livermore, CA 94550 http://www.jessrules.com
>>>
>>> --------------------------------------------------------------------
>>> 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]
>> --------------------------------------------------------------------
>> 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]
>--------------------------------------------------------------------
>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