Fork me on GitHub
#clara
<
2021-05-07
>
Heather21:05:31

Hi, I have a question. I’m doing some basic exploration/learning of clara and rules engines and I’m making a small app that tells you what to wear, depending on the weather. For example, I have rules like these:

(defrule precipitation-more-than-20%-chance
  "keep your head dry"
  [Weather (>= precipitation 20)]
  =>
  (insert! (->Hat :bucket-hat)))

(defrule above-70-wear-a-sun-hat
  "If it's warm, the sun might be in your eyes"
  [Weather (>= temperature 70)]
  =>
  (insert! (->Hat :straw-hat)))
I want hats to be determined by temperature, but allow for a waterproof hat to override any temperature-determined hat, if it’s going to rain. The only way I can do this right now is if my rules are ordered like they are above, with the precipitation rule first and the temperature rule second, but I know for the engine to be robust, the rules should be able to occur in any order. How would I get the waterproof hat to supersede all other hats? Is there there a better way to handle insert! ? I don’t want the rules to be order-dependent like they are right now.

Mario C.21:05:09

Perhaps instead of having the precipitation-rule insert a hat fact. Have it insert a "it-will-rain" fact. And then have the hat rules test against that fact

Heather21:05:58

ah interesting. I’ll look into that.

Mario C.21:05:05

There are different ways to achieve it, not sure what the "best" way is

Mario C.21:05:28

You can keep it that flow and have another rule that decides what hat to wear and pulls in all the hat facts

phronmophobic21:05:30

logically, you want to make decisions based on the properties of hats, rather than just being categorized as a "hat" (what is a hat, really?). some hats provide shade, some don't. some hat's provide rain resistance, some don't. etc.

Mario C.21:05:17

A temperature of 70 could still be a cloudy and rainy day

Mario C.21:05:01

That could have just been a simplified example but just something to keep in mind lol

😎 2
phronmophobic21:05:30

and there's a difference between finding the "optimal" outfit based on the weather and finding all "acceptable" outfits.