Ejemplo n.º 1
0
    def test_constraints_and_basic(self):
        line_source = LineSource('localhost', 'node_0.log')
        effect = Clue(('Pear', 2), 'Pear, 2 times', 3000, line_source)
        clues_lists = [
            ([
                Clue(('Milk', 3), 'Milk, 3 times', 50, line_source),
                Clue(('Chocolate', 2), 'Chocolate, 2 times', 100, line_source),
                Clue(('Pear', 2), 'Pear, 2 times', 150, line_source)
            ], 1), ([
                Clue(('Pear', 2), 'Pear, 2 times', 1050, line_source),
                Clue(('Milk', 1), 'Milk, 1 times', 1100, line_source)
            ], 1)
        ]  # yapf: disable
        constraints = [{
            'clues_groups': [[0, 1], [1, 1], [2, 1]],
            'name': 'identical',
            'params': {}
        }, {
            'clues_groups': [[0, 2], [1, 2], [2, 2]],
            'name': 'identical',
            'params': {}
        }]
        causes = Verifier.constraints_and(clues_lists, effect, constraints,
                                          ConstraintManager())
        assert len(causes) == 1

        assert all(cause.constraints_linkage == InvestigationResult.AND
                   for cause in causes)
        assert causes[0].lines == [
            FrontInput.from_clue(Clue(
                ('Pear', 2), 'Pear, 2 times', 150, line_source)),
            FrontInput.from_clue(Clue(
                ('Pear', 2), 'Pear, 2 times', 1050, line_source))
        ]  # yapf: disable
Ejemplo n.º 2
0
    def test_single_constraint_not_basic(self):
        line_source = LineSource('localhost', 'node_0.log')
        effect = Clue(('Dinner', 44), 'Dinner, 44 times', 440, line_source)
        clues_lists = [
            ([
                Clue(('Car', 40), 'Car, 40 times', 400, line_source),
                Clue(('Car', 1), 'Car, 1 times', 100, line_source),
                Clue(('Car', 15), 'Car, 15 times', 150, line_source)
            ], 1),
            ([
                Clue(('Forest', 0), 'Forest, 0 times', 0, line_source),
                Clue(('Forest', 3), 'Forest, 3 times', 30, line_source),
                Clue(('Forest', 42), 'Forest, 42 times', 420, line_source)
            ], 1)
        ]  # yapf: disable
        constraint = {
            'clues_groups': [[0, 1], [1, 1], [2, 1]],
            'name': 'identical',
            'params': {}
        }
        causes = Verifier.single_constraint_not(clues_lists,
                                                effect, constraint,
                                                ConstraintManager())

        assert len(causes) == 1
        assert causes[0].constraints_linkage == InvestigationResult.NOT
        assert causes[0].constraints[0] == constraint
Ejemplo n.º 3
0
    def test_constraints_or_two_time_constraints(self):
        line_source = LineSource('localhost', 'node_0.log')
        effect = Clue(('Foo occurred', datetime(2000, 6, 14, second=15)),
                      'Foo occurred, 2000 06 14 00:00:15', 500, line_source)
        clues_lists = [
            ([
                 Clue(('Bar occurred', datetime(2000, 6, 14, second=10)), 'Bar occurred, 2000 06 14 00:00:10', 250, line_source),
             ], 1)
        ]  # yapf: disable
        constraints = [{
            'clues_groups': [[1, 2], [0, 2]],
            'name': 'time_delta',
            'params': {
                'max_delta': 8.0
            }
        }, {
            'clues_groups': [[1, 2], [0, 2]],
            'name': 'time_delta',
            'params': {
                'max_delta': 3.0
            }
        }]
        causes = Verifier.constraints_or(clues_lists, effect, constraints,
                                         ConstraintManager())
        assert len(causes) == 1

        assert all(cause.constraints_linkage == InvestigationResult.OR
                   for cause in causes)
        assert causes[0].lines == [
            FrontInput.from_clue(Clue(
                ('Bar occurred', datetime(2000, 6, 14, second=10)), 'Bar occurred, 2000 06 14 00:00:10', 250, line_source))
        ]  # yapf: disable
        assert len(causes[0].constraints) == 1
        assert causes[0].constraints[0] == constraints[0]
Ejemplo n.º 4
0
    def test_constraints_or_without_constraints(self):
        line_source = LineSource('localhost', 'node_0.log')
        effect = Clue(('Pineapple', 44), 'Pineapple, 44 times', 3000,
                      line_source)
        clues_lists = [
            ([
                Clue(('Banana', 2), 'Banana, 2 times', 1050, line_source),
                Clue(('Milk', 1), 'Milk, 2 times', 1100, line_source)
            ], 1),
            ([
                Clue(('Chocolate', 2), 'Chocolate, 2 times', 100, line_source)
            ], 1)
        ]  # yapf: disable
        constraints = []

        causes = Verifier.constraints_or(clues_lists, effect, constraints,
                                         ConstraintManager())
        assert len(causes) == 2

        assert all(cause.constraints_linkage == InvestigationResult.OR
                   for cause in causes)
        assert causes[0].lines == [
            FrontInput.from_clue(Clue(
                ('Banana', 2), 'Banana, 2 times', 1050, line_source)),
            FrontInput.from_clue(Clue(
                ('Chocolate', 2), 'Chocolate, 2 times', 100, line_source))
        ]  # yapf: disable
        assert causes[1].lines == [
            FrontInput.from_clue(Clue(
                ('Milk', 1), 'Milk, 2 times', 1100, line_source)),
            FrontInput.from_clue(Clue(
                ('Chocolate', 2), 'Chocolate, 2 times', 100, line_source))
        ]  # yapf: disable
        assert all(not cause.constraints for cause in causes)
