setTemplate

Sets the template of the group at path to template, replacing the whole of the old one, nested templates included; null removes it. A template is what lets the app attach children to the group while it runs, and says what each one gets (see TopologyDocument.ChildTemplate). To change one field, pass a copy of the group's own. Nothing in template is checked here; Topology.from reports a bad weight or a negative ceiling at the group's path.

The history is what the edit does to the children attached under the template:

  • TopologyEdit.History.KEPT for a template added, the same template again, a new ceiling at any level, or a nested template added, which turns each attached leaf into a group that keeps its history;

  • TopologyEdit.History.SHARE_RESTARTS for a new weight at any level, which restarts the share of every child attached at that level;

  • TopologyEdit.History.DRAINS for a template removed, or a nested one, which closes every child attached under it. It wins over a new weight in the same edit.

Weights compare as setWeight's do, so "3/2" for "6/4" is the same weight.

Refused with TopologyEdit.AtRoot for [], since a document's root carries no template, TopologyEdit.NoSuchNode, or TopologyEdit.NotAGroup for a lane.

Samples

val running = draft("pool") {
    group("acme", Weight.of(3)) {
        lane("interactive")
        lane("batch")
    }
    group("hobby") { lane("batch") }
}
var document = running.toDocument()

// A path is child indices, as on a TopologyProblem: [0, 1] is acme's batch lane.
val reweighted = document.setWeight(listOf(0, 1), "2")
check(reweighted is TopologyEdit.Edited && reweighted.history == TopologyEdit.History.SHARE_RESTARTS)
document = reweighted.document

// history is what one edit does: a rename replaces hobby, and so does renaming it back.
val renamed = document.rename(listOf(1), "hobbyists")
check(renamed is TopologyEdit.Edited && renamed.history == TopologyEdit.History.REPLACED)
val back = renamed.document.rename(listOf(1), "hobby")
check(back is TopologyEdit.Edited && back.history == TopologyEdit.History.REPLACED)
document = back.document

// A template lets the app attach children to hobby while it runs, here two levels deep.
// Adding one costs nobody history; removing it later would drain the attached children.
val attaching = document.setTemplate(listOf(1), TopologyDocument.ChildTemplate("2", template = TopologyDocument.ChildTemplate()))
check(attaching is TopologyEdit.Edited && attaching.history == TopologyEdit.History.KEPT)
document = attaching.document

// An edit that would give two siblings one name is refused, naming the sibling that has it.
val refused = document.addChild(listOf(0), TopologyDocument.Lane("batch"))
check(refused == TopologyEdit.DuplicateName(listOf(0, 1), "batch"))

// The warning before saving compares with the tree the editor loaded: the round trip is gone.
val candidate = Topology.from(document)
check(candidate is TopologyParse.Valid)
val attached = Topology.ChildTemplate(Weight.of(2), template = Topology.ChildTemplate(Weight.ONE))
check(
    running.changesTo(candidate.topology) ==
        listOf(
            TopologyChange.WeightChanged(listOf(0, 1), listOf("pool", "acme", "batch"), Weight.ONE, Weight.of(2)),
            TopologyChange.TemplateAdded(listOf(1), listOf("pool", "hobby"), attached),
        ),
)