TopologyEdit

sealed interface TopologyEdit

The outcome of one edit of a TopologyDocument: the edited document, or why the edit was refused. An edit never throws, and a refused edit changes nothing.

Addressed the way TopologyProblem and TopologyChange are. An edit names its node by child indices from the root down, counted from zero in the order written: [] is the root and [0, 2] is the third child of the root's first child. So the path a Topology.from problem comes back with is the path to edit, with nothing to translate, and a document that is invalid while it is being edited, with two children of one name or a child with none, still has an address for every node.

An index names a place, not a node, so a removal or a move shifts the later siblings of the node it takes out. Edited.path says where the edited node is in the new document; read paths afresh from the new document, or from a new Topology.from, after every edit.

Edits check the tree's shape, never its values. A weight that is not a weight, an empty name, a negative ceiling or a group left with no children all go into the document, as they would from a file, and Topology.from reports them. The one value an edit refuses is a name a sibling already has, since Topology.from would refuse the document for it anyway.

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),
        ),
)

Inheritors

Types

Link copied to clipboard

The edit needs a node the root is not, and the path is []. The root has no weight, no ceiling and no template in a document, and no parent to be removed from or moved away from.

Link copied to clipboard
data class DuplicateName(val path: List<Int>, val name: String) : TopologyEdit.Refused

The edit would give a node name, which the sibling at path already has. Siblings are compared by exact name, the empty name included.

Link copied to clipboard
data class Edited(val document: TopologyDocument, val path: List<Int>, val history: TopologyEdit.History) : TopologyEdit

The edit applied.

Link copied to clipboard

What this one edit does to the document it was applied to, read as fairness history: what LocalHeddle.apply would do were this edit the whole difference. A child is identified by its names from the root down, so an edit that changes a name along the way makes a new child (see Topology).

Link copied to clipboard
data class IntoItself(val path: List<Int>, val parent: List<Int>) : TopologyEdit.Refused

A move of the node at path under parent, which is that node or lies beneath it.

Link copied to clipboard
data class NoSuchNode(val path: List<Int>) : TopologyEdit.Refused

No node is at path: an index is negative or past the last child, or the path runs on below a lane.

Link copied to clipboard
data class NotAGroup(val path: List<Int>) : TopologyEdit.Refused

path names a lane, and the edit needs a group to put a child into.

Link copied to clipboard
sealed interface Refused : TopologyEdit

Why an edit was refused. path is the address, in the document the edit was asked of, of the node to blame.