Esempio n. 1
0
    def test_to_dict(self):
        # Create primitive event
        p_timestamp = EpochNSClock.generate_timestamp()
        p_event = PrimitiveEvent(timestamp=p_timestamp, data=P_DATA)
        p_hist = "p_hist"

        # Create composite event
        c_timestamp = EpochNSClock.generate_timestamp()
        c_history = BoboHistory()
        c_event = CompositeEvent(timestamp=c_timestamp,
                                 name=C_NAME,
                                 history=c_history,
                                 data=C_DATA)
        c_hist = "c_hist"

        # Create list of events for history
        events = {p_hist: [p_event], c_hist: [c_event]}

        # Create history and add events
        history = BoboHistory(events=events)

        self.assertDictEqual(history.to_dict(), {
            p_hist: [p_event.to_dict()],
            c_hist: [c_event.to_dict()]
        })
Esempio n. 2
0
    def test_invalid_min_0(self):
        validator = StrDictValidator(min_length=0)

        self.assertFalse(validator.validate(None))
        self.assertFalse(validator.validate(""))
        self.assertFalse(validator.validate(123))
        self.assertFalse(validator.validate(123.456))
        self.assertFalse(validator.validate(True))
        self.assertFalse(validator.validate(False))
        self.assertFalse(validator.validate(BoboHistory()))
        self.assertFalse(
            validator.validate(
                PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())))

        # as a dict
        self.assertFalse(validator.validate({KEY_A: None}))
        self.assertFalse(validator.validate({KEY_A: 123}))
        self.assertFalse(validator.validate({KEY_A: 123.456}))
        self.assertFalse(validator.validate({KEY_A: True}))
        self.assertFalse(validator.validate({KEY_A: False}))
        self.assertFalse(validator.validate({KEY_A: BoboHistory()}))
        self.assertFalse(
            validator.validate({
                KEY_A:
                PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())
            }))
Esempio n. 3
0
    def test_action_from_producer_to_decider_not_recursive(self):
        setup = BoboSetup(recursive=False)

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_4))
        setup.config_receiver(StrDictValidator())
        setup.configure()

        decider = setup.get_decider()
        decider.setup()

        producer = setup.get_producer()
        producer.setup()

        a_event = NoAction().execute(
            CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                           name=NAME_NFA_A,
                           history=BoboHistory(),
                           data={}))

        producer.on_action_attempt(a_event)
        producer.loop()

        handler = decider.get_all_handlers()[0]
        self.assertEqual(0, len(handler.get_all_recent()))
Esempio n. 4
0
    def test_add_recent(self):
        nfa, buffer, handler, handlersub = handler_setup(
            nfa_name=NFA_NAME_A, pattern=pattern_relaxed, max_recent=2)

        p_event = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 data={})
        sleep(0.1)
        c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 name=EVENT_NAME_A,
                                 history=BoboHistory(),
                                 data={})
        sleep(0.1)
        a_event = ActionEvent(timestamp=EpochNSClock.generate_timestamp(),
                              name=EVENT_NAME_B,
                              success=True,
                              for_event=c_event)

        handler.add_recent(p_event)
        handler.add_recent(c_event)

        self.assertTrue(p_event in handler._recent)
        self.assertTrue(c_event in handler._recent)

        handler.add_recent(a_event)

        self.assertFalse(p_event in handler._recent)
        self.assertTrue(c_event in handler._recent)
        self.assertTrue(a_event in handler._recent)
Esempio n. 5
0
    def test_subscribe_forward_action_failure(self):
        setup = BoboSetup()
        sub = StubSubscriberSetup()

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction()))

        # this rejects all events passed to Forwarder
        setup.config_forwarder(NoAction(bool_return=False))

        setup.subscribe_forwarder(sub)
        setup.configure()

        forwarder = setup.get_forwarder()
        forwarder.setup()

        a_event = NoAction().execute(
            CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                           name=NAME_NFA_A,
                           history=BoboHistory(),
                           data={}))

        forwarder.on_producer_action(a_event)
        forwarder.loop()

        self.assertEqual(a_event, sub.forwarder_failure_event[0])
