mergeRules

Builds MergeRules from one rule for each property that nodes of the same step may change, in place of a reducer that merges whole states by hand:

data class Research(
val notes: List<String> = emptyList(),
val status: String = "",
)

val graph = StateGraph<Research> {
val web = node("web") { it.copy(notes = it.notes + "web") }
val docs = node("docs") { it.copy(notes = it.notes + "docs") }
START then web
START then docs
}.compile(
reducer = mergeRules {
append(Research::notes) { copy(notes = it) } // add what each node added
replace(Research::status) { copy(status = it) } // take the value of the node that changed it
},
)

A rule names a property and says how to write its merged value back, which for a data class is a copy. The rules compare what each node returned with the state before the step, so a node is written as usual: it returns a whole state with copy.

A node that changes a property without a rule fails the run with a ReducerException instead of losing the change. For that check the state needs equals, which a data class has.

Given to StateGraph.compile, as rules or as reducer, the rules merge every node of a step, the nodes with a work and an update too. Each node then gets the state from before the step, its update too, and the results are merged in the order the nodes were added to the graph.

Throws

if there is no rule, or two rules for the same property.