clara

http://www.clara-rules.org/
2020-07-01T05:27:39.189600Z

Is there an idiomatic way to do hierarchical rules in clara? I have a set of rules that all calculate the same property from a fact but I would like the calculation only to be made by the most specific of the rules that match. I have tried inserting a guard fact when a rule (e.g. [:not [:guard (= (:active this) true] and (insert! {:type guard :active true} ) matches and using salience to control firing order, but this results in an endless loop for reasons I cannot figure out.

2020-07-01T10:00:04.190600Z

As an example I don't understand why the code below goes into an endless loop.

(defrule test-rule
    "test guard"
    [:not [:guard (= (:active this) true)]]
    =>
    (do
      (println "Inserting guard!")
      (insert! {:type :guard :active true})))
  (-> (mk-session [test-rule]
                  :fact-type-fn :type)
      (fire-rules))

2020-07-01T11:06:03.191100Z

@mac use a different guard type for each tier

2020-07-01T11:06:13.191400Z

Where the rule never inserts it’s own type

2020-07-01T11:12:08.192400Z

@mac https://gist.github.com/mrrodriguez/6a6f8373b25d69826b3efe154c928fac this actually has a somewhat similar model as well. For a slightly different goal but notice the guarding not conditions

2020-07-01T11:19:28.193200Z

@mikerod Thanks, the gist looks very useful. Do you know why my example would loop?

2020-07-01T11:30:38.194100Z

@mac the rules attempt to find a logical consistency balance

2020-07-01T11:30:54.194600Z

Your rule Left side condition is invalidated when the right side inserts.

2020-07-01T11:31:09.195200Z

The engine then retracts that’s fact because it is no longer logically supported.

2020-07-01T11:31:28.195500Z

It’s called the truth maintenance system

2020-07-01T11:31:59.196400Z

Then it can activate again and re insert. And again it invalidates itself. So it’s logical looping

2020-07-01T11:35:23.198Z

@mikerod OK, so the guard gets removed because the LHS match that caused it to be inserted no longer matches?

2020-07-01T12:39:09.198200Z

Yes

2020-07-01T12:39:33.199Z

Think of rules not like in order. But instead that the rhs facts inserted are supported by the lhs

2020-07-01T14:07:28.200Z

@mikerod Got it, this is much closer to logic programming than I thought, but it makes sense when framed like that.

👍 2