Combining query rules in Datomisca

I am trying to write a Datomic query that invokes 2 rules using a Scala Datomisca wrapper .

How to combine two separate requests?

My code is as follows:

val rule1 = Query.rules("[[(rule1 ?a) [ ... ]]]")
val rule2 = Query.rules("[[(rule2 ?b) [ ... ]]]")

Datomic.q(Query("""[:find ?x
                    :in $ % %
                    :where (rule1 ?a) (rule2 ?b)]"""), conn.db(), rule1, rule2)

This gives me the error message "Unable to resolve key rule1"). I tried it with only one %, but it will not compile (type of mismatch).

I would prefer not to combine them into one line in the call Query.rules, because this means that I have to repeat them to use different combinations of rules (for example: one request with both, the other with a single rule 1).

Since Query.rulesthis is a macro, I have to use String literal values, otherwise it will not compile.

+3
source share
1 answer

You can combine the rules, treating them as Stringto concatenate them. You do not get the benefits of static type checking, but I have not found another way.

val rule1 = "[[(rule1 ?a) [ ... ]]]"
val rule2 = "[[(rule2 ?b) [ ... ]]]"

Datomic.q(Query(...), conn.db(), DString(s"[$rule1 $rule2]"), ...)
+1
source

All Articles