Beispiel #1
0
    def __init__(self):
        """ Initialize MainTree.

        @param root - the "root" node which contains all nodes
        """

        self.nodes = {}
        self.pending_relationships = []

        self.__cllbcks = {}

        self.root_id = 'root'
        self.root = _Node(self.root_id)
        _Node._set_tree(self.root,self)

        self._queue = processqueue.SyncQueue()
Beispiel #2
0
    def __init__(self):
        """ Initialize MainTree.

        @param root - the "root" node which contains all nodes
        """

        self.nodes = {}
        self.pending_relationships = []

        self.__cllbcks = {}

        self.root_id = 'root'
        self.root = _Node(self.root_id)
        _Node._set_tree(self.root, self)

        self._queue = processqueue.SyncQueue()
Beispiel #3
0
    def _add_node(self, node, parent_id):
        """ Add a node to the tree

        @param node - node to be added
        @param parent_id - parent to add or it will be add to root
        """
        node_id = node.get_id()
        if node_id in self.nodes:
            print("Error: Node '%s' already exists" % node_id)
            return False

        _Node._set_tree(node,self)
        for relationship in node.pending_relationships:
            if relationship not in self.pending_relationships:
                self.pending_relationships.append(relationship)
        node.pending_relationships = []

        self.nodes[node_id] = node

        add_to_root = True
        parents_to_refresh = []
        children_to_refresh = []

        # Build pending relationships
        for rel_parent_id, rel_child_id in list(self.pending_relationships):
            # Adding as a child
            if rel_child_id == node_id and rel_parent_id in self.nodes:
                if not self._is_circular_relation(rel_parent_id, node_id):
                    self._create_relationship(rel_parent_id, node_id)
                    add_to_root = False
                    parents_to_refresh.append(rel_parent_id)
                else:
                    print("Error: Detected pending circular relationship", \
                        rel_parent_id, rel_child_id)
                self.pending_relationships.remove((rel_parent_id, rel_child_id))

            # Adding as a parent
            if rel_parent_id == node_id and rel_child_id in self.nodes:
                if not self._is_circular_relation(node_id, rel_child_id):
                    self._create_relationship(node_id, rel_child_id)
                    children_to_refresh.append(rel_child_id)
                else:
                    print("Error: Detected pending circular relationship", \
                        rel_parent_id, rel_child_id)
                self.pending_relationships.remove((rel_parent_id, rel_child_id))
        
        # Build relationship with given parent
        if parent_id is not None:
            if self._is_circular_relation(parent_id, node_id):
                raise Exception('Creating circular relationship between %s and %s' % \
                     (parent_id, node_id))
            if parent_id in self.nodes:
                self._create_relationship(parent_id, node_id)
                add_to_root = False
                parents_to_refresh.append(parent_id)
            else:
                self.pending_relationships.append((parent_id, node_id))

        # Add at least to root
        if add_to_root:
            self.root.children.append(node_id)

        # Send callbacks
        #updating the parent and the children is handled by the FT
        self._callback("node-added", node_id)
Beispiel #4
0
    def _add_node(self, node, parent_id):
        """ Add a node to the tree

        @param node - node to be added
        @param parent_id - parent to add or it will be add to root
        """
        node_id = node.get_id()
        if node_id in self.nodes:
            print "Error: Node '%s' already exists" % node_id
            return False

        _Node._set_tree(node, self)
        for relationship in node.pending_relationships:
            if relationship not in self.pending_relationships:
                self.pending_relationships.append(relationship)
        node.pending_relationships = []

        self.nodes[node_id] = node

        add_to_root = True
        parents_to_refresh = []
        children_to_refresh = []

        # Build pending relationships
        for rel_parent_id, rel_child_id in list(self.pending_relationships):
            # Adding as a child
            if rel_child_id == node_id and rel_parent_id in self.nodes:
                if not self._is_circular_relation(rel_parent_id, node_id):
                    self._create_relationship(rel_parent_id, node_id)
                    add_to_root = False
                    parents_to_refresh.append(rel_parent_id)
                else:
                    print "Error: Detected pending circular relationship", \
                        rel_parent_id, rel_child_id
                self.pending_relationships.remove(
                    (rel_parent_id, rel_child_id))

            # Adding as a parent
            if rel_parent_id == node_id and rel_child_id in self.nodes:
                if not self._is_circular_relation(node_id, rel_child_id):
                    self._create_relationship(node_id, rel_child_id)
                    children_to_refresh.append(rel_child_id)
                else:
                    print "Error: Detected pending circular relationship", \
                        rel_parent_id, rel_child_id
                self.pending_relationships.remove(
                    (rel_parent_id, rel_child_id))

        # Build relationship with given parent
        if parent_id is not None:
            if self._is_circular_relation(parent_id, node_id):
                raise Exception('Creating circular relationship between %s and %s' % \
                     (parent_id, node_id))
            if parent_id in self.nodes:
                self._create_relationship(parent_id, node_id)
                add_to_root = False
                parents_to_refresh.append(parent_id)
            else:
                self.pending_relationships.append((parent_id, node_id))

        # Add at least to root
        if add_to_root:
            self.root.children.append(node_id)

        # Send callbacks
        #updating the parent and the children is handled by the FT
        self._callback("node-added", node_id)