Ejemplo n.º 5
0
 def test_constraints_or_verification_failed(self):
     line_source = LineSource('localhost', 'node_0.log')
     effect = Clue(('Pineapple', 44), 'Pineapple, 44 times', 3000,
                   line_source)
     clues_lists = [
         ([
             Clue(('Milk', 3), 'Milk, 3 times', 50, line_source),
             Clue(('Chocolate', 2), 'Chocolate, 2 times', 100, line_source),
             Clue(('Banana', 1), 'Banana, 1 times', 150, line_source)
         ], 1), ([
             Clue(('Banana', 2), 'Banana, 2 times', 1050, line_source),
             Clue(('Milk', 1), 'Milk, 2 times', 1100, line_source)
         ], 1)
     ]  # yapf: disable
     constraints = [{
         'clues_groups': [[0, 1], [1, 1], [2, 1]],
         'name': 'identical',
         'params': {}
     }, {
         'clues_groups': [[0, 2], [1, 2], [2, 2]],
         'name': 'identical',
         'params': {}
     }]
     causes = Verifier.constraints_or(clues_lists, effect, constraints,
                                      ConstraintManager())
     assert not causes
Ejemplo n.º 6
0
    def test_constraints_and_verification_failed_when_or_succeeded(self):
        line_source = LineSource('localhost', 'node_0.log')
        effect = Clue(('Banana', 44), 'Banana, 44 times', 3000, line_source)
        clues_lists = [
            ([
                Clue(('Milk', 3), 'Milk, 3 times', 50, line_source),
                Clue(('Chocolate', 4), 'Chocolate, 4 times', 100, line_source),
                Clue(('Pear', 2), 'Pear, 2 times', 150, line_source)                # <- should be found (parser 1)
            ], 1), ([
                Clue(('Pineapple', 2), 'Pineapple, 2 times', 1050, line_source),    # <- should be found (parser 2)
                Clue(('Milk', 1), 'Milk, 1 times', 1100, line_source)
            ], 1)
        ]  # yapf: disable
        constraints = [{
            'clues_groups': [[0, 1], [1, 1], [2, 1]],
            'name': 'identical',
            'params': {}
        }, {
            'clues_groups': [[1, 2], [2, 2]],
            'name': 'identical',
            'params': {}
        }]

        # testing 'and'
        causes = Verifier.constraints_and(clues_lists, effect, constraints,
                                          ConstraintManager())
        assert not causes

        # testing 'or'
        causes = Verifier.constraints_or(clues_lists, effect, constraints,
                                         ConstraintManager())
        assert len(causes) == 1

        assert all(cause.constraints_linkage == InvestigationResult.OR
                   for cause in causes)
        assert causes[0].lines == [
            FrontInput.from_clue(Clue(
                ('Pear', 2), 'Pear, 2 times', 150, line_source)),
            FrontInput.from_clue(Clue(
                ('Pineapple', 2), 'Pineapple, 2 times', 1050, line_source))
        ]  # yapf: disable
        assert causes[0].constraints == [{
            'clues_groups': [[1, 2], [2, 2]],
            'name': 'identical',
            'params': {}
        }]
Ejemplo n.º 7
0
    def test_constraints_when_one_unmatched(self):
        line_source = LineSource('localhost', 'node_0.log')
        effect = Clue(('Banana', 2), 'Banana, 2 times', 3000, line_source)
        clues_lists = [
            ([
            ], 1), ([
                Clue(('Banana', 2), 'Banana, 2 times', 1050, line_source),
                Clue(('Milk', 1), 'Milk, 1 times', 1100, line_source)
            ], 1)
        ]  # yapf: disable
        constraints = [{
            'clues_groups': [[0, 1], [1, 1], [2, 1]],
            'name': 'identical',
            'params': {}
        }, {
            'clues_groups': [[0, 2], [2, 2]],
            'name': 'identical',
            'params': {}
        }]

        # testing 'or'
        causes = Verifier.constraints_or(clues_lists, effect, constraints,
                                         ConstraintManager())
        assert len(causes) == 1

        assert all(cause.constraints_linkage == InvestigationResult.OR
                   for cause in causes)
        assert causes[0].lines == [
            FrontInput.from_clue(Clue(
                ('Banana', 2), 'Banana, 2 times', 1050, line_source))
        ]  # yapf: disable
        assert causes[0].constraints == [{
            'clues_groups': [[0, 2], [2, 2]],
            'name': 'identical',
            'params': {}
        }]

        # testing 'and'
        causes = Verifier.constraints_and(clues_lists, effect, constraints,
                                          ConstraintManager())
        assert not causes
Ejemplo n.º 8
0
 def constraints_check(self, clues, effect_clues_dict):
     """
     check if given clues satisfy rule
     basing on its causes, effect and constraints.
     returns list of InvestigationResult objects
     """
     clues_lists = [
         (clues[parser_name], occurrences)
         for parser_name, occurrences in self._frequency_information
         if clues.get(parser_name) is not None
     ]
     effect_clue = effect_clues_dict[self._effect.name]
     constraint_manager = ConstraintManager()
     return self.LINKAGE_SELECTOR[self._linkage](clues_lists, effect_clue,
                                                 self._constraints,
                                                 constraint_manager)