Fork me on GitHub
#clara
<
2020-06-08
>
blob62613:06:22

Hi, We are are going to be using clara rules at work, the example at http://www.clara-rules.org/docs/fact_type_fn_perf/ 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?

felix.holmgren14:06:46

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! https://thesearch.space/episodes/2-ryan-brush-on-retaking-rules-for-developers

mikerod16:06:03

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

ethanc18:06:11

@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
  [fact]
  (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/fire-rules)
      (r/query get-incomplete-facts)))
Specifically,
[{:keys [another-identifier status]}]

apbleonard19:06:00

Clara Rules has a mention in Rich Hickey's HOPL IV "A History of Clojure" paper! 🙂 https://clojure.org/about/history

mikerod22:06:05

thanks for making the example @ethanc

mikerod22:06:09

perhaps our docs are too weak on this

mikerod22:06:15

I need to re-read through

mikerod22:06:36

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