File size: 4,002 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""``CLEAN_DELETE_V1`` — remove a node and its incident relations (§10.3).

The node and every relation/constraint incident to it are deleted from the
world graph, the remaining constraints are re-validated for feasibility, and the
whole world is re-rendered fresh later (P6). The original raster is never
inpainted — deletion is structural, not pixel-level. After deletion the executor
may report ``MISSING_INFORMATION`` / ``INVALID_REFERENT`` (a ``U`` state) or a
changed answer; the operator does not decide which (P7 does, by execution).

An infeasible result (a dangling reference or a now-broken constraint graph) is
rejected via :class:`InterventionError` — never emitted as a broken world.
"""

from __future__ import annotations

from typing import Any

from ..dsl.ast import Dsl
from ..sources.base import World
from ..sources.world_ops import (
    clone_world,
    find_point,
    find_row,
    find_series,
    is_geometry,
    is_plot,
    is_table,
    node_kind,
    world_sha256,
)
from .base import (
    InterventionError,
    OperatorName,
    TransformedWorld,
    transformed_hash,
)
from .feasibility import FeasibilityError, is_feasible

_NAME: OperatorName = "CLEAN_DELETE_V1"


class CleanDeleteOperator:
    """Delete ``target_node`` (and incident relations) from the world graph."""

    name = _NAME

    def apply(
        self,
        world: World,
        target_node: str,
        *,
        seed: int,
        dsl: Dsl,
        **_kwargs: Any,
    ) -> TransformedWorld:
        if node_kind(world, target_node) is None:
            raise InterventionError(f"CLEAN_DELETE_V1: target {target_node!r} is not a world node")
        new = clone_world(world)
        _delete(new, target_node)
        try:
            is_feasible(new, dsl=dsl)
        except FeasibilityError as exc:
            raise InterventionError(
                f"CLEAN_DELETE_V1: infeasible after deleting {target_node!r}: {exc}"
            ) from exc
        return TransformedWorld(
            world=new,
            operator=_NAME,
            target_node=target_node,
            seed=seed,
            transformed_world_sha256=transformed_hash(
                operator=_NAME,
                target_node=target_node,
                seed=seed,
                source_world_sha256=world_sha256(world),
                transformed_world_sha256=world_sha256(new),
                hidden_nodes=frozenset(),
                substitute_value=None,
            ),
            hidden_nodes=frozenset(),
            substitute_value=None,
            description=f"clean-delete {target_node}",
        )


def _delete(world: World, node_id: str) -> None:
    """Remove ``node_id`` and its incident relations, in place."""
    if is_plot(world):
        point = find_point(world, node_id)
        if point is not None:
            sidx, _pidx, _p = point
            pts = world["series"][sidx].get("points", []) or []
            world["series"][sidx]["points"] = [q for q in pts if q.get("id") != node_id]
            return
        series = find_series(world, node_id)
        if series is not None:
            world["series"] = [s for s in world.get("series", []) if s.get("id") != node_id]
        return
    if is_table(world):
        if find_row(world, node_id) is not None:
            world["rows"] = [r for r in world.get("rows", []) if r.get("id") != node_id]
        return
    if is_geometry(world):
        # Delete the entity plus every constraint that mentions it (incident
        # relations). A constraint whose only stated entity is the deleted one
        # is gone entirely; the feasibility check then guards dangling refs.
        world["entities"] = [e for e in world.get("entities", []) if e.get("id") != node_id]
        world["constraints"] = [
            c
            for c in world.get("constraints", [])
            if node_id not in {str(a) for a in (c.get("args") or []) if isinstance(a, str)}
        ]
        return


__all__ = ["CleanDeleteOperator"]