clara

http://www.clara-rules.org/
eraserhd 2019-03-11T14:06:28.008100Z

We are working on our library to compute the results of pull expressions in Clara Rules, and we are bumping up against the fact that we can't accumulate over tokens (e.g. :and/:or).

eraserhd 2019-03-11T14:08:58.009400Z

We have a way to do it, but it seems terribly inefficient, since we'd need to add a fact for every entity+attribute in the system. Hmm, or at least the ones where the attribute can have multiple values.

2019-03-11T15:06:19.009900Z

@eraserhd > we are bumping up against the fact that we can’t accumulate over tokens (e.g. :and/:or). I don’t understand this part clearly enough to follow.

eraserhd 2019-03-11T15:09:48.012100Z

We would like to do, e.g. ?result <- (acc/all ?foo) :from [:and [Foo (= value ?foo) (= field ?x)] [Bar (= field ?x)]]

eraserhd 2019-03-11T15:14:21.013200Z

Is Clara good at sharing the beta network, if I have a bunch of rules that start with the same set of expressions, for example? If so, this might be a non issue.

2019-03-11T15:43:12.013800Z

@eraserhd Ok, I understand the accumulate part now, and the answer would be to accumulate on an intermediate fact

2019-03-11T15:43:30.014200Z

But perhaps, that’s what you were saying was a lot for you to explicitly include?

2019-03-11T15:43:56.014600Z

> Is Clara good at sharing the beta network Good at sharing hmm. I don’t know that I follow

2019-03-11T15:46:06.016200Z

Accumulating something like you had.

eraserhd 2019-03-11T15:46:44.016900Z

I mean, if I say (defrule one cond_1 cond_2 ... cond_10 bar => rhs) and (defrule two cond_1 cond_2 ... cond_10 baz => rhs2), where cond_1 through cond_10 are identical, will the beta network contain 12 or 22 nodes?

eraserhd 2019-03-11T15:50:26.018600Z

I ask, because if so, it shouldn't be a problem for me to use intermediate facts, and copy all the preamble logic from the main rule to the sub-rules. But if it isn't the case, then there would be an explosion in the work Clara does to support this.

2019-03-11T15:51:31.019500Z

@eraserhd in most cases, I’d expect the cond_1 through cond_10 to be represented within the network as the same nodes - when the engine is compiled there is an attempt to node-share where possible

2019-03-11T15:52:48.021100Z

You could however, always write a rule that did (defrule preamble cond_1 ... cond_10 => result-for-1-to-10) (defrule one <use> result-for-1-to-10 bar => rhs) (defrule two <use> result-for-1-to-10 baz => rhs)

2019-03-11T15:53:23.021600Z

if for some reason the nodes weren’t shared or the propagation was repeating for some reason

2019-03-11T15:53:38.022Z

I’d tend to avoid long lists of conditions within the same rule though

2019-03-11T15:53:50.022200Z

if they are beta-graph oriented - aka doing joins

2019-03-11T15:54:05.022600Z

It may cause unwanted thrash I believe

2019-03-11T15:54:55.023600Z

if you have cond_1 ... cond_10 and cond_1 is changing often, but no change happens after cond_5, it may still ripple through to cond_10 each time - but now that I say this, I think I’d have to look a bit more

2019-03-11T15:55:06.023900Z

There is at least some attempt in the engine to avoid propagating “no change”

2019-03-11T15:55:20.024300Z

I just can’t remember if it applies to all types of nodes

2019-03-11T15:55:52.025200Z

It may be good enough to mean what I just said was irrelevant though

eraserhd 2019-03-11T15:58:41.026400Z

This is for the mechanical, recursive transformation of a pull expression to clara rules in a macro, which is why it is getting complicated. Each nesting level needs the context of everything above it. Breaking out result-1-to-10 is possible, I guess, but it makes the macro harder to write.

2019-03-11T16:01:30.027800Z

hmm I see

eraserhd 2019-03-11T16:01:33.028Z

OK, so I think the naive solution is actually not bad, then. Intermediate facts, and copying all the parent logic into each rule.

2019-03-11T16:01:36.028200Z

well, I think you can fairly reliably get node sharing

2019-03-11T16:02:11.028900Z

I say fairly reliable, just because there may be edge case. Node sharing is an optimization that isn’t required for “correctness”, so it may have edge cases

2019-03-11T16:02:17.029100Z

in most cases though, I think it works out ok

2019-03-11T16:02:26.029400Z

You could mess around and look at the rule network node count for your case

2019-03-11T16:02:31.029700Z

and see if it seems to not be duplicating nodes

2019-03-11T16:02:44.030Z

by looking at the “rulebase” of the session

👍 1
2019-03-11T23:59:29.036100Z

@eraserhd If all of the conditions up to a point are equal as in the case you describe I’d expect the nodes to be shared - if you find that that isn’t the case when inspecting the rulebase please log an issue. Regarding the avoidance of propagating “no change” I’m guessing @mikerod is referring to the suppression of changes to accumulation and negations that make no difference. So for example, if you’re finding the minimum of some numeric field on a fact type and you have [-5 0 5] already and a new fact with a value of 10 comes in, a min accumulator shouldn’t trigger any downstream work. There are similar optimizations on negation nodes. If you’re interested in the specifics see https://github.com/cerner/clara-rules/issues/182 for accumulators and https://github.com/cerner/clara-rules/issues/231 for negations. Any questions feel free to ask, I’m headed to bed for tonight though. 🙂

👍 1