Ejemplo n.º 1
0
 def test_minify_dfa(self):
     """Should minify a given DFA."""
     # This DFA accepts all words which are at least two characters long.
     # The states q1/q2 and q3/q4/q5/q6 are redundant.
     # The state q7 is not reachable.
     dfa = DFA(
         states={'q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7'},
         input_symbols={'0', '1'},
         transitions={
             'q0': {'0': 'q1', '1': 'q2'},
             'q1': {'0': 'q3', '1': 'q4'},
             'q2': {'0': 'q5', '1': 'q6'},
             'q3': {'0': 'q3', '1': 'q3'},
             'q4': {'0': 'q4', '1': 'q4'},
             'q5': {'0': 'q5', '1': 'q5'},
             'q6': {'0': 'q6', '1': 'q6'},
             'q7': {'0': 'q7', '1': 'q7'},
         },
         initial_state='q0',
         final_states={'q3', 'q4', 'q5', 'q6'}
     )
     minimal_dfa = dfa.minify()
     nose.assert_equal(minimal_dfa.states, {
         'q0', '{q1,q2}', '{q3,q4,q5,q6}'
     })
     nose.assert_equal(minimal_dfa.input_symbols, {'0', '1'})
     nose.assert_equal(minimal_dfa.transitions, {
         'q0': {'0': '{q1,q2}', '1': '{q1,q2}'},
         '{q1,q2}': {'0': '{q3,q4,q5,q6}', '1': '{q3,q4,q5,q6}'},
         '{q3,q4,q5,q6}': {'0': '{q3,q4,q5,q6}', '1': '{q3,q4,q5,q6}'}
     })
     nose.assert_equal(minimal_dfa.initial_state, 'q0')
     nose.assert_equal(minimal_dfa.final_states, {'{q3,q4,q5,q6}'})
Ejemplo n.º 2
0
def validar(sets, tipo):
    if tipo == False:
        aux = list(sets[2].values())
        for i in range(len(sets[0])):
            if '' in aux[i].values() or not sets[4]:
                return False
        automata = DFA(states=sets[0],
                       input_symbols=sets[1],
                       transitions=sets[2],
                       initial_state=sets[3],
                       final_states=sets[4])
    else:
        if sets[4]:
            automata = NFA(states=sets[0],
                           input_symbols=sets[1],
                           transitions=sets[2],
                           initial_state=sets[3],
                           final_states=sets[4])
        else:
            return False
    if automata.validate():
        del automata
        return True
    else:
        del automata
        return False
Ejemplo n.º 3
0
 def test_minify_dfa_initial_state(self):
     """Should minify a DFA where the initial state is being changed."""
     # This DFA accepts all words with ones and zeroes.
     # The two states can be merged into one.
     dfa = DFA(
         states={'q0', 'q1'},
         input_symbols={'0', '1'},
         transitions={
             'q0': {
                 '0': 'q1',
                 '1': 'q1'
             },
             'q1': {
                 '0': 'q0',
                 '1': 'q0'
             },
         },
         initial_state='q0',
         final_states={'q0', 'q1'},
     )
     minimal_dfa = dfa.minify()
     nose.assert_equal(minimal_dfa.states, {'{q0,q1}'})
     nose.assert_equal(minimal_dfa.input_symbols, {'0', '1'})
     nose.assert_equal(minimal_dfa.transitions, {
         '{q0,q1}': {
             '0': '{q0,q1}',
             '1': '{q0,q1}'
         },
     })
     nose.assert_equal(minimal_dfa.initial_state, '{q0,q1}')
     nose.assert_equal(minimal_dfa.final_states, {'{q0,q1}'})
Ejemplo n.º 4
0
 def __attrs_post_init__(self):
     """Initialization by creating the default object"""
     if self.dfa is None:
         self.dfa = DFA(
             states=self.state,
             input_symbols=self.input_symbols,
             transitions=self.transitions,
             initial_state=self.initial_state,
             final_states=self.final_states,
         )
