|
| 1 | +Object lifecycle and graph-membership policy |
| 2 | +============================================= |
| 3 | + |
| 4 | +``BaseVertex``, ``UVertex``, ``DVertex`` and ``Edge`` can all be constructed |
| 5 | +directly, independent of any graph. This is deliberate -- it is not an |
| 6 | +oversight and not a "factory-only" design -- but it means the rules for what |
| 7 | +those objects can and can't do *before* they belong to a graph need to be |
| 8 | +stated explicitly rather than left implicit. |
| 9 | + |
| 10 | +Standalone construction |
| 11 | +------------------------ |
| 12 | + |
| 13 | +A vertex or edge built directly, e.g. ``UVertex(coord=[1, 2], name="A")`` or |
| 14 | +``Edge(v1, v2, cost=5.0)``, is a fully valid object in its own right. The |
| 15 | +main supported use of this is lightweight, graph-independent work: building a |
| 16 | +couple of vertices purely to demonstrate or test :class:`Edge` cost |
| 17 | +computation, without needing a graph at all. |
| 18 | + |
| 19 | +An object in this state has ``._graph is None``. It cannot be connected to |
| 20 | +anything (see below) until it is added to a graph via |
| 21 | +:meth:`UGraph.add_vertex` / :meth:`DGraph.add_vertex`. |
| 22 | + |
| 23 | +Adding a vertex to a graph |
| 24 | +--------------------------- |
| 25 | + |
| 26 | +:meth:`UGraph.add_vertex` and :meth:`DGraph.add_vertex` each only accept |
| 27 | +their own matching vertex subclass -- a ``UVertex`` (or coordinate data, from |
| 28 | +which one is built) for :class:`UGraph`, a ``DVertex`` for :class:`DGraph`. |
| 29 | +Passing the wrong kind of vertex -- a plain ``BaseVertex``, or the other |
| 30 | +graph's subclass -- raises ``TypeError`` rather than being silently accepted |
| 31 | +or converted. |
| 32 | + |
| 33 | +This is the mechanism that makes graph membership meaningful: once added, a |
| 34 | +vertex's ``._graph`` is fixed to that specific graph instance, and its |
| 35 | +concrete type (``UVertex`` or ``DVertex``) is guaranteed correct for that |
| 36 | +graph's edge semantics (undirected vs. directed edge-list bookkeeping). |
| 37 | + |
| 38 | +Connecting two vertices |
| 39 | +------------------------- |
| 40 | + |
| 41 | +``v1.connect(v2)`` (and the equivalent ``graph.add_edge(v1, v2)``) requires |
| 42 | +**both vertices to already belong to the same graph**: |
| 43 | + |
| 44 | +- If either vertex has not been added to any graph (``._graph is None``), |
| 45 | + ``connect()`` raises ``ValueError``. |
| 46 | +- If the two vertices belong to different graph instances, ``connect()`` |
| 47 | + raises ``ValueError``. |
| 48 | + |
| 49 | +There is deliberately no separate check for "same vertex subclass" (e.g. |
| 50 | +rejecting a ``UVertex``-to-``DVertex`` connection directly). It doesn't need |
| 51 | +one: since a ``UVertex`` can only ever belong to a ``UGraph`` and a |
| 52 | +``DVertex`` only to a ``DGraph`` (enforced by ``add_vertex()`` above), the |
| 53 | +same-graph check above already makes a ``UVertex``-to-``DVertex`` connection |
| 54 | +impossible as a structural consequence, not as a separately-maintained rule. |
| 55 | + |
| 56 | +Constructing an ``Edge`` directly |
| 57 | +----------------------------------- |
| 58 | + |
| 59 | +``Edge(v1, v2)`` (without going through ``connect()``) does not raise if the |
| 60 | +vertices aren't graph-connected -- this constructor is also the supported |
| 61 | +standalone-use path described above, so it stays permissive. Instead: |
| 62 | + |
| 63 | +- If an explicit ``cost`` is given, it is used as-is. |
| 64 | +- Otherwise, the edge cost is auto-computed from vertex coordinates via the |
| 65 | + graph's metric *only if* both vertices are added to the same graph. |
| 66 | + Otherwise ``cost`` is left as ``None`` rather than raising. |
| 67 | + |
| 68 | +This means a bare ``Edge(v1, v2)`` with orphaned or cross-graph vertices is a |
| 69 | +valid object with ``cost=None`` -- it is only the *connecting* operations |
| 70 | +(``connect()``, ``add_edge()``) that enforce the graph-membership invariant |
| 71 | +strictly. |
| 72 | + |
| 73 | +Summary |
| 74 | +-------- |
| 75 | + |
| 76 | +.. list-table:: |
| 77 | + :header-rows: 1 |
| 78 | + |
| 79 | + * - Operation |
| 80 | + - Requires graph membership? |
| 81 | + - Failure mode |
| 82 | + * - ``UVertex()`` / ``DVertex()`` / ``BaseVertex()`` |
| 83 | + - No |
| 84 | + - -- |
| 85 | + * - ``Edge(v1, v2)`` |
| 86 | + - No (cost falls back to ``None`` if ungraphed/cross-graph) |
| 87 | + - -- |
| 88 | + * - ``graph.add_vertex(v)`` |
| 89 | + - ``v`` must be the graph's own vertex subclass, or coordinate data |
| 90 | + - ``TypeError`` on wrong vertex subclass |
| 91 | + * - ``v1.connect(v2)`` / ``graph.add_edge(v1, v2)`` |
| 92 | + - Both vertices must already belong to the same graph |
| 93 | + - ``ValueError`` if either is ungraphed or they belong to different |
| 94 | + graphs |
0 commit comments