예제 #1
0
    def test_parent_child_by_name_topology_signature(self):
        signature = ParentChildByNameTopologySignature()
        self._initialize_signature(signature)

        # ensure that "same nodes" get same signature
        root = self.prototype.root()
        signatures = set()
        for child in root.children():
            current_signature = signature.get_signature(child, root)
            signatures.add(current_signature)
            self.assertEqual(
                current_signature, ParentChildByNameTopologySignature.signature_string(
                    child.name, signature.get_signature(
                        root, None) if root is not None else ""))
        self.assertEqual(len(signatures), 2)

        child_node = list(root.children())[2]
        signatures = set()
        for child in child_node.children():
            current_signature = signature.get_signature(child, child_node)
            signatures.add(current_signature)
            self.assertEqual(
                current_signature, ParentChildByNameTopologySignature.signature_string(
                    child.name, signature.get_signature(
                        child_node, None) if child_node is not None else ""))
        self.assertEqual(len(signatures), 10)

        # test if there are "just" 13 different signatures
        signatures = set()
        for node in self.prototype.nodes():
            current_signature = signature.get_signature(node, node.parent())
            signatures.add(current_signature)
            self.assertEqual(
                current_signature, ParentChildByNameTopologySignature.signature_string(
                    node.name, signature.get_signature(
                        node.parent(), None) if node.parent() is not None else ""))
        self.assertEqual(len(signatures), 14)
예제 #2
0
 def prepare_signature(self, node, parent):
     ordered_nodes = [node.name] + list(
         self.sibling_generator(node, self._width + 1))
     ordered_nodes.sort()  # ascending order
     algorithm_id = "%s_%s_%s" % (
         "_".join(ordered_nodes[1:-1]),  # up to width ordered siblings
         ordered_nodes[-1],  # last element as anchor node name
         zlib.adler32(
             self.get_signature(parent, None, dimension="p").
             encode('utf-8', errors='surrogateescape'
                    ) if parent is not None else b''))
     p_signature = ParentChildByNameTopologySignature.signature_string(
         node.name,
         self.get_signature(parent, None, dimension="p")
         if parent is not None else '')
     self._prepare_signature(node, algorithm_id, p=p_signature)
예제 #3
0
 def finish_node(self, node):
     # node is the PARENT of the current hierarchy :D
     result = []
     p_signature = ParentChildByNameTopologySignature.signature_string(
         '', self.get_signature(node, None, dimension="p"))
     if node.children_list():
         # we need to consider the insertion of empty nodes
         ordered_nodes = list(
             self.sibling_finish_generator(node, self._width + 1))
         if ordered_nodes:
             ordered_nodes.sort()
             ordered_nodes.pop(0)
             while ordered_nodes:
                 algorithm_id = "%s_%s" % ("_".join(ordered_nodes),
                                           p_signature)
                 result.append(algorithm_id)
                 ordered_nodes.pop(0)
     return result