def test_parallel_many_events(self):
        event_group = EventGroupParallel([
            EventGroupParallel([Event('a'), Event('b')]),
            # Event('c'), Event('d'), Event('e'), Event('f'), Event('g'),
            # Event('h'),
            # Event('i'),
            Event('j'),
            Event('k'),
            Event('l'),
            Event('m'),
            Event('n'),
            EventGroupParallel([Event('o'), Event('p'),
                                Event('q')])
        ])

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, [
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
                'm', 'n'
            ], dict())
        char_list = events_to_char_list(model_result)
        print(events_to_char_list(model_result))
        self.assertEqual(-10, result)
        self.assertCountEqual([x for x in 'abcdefghijklmn'], char_list)
    def test_nw_with_wrapper_parallel_inside_7(self):
        event_group = EventGroupParallel(
            [EventGroupParallel(string_to_events('tp')),
             Event('q')])

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, ['q'], dict())
        char_list = events_to_char_list(model_result)
        print(events_to_char_list(model_result))
        self.assertEqual(-2, result)
        self.assertCountEqual([x for x in ['q']], char_list)
        self.assertEqual(['q'], events_to_char_list(model_result))
    def test_additional3(self):
        event_group = EventGroup([
            EventGroupParallel([Event('c'), Event('f')]),
            EventGroupParallel([Event('a'), Event('b')]),
            Event('c')
        ])

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, ['b', 'c', 'd'], dict())
        char_list = events_to_char_list(model_result)
        print(events_to_char_list(model_result))
        self.assertEqual(-4, result)
        self.assertCountEqual([x for x in ['b', 'c']], char_list)
        self.assertEqual(['b', 'c'], events_to_char_list(model_result))
    def test_nw_with_wrapper_parallel_inside(self):
        event_group = EventGroupParallel(
            [Event('t'),
             EventGroupParallel(string_to_events('spqacezxy'))])

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, ['z', 'x', 'a', 'b', 'c', 'd', 'e', 'z', 't'], dict())
        char_list = events_to_char_list(model_result)
        print(events_to_char_list(model_result))
        self.assertEqual(-7, result)
        self.assertCountEqual([x for x in ['z', 'x', 'a', 'c', 'e', 't']],
                              char_list)
        self.assertEqual(['z', 'x', 'a', 'c', 'e', 't'],
                         events_to_char_list(model_result))
 def test_92(self):
     gate = SeqGate()
     gate.parse('and({a}{f}opt(and({b}{e}lop({c}))){d})')
     all_length_6_routes = gate.get_all_n_length_routes(6)
     expected = [
         EventGroupMatcher(
             EventGroup([
                 EventGroupParallel([
                     Event('a'),
                     Event('f'),
                     EventGroupParallel(string_to_events('bec')),
                     Event('d')
                 ])
             ]))
     ]
     self.assertCountEqual(expected, all_length_6_routes)
    def test_all_events_in_model(self):
        # when your algorithm is smarter than you
        event_group = EventGroupParallel([
            Event('a'),
            Event('f'),
            EventGroupParallel(string_to_events('bec')),
            Event('d')
        ])

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, ['a', 'b', 'c', 'd', 'e', 'f'], dict())
        char_list = events_to_char_list(model_result)
        print(events_to_char_list(model_result))
        self.assertEqual(-2, result)
        self.assertCountEqual([x for x in ['a', 'b', 'c', 'e', 'f']],
                              char_list)
 def test_6(self):
     gate = SeqGate()
     gate.parse('xor(and({c}and(and({c}{b}and({d}{f})){b}{e})){e}{e})')
     self.assertEqual([
         EventGroup(
             [EventGroupParallel(string_to_events('abcdefghijklmn'))])
     ], gate.get_all_n_length_routes(7))
     self.assertEqual([], gate.get_all_n_length_routes(6))
 def test_7(self):
     gate = SeqGate()
     gate.parse('lop({c})')
     self.assertEqual(None, gate.get_all_n_length_routes(6))
     self.assertEqual([
         EventGroupMatcher(
             EventGroup([EventGroupParallel(string_to_events('ccc'))]))
     ], gate.get_all_n_length_routes(3))
 def test_9(self):
     gate = SeqGate()
     gate.parse('and({c}and({a}lop({e}opt({d}))seq({c}{b})))')
     all_length_5_routes = gate.get_all_n_length_routes(5)
     expected = [
         EventGroupMatcher(
             EventGroup([
                 EventGroupParallel([
                     Event('c'),
                     EventGroupParallel([
                         Event('a'),
                         Event('e'),
                         EventGroup(string_to_events('cb'))
                     ])
                 ])
             ]))
     ]
     self.assertCountEqual(expected, all_length_5_routes)
 def test_8(self):
     gate = SeqGate()
     gate.parse(
         'xor({f}{d}and({e}xor(lop(xor({f}{d}))lop({a}))))and({b}{a})')
     all_length_3_routes = gate.get_all_n_length_routes(3)
     self.assertCountEqual([
         EventGroupMatcher(
             EventGroup(
                 [Event('f'),
                  EventGroupParallel(string_to_events('ba'))])),
         EventGroupMatcher(
             EventGroup(
                 [Event('d'),
                  EventGroupParallel(string_to_events('ba'))])),
         EventGroupMatcher(
             EventGroup(
                 [Event('e'),
                  EventGroupParallel(string_to_events('ba'))]))
     ], all_length_3_routes)
    def test_nw_with_wrapper_parallel(self):
        event_group = EventGroupParallel(string_to_events('pqacezxys'))

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, ['z', 'x', 'k', 'l', 'm', 'n', 'o', 'z', 'x'], dict())
        print(events_to_char_list(model_result))
        self.assertEqual(-14, result)
        self.assertCountEqual([x for x in ['z', 'x']],
                              events_to_char_list(model_result))
        self.assertEqual(['z', 'x'], events_to_char_list(model_result))
 def test_cache(self):
     event_group = [
         EventGroupParallel([
             Event('a'),
             Event('f'),
             EventGroupParallel(string_to_events('bec')),
             Event('d')
         ])
     ]
     event_group2 = [
         EventGroupParallel([
             Event('a'),
             Event('f'),
             EventGroupParallel(string_to_events('bec')),
             Event('d')
         ])
     ]
     log = ['a', 'b', 'c', 'd', 'e', 'f']
     log2 = ['a', 'b', 'c', 'd', 'e', 'f']
     cache1 = get_cache_id(event_group, log)
     cache2 = get_cache_id(event_group2, log2)
     self.assertEqual(cache1, cache2)
    def test_2(self):
        gate = SeqGate()
        gate.parse('{f}xor({d}and({b}lop({c})opt({a})))')

        actual = gate.get_all_n_length_routes(5)
        expected_1 = EventGroupMatcher(
            EventGroup([
                Event('f'),
                EventGroupParallel(
                    [Event('b'),
                     EventGroup(string_to_events('ccc'))])
            ]))
        expected_2 = EventGroupMatcher(
            EventGroup([
                Event('f'),
                EventGroupParallel([
                    Event('b'),
                    EventGroup(string_to_events('cc')),
                    Event('a')
                ])
            ]))
        expected = [expected_1, expected_2]
        self.assertCountEqual(expected, actual)
 def test_1(self):
     gate = SeqGate()
     gate.parse('and({a}{b}{c}{d}{e}{f}{g}{h}{i}{j}{k}{l}{m}{n})')
     self.assertCountEqual([
         EventGroupMatcher(
             EventGroup(
                 [EventGroupParallel(string_to_events('abcdefghijklmn'))]))
     ],
                           gate.get_all_n_length_routes(
                               14, ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                                    'i', 'j', 'k', 'l', 'm', 'n')))
     self.assertEqual(
         None,
         gate.get_all_n_length_routes(13,
                                      ('a', 'b', 'c', 'd', 'e', 'f', 'g',
                                       'h', 'i', 'j', 'k', 'l', 'm', 'n')))
    def get_all_n_length_routes(self, n: int, process) -> []:
        if n == 0:
            return []
        if self.model_max_length < n or n < self.model_min_length:
            return None

        min_lengths = self.get_children_min_length()
        max_lengths = self.get_children_max_length()
        global_list = []

        for elem in self.elements:
            local_list = []
            if isinstance(elem, Event):
                local_list.append(elem)
                min_lengths.pop(0)
                max_lengths.pop(0)
            else:
                lower_limit, upper_limit = self.get_goal_length_range(
                    n, global_list, min_lengths, max_lengths)
                for i in range(lower_limit, upper_limit + 1):
                    try:
                        child_all_n_length_routes = elem.get_all_n_length_routes(
                            i, process)
                    except ValueError:
                        return None
                    if child_all_n_length_routes is not None:
                        local_list.append(child_all_n_length_routes)

            if local_list:
                global_list.append(local_list)

        result = []
        if global_list:
            for elem in flatten_values(global_list):
                if self.check_length(n, elem):
                    if n == 1:
                        # because always 1 elem list
                        result.append(elem[0])
                    else:
                        self.check_valid_for_get_n_length(elem)
                        result.append(EventGroupParallel(elem))
        if result:
            return result
        else:
            return None
    def test_legend(self):
        event_group = EventGroup([
            Event('a'),
            EventGroupParallel([Event('c'), Event('d')]),
            Event('e'),
            Event('h')
        ])

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, ['a', 'c', 'd', 'e', 'h'], dict())
        char_list = events_to_char_list(model_result)
        print(events_to_char_list(model_result))
        self.assertEqual(0, result)
        self.assertCountEqual([x for x in ['a', 'c', 'd', 'e', 'h']],
                              char_list)
        self.assertEqual(['a', 'c', 'd', 'e', 'h'],
                         events_to_char_list(model_result))
Ejemplo n.º 17
0
def to_n_length_opt(n, child_list):
    min_length = min([len(x) for x in child_list])
    max_length = n - min_length
    global_result = []
    while child_list:
        child = child_list[0]
        len_child = len(child)
        child_list.remove(child)
        if len_child <= max_length:
            [
                global_result.append(EventGroupParallel(x))
                for x in to_n_length_inner_opt(n - len_child, max_length -
                                               len_child, child,
                                               copy.copy(child_list))
            ]
        elif len_child == n:
            if isinstance(child, list):
                global_result.append(child[0])
            # possibly should be always list
            else:
                global_result.append(child)

    return global_result
 def test_eq4(self):
     e1 = EventGroup([Event('f'), EventGroupParallel([Event('b'),
                                                      EventGroup(string_to_events('ccc'))])])
     e2 = EventGroupParallel([Event('f'), EventGroup([Event('b'),
                                                      EventGroupParallel(string_to_events('ccc'))])])
     self.assertFalse(e1 == e2)