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)
def match_event(d: dict) -> 'MatchEvent': """ :param d: A dict representation of a MatchEvent instance. :type d: dict :return: A new MatchEvent instance. """ return MatchEvent(nfa_name=d[MatchEvent.NFA_NAME], label=d[MatchEvent.LABEL], event=BoboRuleBuilder.event(d[MatchEvent.EVENT]), next_ids=d[MatchEvent.NEXT_IDS], previous_ids=d[MatchEvent.PREVIOUS_IDS])
def _handle_clone(self, data: str) -> None: json_data = json.loads(data) nfa_name = json_data[bdc.NFA_NAME] run_id = json_data[bdc.RUN_ID] state_to = json_data[bdc.STATE_TO] event = BoboRuleBuilder.event(json_data[bdc.EVENT]) for subscriber in self._subs: subscriber.on_dist_run_clone( nfa_name=nfa_name, run_id=run_id, next_state_name=state_to, next_event=event )
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)
def _handle_transition(self, data: str) -> None: json_data = json.loads(data) nfa_name = json_data[bdc.NFA_NAME] run_id = json_data[bdc.RUN_ID] state_from = json_data[bdc.STATE_FROM] state_to = json_data[bdc.STATE_TO] event = BoboRuleBuilder.event(json_data[bdc.EVENT]) for subscriber in self._subs: subscriber.on_dist_run_transition( nfa_name=nfa_name, run_id=run_id, state_name_from=state_from, state_name_to=state_to, event=event )
def test_composite(self): # composite event data c_timestamp = EpochNSClock.generate_timestamp() c_name = NAME_A c_data = DATA_A c_id = EVENT_ID_A # primitive event used in composite event's history p_timestamp = EpochNSClock.generate_timestamp() p_data = NAME_B p_label = LABEL_A p_id = EVENT_ID_B # create dict representation of composite event c_dict = { CompositeEvent.TIMESTAMP: c_timestamp, CompositeEvent.NAME: c_name, CompositeEvent.HISTORY: { p_label: [{ PrimitiveEvent.TIMESTAMP: p_timestamp, PrimitiveEvent.DATA: p_data, PrimitiveEvent.EVENT_ID: p_id }] }, CompositeEvent.DATA: c_data, CompositeEvent.EVENT_ID: c_id } # check composite event c_event_1 = BoboRuleBuilder.composite(c_dict) c_event_2 = BoboRuleBuilder.event(c_dict) for c_event in [c_event_1, c_event_2]: self.assertEqual(c_event.timestamp, c_timestamp) self.assertEqual(c_event.name, c_name) self.assertEqual(c_event.data, c_data) self.assertEqual(c_event.event_id, c_id) # Check primitive event in history p_event = c_event.history.events[p_label][0] self.assertEqual(p_event.timestamp, p_timestamp) self.assertEqual(p_event.data, p_data) self.assertEqual(p_event.event_id, p_id)
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)