def test_nfa_union_djsoint(self): """ Tests a nfa union between NFAs with no state in common """ union = NFA.nfa_union(self.nfa_union_1_test_01, self.nfa_union_2_test_01) # automata_IO.nfa_to_dot(union, 'nfa_union') self.assertDictEqual(union, self.nfa_union_test_01_solution)
def test_nfa_union_intersecting(self): """ Tests a nfa union between NFAs with some state in common """ union = NFA.nfa_union(self.nfa_union_2_test_01, self.nfa_union_3_test_01) # automata_IO.nfa_to_dot(union, 'nfa_union_intersecting') self.assertDictEqual(union, self.nfa_union_test_02_solution)
def read_determinize_minimized_write(path, model): nfa = automata_IO.nfa_dot_importer(path + model) dfa = NFA.nfa_determinization(nfa) automata_IO.dfa_to_dot(dfa, model + '_det', path) new_dfa = DFA.dfa_minimization(dfa) automata_IO.dfa_to_dot(new_dfa, model + "_det_min", path)
def is_empty(self): if self.special_attr == 'true': return False elif self.special_attr == 'false': return True else: return not NFA.nfa_nonemptiness_check(self.aut)
def test_nfa_complementation(self): """ Tests a correct nfa complementation """ dfa_complemented = NFA.nfa_complementation( self.nfa_complementation_test_01) self.assertEqual(len(dfa_complemented['alphabet']), 2) self.assertEqual(len(dfa_complemented['states']), 10 + 1) self.assertEqual(len(dfa_complemented['accepting_states']), 4 + 1) self.assertEqual(len(dfa_complemented['transitions']), 22)
def disjunction(self, other): if self.special_attr == 'true' or other.special_attr == 'true': return self elif self.special_attr == 'false' or other.special_attr == 'false': return other else: aut_l, aut_r, new_var_map = self.augment_vars(other) return FiniteAutomaton(NFA.nfa_union(aut_l, aut_r), new_var_map)
def test_nfa_determinization_bis(self): """ Tests an other correct nfa determinization """ dfa_determined = NFA.nfa_determinization( self.nfa_determinization_test_02) # automata_IO.dfa_to_dot(dfa_determined, 'nfa_determined_2') self.assertEqual(len(dfa_determined['alphabet']), 3) self.assertEqual(len(dfa_determined['states']), 14) self.assertEqual(len(dfa_determined['accepting_states']), 11) self.assertEqual(len(dfa_determined['transitions']), 39)
def test_nfa_determinization(self): """ Tests a correct nfa determinization """ dfa_determined = NFA.nfa_determinization( self.nfa_determinization_test_01) # automata_IO.dfa_to_dot(dfa_determined, 'nfa_determined') self.assertEqual(len(dfa_determined['alphabet']), 2) self.assertEqual(len(dfa_determined['states']), 10) self.assertEqual(len(dfa_determined['accepting_states']), 6) self.assertEqual(len(dfa_determined['transitions']), 19)
def afw_nonemptiness_check(afw: dict) -> bool: """ Checks if the input AFW reads any language other than the empty one, returning True/False. The afw is translated into a nfa and then its nonemptiness is checked. :param dict afw: input AFW. :return: *(bool)*, True if input afw is nonempty, False otherwise. """ nfa = afw_to_nfa_conversion(afw) return NFA.nfa_nonemptiness_check(nfa)
def afw_nonuniversality_check(afw: dict) -> bool: """ Checks if the language read by the input AFW is different from Σ∗, returning True/False. The afw is translated into a nfa and then its nonuniversality is checked. :param dict afw: input AFW. :return: *(bool)*, True if input afw is nonuniversal, False otherwise. """ nfa = afw_to_nfa_conversion(afw) return NFA.nfa_nonuniversality_check(nfa)
def test_nfa_complementation_empty_states(self): """ Tests a NFA complementation with an empty NFA """ dfa_complemented = NFA.nfa_complementation( self.nfa_complementation_test_empty) # automata_IO.dfa_to_dot(dfa_complemented, # 'nfa_complemented_empty_States') self.assertDictEqual( dfa_complemented, { 'alphabet': set(), 'states': {'sink'}, 'initial_state': None, 'accepting_states': {'sink'}, 'transitions': {} })
def test_nfa_determinization_empty_transitions(self): """ Tests a NFA determinization with a NFA without transitions """ self.nfa_determinization_test_01['transitions'] = {} dfa_determined = NFA.nfa_determinization( self.nfa_determinization_test_01) # automata_IO.dfa_to_dot(dfa_determined, # 'nfa_determined_empty_transition') self.assertDictEqual( dfa_determined, { 'alphabet': self.nfa_determinization_test_01['alphabet'], 'states': {str(self.nfa_determinization_test_01['initial_states'])}, 'initial_state': str(self.nfa_determinization_test_01['initial_states']), 'accepting_states': set(), 'transitions': {} })
def complement(self): if self.special_attr == 'true': return FiniteAutomaton.false_aut() elif self.special_attr == 'false': return FiniteAutomaton.true_aut() else: # print('PERFORMING COMPLEMENT') # print(self.relabel_states().to_str()) new_aut = NFA.nfa_complementation(self.aut) # Convert to an NFA new_aut['initial_states'] = {new_aut.pop('initial_state')} new_aut['transitions'] = { k: [v] for k, v in new_aut.pop('transitions').items() } res = FiniteAutomaton(new_aut, self.var_map) # print(res.relabel_states().to_str()) return res
def test_afw_to_nfa_conversion_language_bis_bis(self): """ Test a correct afw conversion to nfa comparing the language read by the two automaton """ nfa_01 = AFW.afw_to_nfa_conversion(self.afw_nonemptiness_check_test_2) # automata_IO.nfa_to_dot(nfa_01, 'afw_to_nfa_strange') i = 0 last = 7 while i <= last: base = list(itertools.repeat('a', i)) base += list(itertools.repeat('b', i)) # build all permutation of 'a' and 'b' till length i word_set = set(itertools.permutations(base, i)) for word in word_set: word = list(word) # print(word) afw_acceptance = AFW.afw_word_acceptance( self.afw_nonemptiness_check_test_2, word) nfa_acceptance = NFA.nfa_word_acceptance(nfa_01, word) self.assertEqual(afw_acceptance, nfa_acceptance) i += 1
def with_var_map(self, new_var_map): alphabets = list(range(len(new_var_map))) for v, (idx, alphabet) in new_var_map.items(): alphabets[idx] = alphabet if len(alphabets) == 0: new_alphabet = set() else: new_alphabet = set(' '.join(syms) for syms in it.product(*alphabets)) new_transitions = {} for (src, sym), dsts in self.aut['transitions'].items(): new_syms = list(range(len(new_var_map))) # Copy over the old symbols syms = sym.split(' ') for v, (idx, alphabet) in self.var_map.items(): # If a symbol isn't in the new map, just drop it if v in new_var_map: new_syms[new_var_map[v][0]] = [syms[idx]] for v, (idx, alphabet) in new_var_map.items(): if not v in self.var_map: new_syms[idx] = alphabet for new_sym in it.product(*new_syms): new_transitions[(src, ' '.join(new_sym))] = dsts # Rename all states because PySimpleAutomata can't union automata with the same state names... # TODO: Probably very inefficient. return NFA.rename_nfa_states( { 'alphabet': new_alphabet, 'states': self.aut['states'], 'initial_states': self.aut['initial_states'], 'accepting_states': self.aut['accepting_states'], 'transitions': new_transitions }, FiniteAutomaton.fresh_ap())
def test_nfa_interestingness_check_wrong_input(self): """ Tests the nonemptines of an input different from a dict object. [EXPECTED FAILURE] """ self.assertFalse(NFA.nfa_interestingness_check(0))
def test_nfa_intersection_wrong_dict_2(self): """ Tests a dict() in input different from a well formatted dict() representing a NFA. [EXPECTED FAILURE]""" NFA.nfa_intersection(self.nfa_intersection_1_test_01, {'goofy': 'donald'})
def test_nfa_intersection_wrong_input_2(self): """ Tests an input different from a dict() object. [ EXPECTED FAILURE] """ NFA.nfa_intersection(self.nfa_intersection_1_test_01, 0)
def test_nfa_intersection_empty(self): """ Tests a NFAs intersection where one of them is empty """ intersection = NFA.nfa_intersection( self.nfa_intersection_1_test_01, self.nfa_intersection_test_02_empty) self.assertDictEqual(intersection, self.nfa_intersection_test_02_empty)
def test_rename_nfa_states(self): """ Tests a correct NFA states renaming """ automata_IO.nfa_to_dot(NFA.rename_nfa_states(self.nfa_1, 'TOP_'), 'nfa_renamed_1', 'tests/outputs')
def test_word_acceptance_wrong_dict(self): """ Tests a dict() in input different from a well formatted dict() representing a NFA. [EXPECTED FAILURE]""" NFA.nfa_word_acceptance({'goofy': 'donald'}, ['a', 'b', 'b', 'a', 'b'])
def test_word_acceptance_wrong_input_2(self): """ Tests an input different from a list() object. [EXPECTED FAILURE]""" NFA.nfa_word_acceptance(self.nfa_word_acceptance_test_01, 1)
def test_word_acceptance_wrong_input_1(self): """ Tests an input different from a dict() object. [EXPECTED FAILURE]""" NFA.nfa_word_acceptance(1, ['a', 'b', 'b', 'a', 'b'])
def test_word_acceptance_empty_word(self): """ Tests an empty word""" self.assertFalse( NFA.nfa_word_acceptance(self.nfa_word_acceptance_test_empty, []))
def test_word_acceptance_wrong_alphabet(self): """ Tests a non correct word, with letters not form the nfa alphabet """ self.assertFalse( NFA.nfa_word_acceptance(self.nfa_word_acceptance_test_01, ['a', 'b', 'wrong']))
def test_word_acceptance_false(self): """ Tests a non correct word, with good alphabet""" self.assertFalse( NFA.nfa_word_acceptance(self.nfa_word_acceptance_test_01, ['a', 'a', 'a']))
def test_word_acceptance(self): """ Tests a correct word """ self.assertTrue( NFA.nfa_word_acceptance(self.nfa_word_acceptance_test_01, ['a', 'b', 'b', 'a', 'b']))
def test_nfa_intersection(self): """ Tests a correct NFAs intersection """ intersection = NFA.nfa_intersection(self.nfa_intersection_1_test_01, self.nfa_intersection_2_test_01) self.assertDictEqual(intersection, self.nfa_intersection_test_01_solution)
def test_nfa_interestingness_check_side_effects(self): """ Tests that the function doesn't make any side effect on the input""" before = copy.deepcopy(self.nfa_interestingness_test_01) NFA.nfa_interestingness_check(self.nfa_interestingness_test_01) self.assertDictEqual(before, self.nfa_interestingness_test_01)
def test_nfa_interestingness_check_wrong_dict(self): """ Tests the interestingness of an input dict different from a dict representing a nfa. [EXPECTED FAILURE] """ self.assertFalse(NFA.nfa_interestingness_check({}))