clara

http://www.clara-rules.org/
bherrmann 2021-02-11T13:05:16.028Z

I've recently been thinking of playing with rules engines (perhaps I need a hobby?) I recently watched and enjoyed (@sekao is a funny character. ) this presentation on O'Doyle - https://youtu.be/XONRaJJAhpA The talk references Clara a lot. I wondered though does O'Dolyle have the ability to explain why a rule was executed... I'm starting to think about using a rules engine, and the idea of being able to trace why a rule fired seems important. Also did O'Dolye not have retraction? He mentioned you can "replace" a fact.

2021-02-11T16:33:06.030600Z

@bherrmann I still need to watch that talk on O’Doyle. It did sound to me like what it wasn’t planned to have is a truth maintenance system. This is a key part of Clara’s feature sets/priority. It allows rules to be written in a way that is less coupled to order of evaluation and instead a more “declarative logic system”. Also, there is retraction, but typically external to the rules retraction tends to be the more stable given the truth maintenance system mentioned.

2021-02-11T16:33:14.030900Z

There is no concept in clara of fact “replacement/modification”

2021-02-11T16:33:34.031300Z

In terms of “tracing why” something happened

bherrmann 2021-02-11T16:38:10.032500Z

Thanks!

👍 1
2021-02-11T16:42:01.032800Z

Clara has a few built in facilities to give back various granularity detailed traces of what happened within the rules engine - which can include inserts and retract etc. For a higher-level view though, it often is enough to just write rules in a way that track their “supporting facts” directly

(defrule do-thing-with-support
  [?a <- A]
  [?b <- B]
  =>
  (r/insert! (map->C {:value :something
                      :support [?a ?b]})))

(defrule do-other-with-support
  [?c <- C]
  [?d <- D]
  =>
  (r/insert! (map->E {:value :other
                      :support [?c ?d]})))

;;; Later

(defn get-supporting-facts [{:keys [support] :as fact}]
  (into support
        (mapcat get-supporting-facts)
        support))

2021-02-11T16:42:12.033100Z

get-suporting-facts could remove duplicates etc as needed

2021-02-11T16:42:26.033400Z

and you can also just build helpers for these sorts of things to not be so repetitive in rules

2021-02-11T16:43:22.034400Z

With truth maintenance, a nice part is if facts becomes invalidated because newer insertions cause formerly fired rules to become false, then they are automatically retracted and the rule network re-establishes consistency across the rules - which would fix these supporting facts as well.