Ejemplo n.º 5
0
def evaluateDFA2():
    print("Eval DFA2")
    value = str(mainWidget.lineEdit_eval.text())
    dfa = DFA(states=getStates(),
              input_symbols=symbols,
              transitions=getTransitions(),
              initial_state=str(startNode.label.text()),
              final_states=getFinalStates())
    try:
        print(str(list(dfa.validate_input(value, step=True))))
        showMsg("DFA Successful, Last:" + str(dfa.validate_input(value)))
    except:
        showMsg("ERROR EVALUATING DFA")

    return dfa
Ejemplo n.º 6
0
    def test_are_not_isomorphic_useless_state(self):
        dfa = DFA(states={'q0', 'q1', 'q2', 'q3'},
                  input_symbols={'0', '1'},
                  transitions={
                      'q0': {
                          '0': 'q0',
                          '1': 'q1'
                      },
                      'q1': {
                          '0': 'q0',
                          '1': 'q2'
                      },
                      'q2': {
                          '0': 'q2',
                          '1': 'q1'
                      },
                      'q3': {
                          '0': 'q2',
                          '1': 'q1'
                      }
                  },
                  initial_state='q0',
                  final_states={'q1'})

        dfae = DFA(states={'q0', 'q1', 'q2', 'q3'},
                   input_symbols={'0', '1'},
                   transitions={
                       'q0': {
                           '0': 'q0',
                           '1': 'q2'
                       },
                       'q1': {
                           '0': 'q1',
                           '1': 'q2'
                       },
                       'q2': {
                           '0': 'q0',
                           '1': 'q1'
                       },
                       'q3': {
                           '0': 'q2',
                           '1': 'q1'
                       }
                   },
                   initial_state='q0',
                   final_states={'q2'})

        self.assertFalse(pkm.are_isomorphic(dfa, dfae))
Ejemplo n.º 7
0
def creatDFA(states, symbols, initialState, finalStates, transition):

    return DFA(states=states,
               input_symbols=symbols,
               transitions=transition,
               initial_state=initialState,
               final_states=finalStates)
def complemento(automata, tipo):
    #intercambiar estados finales por estados
    if tipo == False:

        fin = []
        final = automata.final_states
        estados = automata.states

        for i in estados:
            for l in final:
                if i != l:
                    fin.append(i)

        finales = set(fin)

        automata = DFA(states=automata.states,
                       input_symbols=automata.input_symbols,
                       transitions=automata.transitions,
                       initial_state=automata.initial_state,
                       final_states=finales)
        return automata, tipo

    else:
        ADF = NFA.from_dfa(automata)
        complemento(ADF, False)
def AFNDtoAFD(automata, tipo):
    if tipo:
        dfa = DFA.from_nfa(automata)
        minimal_dfa = dfa.minify()
        return minimal_dfa, False
    else:
        return automata, tipo
Ejemplo n.º 10
0
def parse_json(json_string, is_deterministic):
    fsm = json.loads(json_string)

    try:
        states, final_states, node_map = get_states(fsm)
        input_symbols = get_alphabet(fsm, is_deterministic)
        if is_deterministic:
            initial_state, transitions = \
                get_DFA_transitions(fsm, states, node_map, input_symbols)
        else:
            initial_state, transitions = \
                get_NFA_transitions(fsm, states, node_map, input_symbols)
    except Exception as e:
        return True, str(e)

    if is_deterministic:
        fsm = DFA(states=states,
                  input_symbols=input_symbols,
                  transitions=transitions,
                  initial_state=initial_state,
                  final_states=final_states)
    else:
        fsm = NFA(states=states,
                  input_symbols=input_symbols,
                  transitions=transitions,
                  initial_state=initial_state,
                  final_states=final_states)

    return False, fsm
