odoyle-rules icon indicating copy to clipboard operation
odoyle-rules copied to clipboard

What would it take to abstract the storage?

Open drewverlee opened this issue 5 years ago • 5 comments

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!

drewverlee avatar Nov 16 '20 23:11 drewverlee

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 avatar Nov 17 '20 00:11 oakes

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.

oakes avatar Dec 04 '20 22:12 oakes

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

allentiak avatar May 11 '21 14:05 allentiak

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

oakes avatar May 11 '21 19:05 oakes

Thanks for the pointer!

allentiak avatar May 11 '21 20:05 allentiak