Fork me on GitHub
#datalevin
<
2023-11-12
>
Tommy Jolly18:11:38

Struggling with some aspects of datalog, hope it's OK to ask here -- I'm not sure how much implementations differ. My question is essentially "how do I call a function within a query", but I'm going to spell everything out, because this might be an XY-problem if I'm modelling it all in the wrong way. My database contains 3 types of entity: items, group-sets, and groups. A group-set defines a number of groups via :group-set/f, a function that maps items to its groups, i.e. (f item-entity) returns a valid group/id. I guess the groupsets are like demographic splits - if the items were people, the groupsets might be "by gender", "by vote", "by age" etc. I'd like to extract the groups for a given groupset. My current approach is to get the function with one query, and I'm hoping I can apply it in another, something like:

(d/q '[:find ?group ?item
         :in $ ?g
         :where
         [?item :item/id _]
         [?group :group/id (?g (d/pull ?item [*]))]]
       db group-set-g)
But obviously I don't know how to call the function ?g, and I'm also not sure if this is the right approach in the first place.

Huahai18:11:53

You don’t need to use a function for group-set. Model things as data, don’t use function if you don’t have to.

Huahai18:11:16

That is to say, you don’t need group-set.

Tommy Jolly20:11:18

The data's very dynamic, so functions are a must! I've just spent a couple of hours playing around with rules and aggregators and they're all cool, but the syntax I needed was [(?f ?thing) ?binding] -- combined with rules, that will be much cleaner than repeatedly calling d/q.

Tommy Jolly20:11:04

One more question! Is it inefficient to define the rule '[[(identity ?x) [_ _ _]]] ? Is there a better way to specify that a rule should do nothing?

Tommy Jolly21:11:07

Actually, forget that! The rule I wanted is '[[(f ?x ?e) [?x ?a ?v] [?e ?a ?v]]] to map something to itself, and I guess it's as efficient as it can be! Thank you for your time, and thanks for making such an awesome product ❤️ I was surprised to find so many Datomic features implemented.

Huahai23:11:37

Thanks. Except for the temporal features, we probably have more features than Datomic, as we have KV store features. The goal is to have a versatile database.