oakes / odoyle-rules

A rules engine for Clojure(Script)
The Unlicense
530 stars 20 forks source link

Is there a proper way to reload the rules? #8

Closed frankitox closed 3 years ago

frankitox commented 3 years ago

Hi! Based on what I read on the README I started coding with this basic setup:

(def rules
  (o/ruleset
    ...))

(defonce *session
  (atom (reduce o/add-rule (o/->session) rules)))

Now I'd like to modify rules from an editor and see the new rule behavior right away (after the file is saved). In the post save hook I tried re-adding all the rules with:

(swap! *session
  (fn [session]
    (reduce o/add-rule session rules)))

But I get an exception telling that those rules already exist. So for now I just reset the whole session loosing the previous datoms. It's possible to remove all rules from the session? Or there's any other way to get the reloading behavior?

frankitox commented 3 years ago

Ah! Just read about session serialization. I can extract all datoms with query-all and add them to a new fresh session. Something like this:

(swap! *session
  (fn [session]
    (let [new-session (reduce o/add-rule (o/->session) rules)]
      (o/fire-rules
        (reduce o/insert new-session (o/query-all session))))))