Example #1
0
    def test_tree_creation(self):
        prototype = Prototype()
        node_1 = prototype.add_node("node_1")
        self.assertEqual(
            node_1.name, "node_1", "The name of created node should be node_1")
        node_2 = prototype.add_node("node_2", node_1)
        self.assertEqual(
            node_2.name, "node_2", "The name of created node should be node_2")
        self.assertEqual(
            node_2.parent(), node_1, "The parent of node_2 should be node_1")
        node_3 = prototype.add_node("node_3", node_2)
        node_4 = prototype.add_node("node_4", node_2)
        self.assertEqual(
            node_1.child_count(), 1, "Node_1 should have only one child")
        self.assertEqual(
            node_2.child_count(), 2, "Node_2 should have two children")
        self.assertEqual(
            prototype.subtree_node_count(node_1), 4,
            "Subtree of node_1 should have 4 nodes")
        self.assertEqual(
            prototype.subtree_node_count(node_2), 3,
            "Subtree of node_2 should have 3 nodes")
        self.assertEqual(
            prototype.subtree_node_count(node_3), 1,
            "Subtree of node_3 should have 1 node")

        nodes = [node_1, node_2, node_3, node_4]
        self.assertEqual(len(list(prototype.nodes())), len(nodes))
        for node in prototype.nodes():
            self.assertTrue(node in nodes)

        self.assertEqual(
            prototype.subtree_node_count(prototype.root()), prototype.node_count())
        self.assertEqual(prototype.node_count(), 4)
Example #2
0
    def test_node_removal(self):
        tree = Prototype()
        root = tree.add_node("root")
        node_1 = root.add_node("node_1")
        node_2 = root.add_node("node_2")
        node_3 = node_1.add_node("node_3")
        node_4 = node_3.add_node("node_4")
        node_5 = node_2.add_node("node_5")

        self.assertEqual(tree.node_count(), 6)
        self.assertRaises(NodeNotEmptyException, tree.remove_node, node_3)
        tree.remove_node(node=node_5)
        self.assertEqual(tree.node_count(), 5)
        # check correct order
        self.assertEqual(node_4, tree._graph._last_node)
        self.assertEqual(None, node_4.next_node)

        tree.remove_node(node=node_2)
        self.assertEqual(tree.node_count(), 4)
        self.assertEqual(node_1.next_node, node_3)
        self.assertEqual(node_3.previous_node, node_1)

        child_1 = node_3.add_node("child_1")
        child_2 = node_3.add_node("child_2")
        child_3 = node_3.add_node("child_3")
        self.assertEqual(node_4.position, 0)
        self.assertEqual(child_1.position, 1)
        self.assertEqual(child_2.position, 2)
        self.assertEqual(child_3.position, 3)
        tree.remove_node(node=child_2)
        self.assertEqual(child_1.position, 1)
        self.assertEqual(child_2.position, 2)

        tree.remove_subtree(node=node_3)
        self.assertEqual(tree.node_count(), 2)
Example #3
0
    def prototype(self):
        """
        Getter property for prototype that is used to generate a random monitoring
        event stream.

        :return: Prototype where random tree is based on
        """
        if self._prototype is None:
            prototype = Prototype()
            root = prototype.add_node(name=id_generator(size=6),
                                      tme=0,
                                      exit_tme=0,
                                      pid=1,
                                      ppid=0)
            for i in range(self._prototype_node_count):
                # TODO: check if this is < or <=
                if root.child_count() > 0 and random.random() <= \
                        self._relative_repetition:
                    node_name = random.choice(root.children_list())
                else:
                    node_name = id_generator()
                prototype.add_node(name=node_name,
                                   parent=root,
                                   tme=0,
                                   exit_tme=0,
                                   pid=i + 2,
                                   ppid=1)
            assert prototype.node_count() - 1 == self._prototype_node_count
            self._prototype = prototype
        return self._prototype
Example #4
0
    def test_unique_tree_ids(self):
        prototype = Prototype()
        root = prototype.add_node("node", pid=1, ppid=0)
        for _ in range(20):
            root.add_node("node")
        one_child = list(root.children())[0]
        for _ in range(20):
            one_child.add_node("node")

        self.assertEqual(prototype.node_count(), 41)
Example #5
0
 def test_empty_tree(self):
     prototype = Prototype()
     self.assertEqual(prototype.node_count(), 0,
                      "The node count for an empty prototype should be 0")
