From 9e327a26439ab67bb479abe7eec704a697a0a08f Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Thu, 6 Jan 2022 12:27:11 +0100 Subject: [PATCH] Conditions Tree: first blueprint using anytree (iteratively) --- src/rules_tree.py | 86 ++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 54 deletions(-) diff --git a/src/rules_tree.py b/src/rules_tree.py index 2104cad..329b5d7 100644 --- a/src/rules_tree.py +++ b/src/rules_tree.py @@ -1,69 +1,47 @@ +# https://anytree.readthedocs.io/en/latest/_modules/anytree/node/nodemixin.html +from anytree import NodeMixin, RenderTree import model -from anytree import Node, RenderTree -# from anytree import Node, AnyNode, NodeMixin, RenderTree, find_by_attr -# from anytree.exporter import DotExporter from ete3 import Tree - -# https://stackoverflow.com/ -# read-data-from-a-file-and-create-a-tree-using-anytree-in-python -# https://www.delftstack.com/howto/python/trees-in-python/ -# https://python-gtk-3-tutorial.readthedocs.io/en/latest/treeview.html -# https://anytree.readthedocs.io/en/latest/ - # TODO Contrôles de cohérence des données -# 1) node_id <> pas de doublon -# 2) pas de node sans un node_parent_id +# 1) node_id <> pas de doublons +# 2) tout node doit avoir un node_parent_id conditions_set = set() +conditions_list = [] -# TODO cnd.get_coord("y"),... for i in range(0, len(model.testmodel.get_conditions().get_conditions_array())): - cnd = model.testmodel.get_conditions().get_condition(i) - conditions_set.add((cnd.get_node_self_id(), - cnd.get_node_parent_id(), - cnd.get_coord("x"), - cnd.get_site(), - cnd.get_weight())) + condition = model.testmodel.get_conditions().get_condition(i) + conditions_set.add((condition.get_node_self_id(), + condition.get_node_parent_id(), + condition.get_coord("x"), # TODO y,z,... + condition.get_site(), + condition.get_weight())) - -class TreeNode(object): - - # TODO self.y = y,... - def __init__(self, id, parent, x, site, weight): +class CondTreeNode(NodeMixin): + def __init__(self, id, x, site, weight, parent_node_id): + super(CondTreeNode, self).__init__() self.id = id - self.parent = parent - self.children = [] - self.x = x + self.parent_node_id = parent_node_id + self.parent = None + self.x = x # TODO self.y = y,z... self.site = site self.weight = weight - def add_child(self, children_id): - self.children.append(children_id) - self.children = sorted(self.children, key=id) + def add_parent_node(self, parent_node): + self.parent = parent_node + print('orphan node ', self.id, ' is adopted by node ', parent_node.id) +for i in conditions_set: # sorted(conditions_set, key=i.id): + condition = CondTreeNode(i[0], i[2], i[3], i[4], i[1]) # TODO y,z,... + conditions_list.append(condition) -cnd_list = [] -nodes_list = [] # [Node("0")] +for i in conditions_list: + if (i.parent == None): + for j in conditions_list: + if (i.parent_node_id == j.id): + i.add_parent_node(j) -for i in conditions_set: # sorted(conditions_set, key=id): - cndt = TreeNode(i[0], i[1], i[2], i[3], i[4]) # TODO y,z,... - cnd_list.append(cndt) - -for i in cnd_list: - for j in cnd_list: - if (i.id == j.parent): - i.add_child(j) - nodes_list.append(Node(str(i))) # (Node(str(i), parent=str(i.parent))) #(i) - - -n0 = Node("0") -n1 = Node("1", parent=n0) -n2 = Node("2", parent=n1) -n3 = Node("3", parent=n2) -n4 = Node("4", parent=n2) - -for pre, fill, node in RenderTree(n0): - print("%s%s" % (pre, node.name)) - -for i in nodes_list: - print(i) # print(i.id, ' ', i.parent) +for pre, fill, node in RenderTree(conditions_list[3]): + treestr = u"%s%s" % (pre, node.id) + print(treestr.ljust(16), ' n =', node.weight, + ' at (site', node.site, 'in cell', node.x, ')')