Exemple #1
0
    def test_to_dict(self):
        p_timestamp = EpochNSClock.generate_timestamp()
        p_data = {"p_key": "p_value"}

        p_event = PrimitiveEvent(timestamp=p_timestamp, data=p_data)

        self.assertDictEqual(
            p_event.to_dict(), {
                PrimitiveEvent.TIMESTAMP: p_timestamp,
                PrimitiveEvent.DATA: p_data,
                PrimitiveEvent.EVENT_ID: p_event.event_id
            })
Exemple #2
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)
Exemple #3
0
    def test_history(self):
        p1_hist = "p1_hist"
        p2_hist = "p2_hist"

        # Primitive events used in history
        p1_timestamp = EpochNSClock.generate_timestamp()
        p1_data = "p1_data"

        p2_timestamp = EpochNSClock.generate_timestamp()
        p2_data = "p2_data"

        p1_id = "p1_id_123"
        p2_id = "p2_id_123"

        h_dict = {
            p1_hist: [{
                PrimitiveEvent.TIMESTAMP: p1_timestamp,
                PrimitiveEvent.DATA: p1_data,
                PrimitiveEvent.EVENT_ID: p1_id
            }],
            p2_hist: [{
                PrimitiveEvent.TIMESTAMP: p2_timestamp,
                PrimitiveEvent.DATA: p2_data,
                PrimitiveEvent.EVENT_ID: p2_id
            }]
        }

        history = BoboRuleBuilder.history(h_dict)

        p1_event = history.events[p1_hist][0]

        self.assertEqual(p1_event.timestamp, p1_timestamp)
        self.assertEqual(p1_event.data, p1_data)
        self.assertEqual(p1_event.event_id, p1_id)

        p2_event = history.events[p2_hist][0]

        self.assertEqual(p2_event.timestamp, p2_timestamp)
        self.assertEqual(p2_event.data, p2_data)
        self.assertEqual(p2_event.event_id, p2_id)
Exemple #4
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)
Exemple #5
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
            })
    def test_1_level_10_increments_5_events_per_increment(self):
        buffer = SharedVersionedMatchBuffer()
        run_id = generate_unique_string()

        events_increments = [
            [
                PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())
                for _ in range(5)
            ]  # events
            for _ in range(10)
        ]  # increments

        version = RunVersion()
        version.add_level(run_id)
        version_current = version.get_version_as_str()

        for events in events_increments:
            version.increment_level(generate_unique_string())
            version_next = version.get_version_as_str()

            buffer.put_event(nfa_name=NFA_NAME_A,
                             run_id=run_id,
                             version=version_current,
                             state_label=LABEL_LAYER_A,
                             event=events[0],
                             new_version=version_next)

            version_current = version_next

            for i in range(1, len(events)):
                buffer.put_event(nfa_name=NFA_NAME_A,
                                 run_id=run_id,
                                 version=version_current,
                                 state_label=LABEL_LAYER_A,
                                 event=events[i])

        history_events = buffer.get_all_events(
            nfa_name=NFA_NAME_A, run_id=run_id,
            version=version).events[LABEL_LAYER_A]

        for events in events_increments:
            for event in events:
                self.assertTrue(event in history_events)
