def test_nw_with_wrapper_parallel_inside_6(self):
        event_group = EventGroup([
            EventGroupParallel(string_to_events('ac')),
            EventGroup(string_to_events('ez'))
        ])

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, ['z', 'x', 'q'], dict())
        char_list = events_to_char_list(model_result)
        print(events_to_char_list(model_result))
        self.assertEqual(-5, result)
        self.assertCountEqual([x for x in ['z']], char_list)
        self.assertEqual(['z'], events_to_char_list(model_result))
 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_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_additional1(self):
        event_group = EventGroup([
            Event('f'),
            EventGroup(
                [EventGroupParallel([Event('a'), Event('f')]),
                 Event('b')]),
            Event('f')
        ])

        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(-5, result)
        self.assertCountEqual([x for x in ['a', 'b', 'f']], char_list)
        self.assertEqual(['a', 'b', 'f'], events_to_char_list(model_result))
 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(self):
        event_group = EventGroup(string_to_events('pqacezxys'))

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, ['z', 'x', 'a', 'b', 'c', 'd', 'e', 'z', 'x'], dict())
        print(events_to_char_list(model_result))
        self.assertEqual(-8, result)
        self.assertCountEqual([x for x in ['a', 'c', 'e', 'z', 'x']],
                              events_to_char_list(model_result))
        self.assertEqual(['a', 'c', 'e', 'z', 'x'],
                         events_to_char_list(model_result))
    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)
Esempio n. 9
0
def to_n_length(n, child_list, process, max_depth, max_error):
    child_list = filter_children_list([], child_list, process, max_error)
    if not child_list:
        return []
    max_allowed_length = n - min(
        [sum([len(y) for y in x]) for x in child_list])
    child_list_copy = copy.copy(child_list)
    while child_list:
        child = child_list[0]
        len_child = sum([len(x) for x in child])
        if len_child <= max_allowed_length:
            yield from [
                EventGroup(x) for x in to_n_length_inner(
                    n - len_child, max_allowed_length -
                    len_child, child, copy.copy(
                        child_list_copy), process, 1, max_depth, max_error)
            ]
        elif len_child == n:
            if len(child) == 1:
                yield child[0]
            else:
                yield EventGroup(child)
        child_list.pop(0)
    def test_nw_with_wrapper_model_bigger(self):
        event_group_events = []
        for x in 'pqacezxysabc':
            event_group_events.append(Event(x))
        event_group = EventGroup(event_group_events)

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, ['z', 'x', 'a', 'b', 'c', 'd', 'e', 'z', 'x'], dict())
        print(events_to_char_list(model_result))
        self.assertEqual(-11, result)
        self.assertCountEqual([x for x in ['a', 'c', 'e', 'z', 'x']],
                              events_to_char_list(model_result))
        self.assertEqual(['a', 'c', 'e', 'z', 'x'],
                         events_to_char_list(model_result))
 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 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_nw_with_wrapper_parallel_inside_9(self):
        event_group = EventGroup([
            Event('f'),
            EventGroupParallel(
                [EventGroupParallel(string_to_events('dc')),
                 Event('f')]),
            Event('b'),
            EventGroupParallel(
                [EventGroupParallel(string_to_events('df')),
                 Event('e')])
        ])

        alignment_cached = BestAlignmentCached()
        result, model_result = alignment_cached.get_best_alignment(
            event_group, ['a', 'c', 'b', 'd'], dict())
        char_list = events_to_char_list(model_result)
        print(events_to_char_list(model_result))
        self.assertEqual(-6, result)
        self.assertCountEqual([x for x in ['c', 'b', 'd']], char_list)
        self.assertEqual(['c', 'b', 'd'], events_to_char_list(model_result))
Esempio n. 14
0
    def get_all_n_length_routes(self, n: int, process) -> [BaseGroup]:
        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):
                    result.append(EventGroup(elem))

        if result:
            return result
        else:
            return None
 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)