I’d like to define a rule that can operate on multiple data sources, for example:
(let [rules '[[(new-foo? [$before $after ?e])
(not [$before ?e :foo])
[$after ?e :foo]]]
query '[:find ?e .
:in $before $after % ?e
:where (new-foo? $before $after ?e)]
uri "datomic:mem://foo"
_ (d/delete-database uri)
_ (d/create-database uri)
conn (d/connect uri)
_ @(d/transact conn [{:db/ident :foo
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}])
db-before (d/db conn)
result (d/with db-before [{:foo "bar"}])
eid (-> result :tempids vals first)]
(println (d/q query db-before (:db-after result) rules eid)))
However this code doesn’t work because rules can’t have source variables as parameters. (You can specify a source variable with ($src rule-name ?a ?b ?c)
, but that only works for a single source).
I’m thinking of working around this by implementing a “rule-expand” function that will take a query and a set of rules and expand any rules that take multiple data sources. So e.g. the output for the example would be this query:
'[:find ?e .
:in $before $after ?e
:where (not [$before ?e :foo])
[$after ?e :foo]]
Is there a possibility that future releases will allow rules with multiple data sources?