Esempio n. 6
0
    def test_subscribe_producer_composite_rejected(self):
        setup = BoboSetup()
        sub = StubSubscriberSetup()

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction()))

        # this rejects all events passed to Producer
        setup.config_producer(NoAction(bool_return=False))

        setup.subscribe_producer(NAME_NFA_A, sub)
        setup.configure()

        producer = setup.get_producer()
        producer.setup()

        c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 name=NAME_NFA_A,
                                 history=BoboHistory(),
                                 data={})

        producer.on_decider_complex_event(c_event)
        producer.loop()

        self.assertEqual(c_event, sub.rejected_producer_event[0])
Esempio n. 7
0
    def test_valid_function(self):
        f = BoboPredicateCallable(valid_function)
        history = BoboHistory()
        recent = []

        self.assertTrue(
            f.evaluate(event=PrimitiveEvent(EpochNSClock.generate_timestamp(),
                                            KEY_VALUE),
                       history=history,
                       recent=recent))
Esempio n. 8
0
    def test_to_dict(self):
        c_timestamp = EpochNSClock.generate_timestamp()
        c_name = "c_name"
        c_history = BoboHistory()
        c_data = {"c_key": "c_value"}

        c_event = CompositeEvent(timestamp=c_timestamp,
                                 name=c_name,
                                 history=c_history,
                                 data=c_data)

        self.assertDictEqual(
            c_event.to_dict(), {
                CompositeEvent.TIMESTAMP: c_timestamp,
                CompositeEvent.NAME: c_name,
                CompositeEvent.HISTORY: c_history.to_dict(),
                CompositeEvent.DATA: c_data,
                CompositeEvent.EVENT_ID: c_event.event_id
            })
Esempio n. 9
0
    def test_valid_method(self):
        obj = TestBoboPredicateCallableMethods()
        f = BoboPredicateCallable(obj.valid_method)
        history = BoboHistory()
        recent = []

        self.assertTrue(
            f.evaluate(event=PrimitiveEvent(EpochNSClock.generate_timestamp(),
                                            KEY_VALUE),
                       history=history,
                       recent=recent))
Esempio n. 10
0
    def test_true(self):
        timestamp = EpochNSClock.generate_timestamp()
        name = "c_name"
        history = BoboHistory()
        data = {"c_key": "c_value"}

        c_event = CompositeEvent(timestamp=timestamp,
                                 name=name,
                                 history=history,
                                 data=data)

        self.assertTrue(NoAction().execute(c_event).success)
Esempio n. 11
0
    def test_action(self):
        # action event data
        a_timestamp = EpochNSClock.generate_timestamp()
        a_name = NAME_A
        a_success = True
        a_for_event = CompositeEvent(
            timestamp=EpochNSClock.generate_timestamp(),
            name=NAME_B,
            history=BoboHistory(),
            data={}
        )
        a_exception = EXCEPTION_A
        a_description = DESCRIPTION_A
        a_data = DATA_A
        a_event_id = EVENT_ID_A

        # create dict representation of action event
        a_dict = {
            ActionEvent.TIMESTAMP: a_timestamp,
            ActionEvent.NAME: a_name,
            ActionEvent.SUCCESS: a_success,
            ActionEvent.FOR_EVENT: a_for_event.to_dict(),
            ActionEvent.EXCEPTION: a_exception,
            ActionEvent.DESCRIPTION: a_description,
            ActionEvent.DATA: a_data,
            ActionEvent.EVENT_ID: a_event_id
        }

        # build actual action event from dict
        a_event_1 = BoboRuleBuilder.action(a_dict)
        a_event_2 = BoboRuleBuilder.event(a_dict)

        for a_event in [a_event_1, a_event_2]:
            self.assertEqual(a_event.timestamp, a_timestamp)
            self.assertEqual(a_event.name, a_name)
            self.assertEqual(a_event.success, a_success)
            self.assertEqual(a_event.exception, a_exception)
            self.assertEqual(a_event.description, a_description)
            self.assertDictEqual(a_event.data, a_data)
            self.assertEqual(a_event.event_id, a_event_id)

            c_event = a_event.for_event
            self.assertIsInstance(c_event, CompositeEvent)
            self.assertEqual(c_event.timestamp, a_for_event.timestamp)
            self.assertEqual(c_event.name, a_for_event.name)
            self.assertIsNone(c_event.history.first)
            self.assertDictEqual(c_event.data, a_for_event.data)
            self.assertEqual(c_event.event_id, a_for_event.event_id)
