Fork me on GitHub

Hi, We are are going to be using clara rules at work, the example at looks like it will be very useful for part of our system. There are many facts that we will use records for, but there is one kind of fact in particular which we have many different instances of the same fact but each with a different status (there can be quite a few of these statuses) Ideally we don't want to create a new type for each of these statuses. So I would like to be able to create a tuple as shown in the link, but I would also need to be able to write other conditions on it. There is a hint of how to do it on the page right at the bottom, but I can't seem to figure it out. Would anyone be able to provide a fleshed out example with extra conditions?


Hi everyone! 👋 I just (finally) published a podcast interview I did with @ryanbrush two years ago, about Clara. I would think most of it is still as relevant, but if Clara has changed in ways that make what's said in the interview misleading I'd be happy to include pointers in the show notes. In any case, I would of course welcome any feedback/criticism from this community!


the one you linked was more about performance than the feature use itself


@blob626, If I remember correctly you should be able to apply constraints to a condition even when using a custom fact type. However, the syntactic sugar that clara provides for field accessors would no longer be available. Meaning that you would have to provide custom destructuring for the fact or have the constraints access the fact via this and drill in that way. For example:

(ns test.rules
  (:require [clara.rules :as r]))

(defrecord SomeFact [id status another-identifier])

(defrecord IncompleteFact [id])

(r/defrule a-rule
  [[SomeFact "id-1"] [{:keys [another-identifier status]}]
    (= ?another-identifier another-identifier)
    (= status "INCOMPLETE")]
  (r/insert! (->IncompleteFact ?another-identifier)))

(r/defquery get-incomplete-facts
  [?f <- IncompleteFact])

(defn fact-type-fn
  (if (instance? SomeFact fact)
    [`SomeFact (:id fact)]
    (type fact))) 

(defn run-test
  (-> (r/mk-session [a-rule get-incomplete-facts]
                    :fact-type-fn fact-type-fn)
      (r/insert (->SomeFact "id-1" "INCOMPLETE" "external-id"))
      (r/query get-incomplete-facts)))
[{:keys [another-identifier status]}]


Clara Rules has a mention in Rich Hickey's HOPL IV "A History of Clojure" paper! 🙂


thanks for making the example @ethanc


perhaps our docs are too weak on this


I need to re-read through


and clara is in the history paper? wow, I’ve been meaning to read it. Clara’s officially famous 😜\