Exemple #7
0
    def test_composite_none(self):
        c_timestamp = EpochNSClock.generate_timestamp()
        c_name = NAME_A
        c_history = {}
        c_data = DATA_A
        c_id = EVENT_ID_A

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.composite({
                CompositeEvent.TIMESTAMP: None,
                CompositeEvent.NAME: c_name,
                CompositeEvent.HISTORY: c_history,
                CompositeEvent.DATA: c_data,
                CompositeEvent.EVENT_ID: c_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.composite({
                CompositeEvent.TIMESTAMP: c_timestamp,
                CompositeEvent.NAME: None,
                CompositeEvent.HISTORY: c_history,
                CompositeEvent.DATA: c_data,
                CompositeEvent.EVENT_ID: c_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.composite({
                CompositeEvent.TIMESTAMP: c_timestamp,
                CompositeEvent.NAME: c_name,
                CompositeEvent.HISTORY: None,
                CompositeEvent.DATA: c_data,
                CompositeEvent.EVENT_ID: c_id
            })

        with self.assertRaises(RuntimeError):
            BoboRuleBuilder.composite({
                CompositeEvent.TIMESTAMP: c_timestamp,
                CompositeEvent.NAME: c_name,
                CompositeEvent.HISTORY: c_history,
                CompositeEvent.DATA: c_data,
                CompositeEvent.EVENT_ID: None
            })
Exemple #8
0
    def test_primitive(self):
        # primitive event data
        p_timestamp = EpochNSClock.generate_timestamp()
        p_data = DATA_A
        p_id = EVENT_ID_A

        # Create dict representation of primitive event
        p_dict = {
            PrimitiveEvent.TIMESTAMP: p_timestamp,
            PrimitiveEvent.DATA: p_data,
            PrimitiveEvent.EVENT_ID: p_id
        }

        # Build actual primitive event from dict
        p_event_1 = BoboRuleBuilder.primitive(p_dict)
        p_event_2 = BoboRuleBuilder.event(p_dict)

        for p_event in [p_event_1, p_event_2]:
            self.assertEqual(p_event.timestamp, p_timestamp)
            self.assertEqual(p_event.data, p_data)
            self.assertEqual(p_event.event_id, p_id)
Exemple #9
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])
Exemple #10
0
    def test_match_event_points_to_itself(self):
        event_a = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())

        match_a = MatchEvent(nfa_name=NFA_NAME_A,
                             label=LABEL_LAYER_A,
                             event=event_a)

        version = RunVersion()
        version.add_level(
            BoboRun._generate_id(nfa_name=NFA_NAME_A,
                                 start_event_id=event_a.event_id))
        version_str = version.get_version_as_str()

        with self.assertRaises(RuntimeError):
            match_a.add_pointer_next(version=version_str,
                                     label=LABEL_LAYER_A,
                                     event_id=event_a.event_id)

        with self.assertRaises(RuntimeError):
            match_a.add_pointer_previous(version=version_str,
                                         label=LABEL_LAYER_A,
                                         event_id=event_a.event_id)
Exemple #11
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])
Exemple #12
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())
Exemple #13
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])
Exemple #14
0
    def on_run_final(self,
                     run_id: str,
                     history: BoboHistory,
                     notify: bool = True) -> None:
        """
        :raises RuntimeError: Run ID not found.
        """

        with self._lock:
            run = self.runs.get(run_id)

            if run is not None:
                run.set_final(history=history, notify=False)

                event = CompositeEvent(
                    timestamp=EpochNSClock.generate_timestamp(),
                    name=self.nfa.name,
                    history=history)

                self.remove_run(run_id)

                if notify:
                    self._notify_final(run_id, event)
Exemple #15
0
def generate_composite_event(name: str) -> CompositeEvent:
    return CompositeEvent(
        timestamp=EpochNSClock.generate_timestamp(),
        name=name,
        history=BoboHistory(),
        data={})
Exemple #16
0
stub_pattern_4 = BoboPattern() \
    .followed_by(
    label=LABEL_A,
    predicate=stub_predicate_true
).followed_by(
    label=LABEL_B,
    predicate=stub_predicate_true
).followed_by(
    label=LABEL_C,
    predicate=stub_predicate_true
).followed_by(
    label=LABEL_D,
    predicate=stub_predicate_true
)

event_a = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())
event_b = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())
event_c = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())
event_d = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())

credentials = PlainCredentials('guest', 'guest')
parameters = ConnectionParameters("127.0.0.1", 5672, '/', credentials)


class StubSubscriberSetup(IReceiverSubscriber, IDeciderSubscriber,
                          IProducerSubscriber, IForwarderSubscriber):
    def __init__(self) -> None:
        super().__init__()

        self.receiver_event = []
        self.invalid_data = []
def generate_unique_string():
    return "{}-{}".format(uuid4(), EpochNSClock.generate_timestamp())
Exemple #18
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
            })
from bobocep.rules.predicates.windows.sliding.window_sliding_first import \
    WindowSlidingFirst

STATE_A = "state_a"
STATE_B = "state_b"
STATE_C = "state_c"
STATE_D = "state_d"

LABEL_LAYER_A = "LABEL_LAYER_A"
LABEL_LAYER_B = "LABEL_LAYER_B"
LABEL_LAYER_C = "LABEL_LAYER_C"
LABEL_LAYER_D = "LABEL_LAYER_D"

NFA_NAME_A = "NFA_NAME_A"

lower_sec = EpochNSClock.generate_timestamp()
sleep(0.5)
upper_sec = EpochNSClock.generate_timestamp()
range_sec = upper_sec - lower_sec

timestamp_low = lower_sec
timestamp_mid = timestamp_low + int(range_sec * 0.5)
timestamp_upp = timestamp_low + range_sec

KEY = "key"
VAL_1 = "1"
VAL_2 = "2"
VAL_3 = "3"
VAL_4 = "4"