clara

http://www.clara-rules.org/
2018-01-16T03:43:22.000236Z

is there a way to compose accumulators outside of writing a custom reducer? e.g., I want to get the latest timestamp of all facts, so some combination of (acc/all) and (acc/max :tx)

2018-01-16T06:33:56.000172Z

@dadair there isn’t anything directly for that. On a case by case you may able to get the behavior you want from an accumulator like the max above.

2018-01-16T06:35:16.000075Z

If you had a unique ID on all facts for example, you could just do ?l <- (acc/max :tx) :from [A (= id? ?id)]

2018-01-16T06:36:17.000016Z

I’m not sure that makes much sense though. acc/max is intended to find the max among all facts. Accumulators are mostly for reasoning about aggregates. I may have misunderstood your usage you were looking for.

2018-01-16T06:54:45.000112Z

The rule engine I work on takes the general from (state, action) -&gt; actions, where action and actions are actions to perform (in aggregate) to transition state to a new state (basically the RE is a reduce, but instead of reducing to a new state, it returns “diff” operations through querying the resulting state that external service(s) can use to transition the state). A difficulty that I’ve had in general with this pattern is that I need to track changes to state as rules propagate, and then insert output actions based on those changes. So for example, given an initial entity A {:id 1, :x true}, there may be cascading changes over the (short) life of the session that cause it to transition to any number of {true,false} values. I always want the stable result, so I have added tx values to track “time”. Given that, I may have a rule that “subscribes” to a number of those facts (e.g., all :ids), but I want the max :tx of each of those entities.

2018-01-16T06:55:34.000117Z

In summary: [?entities &lt;- (acc/all) :from [TimestampedEntity]] ;; ?entities =&gt; [{:id 1, :x true, :tx 1} {:id 1, :x false, :tx 2} {:id 2, :x true, :tx 3}] but I only want the latest :tx for each unique entity, so desired: ?entities =&gt; [{:id 1, :x false, :tx 2} {:id 2, :x true, :tx 3}]

2018-01-16T07:00:27.000152Z

e.g., to calculate a ratio of true entities over all entities (but only for their final states)

2018-01-16T16:02:14.000763Z

@dadair if you use a variable binding bound to the :id field then the accumulator will give you the latest :tx per :id

2018-01-16T16:04:56.000096Z

[?latest &lt;- (acc/max :tx) :from [TsEntity (= ?id id)]]

2018-01-16T16:05:57.000105Z

That’s the “latest :tx “ of each fact group where they are grouped by :id