Fork me on GitHub
#clara
<
2016-11-17
>
wparker09:11:38

@surreal.analysis I think this is expected behavior. The contract from Clara is that the state after all rules finish firing will be consistent with the rules, but the steps Clara takes to get there inside fire-rules are undefined except if you provide salience. Your print statements are reflecting intermediate state in the rules engine that is eliminated before fire-rules returns. So in your first example, collect-invalids fired with no Invalid facts because none had been created yet; once Invalid facts were created it fired with them. If collect-invalids had inserted other fact(s) the facts inserted by the firing with no Invalid facts yet would be removed before fire-rules returned to the caller. For something like this I’d suggest using queries or inserting another fact using insert! since these will be adjusted by the truth maintenance system when Invalid facts are added to the session.

wparker09:11:17

So something like (defquery invalid-query “query for Invalid” [Invalid (= reason ?reason)])

wparker09:11:42

Or (defrecord CollectedInvalid [all-invalid-reasons])

wparker09:11:24

(defrule get-all-invalid [?invalids <- (acc/all) :from [Invalid]] => ;; for exame

wparker09:11:36

(defrule get-all-invalid [?invalids <- (acc/all) :from [Invalid]] =>

wparker09:11:01

=> ;; do something here that doesn’t depend on intermediate state

wparker09:11:46

Ignore "(defrule get-all-invalid [?invalids <- (acc/all) :from [Invalid]] => ;; for exame [9:33] ``” please, I was trying to set up a code block but apparently don’t know how to do so in Slack 😛

wparker09:11:02

When you did

wparker09:11:04

(defrule collect-invalids [?reasons <- (acc/all) :from [Invalid (= ?reason reason)]] => (prn "Invalid for: " (map :reason ?reasons)))

wparker09:11:30

You don’t fire when there are no Invalid facts since ?reason is not bound elsewhere (this is the behavior as of 0.12.0 at least, we fixed some bugs around accumulators that release). However, the effect of adding the ?reason binding in the accumulator condition is that you collect all Invalid facts for each reason and fire once per distinct reason.

wparker09:11:20

So something like (defrule get-windspeeds-at-location [?wind <- (acc/all) :from [WindSpeed (= ?location location)]] => ;; do something )

wparker09:11:49

given four windspeeds, where two come from LocationA and two come from LocationB, would fire twice, once with ?wind bound to the WindSpeed from LocationA and once with ?wind bound to the WindSpeed facts from LocationB

wparker09:11:16

Hopefully this clears things up, please ask if it doesn’t and if anyone has thoughts on things at http://www.clara-rules.org/docs/accumulators/ that could be cleared up to reduce confusion I’d be interested