Пример #1
0
    def test_rule_priority(self):

        matcher = lambda *args: True
        not_matcher = lambda *args: None

        rule_should_run = rule(True, priority=1)(
            mock.MagicMock(return_value=matcher))
        rule_should_not_run = rule(True, priority=0)(
            mock.MagicMock(return_value=not_matcher))

        pipeline = RuleBasedCore(self.person_date_relation, self._candidates,
                                 [rule_should_not_run, rule_should_run])
        pipeline.start()
        # All rules are compiled on start
        self.assertTrue(rule_should_run.called)
        self.assertTrue(rule_should_not_run.called)
        pipeline.process()
        import refo
        with mock.patch.object(refo, 'match') as fake_refo_match:
            fake_refo_match.side_effect = lambda regex, evidence: regex()
            pipeline.predict()
            self.assertEqual(fake_refo_match.call_count, len(self._candidates))
            # check that on every call, the called is rule_match
            for c_args in fake_refo_match.call_args_list:
                args, kwargs = c_args
                self.assertEqual(args[0], matcher)
Пример #2
0
    def test_rule_priority(self):

        matcher = lambda *args: True
        not_matcher = lambda *args: None

        rule_should_run = rule(True, priority=1)(mock.MagicMock(return_value=matcher))
        rule_should_not_run = rule(True, priority=0)(
            mock.MagicMock(return_value=not_matcher))

        pipeline = RuleBasedCore(self.person_date_relation,
                                 [rule_should_not_run, rule_should_run])
        pipeline.start()
        # All rules are compiled on start
        self.assertTrue(rule_should_run.called)
        self.assertTrue(rule_should_not_run.called)
        pipeline.process()
        import refo
        with mock.patch.object(refo, 'match') as fake_refo_match:
            fake_refo_match.side_effect = lambda regex, evidence: regex()
            pipeline.predict(self._candidates)
            self.assertEqual(fake_refo_match.call_count, len(self._candidates))
            # check that on every call, the called is rule_match
            for c_args in fake_refo_match.call_args_list:
                args, kwargs = c_args
                self.assertEqual(args[0], matcher)
Пример #3
0
    def test_match_run_on_every_rule(self):
        mocked_rules = [rule(True)(mock.MagicMock(return_value=Token("asd")))
                        ] * 10
        pipeline = RuleBasedCore(self.person_date_relation, mocked_rules)
        pipeline.start()
        pipeline.process()
        pipeline.predict(self._candidates)

        for mock_rule in mocked_rules:
            self.assertTrue(mock_rule.called)
            Subject, Object = mock_rule.call_args[0]
            self.assertIsInstance(Subject, Pattern)
Пример #4
0
    def test_match_run_on_every_rule(self):
        mocked_rules = [
            rule(True)(mock.MagicMock(return_value=Token("asd")))
        ] * 10
        pipeline = RuleBasedCore(self.person_date_relation, mocked_rules)
        pipeline.start()
        pipeline.process()
        pipeline.predict(self._candidates)

        for mock_rule in mocked_rules:
            self.assertTrue(mock_rule.called)
            Subject, Object = mock_rule.call_args[0]
            self.assertIsInstance(Subject, Pattern)