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)
@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.
If you had a unique ID on all facts for example, you could just do ?l <- (acc/max :tx) :from [A (= id? ?id)]
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.
The rule engine I work on takes the general from (state, action) -> 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
:id
s), but I want the max
:tx
of each of those entities.
In summary: [?entities <- (acc/all) :from [TimestampedEntity]] ;; ?entities => [{: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 => [{:id 1, :x false, :tx 2} {:id 2, :x true, :tx 3}]
e.g., to calculate a ratio of true entities over all entities (but only for their final states)
@dadair if you use a variable binding bound to the :id field then the accumulator will give you the latest :tx per :id
[?latest <- (acc/max :tx) :from [TsEntity (= ?id id)]]
That’s the “latest :tx “ of each fact group where they are grouped by :id