def test_collision(self): """collision()""" # method collision(set_of_symbols): # Try with suitable objects of class sym_char, sym_char_class, # sym_string. Check correct output. abcd = b_Sym_string("abcd", "abcd", 0) e = b_Sym_char('e', 'e', 1) fg = b_Sym_char_class("[fg]", set(['f', 'g']), 2) hello = b_Sym_string("hello", "hello", 3) set_of_symbols = set([e, fg, hello]) self.assertTrue(abcd.collision(set_of_symbols) == False) a = b_Sym_char('a', 'a', 4) set_of_symbols.add(a) self.assertTrue(abcd.collision(set_of_symbols) == True) set_of_symbols.remove(a) self.assertTrue(abcd.collision(set_of_symbols) == False) ab = b_Sym_char_class("[ab]", set(['a', 'b']), 5) set_of_symbols.add(ab) self.assertTrue(abcd.collision(set_of_symbols) == True) set_of_symbols.remove(ab) self.assertTrue(abcd.collision(set_of_symbols) == False) abcd_2 = b_Sym_string("abcd_2", "abcd", 6) set_of_symbols.add(abcd_2) self.assertTrue(abcd.collision(set_of_symbols) == True)
def test_compute_equal(self): """compute_equal()""" # method compute_equal(other): # If is other object of class sym_string, then return True if are # their arguments string same. hello = b_Sym_string("hello", "hello", 0) abba = b_Sym_string("abba", "abba", 1) self.assertTrue(hello.compute_equal(abba) == False) hello_2 = b_Sym_string("hello_2", "hello", 2) self.assertTrue(hello.compute_equal(hello_2) == True) # If is other object of type sym_char, then return True if is length # of string equal to 1 and argument char is same as argument string. hello_short = b_Sym_string("h", "h", 0) a = b_Sym_char('a', 'a', 1) self.assertTrue(hello_short.compute_equal(a) == False) h = b_Sym_char('h', 'h', 2) self.assertTrue(hello_short.compute_equal(h) == True) # If is other object of class sym_char_class, then return True if is # len(other.charClass) == 1, length string equal to 1 and values of # arguments string and charClass are same. hello_short = b_Sym_string("h", "h", 0) set_a = b_Sym_char_class('[a]', set(['a']), 1) self.assertTrue(hello_short.compute_equal(set_a) == False) set_h = b_Sym_char_class('[h]', set(['h']), 2) self.assertTrue(hello_short.compute_equal(set_h) == True)
def test_collision(self): """collision()""" # method collision(set_of_symbols): # Try with suitable objects of class sym_char, sym_char_class, # sym_string. Check correct output. abcd = b_Sym_string("abcd", "abcd", 0) e = b_Sym_char('e', 'e', 1) fg = b_Sym_char_class("[fg]", set(['f', 'g']), 2) hello = b_Sym_string("hello", "hello", 3) set_of_symbols = set([e, fg, hello]) self.assertTrue(abcd.collision(set_of_symbols) == False) a = b_Sym_char('a', 'a', 4) set_of_symbols.add(a) self.assertTrue(abcd.collision(set_of_symbols) == True) set_of_symbols.remove(a) self.assertTrue(abcd.collision(set_of_symbols) == False) ab = b_Sym_char_class("[ab]", set(['a', 'b']), 5) set_of_symbols.add(ab) self.assertTrue(abcd.collision(set_of_symbols) == True) set_of_symbols.remove(ab) self.assertTrue(abcd.collision(set_of_symbols) == False) abcd_2 = b_Sym_string("abcd_2", "abcd", 6) set_of_symbols.add(abcd_2) self.assertTrue(abcd.collision(set_of_symbols) == True)
def test_compute_equal(self): """compute_equal()""" # method compute_equal(other): # If is other object of class sym_string, then return True if are # their arguments string same. hello = b_Sym_string("hello", "hello", 0) abba = b_Sym_string("abba", "abba", 1) self.assertTrue(hello.compute_equal(abba) == False) hello_2 = b_Sym_string("hello_2", "hello", 2) self.assertTrue(hello.compute_equal(hello_2) == True) # If is other object of type sym_char, then return True if is length # of string equal to 1 and argument char is same as argument string. hello_short = b_Sym_string("h", "h", 0) a = b_Sym_char('a', 'a', 1) self.assertTrue(hello_short.compute_equal(a) == False) h = b_Sym_char('h', 'h', 2) self.assertTrue(hello_short.compute_equal(h) == True) # If is other object of class sym_char_class, then return True if is # len(other.charClass) == 1, length string equal to 1 and values of # arguments string and charClass are same. hello_short = b_Sym_string("h", "h", 0) set_a = b_Sym_char_class('[a]', set(['a']), 1) self.assertTrue(hello_short.compute_equal(set_a) == False) set_h = b_Sym_char_class('[h]', set(['h']), 2) self.assertTrue(hello_short.compute_equal(set_h) == True)
def test_accept(self): """accept()""" # method accept(text): # Check if len(text) == 0, # then is call exception symbol_string_to_short ab = b_Sym_char_class("ab", set(['a', 'b']), 0) try: ab.accept("") self.assertTrue(False) except symbol_string_to_short: self.assertTrue(True) # Check if text[0] in self.charClass, then is return value text[1:] ab = b_Sym_char_class("ab", set(['a', 'b']), 0) self.assertTrue(ab.accept("adam") == "dam") # In case text[0] != self.char[0], # then is call exception symbol_accept_exception ab = b_Sym_char_class("ab", set(['a', 'b']), 0) try: ab.accept("eva") self.assertTrue(False) except symbol_accept_exception: self.assertTrue(True)
def test__identify_fading_states(self): """_identify_fading_states(nfa_closure_states)""" history = HistoryFA() history._state_representation = [ set([0]), set([0,1]), set([0,2]), set([0,3]), set([0,4]), set([0,5]), set([0,6]), set([0,2,4]), set([0,2,5]), set([0,2,6]) ] self.assertTrue(history._identify_fading_states([2]) == [2, 7, 8, 9]) act = nfa_data() act.states[0] = b_State(0,set()) act.states[1] = b_State(1,set()) act.states[2] = b_State(2,set()) act.states[3] = b_State(3,set([0])) act.states[4] = b_State(4,set()) act.states[5] = b_State(5,set()) act.states[6] = b_State(6,set([1])) act.alphabet[0] = b_Sym_char("a", "a", 0) act.alphabet[1] = b_Sym_char("b", "b", 1) act.alphabet[2] = b_Sym_char("c", "c", 2) act.alphabet[3] = b_Sym_char("d", "d", 3) act.alphabet[4] = b_Sym_char("e", "e", 4) act.alphabet[5] = b_Sym_char("f", "f", 5) star = set() for ord_char in range(0, 256): star.add(chr(ord_char)) act.alphabet[6] = b_Sym_char_class("*", star, 6) mimo_a = set() for ord_char in range(0, 256): mimo_a.add(chr(ord_char)) mimo_a.remove('a') act.alphabet[7] = b_Sym_char_class("^a", mimo_a, 7) act.start = 0 act.final.add(3) act.final.add(6) act.transitions.add( (0, 6, 0) ) act.transitions.add( (0, 0, 1) ) act.transitions.add( (1, 1, 2) ) act.transitions.add( (2, 7, 2) ) act.transitions.add( (2, 2, 3) ) act.transitions.add( (0, 3, 4) ) act.transitions.add( (4, 4, 5) ) act.transitions.add( (5, 5, 6) ) history = HistoryFA() history._automaton = act history.remove_epsilons() NFA = history.get_automaton(True) history.determinise(create_table = True) nfa_closure_states = history._discover_closure_states(NFA) self.assertTrue(history._identify_fading_states(nfa_closure_states) == [5, 7, 8, 9])
def _replace_length_restriction_with_a_closure(self, NFA): """ The first step in this construction replaces the length restriction with a closure, and constructs the H-FA, with the closure represented by a flag in the history buffer. :param NFA: NFA :type NFA: nfa_data :returns: NFA without counting constraint :rtype: nfa_data """ # identify counting transitions with exactly X counting cnt_transitions = list() for t in NFA.transitions: if NFA.alphabet[t[1]].ctype == io_mapper["b_Sym_cnt_constr"]: if NFA.alphabet[t[1]].m == NFA.alphabet[t[1]].n: cnt_transitions.append(t) # remove founded counting transtions # and replace them with loop transitions # and add epsilon tran. to next state for t in cnt_transitions: NFA.transitions.remove(t) cnt_symbol = NFA.alphabet[t[1]] self.flags_cnt[t[0]] = str(cnt_symbol.m) NFA.transitions.add((t[0], t[1], t[0])) NFA.transitions.add((t[0], -1, t[2])) # replace cnt symbol at char or char class # and add epsilon symbol into alphabet if does not exist for t in cnt_transitions: symbolID = t[1] cnt_symbol = copy.deepcopy(NFA.alphabet[symbolID]) if cnt_symbol.ctype == io_mapper["b_Sym_cnt_constr"]: if isinstance(cnt_symbol.symbol, str): NFA.alphabet[symbolID] = b_Sym_char_class( new_text=cnt_symbol._text, charClass=set([cnt_symbol.symbol]), new_id=cnt_symbol._id) else: NFA.alphabet[symbolID] = b_Sym_char_class( new_text=cnt_symbol._text, charClass=cnt_symbol.symbol, new_id=cnt_symbol._id) epsilonID = -1 if not epsilonID in NFA.alphabet: NFA.alphabet[epsilonID] = b_Sym_char("Epsilon", "", -1) # remove epsilons aut = b_Automaton() aut._automaton = NFA aut.remove_epsilons() return aut._automaton
def test_double_stride(self): """double_stride()""" symbol = b_Sym_char("symbol", 'a', 1) comp_symbol = b_Sym_char("comp_symbol", 'b', 2) cd = b_Sym_char_class("cd", set(['c', 'd']), 3) ef = b_Sym_char_class("ef", set(['e', 'f']), 4) # check returned local_chars for type char class self.assertTrue(cd.double_stride(ef, 2, [set(["e", "f", "g", "h"])])[1] == [set(["g", "h"])]) self.assertTrue(symbol.double_stride(ef, 2, [set(["e", "f", "g", "h"])])[1] == [set(["g", "h"])]) self.assertTrue(symbol.double_stride(cd, 2, [set(["e", "f", "g", "h"])])[1] == [set(["e", "f", "g", "h"])]) # - Check a situation where the operation is able to solve self. self.assertTrue( symbol.double_stride(comp_symbol, 2, [set(["a", "b"])])[0].ctype == '4') self.assertTrue( symbol.double_stride(comp_symbol, 2, [set(["a", "b"])])[0].kchar == (frozenset(['a']), frozenset(['b']))) self.assertTrue( symbol.double_stride(comp_symbol, 2, [set(["a", "b"])])[0].last == 2) self.assertTrue( symbol.double_stride(comp_symbol, 2, [set(["a", "b"])])[1] == [set(['a'])]) # - Check a situation where the operation is able to solve compSymbol. self.assertTrue( comp_symbol.double_stride(symbol, 2, [set(["a", "b"])])[0].ctype == '4') self.assertTrue( comp_symbol.double_stride(symbol, 2, [set(["a", "b"])])[0].kchar == (frozenset(['b']), frozenset(['a']))) self.assertTrue( comp_symbol.double_stride(symbol, 3, [set(["a", "b"])])[0].last == 3) self.assertTrue( comp_symbol.double_stride(symbol, 2, [set(["a", "b"])])[1] == [set(['b'])]) # - Check a situation where the operation is not able to resolve # the double stride neither self nor compSymbol - check thrown # symbol_double_stride_exception. symbol.ctype = '5' comp_symbol.ctype = '5' try: comp_symbol.double_stride(symbol, 2, [set(["a", "b"])]) self.assertTrue(False) except symbol_double_stride_exception: self.assertTrue(True)
def test_is_empty(self): """is_empty()""" # If is len(self.charClass) == 0 and self._id != -1 return True, # otherwise return False. ef = b_Sym_char_class("set(['e', 'f'])", set(['e', 'f']), 1) self.assertTrue(ef.is_empty() == False) near_empty = b_Sym_char_class("near_empty", set(), -1) self.assertTrue(near_empty.is_empty() == False) empty = b_Sym_char_class("empty", set(), 15) self.assertTrue(empty.is_empty() == True)
def test_compute_collision(self): """compute_collision()""" # method compute_collision(compSymbol): # Check compute of collision for object of type sym_char and # sym_char_class. # sym_char and sym_char ; not collision a = b_Sym_char('a', 'a', 1) b = b_Sym_char('b', 'b', 2) self.assertTrue(a.compute_collision(b) == (set([a]), set(), set([b]))) # sym_char and sym_char ; collision a = b_Sym_char('a', 'a', 1) other_a = b_Sym_char('a', 'a', 3) from copy import deepcopy copy_a = deepcopy(a) copy_other_a = deepcopy(other_a) result = a.compute_collision(other_a) # check there are not changes on original symbols new_symbol = result[1].pop() self.assertTrue(result[0] == set()) self.assertTrue(result[2] == set()) self.assertTrue(new_symbol.char == 'a') self.assertTrue(new_symbol._id == -2) # sym_char and sym_char_class ; not collision a = b_Sym_char('a', 'a', 1) c_d = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 4) self.assertTrue( a.compute_collision(c_d) == (set([a]), set(), set([c_d]))) # sym_char and sym_char_class ; collision a = b_Sym_char('a', 'a', 1) a_b = b_Sym_char_class("set(['a', 'b'])", set(['a', 'b']), 5) copy_a = deepcopy(a) copy_a_b = deepcopy(a_b) result = a.compute_collision(a_b) # check there are not changes on original symbols self.assertTrue(a == copy_a) self.assertTrue(a_b == copy_a_b) newSymbol = result[1].pop() self.assertTrue(result[0] == set()) self.assertTrue(newSymbol.char == 'a') self.assertTrue(newSymbol._id == -2) newSymbol = result[2].pop() self.assertTrue(newSymbol.ctype == '1') self.assertTrue(newSymbol.charClass == set(['b']))
def test_compute_equal(self): """compute_equal()""" # method compute_equal(other): # If is other object of type sym_char_class return True if # arguments are same, otherwise return False. cd = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 0) ef = b_Sym_char_class("set(['e', 'f'])", set(['e', 'f']), 1) self.assertTrue(cd.compute_equal(ef) == False) ef = b_Sym_char_class("set(['c', 'd'])", set(['d', 'c']), 1) self.assertTrue(cd.compute_equal(ef) == True) a = b_Sym_char('a', 'a', 0) self.assertTrue(cd.compute_equal(a) == False)
def test_compute_collision(self): """compute_collision()""" # method compute_collision(compSymbol): # Check compute of collision for object of type sym_char and # sym_char_class. # sym_char and sym_char ; not collision a = b_Sym_char('a', 'a', 1) b = b_Sym_char('b', 'b', 2) self.assertTrue(a.compute_collision(b) == (set([a]), set(), set([b]))) # sym_char and sym_char ; collision a = b_Sym_char('a', 'a', 1) other_a = b_Sym_char('a', 'a', 3) from copy import deepcopy copy_a = deepcopy(a) copy_other_a = deepcopy(other_a) result = a.compute_collision(other_a) # check there are not changes on original symbols new_symbol = result[1].pop() self.assertTrue(result[0] == set()) self.assertTrue(result[2] == set()) self.assertTrue(new_symbol.char == 'a') self.assertTrue(new_symbol._id == -2) # sym_char and sym_char_class ; not collision a = b_Sym_char('a', 'a', 1) c_d = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 4) self.assertTrue(a.compute_collision(c_d) == (set([a]), set(), set([c_d]))) # sym_char and sym_char_class ; collision a = b_Sym_char('a', 'a', 1) a_b = b_Sym_char_class("set(['a', 'b'])", set(['a', 'b']), 5) copy_a = deepcopy(a) copy_a_b = deepcopy(a_b) result = a.compute_collision(a_b) # check there are not changes on original symbols self.assertTrue(a == copy_a) self.assertTrue(a_b == copy_a_b) newSymbol = result[1].pop() self.assertTrue(result[0] == set()) self.assertTrue(newSymbol.char == 'a') self.assertTrue(newSymbol._id == -2) newSymbol = result[2].pop() self.assertTrue(newSymbol.ctype == '1') self.assertTrue(newSymbol.charClass == set(['b']))
def test_collision(self): """collision()""" # method collision(set_of_symbols): # Try with suitable objects class sym_char, sym_char_class, # sym_string. Check correct output (is / is not collision). a = b_Sym_char('a', 'a', 0) cd = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 1) ef = b_Sym_char_class("set(['e', 'f'])", set(['e', 'f']), 2) adam = b_Sym_string("baba", "baba", 3) set_of_symbols = set([a, cd, adam]) self.assertTrue(ef.collision(set_of_symbols) == False) fg = b_Sym_char_class("set(['f', 'g'])", set(['f', 'g']), 4) set_of_symbols = set([a, fg, adam]) self.assertTrue(ef.collision(set_of_symbols) == True)
def test_compute_collision(self): """compute_collision()""" # Check correct compute of collision for objects of type sym_char_class. cd = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 0) ef = b_Sym_char_class("set(['e', 'f'])", set(['e', 'f']), 1) self.assertTrue(cd.compute_collision(ef) == (set([cd]), set(), set([ef]))) ef = b_Sym_char_class("set(['e', 'f'])", set(['c', 'f']), 1) result = cd.compute_collision(ef) newSymbol = result[0].pop() self.assertTrue(newSymbol.charClass == set(['d'])) newSymbol = result[2].pop() self.assertTrue(newSymbol.charClass == set(['f'])) newSymbol = result[1].pop() self.assertTrue(newSymbol.charClass == set(['c']))
def test_get_text(self): """get_text()""" # Check return correct representation. ef = b_Sym_char_class("set(['e', 'f'])", set(['e', 'f']), 1) self.assertTrue(ef.get_text() == "[ef]") chars = set() for i in range(0, 256): chars.add(chr(i)) chars.remove('2') chars.remove('3') chars.remove('4') chars.remove('7') chars.remove('8') chars.remove('9') big_set = b_Sym_char_class("big_set", chars, 2) self.assertTrue(big_set.get_text() == "^[234789]")
def test_decode_symbol(self): """decode_symbol()""" # Test if different types of symbols are decoded correctly and # the symbol was removed from the beginning of input string. aut = PHF_DFA() aut._automaton.alphabet[0] = b_Sym_char_class("ch0", set(['a', 'b']), 0) aut._automaton.alphabet[1] = b_Sym_char_class("ch1", set(['c', 'd']), 1) aut._automaton.alphabet[2] = b_Sym_char_class("ch2", set(['e', 'f']), 2) aut._automaton.alphabet[3] = b_Sym_char("ch3", "g", 3) aut._automaton.alphabet[4] = b_Sym_kchar("ch4", (frozenset(['1', '2']), frozenset(['1', '2'])), 4) self.assertEqual(aut.decode_symbol("abeg112"), ("beg112", 0)) self.assertEqual(aut.decode_symbol("beg112"), ("eg112", 0)) self.assertEqual(aut.decode_symbol("eg112"), ("g112", 2)) self.assertEqual(aut.decode_symbol("g112"), ("112", 3)) self.assertEqual(aut.decode_symbol("112"), ("2", 4)) # Nonexistent symbol is removed from the string and -1 is returned self.assertEqual(aut.decode_symbol("2"), ("", -1))
def test_collision(self): """collision()""" # method collision(set_of_symbols): # Try with suitable objects class sym_char, sym_char_class, # sym_string, sym_cnt_constr. Check correct output # (is / is not collision). ac = b_Sym_cnt_constr('a', 'a', 3, 5, 0) bc = b_Sym_cnt_constr('b', 'b', 3, 5, 0) b = b_Sym_char('b', 'b', 0) cd = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 1) adam = b_Sym_string("baba", "baba", 3) set_of_symbols = set([b, bc, cd, adam]) self.assertTrue(ac.collision(set_of_symbols) == False) c = b_Sym_cnt_constr('a', 'a', 1, 9, 0) set_of_symbols = set([c, b, bc, cd, adam]) self.assertTrue(ac.collision(set_of_symbols) == True) c = b_Sym_char('a', 'a', 0) set_of_symbols = set([c, b, bc, cd, adam]) self.assertTrue(ac.collision(set_of_symbols) == True) c = b_Sym_char_class("set(['a', 'd'])", set(['a', 'd']), 1) set_of_symbols = set([c, b, bc, cd, adam]) self.assertTrue(ac.collision(set_of_symbols) == True) c = b_Sym_char('a', 'a', 0) set_of_symbols = set([c, b, bc, cd, adam]) self.assertTrue(ac.collision(set_of_symbols) == True) c = b_Sym_string("aaaa", "aaaa", 3) set_of_symbols = set([c, b, bc, cd, adam]) self.assertTrue(ac.collision(set_of_symbols) == True) c = b_Sym_string("aa", "aa", 3) set_of_symbols = set([c, b, bc, cd, adam]) self.assertTrue(ac.collision(set_of_symbols) == True) c = b_Sym_string("aaaaaaaaaaaa", "aaaaaaaaaaaa", 3) set_of_symbols = set([c, b, bc, cd, adam]) self.assertTrue(ac.collision(set_of_symbols) == True)
def test_compute_equal(self): """compute_equal()""" # method compute_equal(other): # If is other type sym_kchar, then return True if are arguments # kchar same. abc = b_Sym_kchar("abc", ('a', 'b', 'c'), 0) efg = b_Sym_kchar("efg", ('e', 'f', 'g'), 1) self.assertTrue(abc.compute_equal(efg) == False) abc_2 = b_Sym_kchar( "abc", (frozenset(['a']), frozenset(['b']), frozenset(['c'])), 2) self.assertTrue(abc.compute_equal(abc_2) == True) # If is other type sym_string, then return True if is # len(other.string) == len(self.kchar), all subsymbols kchar have # length one (len(self.kchar[i]) == 1) and value string is straight # value kchar. kchar_abc = b_Sym_kchar("abc", ('a', 'b', 'c'), 0) string_abc = b_Sym_string("abc", "abc", 1) string_abcde = b_Sym_string("abcde", "abcde", 2) self.assertTrue(kchar_abc.compute_equal(string_abc) == True) self.assertTrue(kchar_abc.compute_equal(string_abcde) == False) # If is other type sym_char, then return True if is # len(self.kchar) == 1 and len(self.kchar[0]) == 1 and their # arguments are same. kchar_a = b_Sym_kchar("kchar_a", ('a'), 0) a = b_Sym_char("a", 'a', 1) self.assertTrue(kchar_a.compute_equal(a) == True) b = b_Sym_char("b", 'b', 2) self.assertTrue(kchar_a.compute_equal(b) == False) # If is other type sym_char_class, then return True if is # len(self.kchar) == 1 and len(other.charClass) == len(self.kchar[0]) # and values of arguments are same. kchar_abc = b_Sym_kchar("kchar_[abc]", (frozenset(['a', 'b', 'c']), ), 0) set_abc = b_Sym_char_class("[abc]", set(['a', 'b', 'c']), 1) self.assertTrue(kchar_abc.compute_equal(set_abc) == True) cd = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 2) self.assertTrue(kchar_abc.compute_equal(cd) == False)
def test__discover_closure_states(self): """_discover_closure_states(NFA)""" act = nfa_data() act.states[0] = b_State(0,set()) act.states[1] = b_State(1,set()) act.states[2] = b_State(2,set()) act.states[3] = b_State(3,set([0])) act.states[4] = b_State(4,set()) act.states[5] = b_State(5,set()) act.states[6] = b_State(6,set([1])) act.alphabet[0] = b_Sym_char("a", "a", 0) act.alphabet[1] = b_Sym_char("b", "b", 1) act.alphabet[2] = b_Sym_char("c", "c", 2) act.alphabet[3] = b_Sym_char("d", "d", 3) act.alphabet[4] = b_Sym_char("e", "e", 4) act.alphabet[5] = b_Sym_char("f", "f", 5) star = set() for ord_char in range(0, 256): star.add(chr(ord_char)) act.alphabet[6] = b_Sym_char_class("*", star, 6) mimo_a = set() for ord_char in range(0, 256): mimo_a.add(chr(ord_char)) mimo_a.remove('a') act.alphabet[7] = b_Sym_char_class("^a", mimo_a, 7) act.start = 0 act.final.add(3) act.final.add(6) act.transitions.add( (0, 6, 0) ) act.transitions.add( (0, 0, 1) ) act.transitions.add( (1, 1, 2) ) act.transitions.add( (2, 7, 2) ) act.transitions.add( (2, 2, 3) ) act.transitions.add( (0, 3, 4) ) act.transitions.add( (4, 4, 5) ) act.transitions.add( (5, 5, 6) ) history = HistoryFA() history._automaton = act history.remove_epsilons() NFA = history.get_automaton(True) self.assertTrue(history._discover_closure_states(NFA) == [2])
def test_compute_equal(self): """compute_equal()""" # method compute_equal(other): # If is other type sym_kchar, then return True if are arguments # kchar same. abc = b_Sym_kchar("abc", ('a', 'b', 'c'), 0) efg = b_Sym_kchar("efg", ('e', 'f', 'g'), 1) self.assertTrue(abc.compute_equal(efg) == False) abc_2 = b_Sym_kchar("abc", (frozenset(['a']), frozenset(['b']), frozenset(['c'])), 2) self.assertTrue(abc.compute_equal(abc_2) == True) # If is other type sym_string, then return True if is # len(other.string) == len(self.kchar), all subsymbols kchar have # length one (len(self.kchar[i]) == 1) and value string is straight # value kchar. kchar_abc = b_Sym_kchar("abc", ('a', 'b', 'c'), 0) string_abc = b_Sym_string("abc", "abc", 1) string_abcde = b_Sym_string("abcde", "abcde", 2) self.assertTrue(kchar_abc.compute_equal(string_abc) == True) self.assertTrue(kchar_abc.compute_equal(string_abcde) == False) # If is other type sym_char, then return True if is # len(self.kchar) == 1 and len(self.kchar[0]) == 1 and their # arguments are same. kchar_a = b_Sym_kchar("kchar_a", ('a'), 0) a = b_Sym_char("a", 'a', 1) self.assertTrue(kchar_a.compute_equal(a) == True) b = b_Sym_char("b", 'b', 2) self.assertTrue(kchar_a.compute_equal(b) == False) # If is other type sym_char_class, then return True if is # len(self.kchar) == 1 and len(other.charClass) == len(self.kchar[0]) # and values of arguments are same. kchar_abc = b_Sym_kchar("kchar_[abc]", (frozenset(['a', 'b', 'c']),), 0) set_abc = b_Sym_char_class("[abc]", set(['a', 'b', 'c']), 1) self.assertTrue(kchar_abc.compute_equal(set_abc) == True) cd = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 2) self.assertTrue(kchar_abc.compute_equal(cd) == False)
def test_compute_equal(self): """compute_equal()""" # method compute_equal(other): # If other is object b_Sym_char class then return True, if arguments # char are same. sym_char = b_Sym_char('a', 'a', 1) other_sym_char = b_Sym_char('b', 'b', 2) self.assertTrue(sym_char.compute_equal(other_sym_char) == False) sym_char = b_Sym_char('a', 'a', 1) other_sym_char = b_Sym_char('a', 'a', 2) self.assertTrue(sym_char.compute_equal(other_sym_char) == True) # If other is class object b_Sym_char_class, return True, if # len(other.charClass) == 1 and values arguments char and charClass # are same. sym_char = b_Sym_char('a', 'a', 1) sym_char_class = b_Sym_char_class("ch2", set(['c']), 2) self.assertTrue(sym_char.compute_equal(sym_char_class) == False) sym_char = b_Sym_char('a', 'a', 1) sym_char_class = b_Sym_char_class("ch2", set(['a']), 2) self.assertTrue(sym_char.compute_equal(sym_char_class) == True)
def test_compute_equal(self): """compute_equal()""" # method compute_equal(other): # If other is object b_Sym_char class then return True, if arguments # char are same. sym_char = b_Sym_char('a', 'a', 1) other_sym_char = b_Sym_char('b', 'b', 2) self.assertTrue(sym_char.compute_equal(other_sym_char) == False) sym_char = b_Sym_char('a', 'a', 1) other_sym_char = b_Sym_char('a', 'a', 2) self.assertTrue(sym_char.compute_equal(other_sym_char) == True) # If other is class object b_Sym_char_class, return True, if # len(other.charClass) == 1 and values arguments char and charClass # are same. sym_char = b_Sym_char('a', 'a', 1) sym_char_class = b_Sym_char_class("ch2", set(['c']), 2) self.assertTrue(sym_char.compute_equal(sym_char_class) == False) sym_char = b_Sym_char('a', 'a', 1) sym_char_class = b_Sym_char_class("ch2", set(['a']), 2) self.assertTrue(sym_char.compute_equal(sym_char_class) == True)
def test_collision(self): """collision()""" # method collision(set_of_symbols): # Try with suitable objects class sym_char, sym_char_class, # sym_string. Check correct output (is / is not collision). sym_char = b_Sym_char('a', 'a', 1) other_sym_char = b_Sym_char('b', 'b', 2) sym_char_class = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 3) sym_string = b_Sym_string("adam", "adam", 4) set_of_symbols = set([other_sym_char, sym_char_class, sym_string]) self.assertTrue(sym_char.collision(set_of_symbols) == True) sym_string = b_Sym_string("eva", "eva", 4) set_of_symbols = set([other_sym_char, sym_char_class, sym_string]) self.assertTrue(sym_char.collision(set_of_symbols) == False)
def test_collision(self): """collision()""" # method collision(set_of_symbols): # Try with suitable objects class sym_char, sym_char_class, # sym_string. Check correct output (is / is not collision). sym_char = b_Sym_char('a', 'a', 1) other_sym_char = b_Sym_char('b', 'b', 2) sym_char_class = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 3) sym_string = b_Sym_string("adam", "adam", 4) set_of_symbols = set([other_sym_char, sym_char_class, sym_string]) self.assertTrue(sym_char.collision(set_of_symbols) == True) sym_string = b_Sym_string("eva", "eva", 4) set_of_symbols = set([other_sym_char, sym_char_class, sym_string]) self.assertTrue(sym_char.collision(set_of_symbols) == False)
def test_import_symbol(self): """import_symbol()""" # method import_symbol(text_repr, tid): # Check whether is from text_repr created and returned correct object # and having set self._id on tid and all parametrs are correct set. cd = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 0) cd.import_symbol("16566", 15) self.assertTrue(cd.charClass == set(['e', 'f'])) self.assertTrue(cd._text == "[ef]") self.assertTrue(cd._id == 15) # Check if is text_repr represented by other type, then is call # exception symbol_import_exception. try: cd.import_symbol("061", 17) self.assertTrue(False) except symbol_import_exception: self.assertTrue(True)
def test_collision(self): """collision()""" # method collision(set_of_symbols): # Try with suitable objects of class sym_kchar and check correct # result - is / is not collision. abc = b_Sym_kchar("abc", ('a', 'b', 'c'), 0) ac = b_Sym_char_class("ac", set(['a', 'c']), 1) b = b_Sym_char("b", 'b', 2) efg = b_Sym_kchar("efg", ('e', 'f', 'g'), 3) set_of_symbols = set([efg, ac, b]) self.assertTrue(abc.collision(set_of_symbols) == False) a = b_Sym_char("a", 'a', 4) set_of_symbols.add(a) self.assertTrue(abc.collision(set_of_symbols) == False) cba = b_Sym_kchar("cba", ('c', 'b', 'a'), 5) set_of_symbols.add(cba) self.assertTrue(abc.collision(set_of_symbols) == False) abc_2 = b_Sym_kchar("abc", ('a', 'b', 'c'), 6) set_of_symbols.add(abc_2) self.assertTrue(abc.collision(set_of_symbols) == True)
def test_collision(self): """collision()""" # method collision(set_of_symbols): # Try with suitable objects of class sym_kchar and check correct # result - is / is not collision. abc = b_Sym_kchar("abc", ('a', 'b', 'c'), 0) ac = b_Sym_char_class("ac", set(['a', 'c']), 1) b = b_Sym_char("b", 'b', 2) efg = b_Sym_kchar("efg", ('e', 'f', 'g'), 3) set_of_symbols = set([efg, ac, b]) self.assertTrue(abc.collision(set_of_symbols) == False) a = b_Sym_char("a", 'a', 4) set_of_symbols.add(a) self.assertTrue(abc.collision(set_of_symbols) == False) cba = b_Sym_kchar("cba", ('c', 'b', 'a'), 5) set_of_symbols.add(cba) self.assertTrue(abc.collision(set_of_symbols) == False) abc_2 = b_Sym_kchar("abc", ('a', 'b', 'c'), 6) set_of_symbols.add(abc_2) self.assertTrue(abc.collision(set_of_symbols) == True)
def test_compute_double_stride(self): """compute_double_stride()""" # Method compute_double_stride(compSymbol, reverse, last, local_chars) # Test with compSymbol type sym_char and sym_char_class. # If the reverse is True then change order self and compSymbol. # compSymbol type sym_char ; reverse = False a = b_Sym_char('a', 'a', 0) b = b_Sym_char('b', 'b', 1) local_chars = list() chars = set() for i in range(0,256): chars.add(chr(i)) local_chars.append(chars) new_kchar = a.compute_double_stride(b, False, 2, local_chars)[0] new_local_chars = a.compute_double_stride(b, False, 2, local_chars)[1] reference_kchar = b_Sym_kchar("ab", ('a','b'), 2) reference_kchar_2 = \ b_Sym_kchar("ab", (frozenset(['a']),frozenset(['b'])), 2) reference_kchar.last = 2 reference_kchar_2.last = 2 reference_local_chars = local_chars[0] - set([b.char]) self.assertTrue(new_kchar == reference_kchar or new_kchar == reference_kchar_2) self.assertTrue(new_local_chars[0] == reference_local_chars) self.assertTrue(new_kchar.last == 2) # compSymbol type sym_char_class ; reverse = False a = b_Sym_char('a', 'a', 0) bc = b_Sym_char_class("set(['b', 'c'])", set(['b', 'c']), 1) local_chars = list() chars = set() for i in range(0,256): chars.add(chr(i)) local_chars.append(chars) new_kchar = a.compute_double_stride(bc, False, 3, local_chars)[0] new_local_chars = a.compute_double_stride(bc, False, 3, local_chars)[1] reference_kchar = b_Sym_kchar("a[bc]", ('a',set(['b', 'c'])), 2) reference_kchar_2 = \ b_Sym_kchar("a[bc]", (frozenset(['a']),frozenset(['b','c'])), 2) reference_kchar.last = 3 reference_kchar_2.last = 3 reference_local_chars = local_chars[0] - bc.charClass self.assertTrue(new_kchar == reference_kchar or new_kchar == reference_kchar_2) self.assertTrue(new_local_chars[0] == reference_local_chars) self.assertTrue(new_kchar.last == 3) # compSymbol type sym_char ; reverse = True a = b_Sym_char('a', 'a', 0) b = b_Sym_char('b', 'b', 1) local_chars = list() chars = set() for i in range(0,256): chars.add(chr(i)) local_chars.append(chars) new_kchar = a.compute_double_stride(b, True, 2, local_chars)[0] new_local_chars = a.compute_double_stride(b, True, 2, local_chars)[1] reference_kchar = b_Sym_kchar("ba", ('b','a'), 2) reference_kchar_2 = \ b_Sym_kchar("ba", (frozenset(['b']),frozenset(['a'])), 2) reference_kchar.last = 2 reference_kchar_2.last = 2 reference_local_chars = local_chars[0] - set([a.char]) self.assertTrue(new_kchar == reference_kchar or new_kchar == reference_kchar_2) self.assertTrue(new_local_chars[0] == reference_local_chars) self.assertTrue(new_kchar.last == 2)
def test___str__(self): """__str__()""" # Check return self.charClass cd = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 0) self.assertTrue(cd.__str__() == str(cd.charClass))
def test___hash__(self): """__hash__()""" # Check return hash(frozenset(self.charClass)). ef = b_Sym_char_class("set(['e', 'f'])", set(['e', 'f']), 1) self.assertTrue(ef.__hash__() == hash(frozenset(ef.charClass)))
def test_compute(self): """compute()""" # 1. /^abc/ - automaton does not change, PHF table is created nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) result = copy.deepcopy(nfaData) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), len(result.states)) self.assertEqual(len(cp.alphabet), len(result.alphabet)) self.assertEqual(len(cp.transitions), len(result.transitions)) self.assertEqual(len(cp.final), len(result.final)) self.assertNotEqual(aut.trans_table, None) self.assertTrue(aut.get_compute()) # 2. determinization of /^ab|ac/, PHF table is created nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set([0])) nfaData.states[3] = b_State(3,set()) nfaData.states[4] = b_State(4,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (0,0,3) ) nfaData.transitions.add( (3,2,4) ) nfaData.final.add(2) nfaData.final.add(4) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), 3) self.assertEqual(len(cp.alphabet), 3) self.assertEqual(len(cp.transitions), 3) self.assertEqual(len(cp.final), 1) self.assertNotEqual(aut.trans_table, None) self.assertTrue(aut.get_compute()) # 3. resolve alphabet - /^[a-c][b-d]/, PHF table is created nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set([0])) nfaData.alphabet[0] = b_Sym_char_class("ch0", set(['a', 'b', 'c']), 0) nfaData.alphabet[1] = b_Sym_char_class("ch1", set(['b', 'c', 'd']), 1) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.final.add(2) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), 3) self.assertEqual(len(cp.alphabet), 3) self.assertEqual(len(cp.transitions), 4) self.assertEqual(len(cp.final), 1) self.assertNotEqual(aut.trans_table, None) self.assertTrue(aut.get_compute()) # 4. /abc/ and enable_fallback_state - some transitions are removed nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (0,1,0) ) nfaData.transitions.add( (0,2,0) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (1,0,1) ) nfaData.transitions.add( (1,2,0) ) nfaData.transitions.add( (2,2,3) ) nfaData.transitions.add( (2,0,1) ) nfaData.transitions.add( (2,1,0) ) nfaData.transitions.add( (3,0,3) ) nfaData.transitions.add( (3,1,3) ) nfaData.transitions.add( (3,2,3) ) nfaData.final.add(3) result = copy.deepcopy(nfaData) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.enable_fallback_state(warning=False) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), len(result.states)) self.assertEqual(len(cp.alphabet), len(result.alphabet)) self.assertTrue(len(cp.transitions) < len(result.transitions)) self.assertEqual(len(cp.final), len(result.final)) self.assertNotEqual(aut.trans_table, None) self.assertTrue(aut.get_compute())
def test___repr__(self): """__repr__()""" # Check return self.charClass. ef = b_Sym_char_class("set(['e', 'f'])", set(['e', 'f']), 1) self.assertTrue(ef.__repr__() == repr(ef.charClass))
def test_get_support_type(self): """get_support_type()""" # Check return [b_symbol.io_mapper["b_Sym_char_class"]]. ef = b_Sym_char_class("set(['e', 'f'])", set(['e', 'f']), 1) self.assertTrue(ef.get_support_type() == [io_mapper["b_Sym_char_class"]])
def _replace_length_restriction_with_a_closure(self, NFA): """ The first step in this construction replaces the length restriction with a closure, and constructs the H-FA, with the closure represented by a flag in the history buffer. :param NFA: NFA :type NFA: nfa_data :returns: NFA without counting constraint :rtype: nfa_data """ # identify counting transitions with exactly X counting cnt_transitions = list() for t in NFA.transitions: if NFA.alphabet[t[1]].ctype == io_mapper["b_Sym_cnt_constr"]: if NFA.alphabet[t[1]].m == NFA.alphabet[t[1]].n: cnt_transitions.append(t) # remove founded counting transtions # and replace them with loop transitions # and add epsilon tran. to next state for t in cnt_transitions: NFA.transitions.remove(t) cnt_symbol = NFA.alphabet[t[1]] self.flags_cnt[t[0]] = str(cnt_symbol.m) NFA.transitions.add( (t[0], t[1], t[0]) ) NFA.transitions.add( (t[0], -1, t[2]) ) # replace cnt symbol at char or char class # and add epsilon symbol into alphabet if does not exist for t in cnt_transitions: symbolID = t[1] cnt_symbol = copy.deepcopy(NFA.alphabet[symbolID]) if cnt_symbol.ctype == io_mapper["b_Sym_cnt_constr"]: if isinstance(cnt_symbol.symbol, str): NFA.alphabet[symbolID] = b_Sym_char_class( new_text = cnt_symbol._text, charClass = set([cnt_symbol.symbol]), new_id = cnt_symbol._id) else : NFA.alphabet[symbolID] = b_Sym_char_class( new_text = cnt_symbol._text, charClass = cnt_symbol.symbol, new_id = cnt_symbol._id) epsilonID = -1 if not epsilonID in NFA.alphabet: NFA.alphabet[epsilonID] = b_Sym_char("Epsilon", "", -1) # remove epsilons aut = b_Automaton() aut._automaton = NFA aut.remove_epsilons() return aut._automaton
def test_compute_double_stride(self): """compute_double_stride()""" # Method compute_double_stride(compSymbol, reverse, last, local_chars) # Test with compSymbol type sym_char and sym_char_class. # If the reverse is True then change order self and compSymbol. # compSymbol type sym_char ; reverse = False a = b_Sym_char('a', 'a', 0) b = b_Sym_char('b', 'b', 1) local_chars = list() chars = set() for i in range(0, 256): chars.add(chr(i)) local_chars.append(chars) new_kchar = a.compute_double_stride(b, False, 2, local_chars)[0] new_local_chars = a.compute_double_stride(b, False, 2, local_chars)[1] reference_kchar = b_Sym_kchar("ab", ('a', 'b'), 2) reference_kchar_2 = \ b_Sym_kchar("ab", (frozenset(['a']),frozenset(['b'])), 2) reference_kchar.last = 2 reference_kchar_2.last = 2 reference_local_chars = local_chars[0] - set([b.char]) self.assertTrue(new_kchar == reference_kchar or new_kchar == reference_kchar_2) self.assertTrue(new_local_chars[0] == reference_local_chars) self.assertTrue(new_kchar.last == 2) # compSymbol type sym_char_class ; reverse = False a = b_Sym_char('a', 'a', 0) bc = b_Sym_char_class("set(['b', 'c'])", set(['b', 'c']), 1) local_chars = list() chars = set() for i in range(0, 256): chars.add(chr(i)) local_chars.append(chars) new_kchar = a.compute_double_stride(bc, False, 3, local_chars)[0] new_local_chars = a.compute_double_stride(bc, False, 3, local_chars)[1] reference_kchar = b_Sym_kchar("a[bc]", ('a', set(['b', 'c'])), 2) reference_kchar_2 = \ b_Sym_kchar("a[bc]", (frozenset(['a']),frozenset(['b','c'])), 2) reference_kchar.last = 3 reference_kchar_2.last = 3 reference_local_chars = local_chars[0] - bc.charClass self.assertTrue(new_kchar == reference_kchar or new_kchar == reference_kchar_2) self.assertTrue(new_local_chars[0] == reference_local_chars) self.assertTrue(new_kchar.last == 3) # compSymbol type sym_char ; reverse = True a = b_Sym_char('a', 'a', 0) b = b_Sym_char('b', 'b', 1) local_chars = list() chars = set() for i in range(0, 256): chars.add(chr(i)) local_chars.append(chars) new_kchar = a.compute_double_stride(b, True, 2, local_chars)[0] new_local_chars = a.compute_double_stride(b, True, 2, local_chars)[1] reference_kchar = b_Sym_kchar("ba", ('b', 'a'), 2) reference_kchar_2 = \ b_Sym_kchar("ba", (frozenset(['b']),frozenset(['a'])), 2) reference_kchar.last = 2 reference_kchar_2.last = 2 reference_local_chars = local_chars[0] - set([a.char]) self.assertTrue(new_kchar == reference_kchar or new_kchar == reference_kchar_2) self.assertTrue(new_local_chars[0] == reference_local_chars) self.assertTrue(new_kchar.last == 2)
def test_export_symbol(self): """export_symbol()""" # Check return correct representation of symbol. cd = b_Sym_char_class("set(['c', 'd'])", set(['c', 'd']), 0) self.assertTrue(cd.export_symbol() == "16364")