Esempio n. 1
0
    def test_fixed_window_to_halt(self):
        timestamp_lower = EpochNSClock.generate_timestamp()
        sleep(0.1)
        timestamp_a = EpochNSClock.generate_timestamp()
        sleep(0.1)
        timestamp_b = EpochNSClock.generate_timestamp()
        sleep(0.1)
        timestamp_upper = EpochNSClock.generate_timestamp()
        sleep(0.1)
        timestamp_bad = EpochNSClock.generate_timestamp()
        sleep(0.1)

        predicate_a = BoboPredicateCallable(predicate_key_a_value_a)
        predicate_b = BoboPredicateCallable(predicate_key_a_value_b)
        predicate_c = BoboPredicateCallable(predicate_key_a_value_c)
        predicate_fixed_window = WindowFixed(timestamp_lower,
                                             timestamp_upper)

        event_a = PrimitiveEvent(timestamp_a, {KEY: VAL_1})
        event_b = PrimitiveEvent(timestamp_b, {KEY: VAL_2})
        event_c = PrimitiveEvent(timestamp_bad, {KEY: VAL_3})

        pattern_a = BoboPattern() \
            .followed_by(LABEL_LAYER_A, predicate_a) \
            .followed_by(LABEL_LAYER_B, predicate_b) \
            .followed_by(LABEL_LAYER_C, predicate_c) \
            .precondition(predicate_fixed_window)

        handler = BoboNFAHandler(
            BoboRuleBuilder.nfa(NFA_NAME_A, pattern_a),
            SharedVersionedMatchBuffer())
        handlersub = NFAHandlerSubscriber()
        handler.subscribe(handlersub)

        handler.process(event_a)
        handler.process(event_b)
        handler.process(event_c)

        self.assertEqual(len(handlersub.halt), 1)
Esempio n. 2
0
    def test_generate_nfa_loop(self):
        predicate_a = BoboPredicateCallable(predicate_key_a_value_a)
        predicate_b = BoboPredicateCallable(predicate_key_a_value_b)
        predicate_c = BoboPredicateCallable(predicate_key_a_value_c)

        pattern = BoboPattern() \
            .followed_by(LABEL_LAYER_A, predicate_a) \
            .followed_by(LABEL_LAYER_B, predicate_b, loop=True) \
            .followed_by(LABEL_LAYER_C, predicate_c)

        nfa = BoboRuleBuilder.nfa(NFA_NAME_A, pattern)

        state_name_a = "{}-{}-{}".format(LABEL_LAYER_A, 1, 1)
        state_name_b = "{}-{}-{}".format(LABEL_LAYER_B, 1, 1)
        state_name_c = "{}-{}-{}".format(LABEL_LAYER_C, 1, 1)

        # variables
        self.assertEqual(3, len(nfa.states))
        self.assertEqual(3, len(nfa.transitions))
        self.assertEqual(state_name_a, nfa.start_state.name)
        self.assertEqual(state_name_c, nfa.final_state.name)

        # states
        self.assertEqual(state_name_a, nfa.states[state_name_a].name)
        self.assertEqual(state_name_b, nfa.states[state_name_b].name)
        self.assertEqual(state_name_c, nfa.states[state_name_c].name)

        self.assertEqual(LABEL_LAYER_A, nfa.states[state_name_a].label)
        self.assertEqual(LABEL_LAYER_B, nfa.states[state_name_b].label)
        self.assertEqual(LABEL_LAYER_C, nfa.states[state_name_c].label)

        # transitions
        self.assertListEqual([state_name_b],
                             nfa.transitions[state_name_a].state_names)
        self.assertEqual({state_name_b, state_name_c},
                         set(nfa.transitions[state_name_b].state_names))
        self.assertListEqual([], nfa.transitions[state_name_c].state_names)