Ejemplo n.º 11
0
 def test_init_dfa_missing_formal_params(self):
     """Should raise an error if formal DFA parameters are missing."""
     with nose.assert_raises(TypeError):
         DFA(states={'q0', 'q1'},
             input_symbols={'0', '1'},
             initial_state='q0',
             final_states={'q1'})
Ejemplo n.º 12
0
def create_test_dfa():
    """
    Create a Deterministic Finite Automaton for testing purpose.
    
    Returns: a DFA that can be used for tests
    """
    dfa = DFA(states={'q0', 'q1', 'q2'},
              input_symbols={'0', '1'},
              transitions={
                  'q0': {
                      '0': 'q0',
                      '1': 'q1'
                  },
                  'q1': {
                      '0': 'q0',
                      '1': 'q2'
                  },
                  'q2': {
                      '0': 'q2',
                      '1': 'q1'
                  }
              },
              initial_state='q0',
              final_states={'q1'})
    return dfa
Ejemplo n.º 13
0
def make_DFA_for_M() -> "DFA":
    return DFA(states={"0", "1", "2", "3"},
               input_symbols=set(DIRS),
               transitions={
                   "0": {
                       "U": "1",
                       "D": "1",
                       "L": "2",
                       "R": "2"
                   },
                   "1": {
                       "U": "3",
                       "D": "3",
                       "L": "2",
                       "R": "2"
                   },
                   "2": {
                       "U": "1",
                       "D": "1",
                       "L": "3",
                       "R": "3"
                   },
                   "3": {
                       "U": "3",
                       "D": "3",
                       "L": "3",
                       "R": "3"
                   }
               },
               initial_state="0",
               final_states={"0", "1", "2"})
Ejemplo n.º 14
0
 def test_init_nfa_more_complex(self):
     """Should convert to a DFA a more complex NFA."""
     nfa = NFA(
         states={'q0', 'q1', 'q2'},
         input_symbols={'0', '1'},
         transitions={
             'q0': {'0': {'q0', 'q1'}, '1': {'q0'}},
             'q1': {'0': {'q1'}, '1': {'q2'}},
             'q2': {'0': {'q2'}, '1': {'q1'}}
         },
         initial_state='q0',
         final_states={'q2'}
     )
     dfa = DFA.from_nfa(nfa)
     nose.assert_equal(dfa.states, {
         '{q0}', '{q0,q1}', '{q0,q2}', '{q0,q1,q2}'
     })
     nose.assert_equal(dfa.input_symbols, {'0', '1'})
     nose.assert_equal(dfa.transitions, {
         '{q0}': {'1': '{q0}', '0': '{q0,q1}'},
         '{q0,q1}': {'1': '{q0,q2}', '0': '{q0,q1}'},
         '{q0,q2}': {'1': '{q0,q1}', '0': '{q0,q1,q2}'},
         '{q0,q1,q2}': {'1': '{q0,q1,q2}', '0': '{q0,q1,q2}'}
     })
     nose.assert_equal(dfa.initial_state, '{q0}')
     nose.assert_equal(dfa.final_states, {'{q0,q1,q2}', '{q0,q2}'})
