Ejemplo n.º 1
0
    def test_bobo_run(self):
        buffer_orig = SharedVersionedMatchBuffer()
        nfa_orig = BoboRuleBuilder.nfa(name_nfa=NFA_NAME_A,
                                       pattern=stub_pattern)

        original = BoboRun(buffer=buffer_orig, nfa=nfa_orig, event=event_a)

        generated = BoboDeciderBuilder \
            .run(d=original.to_dict(),
                 buffer=original.buffer,
                 nfa=original.nfa)

        self.assertEqual(original.buffer, generated.buffer)
        self.assertEqual(original.nfa, generated.nfa)
        self.assertDictEqual(original.event.to_dict(),
                             generated.event.to_dict())
        self.assertEqual(original.start_time, generated.start_time)
        self.assertEqual(original.start_state, generated.start_state)
        self.assertEqual(original.current_state, generated.current_state)
        self.assertEqual(original.id, generated.id)
        self.assertEqual(original.version.get_version_as_str(),
                         generated.version.get_version_as_str())
        self.assertEqual(original.last_process_cloned(),
                         generated.last_process_cloned())
        self.assertEqual(original.is_halted(), generated.is_halted())
Ejemplo n.º 2
0
    def test_add_then_clear_runs_halt_notify(self):
        nfa, buffer, handler, handlersub = handler_setup(
            nfa_name=NFA_NAME_A, pattern=pattern_relaxed)

        run_a = BoboRun(buffer=buffer, nfa=nfa, event=event_a)
        run_b = BoboRun(buffer=buffer, nfa=nfa, event=event_b)
        run_c = BoboRun(buffer=buffer, nfa=nfa, event=event_c)

        # add runs
        handler.add_run(run_a)
        handler.add_run(run_b)
        handler.add_run(run_c)

        self.assertDictEqual(handler.runs, {
            run_a.id: run_a,
            run_b.id: run_b,
            run_c.id: run_c
        })

        # clear runs
        handler.clear_runs(halt=True, notify=True)

        self.assertDictEqual(handler.runs, {})
        self.assertTrue(run_a.is_halted())
        self.assertTrue(run_b.is_halted())
        self.assertTrue(run_c.is_halted())
        self.assertListEqual(handlersub.halt, [run_a.id, run_b.id, run_c.id])
Ejemplo n.º 3
0
    def test_duplicate_run_id(self):
        nfa, buffer, handler, handlersub = handler_setup(
            nfa_name=NFA_NAME_A, pattern=pattern_relaxed)

        run_id = "abc123"
        run_a = BoboRun(buffer=buffer, nfa=nfa, event=event_a, run_id=run_id)
        run_b = BoboRun(buffer=buffer, nfa=nfa, event=event_b, run_id=run_id)

        self.assertIsNone(handler.add_run(run_a))

        with self.assertRaises(RuntimeError):
            handler.add_run(run_b)
Ejemplo n.º 4
0
    def run(d: dict, buffer: SharedVersionedMatchBuffer,
            nfa: BoboNFA) -> 'BoboRun':
        """
        :param d: A dict representation of a BoboRun instance.
        :type d: dict

        :param buffer: A buffer to use with the new BoboRun instance.
        :type buffer: SharedVersionedMatchBuffer

        :param nfa: An automaton to use with the new BoboRun instance.
        :type nfa: BoboNFA

        :return: A new BoboRun instance.
        """

        event = BoboRuleBuilder.event(d[BoboRun.EVENT])
        start_time = d[BoboRun.START_TIME]
        start_state = nfa.states[d[BoboRun.START_STATE_NAME]]
        current_state = nfa.states[d[BoboRun.CURRENT_STATE_NAME]]
        run_id = d[BoboRun.RUN_ID]
        version = RunVersion.list_to_version(d[BoboRun.VERSION])
        last_proceed_had_clone = d[BoboRun.LAST_PROCESS_CLONED]
        halted = d[BoboRun.HALTED]

        return BoboRun(buffer=buffer,
                       nfa=nfa,
                       event=event,
                       start_time=start_time,
                       start_state=start_state,
                       current_state=current_state,
                       run_id=run_id,
                       version=version,
                       put_event=False,
                       last_process_cloned=last_proceed_had_clone,
                       halted=halted)
Ejemplo n.º 5
0
    def test_add_then_remove_run_halt_notify(self):
        nfa, buffer, handler, handlersub = handler_setup(
            nfa_name=NFA_NAME_A, pattern=pattern_relaxed)

        run_a = BoboRun(buffer=buffer, nfa=nfa, event=event_a)

        # add run
        handler.add_run(run_a)
        self.assertDictEqual(handler.runs, {run_a.id: run_a})

        # remove run
        handler.remove_run(run_a.id, halt=True, notify=True)

        self.assertDictEqual(handler.runs, {})
        self.assertTrue(run_a.is_halted())
        self.assertListEqual([run_a.id], handlersub.halt)
