clara

http://www.clara-rules.org/
Heather 2021-03-04T01:07:04.044100Z

Hi I have a question. I’m playing with Clara Rules so that I can understand rules engines better. I have 2 queries that look something like this:

(defquery query-1?
  "Returns what is set on Record 1"
  []
  [Record1 (= ?foo foo) (= ?bar bar)])

(defquery query-2?
  "Returns what is set on Record 2"
  []
  [Record2 (= ?baz baz) (= ?oof oof)])
And I want to make a query that returns the aggregate of both the queries. Something like this:
(defquery unified-query?
  "Returns what is set on both Record 1 and Record 2"
  []
  ;; [Record1 (= ?hmm hmm) (= ?huh huh)
  ;; [Record2 (= ?ooh ooh) (= ?aah aah)]]
  )

Heather 2021-03-04T01:07:41.044900Z

I’m not sure how to set up that unified query to return both Records. Is that possible? Or do I need a third Record that represents the 2 records together?

Heather 2021-03-04T01:08:39.045600Z

I couldn’t find examples of aggregating queries in any of the tutorials I saw, which made me wonder if it’s not something that happens often- it seems like it would be.

ethanc 2021-03-04T01:13:22.046Z

Would Record1 and Record2 have anything in common?

ethanc 2021-03-04T01:19:51.048Z

(r/defquery q1
  []
  [:and
   [Rec1 (= ?hmm hmm) (= ?huh huh)]
   [Rec2 (= ?ooh ooh) (= ?ahh ahh)]])
would be the way to query both records, however since the records don’t share anything in common you would end up with a cross product, ex.
(defn test
  []
  (-> (r/mk-session)
      (r/insert (->Rec1 :a 1) (->Rec1 :b 2) (->Rec2 :c 3) (->Rec2 :d 4))
      (r/fire-rules)
      (r/query q1)))
ends up producing:
({:?hmm :a, :?huh 1, :?ooh :c, :?ahh 3}
 {:?hmm :a, :?huh 1, :?ooh :d, :?ahh 4}
 {:?hmm :b, :?huh 2, :?ooh :c, :?ahh 3}
 {:?hmm :b, :?huh 2, :?ooh :d, :?ahh 4})

ethanc 2021-03-04T01:30:57.048900Z

or perhaps the less explicit way:

(r/defquery q1
  []
  [Rec1 (= ?hmm hmm) (= ?huh huh)]
  [Rec2 (= ?ooh ooh) (= ?ahh ahh)])
they both behave the same way

ethanc 2021-03-04T01:35:11.051300Z

I can’t speak for other’s usage of queries, but in our stack it usually ends up boiling down to a single record type that encompasses several pieces of smaller data.

ethanc 2021-03-04T01:49:48.052500Z

It should also be noted, to my knowledge, anything that is possible in the lhs of a rule should also be possible in a query

👍 2
Heather 2021-03-04T16:36:18.053600Z

Yeah, they have nothing in common and I just wanted to squish them together without a cross product. I was getting a cross product, but really wanted to just concat them.

ethanc 2021-03-04T16:44:50.056500Z

hmm, not sure i am following you there, however perhaps an accumulator or two might be what you are looking for:

(r/defquery q2
  []
  [?rec-1s <- (acc/all) :from [Rec1]]
  [?rec-2s <- (acc/all) :from [Rec2]])
which nets:
({:?rec-1s [#test_queries.Rec1{:hmm :a, :huh 1} #test_queries.Rec1{:hmm :b, :huh 2}],
  :?rec-2s [#test_queries.Rec2{:ooh :c, :ahh 3} #test_queries.Rec2{:ooh :d, :ahh 4}]})
note that this doesn’t have the niceties of the field level binding because of accumulator grouping semantics(and me wanting to avoid that), but instead offers the full records

ethanc 2021-03-04T16:45:26.057100Z

acc in this context is an alias for clara.rules.accumulators