clara

http://www.clara-rules.org/
Heather 2021-05-07T21:08:31.059100Z

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. 2021-05-07T21:19:09.062100Z

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

Heather 2021-05-07T21:20:58.062400Z

ah interesting. I’ll look into that.

Mario C. 2021-05-07T21:24:05.063400Z

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

Mario C. 2021-05-07T21:24:28.064200Z

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

phronmophobic 2021-05-07T21:24:30.064300Z

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. 2021-05-07T21:26:17.065200Z

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

Mario C. 2021-05-07T21:27:01.065700Z

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

😎 1
phronmophobic 2021-05-07T21:30:30.067600Z

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