def _config_definitions(self) -> None: for event_def in self._event_defs: handler = BoboNFAHandler(nfa=BoboRuleBuilder.nfa( name_nfa=event_def.name, pattern=event_def.pattern), buffer=SharedVersionedMatchBuffer(), max_recent=self._max_recent) self._decider.add_nfa_handler(handler) # Decider -> Producer self._decider.subscribe(event_def.name, self._producer) if self._recursive: # Producer -> Decider self._producer.subscribe(event_def.name, self._decider) # Producer -> Forwarder self._producer.subscribe(event_def.name, self._forwarder) if event_def.action is not None: # Producer -> Action self._producer.subscribe(event_def.name, event_def.action) # Action -> Producer event_def.action.subscribe(self._producer) if self._distributed: # Handler -> Outgoing handler.subscribe(self._manager.outgoing) # Producer -> Outgoing self._producer.subscribe(event_def.name, self._manager.outgoing)
def handler_setup(nfa_name, pattern, max_recent: int = 1): buffer = SharedVersionedMatchBuffer() nfa = BoboRuleBuilder.nfa(name_nfa=nfa_name, pattern=pattern) handler = BoboNFAHandler(nfa=nfa, buffer=buffer, max_recent=max_recent) handlersub = NFAHandlerSubscriber() handler.subscribe(handlersub) return nfa, buffer, handler, handlersub
def test_sliding_first_window_to_halt(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_first_window = WindowSlidingFirst( interval_sec=range_sec) event_a = PrimitiveEvent(timestamp_low, {KEY: VAL_1}) event_b = PrimitiveEvent(timestamp_mid, {KEY: VAL_2}) event_c = PrimitiveEvent(timestamp_upp, {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_first_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.final), 1)
def test_sliding_last_window_to_halt(self): timestamp_lower = EpochNSClock.generate_timestamp() sleep(0.5) timestamp_upper = EpochNSClock.generate_timestamp() window_range_ns = timestamp_upper - timestamp_lower timestamp_a = timestamp_lower timestamp_b = timestamp_a + window_range_ns timestamp_c = timestamp_b + window_range_ns + 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_first_window = WindowSlidingLast(window_range_ns) event_a = PrimitiveEvent(timestamp_a, {KEY: VAL_1}) event_b = PrimitiveEvent(timestamp_b, {KEY: VAL_2}) event_c = PrimitiveEvent(timestamp_c, {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_first_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.final), 1)
def add_nfa_handler(self, nfa_handler: BoboNFAHandler) -> None: """ Adds a new NFA handler to the decider. :param nfa_handler: The NFA handler to add. :type nfa_handler: BoboNFAHandler :raises RuntimeError: NFA handler name already in use. """ with self._lock: if not self._is_cancelled: if nfa_handler.nfa.name not in self._nfa_handlers: self._nfa_handlers[nfa_handler.nfa.name] = nfa_handler nfa_handler.subscribe(self) else: raise RuntimeError("NFA handler name {} already in use." .format(nfa_handler.nfa.name))
def test_fixed_window_to_final(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_c = EpochNSClock.generate_timestamp() sleep(0.1) timestamp_upper = 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_c, {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.final_history), 1) self.assertDictEqual(handlersub.final_history[0].events, {LABEL_LAYER_A: [event_a], LABEL_LAYER_B: [event_b], LABEL_LAYER_C: [event_c]})