Fork me on GitHub

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 - 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.


@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.


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



👍 4

In terms of “tracing why” something happened


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)


get-suporting-facts could remove duplicates etc as needed


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


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.