Ejemplo n.º 15
0
def main():
    nfa = NFA(  # this code builds the weakly divisible by 7 NFA
        states={'start_state', 'q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q0prime', 'q1prime', 'q2prime', 'q3prime',
                'q4prime', 'q5prime', 'q6prime', 'fail_state'},
        input_symbols={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'},
        transitions={
            'start_state': {'0': {'fail_state'}, '1': {'q1', 'q0prime'}, '2': {'q2', 'q0prime'}, '3': {'q3', 'q0prime'},
                            '4': {'q4', 'q0prime'}, '5': {'q5', 'q0prime'}, '6': {'q6', 'q0prime'},
                            '7': {'q0', 'q0prime'},
                            '8': {'q1', 'q0prime'}, '9': {'q2', 'q0prime'}},
            'fail_state': {'0': {'fail_state'}, '1': {'fail_state'}, '2': {'fail_state'}, '3': {'fail_state'},
                           '4': {'fail_state'}, '5': {'fail_state'}, '6': {'fail_state'}, '7': {'fail_state'},
                           '8': {'fail_state'}, '9': {'fail_state'}},
            'q0': {'0': {'q0', 'q0prime'}, '1': {'q1', 'q0prime'}, '2': {'q2', 'q0prime'}, '3': {'q3', 'q0prime'},
                   '4': {'q4', 'q0prime'}, '5': {'q5', 'q0prime'}, '6': {'q6', 'q0prime'}, '7': {'q0', 'q0prime'},
                   '8': {'q1', 'q0prime'}, '9': {'q2', 'q0prime'}},
            'q1': {'0': {'q3', 'q1prime'}, '1': {'q4', 'q1prime'}, '2': {'q5', 'q1prime'}, '3': {'q6', 'q1prime'},
                   '4': {'q0', 'q1prime'}, '5': {'q1', 'q1prime'}, '6': {'q2', 'q1prime'}, '7': {'q3', 'q1prime'},
                   '8': {'q4', 'q1prime'}, '9': {'q5', 'q1prime'}, '': {'q1', 'q1prime'}},
            'q2': {'0': {'q6', 'q2prime'}, '1': {'q0', 'q2prime'}, '2': {'q1', 'q2prime'}, '3': {'q2', 'q2prime'},
                   '4': {'q3', 'q2prime'}, '5': {'q4', 'q2prime'}, '6': {'q5', 'q2prime'}, '7': {'q6', 'q2prime'},
                   '8': {'q0', 'q2prime'}, '9': {'q1', 'q2prime'}, '': {'q2', 'q2prime'}},
            'q3': {'0': {'q2', 'q3prime'}, '1': {'q3', 'q3prime'}, '2': {'q4', 'q3prime'}, '3': {'q5', 'q3prime'},
                   '4': {'q6', 'q3prime'}, '5': {'q0', 'q3prime'}, '6': {'q1', 'q3prime'}, '7': {'q2', 'q3prime'},
                   '8': {'q3', 'q3prime'}, '9': {'q4', 'q3prime'}, '': {'q3'}},
            'q4': {'0': {'q5', 'q4prime'}, '1': {'q6', 'q4prime'}, '2': {'q0', 'q4prime'}, '3': {'q1', 'q4prime'},
                   '4': {'q2', 'q4prime'}, '5': {'q3', 'q4prime'}, '6': {'q4', 'q4prime'}, '7': {'q5', 'q4prime'},
                   '8': {'q6', 'q4prime'}, '9': {'q0', 'q4prime'}, '': {'q4'}},
            'q5': {'0': {'q1', 'q5prime'}, '1': {'q2', 'q5prime'}, '2': {'q3', 'q5prime'}, '3': {'q4', 'q5prime'},
                   '4': {'q5', 'q5prime'}, '5': {'q6', 'q5prime'}, '6': {'q0', 'q5prime'}, '7': {'q1', 'q5prime'},
                   '8': {'q2', 'q5prime'}, '9': {'q3', 'q5prime'}, '': {'q5'}},
            'q6': {'0': {'q4', 'q6prime'}, '1': {'q5', 'q6prime'}, '2': {'q6', 'q6prime'}, '3': {'q0', 'q6prime'},
                   '4': {'q1', 'q6prime'}, '5': {'q2', 'q6prime'}, '6': {'q3', 'q6prime'}, '7': {'q4', 'q6prime'},
                   '8': {'q5', 'q6prime'}, '9': {'q6', 'q6prime'}, '': {'q6'}},
            'q0prime': {'0': {'q0prime'}, '1': {'q1prime'}, '2': {'q2prime'}, '3': {'q3prime'}, '4': {'q4prime'},
                        '5': {'q5prime'}, '6': {'q6prime'}, '7': {'q0prime'}, '8': {'q1prime'}, '9': {'q2prime'}},
            'q1prime': {'0': {'q3prime'}, '1': {'q4prime'}, '2': {'q5prime'}, '3': {'q6prime'}, '4': {'q0prime'},
                        '5': {'q1prime'}, '6': {'q2prime'}, '7': {'q3prime'}, '8': {'q4prime'}, '9': {'q5prime'}},
            'q2prime': {'0': {'q6prime'}, '1': {'q0prime'}, '2': {'q1prime'}, '3': {'q2prime'}, '4': {'q3prime'},
                        '5': {'q4prime'}, '6': {'q5prime'}, '7': {'q6prime'}, '8': {'q0prime'}, '9': {'q1prime'}},
            'q3prime': {'0': {'q2prime'}, '1': {'q3prime'}, '2': {'q4prime'}, '3': {'q5prime'}, '4': {'q6prime'},
                        '5': {'q0prime'}, '6': {'q1prime'}, '7': {'q2prime'}, '8': {'q3prime'}, '9': {'q4prime'}},
            'q4prime': {'0': {'q5prime'}, '1': {'q6prime'}, '2': {'q0prime'}, '3': {'q1prime'}, '4': {'q2prime'},
                        '5': {'q3prime'}, '6': {'q4prime'}, '7': {'q5prime'}, '8': {'q6prime'}, '9': {'q0prime'}},
            'q5prime': {'0': {'q1prime'}, '1': {'q2prime'}, '2': {'q3prime'}, '3': {'q4prime'}, '4': {'q5prime'},
                        '5': {'q6prime'}, '6': {'q0prime'}, '7': {'q1prime'}, '8': {'q2prime'}, '9': {'q3prime'}},
            'q6prime': {'0': {'q4prime'}, '1': {'q5prime'}, '2': {'q6prime'}, '3': {'q0prime'}, '4': {'q1prime'},
                        '5': {'q2prime'}, '6': {'q3prime'}, '7': {'q4prime'}, '8': {'q5prime'}, '9': {'q6prime'}}
        },
        initial_state='start_state',
        final_states={'q0', 'q0prime'}
    )

    str_length = int(input("Enter an integer for string length: "))
    dfa = DFA.from_nfa(nfa)  # creates a DFA from the above built NFA

    num_strings = count(str_length, dfa)
    print("The number of strings of length", str_length,
          "accepted by the DFA is", num_strings)
Ejemplo n.º 16
0
def convertNFAtoDFA():
    nfa = evaluateNFA()
    dfa = DFA(nfa)  # returns an equivalent DFA

    states = dfa.states
    transitions = dfa.transitions
    initial = dfa.initial_state
    finals = dfa.final_states

    print("states ", states, " transitions ", transitions, " initial ",
          initial, " final ", finals)

    global startNode
    global evalValue
    startNode = None
    evalValue = None

    currNode = None
    nextNode = None
    for node in nodeList:
        node.delNode()
    conList.clear()
    nodeList.clear()

    for state in states:
        isFinal = False
        isInitial = False
        img = "normal.png"
        if initial == state:
            isInitial = True
            img = "start.png"
        else:
            for f in finals:
                if f == state:
                    isFinal = True
                    img = "final.png"
        new_node = Node(mainWidget.canvas, isInitial, isFinal, img, state)
        nodeList.append(new_node)
        if (isInitial):
            startNode = new_node
            print("startNode ", new_node.label.text())

        #Py2
        #for key, value in d.iteritems():
        #Py3
    for key, value in transitions.items():
        n0 = searchNode(key)
        print("key ", key, " value ", str(value))
        for key2, value2 in value.items():
            print("key2 ", key2, " value2 ", value2)
            isSameNode = False
            if (key == value2):
                isSameNode = True
            nX = searchNode(value2)
            # print(str(nX))
            new_connection = Connection(n0, nX, isSameNode)
            new_connection.name = key2
            conList.append(new_connection)
Ejemplo n.º 17
0
 def test_minify_minimal_dfa(self):
     """Should minify an already minimal DFA."""
     # This DFA just accepts words ending in 1.
     dfa = DFA(
         states={'q0', 'q1'},
         input_symbols={'0', '1'},
         transitions={
             'q0': {'0': 'q0', '1': 'q1'},
             'q1': {'0': 'q0', '1': 'q1'}
         },
         initial_state='q0',
         final_states={'q1'}
     )
     minimal_dfa = dfa.minify()
     nose.assert_equal(minimal_dfa.states, dfa.states)
     nose.assert_equal(minimal_dfa.input_symbols, dfa.input_symbols)
     nose.assert_equal(minimal_dfa.transitions, dfa.transitions)
     nose.assert_equal(minimal_dfa.initial_state, dfa.initial_state)
     nose.assert_equal(minimal_dfa.final_states, dfa.final_states)
Ejemplo n.º 18
0
class FiniteState(object):
    """Using the Automata library for Finate State"""
    state = attr.ib(default={"S0", "S1", "S2"})
    input_symbols = attr.ib(default={"0", "1"})
    transitions = attr.ib(
        default={
            "S0": {
                "0": "S0",
                "1": "S1"
            },
            "S1": {
                "0": "S2",
                "1": "S0"
            },
            "S2": {
                "0": "S1",
                "1": "S2"
            },
        })
    initial_state = attr.ib(default="S0")
    final_states = attr.ib(default={"S0", "S1", "S2"})
    dfa = attr.ib(default=None)

    def __attrs_post_init__(self):
        """Initialization by creating the default object"""
        if self.dfa is None:
            self.dfa = DFA(
                states=self.state,
                input_symbols=self.input_symbols,
                transitions=self.transitions,
                initial_state=self.initial_state,
                final_states=self.final_states,
            )

    def get_final_value(self, input_string):
        """Returns the final value, rasies RejectionException if a problem is found"""
        value = self.dfa.read_input(input_string)[1:]
        return value

    def get_final_state(self, input_string):
        """Returns the final state, rasies RejectionException if a problem is found"""
        value = self.dfa.read_input(input_string)[:1]
        return value
Ejemplo n.º 19
0
 def test_minify_dfa_no_final_states(self):
     dfa = DFA(
         states={'q0', 'q1'},
         input_symbols={'0', '1'},
         transitions={
             'q0': {'0': 'q1', '1': 'q1'},
             'q1': {'0': 'q0', '1': 'q0'},
         },
         initial_state='q0',
         final_states=set(),
     )
     minimal_dfa = dfa.minify()
     nose.assert_equal(minimal_dfa.states, {'{q0,q1}'})
     nose.assert_equal(minimal_dfa.input_symbols, {'0', '1'})
     nose.assert_equal(minimal_dfa.transitions, {
         '{q0,q1}': {'0': '{q0,q1}', '1': '{q0,q1}'},
     })
     nose.assert_equal(minimal_dfa.initial_state, '{q0,q1}')
     nose.assert_equal(minimal_dfa.final_states, set())
Ejemplo n.º 20
0
def get_complementary(automaton):
    temp_complementary = convert_to_dfa(automaton)
    temp_final_state = list(
        set(complementary.states) - set(complementary.final_states) -
        set(complementary.initial_states))
    complementary = DFA(states=temp_complementary.states,
                        input_symbols=temp_complementary.input_symbols,
                        transitions=temp_complementary.transitions,
                        initial_state=temp_complementary.initial_state,
                        final_states=temp_final_state)
    return complementary
Ejemplo n.º 21
0
def converter_nfa_dfa(estados, alfabeto, funcao_transicao, est_inicial,
                      est_finais):
    nfa = NFA(states=set(estados),
              input_symbols=set(alfabeto),
              transitions=funcao_transicao,
              initial_state=est_inicial,
              final_states=set(est_finais))

    dfa = DFA(nfa)

    return dfa
Ejemplo n.º 22
0
 def test_init_nfa_lambda_transition(self):
     """Should convert to a DFA an NFA with a lambda transition."""
     dfa = DFA.from_nfa(self.nfa)
     nose.assert_equal(dfa.states, {'{}', '{q0}', '{q1,q2}'})
     nose.assert_equal(dfa.input_symbols, {'a', 'b'})
     nose.assert_equal(dfa.transitions, {
         '{}': {'a': '{}', 'b': '{}'},
         '{q0}': {'a': '{q1,q2}', 'b': '{}'},
         '{q1,q2}': {'a': '{q1,q2}', 'b': '{q0}'},
     })
     nose.assert_equal(dfa.initial_state, '{q0}')
     nose.assert_equal(dfa.final_states, {'{q1,q2}'})
Ejemplo n.º 23
0
    def __init__(
        self,
        dfa: DFA = None,
        *,
        states: set = None,
        input_symbols: set = None,
        transitions: dict = None,
        initial_state: str = None,
        final_states: set = None
    ):

        if dfa:
            self.dfa = dfa
        else:
            self.dfa = DFA(
                states=states,
                input_symbols=input_symbols,
                transitions=transitions,
                initial_state=initial_state,
                final_states=final_states,
            )
Ejemplo n.º 24
0
    def test_are_isomorphic(self):
        dfa = DFA(states={'q0', 'q1', 'q2'},
                  input_symbols={'0', '1'},
                  transitions={
                      'q0': {
                          '0': 'q0',
                          '1': 'q1'
                      },
                      'q1': {
                          '0': 'q0',
                          '1': 'q2'
                      },
                      'q2': {
                          '0': 'q2',
                          '1': 'q1'
                      }
                  },
                  initial_state='q0',
                  final_states={'q1'})

        dfac = DFA(states={'q0', 'q1', 'q2'},
                   input_symbols={'0', '1'},
                   transitions={
                       'q0': {
                           '0': 'q0',
                           '1': 'q2'
                       },
                       'q1': {
                           '0': 'q1',
                           '1': 'q2'
                       },
                       'q2': {
                           '0': 'q0',
                           '1': 'q1'
                       }
                   },
                   initial_state='q0',
                   final_states={'q2'})

        self.assertTrue(pkm.are_isomorphic(dfa, dfac))
Ejemplo n.º 25
0
def crear(sets, tipo):
    if tipo == False:
        automata = DFA(states=sets[0],
                       input_symbols=sets[1],
                       transitions=sets[2],
                       initial_state=sets[3],
                       final_states=sets[4])

    else:
        automata = NFA(states=sets[0],
                       input_symbols=sets[1],
                       transitions=sets[2],
                       initial_state=sets[3],
                       final_states=sets[4])
    return automata
Ejemplo n.º 26
0
 def test_nfa_to_dfa_with_lambda_transitions(self):
     """ Test NFA->DFA when initial state has lambda transitions """
     nfa = NFA(
         states={'q0', 'q1', 'q2'},
         input_symbols={'a', 'b'},
         transitions={
             'q0': {'': {'q2'}},
             'q1': {'a': {'q1'}},
             'q2': {'a': {'q1'}}
         },
         initial_state='q0',
         final_states={'q1'}
     )
     dfa = DFA.from_nfa(nfa)  # returns an equivalent DFA
     nose.assert_equal(dfa.read_input('a'), '{q1}')
Ejemplo n.º 27
0
def img():

    dfa = DFA(
    states={'q0', 'q1', 'q2'},
    input_symbols={'0', '1'},
    transitions={
        'q0': {'0': 'q0', '1': 'q1'},
        'q1': {'0': 'q0', '1': 'q2'},
        'q2': {'0': 'q2', '1': 'q1'}
    },
    initial_state='q0',
    final_states={'q1'}
)

    automata = draw(dfa, False)
    automata = base64.b64encode(automata).decode('utf-8')

    return render_template('image.html', automata=automata)
Ejemplo n.º 28
0
def generate(entry, label):
    dfa = DFA(
        states={'q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', ''},
        input_symbols={'1', '2', '3', '4', '5', '6', '7', '8', '9', ' '},
        transitions={
            'q0': {'1': 'q1', '2': 'q2', '3': '', '4': '', '5': '', '6': '', '7': '', '8': '', '9': '', ' ': ''},
            'q1': {'1': '', '2': 'q3', '3': '', '4': '', '5': '', '6': '', '7': '', '8': '', '9': '', ' ': ''},
            'q2': {'1': 'q1', '2': 'q0', '3': '', '4': '', '5': '', '6': '', '7': '', '8': '', '9': '', ' ': ''},
            'q3': {'1': '', '2': 'q0', '3': 'q4', '4': '', '5': '', '6': '', '7': '', '8': '', '9': '', ' ': ''},
            'q4': {'1': '', '2': '', '3': '', '4': 'q5', '5': 'q4', '6': 'q3', '7': 'q6', '8': '', '9': '', ' ': ''},
            'q5': {'1': '', '2': '', '3': '', '4': '', '5': 'q5', '6': '', '7': '', '8': 'q5', '9': 'q7', ' ': 'q4'},
            'q6': {'1': 'q4', '2': '', '3': '', '4': '', '5': 'q6', '6': '', '7': '', '8': 'q6', '9': '', ' ': ''},
            'q7': {'1': 'q4', '2': '', '3': '', '4': '', '5': 'q7', '6': '', '7': '', '8': 'q5', '9': '', ' ': ''},
            '': {'1': '', '2': '', '3': '', '4': '', '5': '', '6': '', '7': '', '8': '', '9': '', ' ': ''}
        },
        initial_state='q0',
        final_states={'q0'}
    )

    label['text'] = response(dfa, entry)
Ejemplo n.º 29
0
def DFA_name_reset(L, minimize=True):
    if minimize:
        L = L.minify(False)
    else:
        m = dict()
        for x in L.states:
            m[x] = str(len(m))
        #print("DFA name reset:")
        #print("Before:\n{}\n{}\n{}".format(L.states, L.transitions, L.final_states))
        #print("After:\n{}".format({m[x] for x in L.states}))# {m[x]:{k:m[v] for k,v in L.transitions[x].items()} for x in L.transitions}, {m[x] for x in L.final_states}))
        L = DFA(states={m[x]
                        for x in L.states},
                input_symbols=L.input_symbols,
                transitions={
                    m[x]: {k: m[v]
                           for k, v in L.transitions[x].items()}
                    for x in L.transitions
                },
                initial_state=m[L.initial_state],
                final_states={m[x]
                              for x in L.final_states})
    return L
Ejemplo n.º 30
0
 def setUp(self):
     """Reset test automata before every test function."""
     # DFA which matches all binary strings ending in an odd number of '1's
     self.dfa = DFA(states={'q0', 'q1', 'q2'},
                    input_symbols={'0', '1'},
                    transitions={
                        'q0': {
                            '0': 'q0',
                            '1': 'q1'
                        },
                        'q1': {
                            '0': 'q0',
                            '1': 'q2'
                        },
                        'q2': {
                            '0': 'q2',
                            '1': 'q1'
                        }
                    },
                    initial_state='q0',
                    final_states={'q1'})
     # NFA which matches strings beginning with 'a', ending with 'a', and
     # containing no consecutive 'b's
     self.nfa = NFA(states={'q0', 'q1', 'q2'},
                    input_symbols={'a', 'b'},
                    transitions={
                        'q0': {
                            'a': {'q1'}
                        },
                        'q1': {
                            'a': {'q1'},
                            '': {'q2'}
                        },
                        'q2': {
                            'b': {'q0'}
                        }
                    },
                    initial_state='q0',
                    final_states={'q1'})