Fork me on GitHub
#datomic
<
2021-06-17
>
vlaaad14:06:46

Anyone doing something like that for "dynamic" rules?

(defmacro eval-rules [form]
  (walk/prewalk
    (fn [e]
      (if (and (seq? e) (= `unquote (first e)))
        (eval (second e))
        e))
    form))

(eval-rules
  '[[(reverse-edge ?from-concept ?type ?to-concept ?relation)
     [(ground ~(set/map-invert some-config-map))
      [[?type ?reverse-type]]]
     [?relation :relation/concept-2 ?from-concept]
     [?relation :relation/type ?reverse-type]
     [?relation :relation/concept-1 ?to-concept]]])
I'm trying to create rules configured from some code...

Joe Lane15:06:46

(let [some-config-map {:bar :foo :baz :bin}]
  [['(reverse-edge ?from-concept ?type ?to-concept ?relation)
     [(list 'ground (clojure.set/map-invert some-config-map))
      '[[?type ?reverse-type]]]
     '[?relation :relation/concept-2 ?from-concept]
     '[?relation :relation/type ?reverse-type]
     '[?relation :relation/concept-1 ?to-concept]]])
For more inspiration See the https://github.com/Datomic/mbrainz-sample/blob/master/src/clj/datomic/samples/mbrainz/rules.clj .

potetm15:06:35

yeah “just quote the quoted bits” seems most straightforward to me

vlaaad15:06:51

That's a lot of quoting...

Joe Lane15:06:06

If you come up with something you REALLY like @U47G49KHQ I'd be interested in seeing it.

vlaaad15:06:00

Well, I've been thinking about it for a couple is hours, and so far eval-rules is the best thing I have...

vlaaad15:06:43

Other things I considered is what you suggested, macro that does the same thing as eval-rules, but that was much more cumbersome than fn version, and syntax quoting with a lot of ~'?type -like symbols

vlaaad15:06:47

Ah, I also tried #=, but I couldn't reference the config-map with it, it was interpreted as symbol

Joe Lane15:06:01

Ha, first rule of #= club, don't talk about #= club 😉

vlaaad15:06:18

This #= club membership didn't bear any fruits so far...

vlaaad14:06:32

...and I don't want to pass this config map in addition to rules to queries since it's static. Any advice how to do that?