Ejemplo n.º 6
0
    def test_to_dict(self):
        nfa, buffer, handler, handlersub = handler_setup(
            nfa_name=NFA_NAME_A, pattern=pattern_relaxed, max_recent=2)

        run_a = BoboRun(buffer=buffer, nfa=nfa, event=event_a)
        handler.add_run(run_a)

        run_a_dict = run_a.to_dict()
        buffer_dict = buffer.to_dict()

        self.assertDictEqual(
            handler.to_dict(), {
                BoboNFAHandler.NFA_NAME: nfa.name,
                BoboNFAHandler.BUFFER: buffer_dict,
                BoboNFAHandler.RUNS: [run_a_dict]
            })
Ejemplo n.º 7
0
    def add_run(self, run: BoboRun) -> None:
        """
        Adds a run to the handler.

        :param run: The run.
        :type run: BoboRun
        """

        with self._lock:
            if run.id in self.runs:
                raise RuntimeError(
                    "Run ID {} already exists in handler for NFA {}.".format(
                        run.id, self.nfa.name))

            self.runs[run.id] = run
            run.subscribe(self)
Ejemplo n.º 8
0
    def test_to_dict_two_match_events(self):
        event_a = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())
        event_b = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())

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

        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()

        # match a --next--> match b
        match_a.add_pointer_next(version=version_str,
                                 label=match_b.label,
                                 event_id=match_b.event.event_id)

        # match a dict
        self.assertDictEqual(
            match_a.to_dict(), {
                MatchEvent.NFA_NAME: NFA_NAME_A,
                MatchEvent.LABEL: LABEL_LAYER_A,
                MatchEvent.EVENT: event_a.to_dict(),
                MatchEvent.NEXT_IDS: {
                    version_str: (match_b.label, match_b.event.event_id)
                },
                MatchEvent.PREVIOUS_IDS: {}
            })

        # match a <--previous-- match b
        match_b.add_pointer_previous(version=version_str,
                                     label=match_a.label,
                                     event_id=match_a.event.event_id)

        # match b dict
        self.assertDictEqual(
            match_b.to_dict(), {
                MatchEvent.NFA_NAME: NFA_NAME_A,
                MatchEvent.LABEL: LABEL_LAYER_B,
                MatchEvent.EVENT: event_b.to_dict(),
                MatchEvent.NEXT_IDS: {},
                MatchEvent.PREVIOUS_IDS: {
                    version_str: (match_a.label, match_a.event.event_id)
                }
            })
Ejemplo n.º 9
0
    def on_run_clone(self,
                     state_name: str,
                     event: BoboEvent,
                     parent_run_id: str = None,
                     force_parent: bool = False,
                     notify: bool = True) -> BoboRun:
        """
        :raises RuntimeError: No parent run found when either one is specified
                              or force_parent is True.
        :raises RuntimeError: State name not found in automaton.

        :return: The newly cloned run.
        """

        with self._lock:
            if parent_run_id is None:
                parent_run = None
            else:
                parent_run = self.runs.get(parent_run_id)

            if parent_run is None and force_parent:
                raise RuntimeError("Failed to clone run. No parent run found.")

            next_state = self.nfa.states.get(state_name)

            if next_state is None:
                raise RuntimeError("Failed to clone run. "
                                   "State {} not found in NFA {}.".format(
                                       state_name, self.nfa.name))

            clone_run = BoboRun(buffer=self.buffer,
                                nfa=self.nfa,
                                event=event,
                                start_state=next_state,
                                parent_run=parent_run)

            self.add_run(clone_run)

            if parent_run is not None:
                parent_run.set_cloned()

            if notify:
                self._notify_clone(run_id=clone_run.id,
                                   state_name=clone_run.current_state.name,
                                   event=clone_run.event)

            return clone_run
Ejemplo n.º 10
0
    def test_remove_all_pointers_two_match_events(self):
        event_a = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())
        event_b = PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())

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

        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()

        # match events should start with no pointers
        self.assertFalse(match_a.has_pointers())
        self.assertFalse(match_b.has_pointers())

        # match a --next--> match b
        match_a.add_pointer_next(version=version_str,
                                 event_id=match_b.event.event_id)

        # match a <--previous-- match b
        match_b.add_pointer_previous(version=version_str,
                                     event_id=match_a.event.event_id)

        # match events both have pointers
        self.assertTrue(match_a.has_pointers())
        self.assertTrue(match_b.has_pointers())

        # removing pointers from one match event
        match_a.remove_all_pointers(version=version_str)

        self.assertFalse(match_a.has_pointers())
        self.assertTrue(match_b.has_pointers())

        # all pointers removed
        match_b.remove_all_pointers(version=version_str)

        self.assertFalse(match_a.has_pointers())
        self.assertFalse(match_b.has_pointers())
Ejemplo n.º 11
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)