Esempio n. 3
0
    def test_generate_nfa_nondeterministic_all_relaxed(self):
        predicate_a = BoboPredicateCallable(predicate_key_a_value_a)
        predicate_b1 = BoboPredicateCallable(predicate_key_a_value_b)
        predicate_b2 = BoboPredicateCallable(predicate_key_a_value_c)
        predicate_c = BoboPredicateCallable(predicate_key_a_value_d)

        pattern = BoboPattern() \
            .followed_by(LABEL_LAYER_A, predicate_a) \
            .followed_by_any(LABEL_LAYER_B, [predicate_b1, predicate_b2]) \
            .followed_by(LABEL_LAYER_C, predicate_c)

        nfa = BoboRuleBuilder.nfa(NFA_NAME_A, pattern)

        state_name_a = "{}-{}-{}".format(LABEL_LAYER_A, 1, 1)
        state_name_b1 = "{}-{}-{}".format(LABEL_LAYER_B, 1, 1)
        state_name_b2 = "{}-{}-{}".format(LABEL_LAYER_B, 2, 1)
        state_name_c = "{}-{}-{}".format(LABEL_LAYER_C, 1, 1)

        # variables
        self.assertEqual(nfa.name, NFA_NAME_A)
        self.assertEqual(4, len(nfa.states))
        self.assertEqual(4, len(nfa.transitions))
        self.assertEqual(state_name_a, nfa.start_state.name)
        self.assertEqual(state_name_c, nfa.final_state.name)
        self.assertEqual(0, len(nfa.preconditions))

        # states
        self.assertTrue(state_name_a in nfa.states)
        self.assertTrue(state_name_b1 in nfa.states)
        self.assertTrue(state_name_b2 in nfa.states)
        self.assertTrue(state_name_c in nfa.states)

        self.assertEqual(state_name_a, nfa.states[state_name_a].name)
        self.assertEqual(state_name_b1, nfa.states[state_name_b1].name)
        self.assertEqual(state_name_b2, nfa.states[state_name_b2].name)
        self.assertEqual(state_name_c, nfa.states[state_name_c].name)

        self.assertEqual(LABEL_LAYER_A, nfa.states[state_name_a].label)
        self.assertEqual(LABEL_LAYER_B, nfa.states[state_name_b1].label)
        self.assertEqual(LABEL_LAYER_B, nfa.states[state_name_b2].label)
        self.assertEqual(LABEL_LAYER_C, nfa.states[state_name_c].label)

        self.assertFalse(nfa.states[state_name_a].is_negated)
        self.assertFalse(nfa.states[state_name_b1].is_negated)
        self.assertFalse(nfa.states[state_name_b2].is_negated)
        self.assertFalse(nfa.states[state_name_c].is_negated)

        self.assertFalse(nfa.states[state_name_a].is_optional)
        self.assertFalse(nfa.states[state_name_b1].is_optional)
        self.assertFalse(nfa.states[state_name_b2].is_optional)
        self.assertFalse(nfa.states[state_name_c].is_optional)

        # transitions
        self.assertTrue(state_name_a in nfa.transitions)
        self.assertTrue(state_name_b1 in nfa.transitions)
        self.assertTrue(state_name_b2 in nfa.transitions)
        self.assertTrue(state_name_c in nfa.transitions)

        self.assertListEqual([state_name_b1, state_name_b2],
                             nfa.transitions[state_name_a].state_names)
        self.assertListEqual([state_name_c],
                             nfa.transitions[state_name_b1].state_names)
        self.assertListEqual([state_name_c],
                             nfa.transitions[state_name_b2].state_names)
        self.assertListEqual([], nfa.transitions[state_name_c].state_names)

        self.assertFalse(nfa.transitions[state_name_a].is_strict)
        self.assertFalse(nfa.transitions[state_name_b1].is_strict)
        self.assertFalse(nfa.transitions[state_name_b2].is_strict)
        self.assertFalse(nfa.transitions[state_name_c].is_strict)
Esempio n. 4
0
 def test_invalid_empty_pattern(self):
     with self.assertRaises(RuntimeError):
         BoboRuleBuilder.nfa(NFA_NAME_A, BoboPattern())
