mergeRules

Builds a Reducer 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 reducer compares 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.

Throws

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