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!
; Test Prioritizing Facts.
(printout t crlf crlf)
(deftemplate SomethingIWant "Stuff I want"
(slot Name (default Whatever) )
(slot Price (default 10) )
(slot Priority (default 0) )
(slot Selected (default false) )
(Name "DVD Burner")
?ItemFound <- (SomethingIWant (Name ?ItemName)
(Selected false ) )
?ItemFound2 <- (SomethingIWant (Name ?ItemName2)
(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) )
- JESS: Getting Facts in Sorted Order Eric W. Bonnett
- Re: JESS: Getting Facts in Sorted Order Dusan Sormaz