Ralph
Ralph

Reputation: 32284

Combining query rules in Datomisca

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

How do I combine two separate queries?

My code looks like this:

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 an error about "Cannot resolve key rule1"). I tried it with only one %, but it will not compile (type mismatch).

I'd rather not combine them in a single String in a call to Query.rules, because that means that I have to repeat them to use different combinations of rules (e.g.: one query with both, another with only rule1).

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

Upvotes: 4

Views: 134

Answers (1)

Ralph
Ralph

Reputation: 32284

You can combine rules by treating them as Strings can concatenating them. You do not get the benefit of static type checking, but I have not found any other way.

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

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

Upvotes: 2

Related Questions