Fork me on GitHub
#clara
<
2020-12-01
>
PB19:12:05

Is there a way to update a record? I want to specifically use this with rules that deal with total amounts. I.e. A previous rule would consume part of the total

ethanc19:12:15

Can you give a small example of the use-case? From my knowledge, clara doesn’t play nice with stateful manipulations. That being said, it sounds as if you might be able to use an accumulator, but that depends on the use-case

PB20:12:09

I go shopping, and I have $100 to spend. I want to buy 10 items, which total $200. Each item has a priority, which items can I buy?

PB20:12:04

I'm thinking I'm going to have to create a new fact and use an accumulator to sort by priority

jeff.engebretsen20:12:26

I’ve only casually used clara but its seems like having $100 will be a fact and if that fact changes it’d cause the session to undo it’s fired rules and re-fire.

PB20:12:29

My hope was that it if I could update the root fact with the amount of money left and use salience to trigger rules, and when I ran out of money, the rest of the rules wouldn't trigger

PB20:12:48

@jeff.engebretsen that's my understanding as well

jeff.engebretsen20:12:40

I think like @ethanc said, the accumulator would take in that $100 fact and 10 item priority list fact and spit out 0-10 items

PB20:12:18

Thank you both

PB21:12:10

So it looks like I'll likely need my own reducer here. I only want to include the items I can buy. But I'm not really sure how to pass in a sentinel amount.

(defrule find-what-i-can-get
  [:money (= ?amount (:amount this))]
  [?items <- (acc/reduce-to-accum
              (fn [coll item]
                ;; some way to access `?amount` variable?
                ...))])

PB21:12:11

I have also tried to pass this into the convert and combine fns

PB21:12:43

Is there a way to do this. Or do I need to add the total amount I have available to the items i'm iterating over?

PB21:12:45

It also strikes me that I'd need to sort by priority before the reduce

PB21:12:39

I am not sure this is possible to do in accumulators

PB21:12:20

I guess I could use the combine-fn to sort, but I still can't find a way to pass the ?amount into that fn to limit which items are put out

ethanc21:12:31

Accumulators do not allow access of constraint bound vars. It might require doing some of this logic in RHS of the rule. Something like:

(defrule find-what-i-can-get
  [:money (= ?amount (:amount this))]
  [?items <- (acc/all) :from [:item (< (:cost this) ?amount)]]
  [:test (seq ?items)]
  =>
  (let [sorted-priority (sort-by ... ?items)
        ... <reduce logic>]
  <insert statement>)

PB21:12:26

I don't know why I didn't consider that. Too much tunnel vision I think

PB21:12:27

Thank you

ethanc21:12:58

added a test node to eliminate execution where all items are unaffordable

PB21:12:12

Hmm (< (:cost this) ?amount) would just ensure the cost of 1 item isn't greater than the total amount I have, yes?