Esempio n. 12
0
    def test_subscribe_decider_composite(self):
        setup = BoboSetup()
        sub = StubSubscriberSetup()

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction()))

        setup.subscribe_decider(NAME_NFA_A, sub)
        setup.configure()

        decider = setup.get_decider()
        decider.setup()

        c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 name=NAME_NFA_A,
                                 history=BoboHistory(),
                                 data={})

        decider.on_handler_final(NAME_NFA_A, RUN_ID_A, c_event)

        self.assertEqual(c_event, sub.decider_complex_event[0])
Esempio n. 13
0
    def process(self, event: BoboEvent) -> None:
        with self._lock:
            recent = self._recent[:]
            self._check_event_against_runs(event, recent)

            if self.nfa.start_state.process(event, {}, recent):
                # only notify clone if start state isn't the final state
                clone_run = self.on_run_clone(
                    state_name=self.nfa.start_state.name,
                    event=event,
                    parent_run_id=None,
                    force_parent=False,
                    notify=not self.nfa.start_is_final)

                if clone_run.is_final():
                    self.on_run_final(run_id=clone_run.id,
                                      history=BoboHistory({
                                          clone_run.current_state.label:
                                          [clone_run.event]
                                      }),
                                      notify=True)
Esempio n. 14
0
    def test_force_run_final(self):
        nfa, buffer, handler, handlersub = handler_setup(
            nfa_name=NFA_NAME_A, pattern=pattern_relaxed)

        run_state_a = state_from_layer(nfa=nfa, label=LABEL_LAYER_A)

        handler.process(event_a)

        # only one run has been created
        runs = list(handler.runs.values())
        self.assertEqual(1, len(runs))

        run = runs[0]
        self.assertEqual(event_a, run.event)
        self.assertEqual(run_state_a.name, run.current_state.name)

        handler.process(event_b)

        history = BoboHistory()
        handler.force_run_final(run_id=run.id, history=history)

        self.assertTrue(run.is_halted())
Esempio n. 15
0
    def history(d: dict) -> BoboHistory:
        """Creates a BoboHistory instance from serialized representation of
           one.

        :param d: A dict representation of a BoboHistory instance.
        :type d: dict

        :return: A BoboHistory instance.
        """

        event_history_dict = {}

        for key in d.keys():
            key_events = d[key]

            event_history_dict[key] = []

            for event_dict in key_events:
                event_history_dict[key].append(
                    BoboRuleBuilder.event(event_dict))

        return BoboHistory(events=event_history_dict)
Esempio n. 16
0
    def test_subscribe_forward_composite_success(self):
        setup = BoboSetup()
        sub = StubSubscriberSetup()

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction()))

        setup.subscribe_forwarder(sub)
        setup.configure()

        forwarder = setup.get_forwarder()
        forwarder.setup()

        c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 name=NAME_NFA_A,
                                 history=BoboHistory(),
                                 data={})

        forwarder.on_accepted_producer_event(c_event)
        forwarder.loop()

        self.assertEqual(c_event, sub.forwarder_success_event[0])
Esempio n. 17
0
    def test_composite_from_producer_to_forwarder(self):
        setup = BoboSetup(recursive=True)

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_4))
        setup.config_receiver(StrDictValidator())
        setup.configure()

        producer = setup.get_producer()
        producer.setup()

        forwarder = setup.get_forwarder()
        forwarder.setup()

        c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                                 name=NAME_NFA_A,
                                 history=BoboHistory(),
                                 data={})

        producer.on_decider_complex_event(c_event)
        producer.loop()

        self.assertEqual(c_event, forwarder._event_queue.get_nowait())
