Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
agebhar1 committed Sep 26, 2023
1 parent c8b380d commit 47a5b37
Show file tree
Hide file tree
Showing 2 changed files with 405 additions and 3 deletions.
74 changes: 73 additions & 1 deletion graph/planner/planner.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from typing import Union, Tuple, List
from dataclasses import dataclass, field
from typing import Union

from direct_acyclic_graph import DirectAcyclicGraph, EdgeType


@dataclass
class NodeState:
user_selected: bool = True
primary_action: None | str = None
transitive_action: None | str = None
state: set[str] = field(default_factory=set)
transitive_state: set[str] = field(default_factory=set)


class Planner:
_state: DirectAcyclicGraph
_target: DirectAcyclicGraph
Expand All @@ -24,6 +34,68 @@ def __init__(
self._unhealthy = unhealthy or []
self._selected = selected

def propagate(self) -> dict[str, NodeState]:
nodes_state = self._state.nodes()
nodes_target = self._target.nodes()

nodes_add = (nodes_target - nodes_state).intersection(
(nodes_target - nodes_state)
if self._selected == "*"
else {node for node in self._selected if not node.startswith("-")}
)
nodes_delete = (
(nodes_state - nodes_target)
if self._selected == "*"
else {node[1:] for node in self._selected if node.startswith("-")}
)
nodes_selected = (
nodes_state.union(nodes_target)
if self._selected == "*"
else {
node if not node.startswith("-") else node[1:]
for node in self._selected
}
)

nodes = {
node: NodeState(
user_selected=node in nodes_selected,
primary_action=(
"add"
if node in nodes_add
else "delete"
if node in nodes_delete
else None
),
state=({"modified"} if node in self._modified else set()).union(
{"unhealthy"} if node in self._unhealthy else set()
),
)
for node in (nodes_state.union(nodes_target))
}

for node in nodes_delete:
childs_transitive = self._state.childs_transitive(
node,
accept=lambda adjacent: adjacent[1] != EdgeType.TOOL,
)
for node_transitive_delete in childs_transitive:
# if nodes[node_transitive_delete].primary_action != "delete":
nodes[node_transitive_delete].transitive_action = "delete"

# for node, value in nodes.items():
# if len(value.state) > 0:
# childs_transitive = self._state.childs_transitive(
# node,
# accept=lambda adjacent: adjacent[1] != EdgeType.TOOL,
# )
# for node_transitive_state in childs_transitive:
# nodes[node_transitive_state].transitive_state = nodes[
# node_transitive_state
# ].transitive_state.union(value.state)

return nodes

def apply(self) -> list[str]:
nodes_state = self._state.nodes()
nodes_target = self._target.nodes()
Expand Down
Loading

0 comments on commit 47a5b37

Please sign in to comment.