Fork me on GitHub
#clara
<
2017-05-29
>
nkabrich14:05:57

Why is this rule creating an infinite loop? I'm trying to insert a "default fact" if no other rule has yet inserted that fact. Or is there a better pattern for this?

(defrule infinite-loop
  [:not [SomeFact]]
  =>
  (rules/insert! (map->SomeFact {:a 1})))
Also if I add {:salience -1} to this rule, it seems to hang in the LHS? I'm not exactly sure but in any case when I set the salience a (println "looping") in the RHS fails to print and thread hangs.

souenzzo14:05:48

not sure but, i think you can: first accumulate some fact, if it's equal zero, then insert the default.

alex-dixon15:05:22

@ryanbrush Would it be ok to add a Clara channel here? https://discord.gg/v9QMy9D

nkabrich15:05:17

@souenzzo hmm thanks, but I'm still running into an infinite loop using an accumulator and :test to insert a default

alex-dixon15:05:50

@nkabrich Are you using (acc/count)?

alex-dixon15:05:34

[?nfacts <- (acc/count) :from SomeFact]
[:test (= 0 ?nfacts)]

nkabrich15:05:02

@alex-dixon nope, ill give that a whirl

ryanbrush15:05:16

If an insert on the RHS causes the LHS to become false, the insert is retracted since it is no longer supported...causing the LHS to become true again and an infinite loop occurs.

ryanbrush15:05:09

The key is for the RHS insert to not match the LHS. Like check in the LHS for not a default value that the RHS could insert, so it doesn't retract the RHS inserted fact.

ryanbrush15:05:03

You can also use insert-unconditional to avoid this truth maintenance loop outright, but that can cause other headaches that depend on the order of facts inserted.

nkabrich15:05:21

ahh, makes total sense thanks for the explanation.

ryanbrush15:05:13

Sure thing. It's probably the biggest pitfall of rules engines like this....but can be overcome with some care

ryanbrush15:05:54

@alex-dixon I'm not familiar with that service. Let me take a look at it later today

alex-dixon15:05:18

@ryanbrush Thanks. There’s some discussion within the community to converting from Slack to it. Seems like we’re in a trial period right now. I know from the JS community that Slack’s message limit is a problem and Discord is generally looked to as the solution (they keep unlimited message history) https://github.com/reactjs/redux for example is a popular JS library that has moved to Discord