Esempio n. 18
0
    def test_subscribe_producer_action(self):
        setup = BoboSetup()
        sub = StubSubscriberSetup()

        setup.add_complex_event(event_def=BoboComplexEvent(
            name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction()))

        setup.subscribe_producer(NAME_NFA_A, sub)
        setup.configure()

        producer = setup.get_producer()
        producer.setup()

        a_event = NoAction().execute(
            CompositeEvent(timestamp=EpochNSClock.generate_timestamp(),
                           name=NAME_NFA_A,
                           history=BoboHistory(),
                           data={}))

        producer.on_action_attempt(a_event)
        producer.loop()

        self.assertEqual(a_event, sub.producer_action[0])
Esempio n. 19
0
def generate_composite_event(name: str) -> CompositeEvent:
    return CompositeEvent(
        timestamp=EpochNSClock.generate_timestamp(),
        name=name,
        history=BoboHistory(),
        data={})
Esempio n. 20
0
    def test_invalid(self):
        val = StrValidator(min_length=0)

        self.assertFalse(val.validate(None))
        self.assertFalse(val.validate(123))
        self.assertFalse(val.validate(BoboHistory()))
Esempio n. 21
0
    def get_all_events(self, nfa_name: str, run_id: str,
                       version: RunVersion) -> BoboHistory:
        """
        Gets all events associated with a run and compiles them into a
        BoboHistory instance.

        :param nfa_name: The BoboNFA instance name.
        :type nfa_name: str

        :param run_id: The run ID.
        :type run_id: str

        :param version: The run version.
        :type version: RunVersion

        :return: A BoboHistory instance with all of the events in it.
        """

        all_events = {}
        current_level = 0
        current_incr = 0
        current_version = version.get_version_as_str()

        # start with the latest match event
        current_event = self.get_last_event(nfa_name=nfa_name,
                                            run_id=run_id,
                                            version=current_version)

        while True:
            if current_event is not None:
                # add event to dict, keyed under the label name
                if current_event.label not in all_events:
                    all_events[current_event.label] = []
                all_events[current_event.label].insert(0, current_event.event)

                # get next match event using current version
                next_event = self._get_next_event(event=current_event,
                                                  nfa_name=nfa_name,
                                                  version_str=current_version)

                # no event found under current version
                if next_event is None:
                    # get previous version by decreasing increment
                    current_incr += 1
                    current_version = \
                        version.get_previous_version_as_str(
                            decrease_level=current_level,
                            decrease_incr=current_incr)

                    # get previous version by decreasing level
                    if current_version is None:
                        current_level += 1
                        current_incr = 0
                        current_version = \
                            version.get_previous_version_as_str(
                                decrease_level=current_level,
                                decrease_incr=current_incr)

                        # no previous version, stop search
                        if current_version is None:
                            break

                    # attempt to find next event with new version
                    next_event = self._get_next_event(
                        event=current_event,
                        nfa_name=nfa_name,
                        version_str=current_version)

                    if next_event is None:
                        break

                current_event = next_event
            else:
                break

        return BoboHistory(events=all_events)
Esempio n. 22
0
    def test_action_none(self):
        # action event data
        a_timestamp = EpochNSClock.generate_timestamp()
        a_name = NAME_A
        a_success = True
        a_for_event = CompositeEvent(
            timestamp=EpochNSClock.generate_timestamp(),
            name=NAME_B,
            history=BoboHistory(),
            data={}
        )
        a_exception = EXCEPTION_A
        a_description = DESCRIPTION_A
        a_data = DATA_A
        a_event_id = EVENT_ID_A

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: None,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: None,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: None,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: None,
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: a_event_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.action({
                ActionEvent.TIMESTAMP: a_timestamp,
                ActionEvent.NAME: a_name,
                ActionEvent.SUCCESS: a_success,
                ActionEvent.FOR_EVENT: a_for_event.to_dict(),
                ActionEvent.EXCEPTION: a_exception,
                ActionEvent.DESCRIPTION: a_description,
                ActionEvent.DATA: a_data,
                ActionEvent.EVENT_ID: None
            })