Esempio n. 5
0
    def test_generate_nfa_deterministic_append_patterns(self):
        predicate_a = BoboPredicateCallable(predicate_key_a_value_a)
        predicate_b = BoboPredicateCallable(predicate_key_a_value_b)
        predicate_c = BoboPredicateCallable(predicate_key_a_value_c)
        predicate_d = BoboPredicateCallable(predicate_key_a_value_d)

        pattern_1 = BoboPattern() \
            .followed_by(LABEL_LAYER_A, predicate_a) \
            .next(LABEL_LAYER_B, predicate_b)

        pattern_2 = BoboPattern() \
            .next(LABEL_LAYER_C, predicate_c)

        pattern_3 = BoboPattern() \
            .followed_by(LABEL_LAYER_D, predicate_d)

        pattern_1.append([pattern_2, pattern_3])

        nfa = BoboRuleBuilder.nfa(NFA_NAME_A, pattern_1)

        state_name_a = "{}-{}-{}".format(LABEL_LAYER_A, 1, 1)
        state_name_b = "{}-{}-{}".format(LABEL_LAYER_B, 1, 1)
        state_name_c = "{}-{}-{}".format(LABEL_LAYER_C, 1, 1)
        state_name_d = "{}-{}-{}".format(LABEL_LAYER_D, 1, 1)

        # variables
        self.assertEqual(nfa.name, NFA_NAME_A)
        self.assertEqual(4, len(nfa.states))
        self.assertEqual(4, len(nfa.transitions))
        self.assertEqual(state_name_a, nfa.start_state.name)
        self.assertEqual(state_name_d, nfa.final_state.name)
        self.assertEqual(0, len(nfa.preconditions))

        # states
        self.assertTrue(state_name_a in nfa.states)
        self.assertTrue(state_name_b in nfa.states)
        self.assertTrue(state_name_c in nfa.states)
        self.assertTrue(state_name_d in nfa.states)

        self.assertEqual(state_name_a, nfa.states[state_name_a].name)
        self.assertEqual(state_name_b, nfa.states[state_name_b].name)
        self.assertEqual(state_name_c, nfa.states[state_name_c].name)
        self.assertEqual(state_name_d, nfa.states[state_name_d].name)

        self.assertEqual(LABEL_LAYER_A, nfa.states[state_name_a].label)
        self.assertEqual(LABEL_LAYER_B, nfa.states[state_name_b].label)
        self.assertEqual(LABEL_LAYER_C, nfa.states[state_name_c].label)
        self.assertEqual(LABEL_LAYER_D, nfa.states[state_name_d].label)

        self.assertFalse(nfa.states[state_name_a].is_negated)
        self.assertFalse(nfa.states[state_name_b].is_negated)
        self.assertFalse(nfa.states[state_name_c].is_negated)
        self.assertFalse(nfa.states[state_name_d].is_negated)

        self.assertFalse(nfa.states[state_name_a].is_optional)
        self.assertFalse(nfa.states[state_name_b].is_optional)
        self.assertFalse(nfa.states[state_name_c].is_optional)
        self.assertFalse(nfa.states[state_name_d].is_optional)

        # transitions
        self.assertTrue(state_name_a in nfa.transitions)
        self.assertTrue(state_name_b in nfa.transitions)
        self.assertTrue(state_name_c in nfa.transitions)
        self.assertTrue(state_name_d in nfa.transitions)

        self.assertListEqual([state_name_b],
                             nfa.transitions[state_name_a].state_names)
        self.assertListEqual([state_name_c],
                             nfa.transitions[state_name_b].state_names)
        self.assertListEqual([state_name_d],
                             nfa.transitions[state_name_c].state_names)
        self.assertListEqual([], nfa.transitions[state_name_d].state_names)

        self.assertTrue(nfa.transitions[state_name_a].is_strict)
        self.assertTrue(nfa.transitions[state_name_b].is_strict)
        self.assertFalse(nfa.transitions[state_name_c].is_strict)
        self.assertFalse(nfa.transitions[state_name_d].is_strict)