Example #6
0
class TestSignatureFunctionalities(unittest.TestCase):
    def setUp(self):
        self.prototype = Prototype()
        root = self.prototype.add_node("root", ppid=0, pid=1)
        for _ in range(10):
            root.add_node("child")
        for _ in range(10):
            root.add_node("child2")
        for _ in range(10):
            root.add_node("child")
        for _ in range(10):
            root.add_node("child2")
        child_node = list(root.children())[2]
        for i in range(10):
            child_node.add_node(i)
        child_child_node = list(child_node.children())[0]
        for _ in range(5):
            child_child_node.add_node("child")

    def test_base_signature(self):
        signature = Signature()
        self._initialize_signature(signature)

        # check that node count is still the same like in original tree
        signatures = set()
        for node in self.prototype.nodes():
            signatures.add(signature.get_signature(node, node.parent()))
        # TODO: is this really correct?
        # self.assertEqual(len(signatures), self.prototype.node_count())
        self.assertEqual(len(signatures), 13)

    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)

    def test_parent_child_order_topology_signature(self):
        signature = ParentChildOrderTopologySignature()
        self._initialize_signature(signature)

        # all nodes should have different names
        signatures = set()
        for node in self.prototype.nodes():
            signatures.add(signature.get_signature(node, node.parent()))
        self.assertEqual(len(signatures), len(list(self.prototype.nodes())))

    def test_parent_child_order_by_name_topology_signature(self):
        signature = ParentChildOrderByNameTopologySignature()
        self._initialize_signature(signature)

        signatures = set()
        for node in self.prototype.nodes():
            signatures.add(signature.get_signature(node, node.parent()))
        self.assertEqual(len(signatures), 16)

    def test_parent_counted_children_by_name_topology_signature(self):
        max = self.prototype.node_count()

        # should match by name signatures
        self._check_counted_signature(0, 14)

        self._check_counted_signature(1, 18)
        self._check_counted_signature(2, 22)
        self._check_counted_signature(3, 26)
        self._check_counted_signature(4, 30)
        self._check_counted_signature(5, 33)
        self._check_counted_signature(6, 36)
        self._check_counted_signature(7, 39)
        self._check_counted_signature(8, 42)
        self._check_counted_signature(9, 45)
        self._check_counted_signature(10, 46)
        self._check_counted_signature(11, 47)
        self._check_counted_signature(12, 48)
        self._check_counted_signature(13, 49)
        self._check_counted_signature(14, 50)
        self._check_counted_signature(15, 51)
        self._check_counted_signature(20, max)
        self._check_counted_signature(21, max)
        self._check_counted_signature(50, max)
        self._check_counted_signature(100, max)

    def _check_counted_signature(self, count, result):
        signature = ParentCountedChildrenByNameTopologySignature(count=count)
        self._initialize_signature(signature)

        signatures = set()
        for node in self.prototype.nodes():
            signatures.add(signature.get_signature(node, node.parent()))
        self.assertEqual(len(signatures), result)

    def _initialize_signature(self, signature):
        for node in self.prototype.nodes():
            self.assertIsNotNone(signature.get_signature(node, node.parent()))

    def test_representation(self):
        signature = ParentCountedChildrenByNameTopologySignature(count=5)
        self.assertEqual(
            signature.__repr__(),
            "ParentCountedChildrenByNameTopologySignature (count: 5)")

    def test_custom_creation(self):
        signature = Signature()
        self.assertEqual(Signature, signature.__class__)
        signature = Signature("ParentChildByNameTopologySignature")
        self.assertEqual(ParentChildByNameTopologySignature, signature.__class__)
        signature = Signature("ParentChildOrderTopologySignature")
        self.assertEqual(ParentChildOrderTopologySignature, signature.__class__)
        signature = Signature("ParentChildOrderByNameTopologySignature")
        self.assertEqual(ParentChildOrderByNameTopologySignature, signature.__class__)

    def test_empty_nodes(self):
        signature = ParentCountedChildrenByNameTopologySignature(count=2)

        signatures = set()
        for node in simple_prototype().nodes(include_marker=True):
            try:
                signatures.add(signature.get_signature(node, node.parent()))
            except AttributeError:
                signatures.update(signature.finish_node(node.parent()))
        print(signatures)
        self.assertEqual(
            {'_root_1', '_test_192807604', 'muh__192807604', 'muh_test__192807604',
             'muh_test_muh_192807604', 'test_muh_192807604', 'test_muh_test_192807604'},
            signatures)

    def test_count_signature_for_correct_zero_distance(self):
        signature = ParentCountedChildrenByNameTopologySignature(count=3)
        algorithm = IncrementalDistanceAlgorithm(signature=signature)
        decorator = DistanceMatrixDecorator(normalized=False)
        decorator.wrap_algorithm(algorithm)
        algorithm.prototypes = [real_tree()]

        algorithm.start_tree()
        for event in real_tree().event_iter(include_marker=True, supported=algorithm.supported):
            try:
                algorithm.add_event(event)
            except EventNotSupportedException:
                pass
        algorithm.finish_tree()
        print(algorithm._signature_prototypes._prototype_dict[0]._prototype_dict.keys())
        self.assertEqual([[[0]]], decorator.data())

    def test_node_count_for_correct_zero_distance(self):
        signature = EnsembleSignature(
            signatures=[ParentChildByNameTopologySignature(),
                        ParentCountedChildrenByNameTopologySignature(count=3)])
        algorithm = IncrementalDistanceAlgorithm(
            signature=signature, distance=SimpleDistance)
        data_decorator = DataDecorator()
        data_decorator.wrap_algorithm(algorithm)
        algorithm.prototypes = [real_tree()]

        algorithm.start_tree()
        for event in real_tree().event_iter(include_marker=True, supported=algorithm.supported):
            try:
                algorithm.add_event(event)
            except EventNotSupportedException:
                pass
        algorithm.finish_tree()
        self.assertEqual([tree_value for values in data_decorator.data().get(
            "prototypes", {}).get("converted", []) for tree_value in values],
            [tree_value for values in data_decorator.data().get(
             "monitoring", {}).get("converted", []) for tree_value in values])