Exemplo n.º 1
0
        public void tabla_transiciones()
        {
            Automaton aut = automatons.ElementAt(contador);

            dataGridView1.Columns.Add("Estados", "Estados");
            foreach (string s in aut.getAlphabet())
            {
                dataGridView1.Columns.Add(s, s);
            }
            foreach (Estado e in aut.getEstados())
            {
                dataGridView1.Rows.Add(e.getId(), e.getId());
            }

            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 1; j < dataGridView1.Columns.Count; j++)
                {
                    Estado e = aut.getEstados().ElementAt(i);
                    foreach (Transition t in e.getTransitions())
                    {
                        if (t.getSimbol().ToString().Equals(dataGridView1.Columns[j].HeaderText))
                        {
                            dataGridView1.Rows[i].Cells[j].Value = t.getEnd().getId().ToString();
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public Automaton or(Automaton left, Automaton right)
        {
            Automaton afn = new Automaton();


            Estado actual_begin = new Estado(0);

            actual_begin.setTransitions(new Transition(actual_begin, right.getBegin(), Automaton.EPSILON));

            afn.addEstados(actual_begin);
            afn.setBegin(actual_begin);
            int i = 0;

            for (i = 0; i < left.getEstados().Count(); i++)
            {
                Estado aux = left.getEstados().ElementAt(i);
                aux.setId(i + 1);
                afn.addEstados(aux);
            }

            for (int j = 0; j < right.getEstados().Count(); j++)
            {
                Estado aux = right.getEstados().ElementAt(j);
                aux.setId(i + 1);
                afn.addEstados(aux);
                i++;
            }

            Estado actual_end = new Estado(left.getEstados().Count() + right.getEstados().Count() + 1);

            afn.addEstados(actual_end);
            afn.addEstadosAceptacion(actual_end);


            Estado last_begin = left.getBegin();
            LinkedList <Estado> last_end_left  = left.getEstadosAceptacion();
            LinkedList <Estado> last_end_right = right.getEstadosAceptacion();

            actual_begin.getTransitions().AddLast(new Transition(actual_begin, last_begin, Automaton.EPSILON));


            for (int k = 0; k < last_end_left.Count(); k++)
            {
                last_end_left.ElementAt(k).getTransitions().AddLast(new Transition(last_end_left.ElementAt(k), actual_end, Automaton.EPSILON));
            }

            for (int k = 0; k < last_end_left.Count(); k++)
            {
                last_end_right.ElementAt(k).getTransitions().AddLast(new Transition(last_end_right.ElementAt(k), actual_end, Automaton.EPSILON));
            }

            return(afn);
        }
Exemplo n.º 3
0
        public Automaton cerradura_kleene(Automaton afn)
        {
            //Definir un nuevo automata con su estado inicial
            Automaton kleene = new Automaton();

            /*Se marca el estado inicial como "actual" debido a  que el automata recibido por parametro tambien
             * posee sus estados inicial y final
             */
            Estado actual_begin = new Estado(0);

            kleene.addEstados(actual_begin);
            kleene.setBegin(actual_begin);


            //Actualizar los indicadores de estado del afn recibido
            for (int i = 0; i < afn.getEstados().Count(); i++)
            {
                Estado aux = afn.getEstados().ElementAt(i);
                for (int j = 0; j < aux.getTransitions().Count(); j++)
                {
                    aux.getTransitions().ElementAt(j).getBegin().setId(aux.getTransitions().ElementAt(j).getBegin().getId() + 1);
                }
                aux.setId(i + 1);
                kleene.addEstados(aux);
            }



            Estado actual_end = new Estado(afn.getEstados().Count() + 1);

            kleene.addEstados(actual_end);
            kleene.addEstadosAceptacion(actual_end);

            Estado last_begin = afn.getBegin();

            LinkedList <Estado> last_end = afn.getEstadosAceptacion();

            actual_begin.getTransitions().AddLast(new Transition(actual_begin, last_begin, Automaton.EPSILON));
            actual_begin.getTransitions().AddLast(new Transition(actual_begin, actual_end, Automaton.EPSILON));


            for (int i = 0; i < last_end.Count(); i++)
            {
                last_end.ElementAt(i).getTransitions().AddLast(new Transition(last_end.ElementAt(i), last_begin, Automaton.EPSILON));
                last_end.ElementAt(i).getTransitions().AddLast(new Transition(last_end.ElementAt(i), actual_end, Automaton.EPSILON));
            }

            return(kleene);
        }
Exemplo n.º 4
0
        public Automaton cerradura_positiva(Automaton afn_pos)
        {
            Automaton afn = new Automaton();


            int i = 0;

            //Recorrer el automata actual
            for (i = 0; i < afn_pos.getEstados().Count(); i++)
            {
                //Clonar cada estado (sin sus transiciones)
                Estado aux          = afn_pos.getEstados().ElementAt(i);
                Estado nuevo_estado = (Estado)aux.Clone();

                if (i == 0)
                {
                    afn.setBegin(nuevo_estado);
                }
                afn.addEstados(nuevo_estado);
            }


            //Recorrer el nuevo automata
            for (int j = 0; j < afn.getEstados().Count(); j++)
            {
                //Estado auxiliar del automata nuevo
                Estado e = afn.getEstados().ElementAt(j);

                //Estado auxiliar del automata actual
                Estado e2 = afn_pos.getEstados().ElementAt(j);
                foreach (Transition t in e2.getTransitions())
                {
                    //Agregar las transiciones del nuevo automata
                    Transition aux = new Transition(search(afn.getEstados(), e2.getId()), search(afn.getEstados(), t.getEnd().getId()), t.getSimbol());
                    e.setTransitions(aux);
                }
            }

            for (int j = 0; j < afn_pos.getEstadosAceptacion().Count(); j++)
            {
                Estado aux = afn_pos.getEstadosAceptacion().ElementAt(j);
                Estado n   = search(afn.getEstados(), aux.getId());
                afn.addEstadosAceptacion(n);
            }

            return(afn);
        }
Exemplo n.º 5
0
        public Automaton and(Automaton left, Automaton right)
        {
            Automaton afn = new Automaton();
            int       i   = 0;

            for (i = 0; i < right.getEstados().Count(); i++)
            {
                Estado aux = right.getEstados().ElementAt(i);
                aux.setId(i);

                if (i == 0)
                {
                    afn.setBegin(aux);
                }

                if (i == right.getEstados().Count() - 1)
                {
                    for (int j = 0; j < right.getEstadosAceptacion().Count(); j++)
                    {
                        //aux.setTransitions(new Transition((Estado)right.getEstadosAceptacion().ElementAt(j), left.getBegin(), Automaton.EPSILON));
                        foreach (Transition t in left.getBegin().getTransitions())
                        {
                            //t.setBegin(aux);
                            aux.setTransitions(new Transition((Estado)right.getEstadosAceptacion().ElementAt(j), t.getEnd(), t.getSimbol()));
                        }
                    }
                }
                afn.addEstados(aux);
            }


            for (int j = 1; j < left.getEstados().Count(); j++)
            {
                Estado aux = left.getEstados().ElementAt(j);
                aux.setId(i);

                if (j == left.getEstados().Count() - 1)
                {
                    afn.addEstadosAceptacion(aux);
                }
                afn.addEstados(aux);
                i++;
            }
            return(afn);
        }
Exemplo n.º 6
0
        public String generarDOT(String nombreArchivo, Automaton automataFinito)
        {
            String texto = "digraph automata_finito {\n";

            texto += "\trankdir=LR;" + "\n";

            texto += "\tgraph [label=\"" + nombreArchivo + "\", labelloc=t, fontsize=20]; \n";
            texto += "\tnode [shape=doublecircle, style = filled,color = lightblue];";
            //mediumseagreen
            for (int i = 0; i < automataFinito.getEstadosAceptacion().Count(); i++)
            {
                texto += " " + automataFinito.getEstadosAceptacion().ElementAt(i).getId();
            }

            texto += ";" + "\n";
            texto += "\tnode [shape=circle];" + "\n";
            texto += "\tnode [color=lightblue,fontcolor=black];\n" + "	edge [color=lightblue];"+ "\n";

            texto += "\tsecret_node [style=invis];\n" + "	secret_node -> "+ automataFinito.getBegin().getId().ToString() + " [label=\"inicio\"];" + "\n";



            for (int i = 0; i < automataFinito.getEstados().Count(); i++)
            {
                LinkedList <Transition> t = automataFinito.getEstados().ElementAt(i).getTransitions();
                for (int j = 0; j < t.Count(); j++)
                {
                    texto += "\t" + t.ElementAt(j).DOT_String() + "\n";
                }
            }
            texto += "}";



            return(texto);
        }
Exemplo n.º 7
0
        public void construir_afd()
        {
            Estado inicio = new Estado(0);

            afd.addEstados(inicio);
            afd.setBegin(inicio);
            afd.setAlphabet(alphabet);
            HashSet <HashSet <Estado> > cerraduras = new HashSet <HashSet <Estado> >();
            Queue <HashSet <Estado> >   pila       = new Queue <HashSet <Estado> >();
            int identificador = 0;
            LinkedList <Estado> lista_inicial = new LinkedList <Estado>();

            lista_inicial.AddLast(afn.getBegin());
            HashSet <Estado> cerradura_inicial = e_cerradura(lista_inicial);

            foreach (Estado final in  afn.getEstadosAceptacion())
            {
                if (cerradura_inicial.Contains(final))
                {
                    afd.addEstadosAceptacion(inicio);
                }
            }

            pila.Enqueue(cerradura_inicial);

            while (pila.Count() > 0)
            {
                HashSet <Estado> aux = pila.Dequeue();
                foreach (string id in alphabet)
                {
                    LinkedList <Estado> res_mueve     = mueve(aux, id);
                    HashSet <Estado>    res_cerradura = new HashSet <Estado>();
                    res_cerradura = e_cerradura(res_mueve);
                    Estado anterior = afd.getEstados().ElementAt(identificador);
                    if (res_cerradura.Count() > 0)
                    {
                        if (existe_estado(cerraduras, res_cerradura))
                        {
                            LinkedList <Estado> estados_actuales = afd.getEstados();
                            Estado actual    = anterior;
                            Estado siguiente = estados_actuales.ElementAt(posicion_estado(cerraduras, res_cerradura) + 1);
                            actual.setTransitions(new Transition(actual, siguiente, id));
                        }
                        else
                        {
                            cerraduras.Add(res_cerradura);
                            pila.Enqueue(res_cerradura);

                            Estado agregar = new Estado(cerraduras.Count());
                            anterior.setTransitions(new Transition(anterior, agregar, id));
                            afd.addEstados(agregar);

                            foreach (Estado e in afn.getEstadosAceptacion())
                            {
                                if (res_cerradura.Contains(e))
                                {
                                    if (!afd.getEstadosAceptacion().Contains(agregar))
                                    {
                                        afd.addEstadosAceptacion(agregar);
                                    }
                                }
                            }
                        }
                    }
                }
                identificador++;
            }
            afd.setId(id);
            Form1.automatons.AddLast(afd);
            afd.setType("AFD");
            Graphviz g = new Graphviz();

            g.graph_automaton2(generarDOT(id + " : AFD", afd));
        }