# RE: JESS: Getting Facts in Sorted Order

 Thanks, but that only matches on the single set of facts with the lowest priority. I want to get all of the facts, I just want them to be given to the rule in order of priority. For example, given the following set of facts:   (assert     (SomethingIWant         (Name "TiVo")         (Price 200)         (Priority 1)     ) )   (assert     (SomethingIWant         (Name "Tires")         (Price 800)         (Priority 1)     ) )   (assert     (SomethingIWant         (Name "Carb")         (Price 400)         (Priority 1)     ) )   (assert     (SomethingIWant         (Name "DVD Burner")         (Price 400)         (Priority 5)     ) )   (assert     (SomethingIWant         (Name "Wheels")         (Price 1000)         (Priority 2)     ) )   (assert     (SomethingIWant         (Name "shocks")         (Price 900)         (Priority 2)     ) )   I get the following output:   TRUE Jess> (run) Found: Carb, 400, 1 Found: Tires, 800, 1 Found: TiVo, 200, 1 3 Jess>     The output I want to see would be something like:   Found: Carb, 400, 1 Found: Tires, 800, 1 Found: TiVo, 200, 1 Found: Wheels, 1000, 2 Found: shocks, 900, 2 Found: DVD Burner, 400, 5   If I can get that to work, I’d have some additional logic to stop processing at some point. In this example, maybe I’d keep a running total of items and stop when I’ve reached my budget, but I want to make sure I get the higher priority items first.   Should I maybe be setting another fact to the “current priority”, initialize it to 1 and just match facts that have that priority, then increment the current priority fact and match against facts that have that new priority and continue through the valid range of priorities? That just feels like I’m trying to impose my procedural thinking on the rules engine. So, I’m looking for a more rules friendly approach. Or maybe have separate rules that match on each individual priority and have the “then” part of each rule call the same function, and use salience to fire the rules in the desired order?   Thanks,   Eric   From: owner-jess-users@sandia.gov [mailto:owner-jess-users@sandia.gov] On Behalf Of Dusan Sormaz Sent: Tuesday, February 14, 2006 7:51 AM To: jess-users@sandia.gov Subject: Re: JESS: Getting Facts in Sorted Order   Eric, here is the rule that will work: (defrule items-to-buy     ?ItemFound <- (SomethingIWant   (Name ?ItemName)                             (Price ?ItemPrice)                             (Priority ?ItemPriority1)                             (Selected false ) )  (not (SomethingIWant (Selected false) (Priority ?p&:(< ?p ?ItemPriority1))))       =>     (printout t  "Found: " ?ItemName ", " ?ItemPrice ", " ?ItemPriority1 crlf)      (modify ?ItemFound (Selected true) )  ) In addition you should execute (run) at the end of the file, or from JESS prompt Dusan Sormaz PS. If you see smiley in (not ...) clause replace it by : ( . At 09:30 PM 2/13/2006, you wrote: OK, I’m very, very new to JESS and rules engines, so please forgive me for a very basic question. I have read “JESS in Action” but I’m still stumped on this one. I get that the rules engine will match all possible facts against my rule(s). I think of it as kind of an implied iteration, versus a loop in a procedural language. But, I’d like to have some control over the order in which those facts are applied to my rule. For example, imagine I have a database of stuff on my wish list. I keep the name of each item and the price. And I have a rule that will match on all of the entries in the database, and do something like make a shopping list and keep a running total. So far, so good. But, I have a limited budget, and an infinite wish list. So, I want to make sure my rule matches the things I want the most, first. And this is where I’m stuck. I can add a “Priority” slot. But, how do I write the rule to match on the higher priority facts, first? I played with a (not …) statement I saw in “JESS in Action”, but it doesn’t quite do what I’m looking for. I may have 3 priority 1 items and I’ll want the rule to match on all of them, and I might have 2 priority 2 items and I’ll want to rule to match on both of those. But, I want the rule to match on all of the priority 1 items before any of the priority 2 items. I pasted my test code below. I’m sure I just haven’t gotten my mind wrapped around how you do things in this environment, yet. But, I could use a nudge in the right direction on this one. Thanks in advance for any suggestions!   Eric   ; Test Prioritizing Facts.   (printout t crlf crlf)   (clear)   (reset)   (deftemplate SomethingIWant "Stuff I want"     (slot Name (default Whatever) )     (slot Price (default 10) )     (slot Priority (default 0) )     (slot Selected (default false) ) )   (assert     (SomethingIWant         (Name "TiVo")         (Price 200)         (Priority 1)     ) )   (assert     (SomethingIWant         (Name "DVD Burner")         (Price 400)         (Priority 5)     ) )   (assert     (SomethingIWant         (Name "Wheels")         (Price 1000)         (Priority 2)     ) )   (defrule items-to-buy       ?ItemFound <- (SomethingIWant   (Name ?ItemName)                             (Price ?ItemPrice)                             (Priority ?ItemPriority1)                             (Selected false ) )       ?ItemFound2 <- (SomethingIWant   (Name ?ItemName2)                             (Price ?ItemPrice2)                             (Priority ?ItemPriority2) )     ;    (not (eq ?ItemName ?ItemName2 ) )       (not (SomethingIWant (Priority ?ItemPriority2&:(> ?ItemPriority1 ?ItemPriority2 )))) ;    (test (<= ?ItemPriority1 ?ItemPriority2 ) )       =>       (printout t  "Found: " ?ItemName ", " ?ItemPrice ", " ?ItemPriority1 crlf)       (modify ?ItemFound (Selected true) )   )     ********************************************************************* * Dušan Šormaz, PhD, Associate Professor                      * Ohio University * Industrial and Manufacturing Systems Engineering Department * 277 Stocker Center, Athens, OH 45701-2979 * phone: (740) 593-1545 * fax:   (740) 593-0778  * e-mail: [EMAIL PROTECTED] * url: http://www.ent.ohiou.edu/~sormaz *********************************************************************