Vixen
02b45cc4
csharp
public static class GraphInvariants

The rules every directed graph in the repository enforces, in the one place they are written.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

Why this exists at all. There are two node graphs — one a canvas draws and one a document is saved from — and they are deliberately separate types, because a model that could only be read out of a live element tree would make saving, compiling and diffing a graph depend on a font. What is not deliberate is enforcing the same three invariants twice, by two algorithms, and that is what these methods are: the cycle refusal, the cascade, and the one-edge-per-input rule, each written once.

⚠ A rule change lands here or it lands nowhere. Allowing an input to take more than one edge, say, is a change to Arriving``2's contract — every graph that calls it moves together, and one that had quietly grown its own copy would be the thing this was written to prevent.

Generic over the node and the edge rather than over an interface the graphs implement: one holds object references and the other holds numbered identities, and neither should have to give that up. The selectors are static lambdas at every call site, so the delegates are cached and none of this allocates per call beyond the walk itself.

Methods (4)

  • public static bool Reaches<TNode, TEdge>(IReadOnlyList<TEdge> edges, Func<TEdge, TNode> from, Func<TEdge, TNode> to, TNode start, TNode target) where TNode : notnull

    Whether one node feeds another, directly or through any number of others.

  • public static int Arriving<TPort, TEdge>(IReadOnlyList<TEdge> edges, Func<TEdge, TPort> to, TPort input) where TPort : notnull

    Where the edge arriving at an input is, if there is one.

  • public static int Detach<TNode, TEdge>(List<TEdge> edges, Func<TEdge, TNode> from, Func<TEdge, TNode> to, TNode node, ICollection<TEdge>? detached = null) where TNode : notnull

    Takes out every edge with an end on a node.

  • public static string Describe<TPort>(GraphConnectionError error, TPort from, TPort to)

    What to tell somebody about a refusal.

Used by (3)

  • GraphInvariantTestsVixen.Core.Tests
  • NodeGraphVixen.Ui.Controls.Advanced
  • NodeGraphModelVixen.Editor.NodeGraph