oakes / odoyle-rules

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

What would it take to abstract the storage? #1

Open drewverlee opened 3 years ago

drewverlee commented 3 years ago

My understanding from the readme is that what rule/data is implemented/backed via a clojure atom. Roughly, what do you think it would take to abstarct the storage? in my specific use case, i'm considering what it would take to back odoyle with datomic cloud. Do you see any obvious blockers or refactors?

Thanks for any input!

oakes commented 3 years ago

The library itself doesn't actually care how you store it. All the examples use atoms, but the library has no built-in assumptions about that. The functions like insert and retract just expect the session to be provided, and return a new one, so you can store it in any container you want.

As for datomic, it should be possible. I assume you want to store the matches for a given rule in your datomic db. You can get all the matches for a given rule like this:

(def rules
  (o/ruleset
    {::character-position
     [:what
      [id ::x x]
      [id ::y y]
      :then-finally
      (println (o/query-all o/*session* ::character-position)) ; [{:id :player :x 10 :y 10} {:id :monster :x 1 :y 5} {:id :boss :x 15 :y 0}]
      ]}))

So in theory you could replace that println call with a function that stores that vector in datomic. Every time any character's position is updated, the :then block will be called again, and you'll be able to update the vector in datomic.

You will probably still need to keep the session in some kind of container though. Locally you'll need to keep the session in an atom, volatile, or something like that. But every time you update it, the :then blocks will fire and cause the data to go into your db.

oakes commented 3 years ago

As of version 0.5.0 you can now query all the individual facts from a session, so that might be a better way to store them in datomic. All you do is run (query-all session) and it'll return the facts as [id attribute value] tuples, which could then be directly stored in datomic. I added a section to the readme called "serializing a session" which talks about this.

allentiak commented 3 years ago

@oakes Could you please either document this storage functionality a little bit, or point to examples?

oakes commented 3 years ago

If you mean how to serialize sessions, i wrote about it here: https://github.com/oakes/odoyle-rules#serializing-a-session

allentiak commented 3 years ago

Thanks for the pointer!