Skip to content

Commit

Permalink
[WIP] re-design
Browse files Browse the repository at this point in the history
  • Loading branch information
agebhar1 committed Oct 2, 2023
1 parent 8d455cb commit 993904d
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 158 deletions.
205 changes: 118 additions & 87 deletions graph/planner/planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def __init__(
self._unhealthy = unhealthy or []
self._selected = selected

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

Expand Down Expand Up @@ -90,102 +92,131 @@ def apply(self) -> tuple[dict[str, NodeState], list[str]]:

# add required nodes by parent dependency
for node in nodes_add:
parent_transitive = self._target.parents_transitive(node)
for node_add_transitive in parent_transitive:
if node_add_transitive not in nodes_state:
nodes[node_add_transitive].transitive_action = nodes[
node_add_transitive
for parent in self._target.parents_transitive(node):
nodes[parent].active = nodes[parent].active.union({"transitive"})
if parent not in nodes_state:
nodes[parent].transitive_action = nodes[
parent
].transitive_action.union({"add"})

# delete child nodes
# delete child nodes, consider 'Tools' (parent)
for node in nodes_delete:
childs_transitive = self._state.childs_transitive(
for tool in self._state.parents(
node, accept=lambda adjacent: adjacent[1] == EdgeType.TOOL
):
nodes[tool].active = nodes[tool].active.union({"transitive"})
for parent in self._state.parents_transitive(tool):
nodes[parent].active = nodes[parent].active.union({"transitive"})

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

# propagate 'modified'/'unhealthy' state
unhealthy_adjacent_tool_edge_nodes = set()
for key, value in nodes.items():
node = key
state = value.state
# TODO test if reachable/required from user selected nodes
if "unhealthy" in state:
# propagate 'unhealthy' state to children != EdgeType.TOOL
childs_transitive = self._state.childs_transitive(
if "unhealthy" in value.state:
for child in 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({"unhealthy"})

# collect nodes ('unhealthy')' w/ EdgeType.TOOL in reverse graph ~> incoming edge(s)
if EdgeType.TOOL in value.dependent_edge_types:
unhealthy_adjacent_tool_edge_nodes.add(node)

if "modified" in state and value.active:
# propagate 'modified' state to children != EdgeType.TOOL
childs_transitive = self._state.childs_transitive(
):
nodes[child].active = nodes[child].active.union({"transitive"})
nodes[child].transitive_state = nodes[child].transitive_state.union(
{"unhealthy"}
)

if "modified" in value.state and "user" in value.active:
for child in 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({"modified"})

operations = []
# recover unhealthy nodes (required by dependent 'active' nodes)
if len(unhealthy_adjacent_tool_edge_nodes) > 0:
for node in reversed(self._state.topological_order()):
if node in unhealthy_adjacent_tool_edge_nodes:
operations.append(f"-{node}")
for node in self._state.topological_order():
if node in unhealthy_adjacent_tool_edge_nodes:
operations.append(f"+{node}")

# TODO re-run w/ updated unhealthy list

nodes_re_add_modified_or_unhealthy = (
[]
) # optimization, not evaluate modified/unhealthy again
# 'delete' run
# TODO postpone nodes wich are required as tool
for node in self._state.topological_order():
value = nodes[node]

modified = (
"modified" in value.state
and value.active
or "modified" in value.transitive_state
)
unhealthy = (
"unhealthy" in value.state and value.active
) or "unhealthy" in value.transitive_state
delete = "delete" == value.action or "delete" in value.transitive_action

if not delete and (modified or unhealthy):
operations.append(f"-{node}")
nodes_re_add_modified_or_unhealthy.append(node)

if delete:
operations.append(f"-{node}")

# 'add' run
for node in reversed(self._target.topological_order()):
value = nodes[node]

add = "add" == value.action or "add" in value.transitive_action
if add or node in nodes_re_add_modified_or_unhealthy:
operations.append(f"+{node}")

# TODO delete postponed nodes required as tool but selected for 'delete'

return nodes, operations
):
nodes[child].active = nodes[child].active.union({"transitive"})
nodes[child].transitive_state = nodes[child].transitive_state.union(
{"modified"}
)

# # propagate 'modified'/'unhealthy' state
# unhealthy_adjacent_tool_edge_nodes = set()
# for key, value in nodes.items():
# node = key
# state = value.state
# # TODO test if reachable/required from user selected nodes
# if "unhealthy" in state:
# # propagate 'unhealthy' state to children != EdgeType.TOOL
# 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({"unhealthy"})
#
# # collect nodes ('unhealthy')' w/ EdgeType.TOOL in reverse graph ~> incoming edge(s)
# if EdgeType.TOOL in value.dependent_edge_types:
# unhealthy_adjacent_tool_edge_nodes.add(node)
#
# if "modified" in state and value.active:
# # propagate 'modified' state to children != EdgeType.TOOL
# 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({"modified"})
#
# operations = []
# # recover unhealthy nodes (required by dependent 'active' nodes)
# if len(unhealthy_adjacent_tool_edge_nodes) > 0:
# for node in reversed(self._state.topological_order()):
# if node in unhealthy_adjacent_tool_edge_nodes:
# operations.append(f"-{node}")
# for node in self._state.topological_order():
# if node in unhealthy_adjacent_tool_edge_nodes:
# operations.append(f"+{node}")
#
# # TODO re-run w/ updated unhealthy list
#
# nodes_re_add_modified_or_unhealthy = (
# []
# ) # optimization, not evaluate modified/unhealthy again
# # 'delete' run
# # TODO postpone nodes wich are required as tool
# for node in self._state.topological_order():
# value = nodes[node]
#
# modified = (
# "modified" in value.state
# and value.active
# or "modified" in value.transitive_state
# )
# unhealthy = (
# "unhealthy" in value.state and value.active
# ) or "unhealthy" in value.transitive_state
# delete = "delete" == value.action or "delete" in value.transitive_action
#
# if not delete and (modified or unhealthy):
# operations.append(f"-{node}")
# nodes_re_add_modified_or_unhealthy.append(node)
#
# if delete:
# operations.append(f"-{node}")
#
# # 'add' run
# for node in reversed(self._target.topological_order()):
# value = nodes[node]
#
# add = "add" == value.action or "add" in value.transitive_action
# if add or node in nodes_re_add_modified_or_unhealthy:
# operations.append(f"+{node}")
#
# # TODO delete postponed nodes required as tool but selected for 'delete'

return (nodes,) # , operations
Loading

0 comments on commit 993904d

Please sign in to comment.