Fork me on GitHub
#clara
<
2016-11-16
>
surreal.analysis22:11:41

I think I’m missing something with accumulators - I’d like to have some sort of collection of reasons that something failed

surreal.analysis22:11:51

Originally I thought the best way to do that would be to have rules for each mode of failure

surreal.analysis22:11:59

And those rules would add a fact with a reason

surreal.analysis22:11:13

Test code here:

(defrecord DomainObject [name1 name2])
(defrecord Invalid [reason])

(defrule name1-not-test
  [DomainObject (not= name1 "test")]
  =>
  (insert! (->Invalid "test")))

(defrule name2-is-test
  [DomainObject (= name2 "test")]
  =>
  (insert! (->Invalid "name2 test")))

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

surreal.analysis22:11:27

When run with:

(-> (mk-session 'clara-playground.core)
                           (insert (->DomainObject "nottest" "test"))
                           (fire-rules))

surreal.analysis22:11:34

The output is

"Invalid for: " ()
"Invalid for: " ("test" "name2 test")

surreal.analysis22:11:46

After reading more on the accumulator side, I added a restriction

surreal.analysis22:11:59

And rewrote collect-invalids as

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

surreal.analysis22:11:09

But that output is

"Invalid for: " ("test")
"Invalid for: " ("name2 test")

surreal.analysis22:11:13

Any suggestions on what I’m missing?

surreal.analysis22:11:55

If it’s unclear, I want collect-invalids to fire a single time, with all the reasons

surreal.analysis22:11:06

Or is this something that’s better left to queries?