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 get_hfa(ruleset): """ Generate number of states, transitions and consumed memory for \ History FA. """ # Create parser - use default parser po = parser.parser() # Parse input file po.load_file(ruleset) # Create History FA object history = HistoryFA() # Make automaton from RE which was in input file history.create_by_parser(po) # Remove epsilons history.remove_epsilons() # Store the NFA NFA = history.get_automaton(True) # Determinise the automaton history.determinise(create_table = True) # Create History FA history.compute(NFA) # Return experimental results return ["History FA", history.get_state_num(), history.get_trans_num(), history.report_memory_optimal(), history.report_memory_naive()]
def get_hfa(ruleset): """ Generate number of states, transitions and consumed memory for \ History FA. """ # Create parser - use default parser po = parser.parser() # Parse input file po.load_file(ruleset) # Create History FA object history = HistoryFA() # Make automaton from RE which was in input file history.create_by_parser(po) # Remove epsilons history.remove_epsilons() # Store the NFA NFA = history.get_automaton(True) # Determinise the automaton history.determinise(create_table=True) # Create History FA history.compute(NFA) # Return experimental results return [ "History FA", history.get_state_num(), history.get_trans_num(), history.report_memory_optimal(), history.report_memory_naive() ]
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_report_memory_naive(self): """report_memory_naive()""" # /ab[^1234]*cd|efg/; test with an expression containing one # alternation [^1234]*, the second is not par = parser("pcre_parser") par.set_text("/ab[^1234]*cd|efg/") history = HistoryFA() history.create_by_parser(par) history.remove_epsilons() NFA = history.get_automaton(True) history.determinise(create_table = True) history.compute(NFA) self.assertTrue(history.report_memory_naive() == 187)
def test_search(self): """search()""" # For RE upstairs do tests for this method. # /abcd/; test with an expression that does not use properties # of HistoryFA par = parser("pcre_parser") par.set_text("/abcd/") history = HistoryFA() history.create_by_parser(par) history.remove_epsilons() NFA = history.get_automaton(True) history.determinise(create_table = True) history.compute(NFA) self.assertTrue(history.search("efg") == [0]) self.assertTrue(history.search("efabcg") == [0]) self.assertTrue(history.search("efabcd") == [1]) self.assertTrue(history.search("abcefabcdabcx") == [1]) self.assertTrue(history.search("abcd") == [1]) self.assertTrue(history.search("123abcd456") == [1]) self.assertTrue(history.search("123ab0cd456") == [0]) # /ab.*cd /; test with an expression that contains .* par = parser("pcre_parser") par.set_text("/ab.*cd/") history = HistoryFA() history.create_by_parser(par) history.remove_epsilons() NFA = history.get_automaton(True) history.determinise(create_table = True) history.compute(NFA) self.assertTrue(history.search("efg") == [0]) self.assertTrue(history.search("abnecoefg") == [0]) self.assertTrue(history.search("abnecocx") == [0]) self.assertTrue(history.search("necoabnecocdneco") == [1]) self.assertTrue(history.search("abcd") == [1]) self.assertTrue(history.search("123abcd456") == [1]) self.assertTrue(history.search("123ab0cd456") == [1]) # /ab[^1234]*cd|efg/; test with an expression containing one # alternation [^1234]*, the second is not par = parser("pcre_parser") par.set_text("/ab[^1234]*cd|efg/") history = HistoryFA() history.create_by_parser(par) history.remove_epsilons() NFA = history.get_automaton(True) history.determinise(create_table = True) history.compute(NFA) self.assertTrue(history.search("exfg") == [0]) self.assertTrue(history.search("abnecocx") == [0]) self.assertTrue(history.search("efg") == [1]) self.assertTrue(history.search("textefgtext") == [1]) self.assertTrue(history.search("abnecoefg") == [1]) self.assertTrue(history.search("necoabnecocdneco") == [1]) self.assertTrue(history.search("abcd") == [1]) self.assertTrue(history.search("123abcd456") == [1]) self.assertTrue(history.search("123ab0cd456") == [1]) self.assertTrue(history.search("ab1cd") == [0]) self.assertTrue(history.search("ab4cd") == [0]) self.assertTrue(history.search("abcefg") == [1]) self.assertTrue(history.search("123abblekekcd456") == [1]) self.assertTrue(history.search("123ab0cd456") == [1])
def test_compute(self): """compute()""" # Check the correctness of the logical machine output over # self.assertTrue on individual items + focus on the properties # of HistoryFA (transitions, flags, counters) # /abcd/; test with an expression that does not use properties # of HistoryFA par = parser("pcre_parser") par.set_text("/abcd/") history = HistoryFA() history.create_by_parser(par) history.remove_epsilons() NFA = history.get_automaton(True) history.determinise(create_table = True) history.compute(NFA) copy = history.get_automaton() result = nfa_data().load_from_file(aux_func.getPatternMatchDir() + "/algorithms/history_fa/test_data/test_data_1.nfa_data") self.assertTrue(sorted(copy.states.keys()) == sorted(result.states.keys())) self.assertTrue(copy.alphabet == result.alphabet) self.assertTrue(copy.start == result.start) self.assertTrue(copy.final == result.final) self.assertTrue(copy.transitions == result.transitions) # /ab.*cd /; test with an expression that contains .* par = parser("pcre_parser") par.set_text("/ab.*cd/") history = HistoryFA() history.create_by_parser(par) history.remove_epsilons() NFA = history.get_automaton(True) history.determinise(create_table = True) history.compute(NFA) copy = history.get_automaton() result = nfa_data().load_from_file(aux_func.getPatternMatchDir() + "/algorithms/history_fa/test_data/test_data_2.nfa_data") self.assertTrue(sorted(copy.states.keys()) == sorted(result.states.keys())) self.assertTrue(copy.alphabet == result.alphabet) self.assertTrue(copy.start == result.start) self.assertTrue(copy.final == result.final) self.assertTrue(copy.transitions == result.transitions) self.assertTrue(copy.Flags == result.Flags) # /ab[^1234]*cd|efg/; test with an expression containing one # alternation [^1234]*, the second is not par = parser("pcre_parser") par.set_text("/ab[^1234]*cd|efg/") history = HistoryFA() history.create_by_parser(par) history.remove_epsilons() NFA = history.get_automaton(True) history.determinise(create_table = True) history.compute(NFA) copy = history.get_automaton() result = nfa_data().load_from_file(aux_func.getPatternMatchDir() + "/algorithms/history_fa/test_data/test_data_3.nfa_data") self.assertTrue(sorted(copy.states.keys()) == sorted(result.states.keys())) self.assertTrue(copy.alphabet == result.alphabet) self.assertTrue(copy.start == result.start) self.assertTrue(copy.final == result.final) self.assertTrue(copy.transitions == result.transitions) self.assertTrue(copy.Flags == result.Flags)