def test_export_symbol(self): """export_symbol()""" # Check return correct representation of symbol. sym_char = b_Sym_char('a', 'a', 1) self.assertTrue(sym_char.export_symbol() == "061") sym_char = b_Sym_char('á', 'á', 2) self.assertTrue(sym_char.export_symbol() == "0e1")
def test_accept(self): """accept()""" # method accept(text): # Check return text if self.char == "" sym_char = b_Sym_char('', '', 1) self.assertTrue(sym_char.accept("some_text") == "some_text") # Check if len(text) == 0, # then is call exception symbol_string_to_short sym_char = b_Sym_char('a', 'a', 1) try: sym_char.accept("") self.assertTrue(False) except symbol_string_to_short: self.assertTrue(True) # Check if text[0] == self.char[0], then is return value text[1:] sym_char = b_Sym_char('a', 'a', 1) self.assertTrue(sym_char.accept("adam") == "dam") # In case text[0] != self.char[0], # then is call exception symbol_accept_exception sym_char = b_Sym_char('a', 'a', 1) try: sym_char.accept("eva") self.assertTrue(False) except symbol_accept_exception: self.assertTrue(True)
def test_get_trans_num(self): """get_trans_num()""" # Simple regression test for small automaton. 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) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() self.assertEqual(aut.get_trans_num(), 3) # Test after removing fallback transitions aut.enable_fallback_state(1, warning=False) aut.remove_fallback_transitions() self.assertEqual(aut.get_trans_num(), 2)
def test___ne__(self): """__ne__()""" # Test using the operator != # Test all code branches. # - Check the situation when compared to able to solve the self. # - Check the situation when compared to able to solve other. # - Check the situation when the comparison is not able to solve # even the self or other - check thrown symbol_equality_exception. # - Check a situation where the self or the Other is of a # different type than the inherit from b_symbol -> returns True. symbol = b_Sym_char("symbol", 'a', 1) comp_symbol = b_Sym_char("comp_symbol", 'b', 2) self.assertTrue(symbol != comp_symbol) self.assertTrue(comp_symbol != symbol) symbol.ctype = '4' comp_symbol.ctype = '4' try: symbol != comp_symbol self.assertTrue(False) except symbol_equality_exception: self.assertTrue(True) symbol = b_Sym_char("symbol", 'a', 1) state = b_State(0, set([])) self.assertTrue(symbol != state)
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_compute2(self): delay_dfa = DELAY_DFA() parser = pcre_parser() parser.set_text("/^(a|b)+/") delay_dfa.create_by_parser(parser) delay_dfa.compute() self.assertTrue(delay_dfa.get_compute()) a = delay_dfa.get_automaton() b = nfa_data() b.add_symbols(b_Sym_char("a","a",0)) b.add_symbols(b_Sym_char("b","b",1)) b.add_symbols(DEF_SYMBOLS("default", 2)) b.add_states(b_State(0,set())) b.add_states(b_State(1,set([0]))) b.start = 0 b.final = set([1]) b.add_transitions( (0,0,1) ) b.add_transitions( (0,1,1) ) b.add_transitions( (1,2,0) ) self.assertEqual(a.states.keys(), b.states.keys()) self.assertEqual(a.start, b.start) self.assertEqual(a.final, b.final) self.assertEqual(a.alphabet, b.alphabet) self.assertEqual(a.transitions, b.transitions) self.assertTrue(a.Flags["Delay DFA"])
def test_report_memory_naive(self): """report_memory_naive()""" # Simple regression test for small automaton. 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) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() self.assertEqual(aut.report_memory_naive(), 12) # Test after removing fallback transitions. report_memory_naive depends # on number of states and symbols, not transitions, so nothing changes aut.enable_fallback_state(1, warning=False) aut.remove_fallback_transitions() self.assertEqual(aut.report_memory_naive(), 12) # Manually remove symbol and state from _automaton1 del aut._automaton1.states[2] del aut._automaton1.alphabet[2] self.assertEqual(aut.report_memory_naive(), 6)
def test_get_alpha_num(self): """get_alpha_num()""" # Simple regression test for small automaton. 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) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() self.assertEqual(aut.get_alpha_num(), 3) # Manually remove symbol from _automaton1 del aut._automaton1.alphabet[2] self.assertEqual(aut.get_alpha_num(), 2)
def test_disable_fallback_state(self): """disable_fallback_state()""" # Test if the variables _compute, fallback and fallback_state were set # to the default values. 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) 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() aut.disable_fallback_state() self.assertFalse(aut.get_compute()) self.assertFalse(aut.fallback) self.assertEqual(aut.fallback_state, -1)
def _test_compute2(self): delay_dfa = DELAY_DFA() parser = pcre_parser() parser.set_text("/^(a|b)+/") delay_dfa.create_by_parser(parser) delay_dfa.compute() self.assertTrue(delay_dfa.get_compute()) a = delay_dfa.get_automaton() b = nfa_data() b.add_symbols(b_Sym_char("a", "a", 0)) b.add_symbols(b_Sym_char("b", "b", 1)) b.add_symbols(DEF_SYMBOLS("default", 2)) b.add_states(b_State(0, set())) b.add_states(b_State(1, set([0]))) b.start = 0 b.final = set([1]) b.add_transitions((0, 0, 1)) b.add_transitions((0, 1, 1)) b.add_transitions((1, 2, 0)) self.assertEqual(a.states.keys(), b.states.keys()) self.assertEqual(a.start, b.start) self.assertEqual(a.final, b.final) self.assertEqual(a.alphabet, b.alphabet) self.assertEqual(a.transitions, b.transitions) self.assertTrue(a.Flags["Delay DFA"])
def test_enable_fallback_state(self): """enable_fallback_state()""" # Test if fallback and fallback_state is set accordingly, _compute is # set to False and warning is/is not printed on stdout depending on # value of parameter warning. 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) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() # redirect stdout to file tmp = sys.stdout f = open("stdout.output", 'w') sys.stdout = f aut.enable_fallback_state(2, warning=False) f.close() e = open("stdout.output", 'r') line = e.readline() # warning was set to False, stdout should be empty self.assertFalse(line) # check if the fallback_state was set self.assertEqual(aut.fallback_state, 2) self.assertFalse(aut.get_compute()) self.assertTrue(aut.fallback) f = open("stdout.output", 'w') sys.stdout = f aut.enable_fallback_state() f.close() e = open("stdout.output", 'r') line = e.readline() # warning should be printed by default self.assertTrue(line) # check if the fallback_state was chosen correctly self.assertEqual(aut.fallback_state, 1) self.assertFalse(aut.get_compute()) self.assertTrue(aut.fallback) # restore sys.stdout sys.stdout = tmp os.remove("stdout.output")
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_validate_transition(self): """validate_transition()""" # Test correct transition validation for both faulty and non-faulty # transition table. 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) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() for t in aut._automaton1.transitions: # all transitions must be valid self.assertTrue(aut.validate_transition(aut._transition_rep(t))) # some nonexistent transitions -> invalid t = (0,2,0) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (1,0,2) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (len(aut._automaton1.states), len(aut._automaton1.alphabet), 0) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (0, len(aut._automaton1.alphabet), 0) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (len(aut._automaton1.states), 0, 0) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) # faulty transitions aut.enable_faulty_transitions(32) aut.compute() for t in aut._automaton1.transitions: # all transitions must be valid self.assertTrue(aut.validate_transition(aut._transition_rep(t))) # some nonexistent transitions -> invalid, collisions are improbable t = (0,2,0) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (1,0,2) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (10,10,1) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (11,11,1) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (12,12,1) self.assertFalse(aut.validate_transition(aut._transition_rep(t)))
def test_collision(self): """collision()""" # The method of collision(set_of_symbols): # Returns True only if one object from the set_of_symbols is # object of class DEF_SYMBOLS. In another case, always returns False. def_symbol_1 = DEF_SYMBOLS("default_1", 3) def_symbol_2 = DEF_SYMBOLS("default_2", 4) char_symbol_1 = b_Sym_char("a", "a", 1) char_symbol_2 = b_Sym_char("b", "b", 2) set_of_symbols = set([char_symbol_1, char_symbol_2]) self.assertTrue(def_symbol_1.collision(set_of_symbols) == False) set_of_symbols = set([char_symbol_1, char_symbol_2, def_symbol_2]) self.assertTrue(def_symbol_1.collision(set_of_symbols) == 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_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. sym_char = b_Sym_char('b', 'b', 1) self.assertTrue(sym_char.char == 'b') self.assertTrue(sym_char._text == 'b') self.assertTrue(sym_char._id == 1) sym_char.import_symbol("061", 15) self.assertTrue(sym_char.char == "a") self.assertTrue(sym_char._text == 'a') self.assertTrue(sym_char._id == 15) sym_char.import_symbol("0e1", 16) self.assertTrue(sym_char.char == 'á') self.assertTrue(sym_char._text == 'á') self.assertTrue(sym_char._id == 16) # Check if is text_repr represented by other type, then is call # exception symbol_import_exception. try: sym_char.import_symbol("161", 17) self.assertTrue(False) except symbol_import_exception: self.assertTrue(True)
def test__str__(self): """__str__()""" # Check return self.char. # For test use call str(object). sym_char = b_Sym_char('b', 'b', 1) self.assertTrue(sym_char.__str__() == sym_char.char) self.assertTrue(sym_char.__str__() == "b")
def test_is_empty(self): """is_empty()""" # Check return True if len(self.char) == 0 and self._id != -1, # otherwise return False. a = b_Sym_char('a', 'a', 1) self.assertTrue(a.is_empty() == False) epsilon = b_Sym_char('epsilon', '', -1) self.assertTrue(epsilon.is_empty() == False) a = b_Sym_char('a', 'a', -1) self.assertTrue(a.is_empty() == False) empty = b_Sym_char('empty', '', 15) self.assertTrue(empty.is_empty() == True)
def test_get_support_type(self): """get_support_type()""" # Check return [b_symbol.io_mapper["b_Sym_char"], # b_symbol.io_mapper["b_Sym_char_class"]] sym_char = b_Sym_char('a', 'a', 1) self.assertTrue(sym_char.get_support_type() == [io_mapper["b_Sym_char"], io_mapper["b_Sym_char_class"]])
def test_get_support_type(self): """get_support_type()""" # Check return [b_symbol.io_mapper["b_Sym_char"], # b_symbol.io_mapper["b_Sym_char_class"]] sym_char = b_Sym_char('a', 'a', 1) self.assertTrue(sym_char.get_support_type( ) == [io_mapper["b_Sym_char"], io_mapper["b_Sym_char_class"]])
def _test_compute3(self): # Get test directory tdir = aux_func.getPatternMatchDir() + "/algorithms/delay_dfa/" delay_dfa = DELAY_DFA() nfaData = nfa_data().load_from_file(tdir + "test_data/text_ddfa.nfa_data") delay_dfa.create_from_nfa_data(nfaData) delay_dfa.determinise() delay_dfa.compute(False) self.assertTrue(delay_dfa.get_compute()) a = delay_dfa.get_automaton() b = nfa_data() b.add_symbols(b_Sym_char("a", "a", 0)) b.add_symbols(b_Sym_char("b", "b", 1)) b.add_symbols(b_Sym_char("c", "c", 2)) b.add_symbols(b_Sym_char("d", "d", 3)) b.add_symbols(DEF_SYMBOLS("default", 4)) b.add_states(b_State(0, set())) b.add_states(b_State(1, set([0]))) b.add_states(b_State(2, set())) b.add_states(b_State(3, set([0]))) b.add_states(b_State(4, set([0]))) b.start = 0 b.final = set([1, 3, 4]) b.add_transitions((0, 2, 0)) b.add_transitions((0, 0, 1)) b.add_transitions((0, 1, 2)) b.add_transitions((0, 3, 3)) b.add_transitions((1, 4, 0)) b.add_transitions((2, 2, 4)) b.add_transitions((2, 4, 0)) b.add_transitions((3, 4, 0)) b.add_transitions((4, 4, 0)) self.assertEqual(a.states.keys(), b.states.keys()) self.assertEqual(a.start, b.start) self.assertEqual(a.final, b.final) self.assertEqual(a.alphabet, b.alphabet) self.assertEqual(a.transitions, b.transitions) self.assertTrue(a.Flags["Delay DFA"])
def _test_compute3(self): # Get test directory tdir = aux_func.getPatternMatchDir() + "/algorithms/delay_dfa/" delay_dfa = DELAY_DFA() nfaData = nfa_data().load_from_file(tdir + "test_data/text_ddfa.nfa_data") delay_dfa.create_from_nfa_data(nfaData) delay_dfa.determinise() delay_dfa.compute(False) self.assertTrue(delay_dfa.get_compute()) a = delay_dfa.get_automaton() b = nfa_data() b.add_symbols(b_Sym_char("a","a",0)) b.add_symbols(b_Sym_char("b","b",1)) b.add_symbols(b_Sym_char("c","c",2)) b.add_symbols(b_Sym_char("d","d",3)) b.add_symbols(DEF_SYMBOLS("default", 4)) b.add_states(b_State(0,set())) b.add_states(b_State(1,set([0]))) b.add_states(b_State(2,set())) b.add_states(b_State(3,set([0]))) b.add_states(b_State(4,set([0]))) b.start = 0 b.final = set([1,3,4]) b.add_transitions( (0,2,0) ) b.add_transitions( (0,0,1) ) b.add_transitions( (0,1,2) ) b.add_transitions( (0,3,3) ) b.add_transitions( (1,4,0) ) b.add_transitions( (2,2,4) ) b.add_transitions( (2,4,0) ) b.add_transitions( (3,4,0) ) b.add_transitions( (4,4,0) ) self.assertEqual(a.states.keys(), b.states.keys()) self.assertEqual(a.start, b.start) self.assertEqual(a.final, b.final) self.assertEqual(a.alphabet, b.alphabet) self.assertEqual(a.transitions, b.transitions) self.assertTrue(a.Flags["Delay DFA"])
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_report_memory_real(self): """report_memory_real()""" # Few simple regression tests for different sizes of PHF table, state # and symbol representations and faulty transitions. 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) aut = PHF_DFA() a = bdz() a.set_limit(8) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() self.assertEqual(aut.report_memory_real(), 120) aut.set_table_parameters((4,6)) self.assertEqual(aut.report_memory_real(), 48) aut.set_table_parameters((4,7)) self.assertEqual(aut.report_memory_real(), 72) a.set_limit(5) aut.set_PHF_class(a) aut.compute() self.assertEqual(aut.report_memory_real(), 45) aut.enable_faulty_transitions(10) self.assertEqual(aut.report_memory_real(), 30) aut.enable_faulty_transitions(19) self.assertEqual(aut.report_memory_real(), 60)
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()""" # compute_equal(other): # Return True if other is object of DEF_SYMBOLS class, otherwise # return False. def_symbol = DEF_SYMBOLS("default", 3) char_symbol = b_Sym_char("a", "a", 1) self.assertTrue(def_symbol.compute_equal(char_symbol) == False) other_def_symbol = DEF_SYMBOLS("default", 4) self.assertTrue(def_symbol.compute_equal(other_def_symbol) == 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_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_collision(self): """compute_collision()""" # Method compute_collision(other): # Check the correct outputs. # Self can be in a collision only if other is class object DEF_SYMBOLS def_symbol = DEF_SYMBOLS("default", 3) char_symbol = b_Sym_char("a", "a", 1) self.assertTrue(def_symbol.compute_collision(char_symbol) == (set([def_symbol]), set(), set([char_symbol]))) other_def_symbol = DEF_SYMBOLS("default", 4) self.assertTrue(def_symbol.compute_collision(other_def_symbol) == (set(), set([other_def_symbol]), set()))
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 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_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_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()""" # Method compute_collision(other): # Check the correct outputs. # Self can be in a collision only if other is class object DEF_SYMBOLS def_symbol = DEF_SYMBOLS("default", 3) char_symbol = b_Sym_char("a", "a", 1) self.assertTrue( def_symbol.compute_collision(char_symbol) == (set([def_symbol]), set(), set([char_symbol]))) other_def_symbol = DEF_SYMBOLS("default", 4) self.assertTrue( def_symbol.compute_collision(other_def_symbol) == ( set(), set([other_def_symbol]), set()))
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))