Fork me on GitHub

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


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


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?


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


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.


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


@jeff.engebretsen that's my understanding as well


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


Thank you both


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?


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


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?


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


I am not sure this is possible to do in accumulators


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


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>)


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


Thank you


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


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