Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.13 KB | None | 0 0
  1. package ER;
  2. import java.nio.charset.Charset;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.List;
  6.  
  7. import javax.swing.JOptionPane;
  8. import javax.swing.text.html.HTMLEditorKit.Parser;
  9.  
  10. import matematica.automato.afn.AFN;
  11. import matematica.automato.afn.Estado;
  12. import matematica.automato.afn.buscador.Buscador;
  13. import matematica.automato.alfabeto.Alfabeto;
  14. import matematica.automato.transicao.FuncaoTransicao;
  15.  
  16. public class AfnMenu {
  17.  
  18.     private static final String OPCAO_INVALIDA = "Selecione uma opção válida!";
  19.    
  20.     public static void main(String[] args) {
  21.         String operacao = "";
  22.  
  23.         do {
  24.             operacao = JOptionPane.showInputDialog("Selecione uma opção:\n"
  25.                     + "\t a. Definir AFN.\n"
  26.                     + "\t b. Inserir Texto.\n"
  27.                     + "\t c. Converter ER para AFN.\n"
  28.                     + "\t d. Sair\n"
  29.                     + "\t ex : a");
  30.  
  31.             if (operacao.equals("a"))
  32.                 gerarAFN();
  33.  
  34.             else if (operacao.equals("b"))
  35.                 buscarTexto();
  36.            
  37.             else if(operacao.equals("c"))
  38.                 convertErToAfn();
  39.                
  40.             else if (operacao.equals("d"))
  41.                 showMessage("Bye Bye!.\n");
  42.  
  43.             else
  44.                 showMessage(OPCAO_INVALIDA);
  45.  
  46.         } while(!operacao.equals("d"));
  47.     }
  48.  
  49.     private static void gerarAFN() {
  50.         List<Estado> estados = gerarEstados();
  51.         Collection<Estado> estadosFinais = gerarEstadosFinais(estados);
  52.         Alfabeto alfabeto = gerarAlfabeto();
  53.         FuncaoTransicao funcaoTransicao = gerarFuncaoTransicao(estados);
  54.  
  55.         String indexEstadoInicial = JOptionPane.showInputDialog("Digite o estado inicial do autômato");
  56.         Estado estadoInicial = estados.get(Integer.parseInt(indexEstadoInicial));
  57.  
  58.         AFN afn = new AFN(
  59.             estados,
  60.             alfabeto,
  61.             funcaoTransicao,
  62.             estadoInicial,
  63.             estadosFinais
  64.         );
  65.  
  66.         while (!processarString(afn).equals("b"));
  67.     }
  68.  
  69.     private static FuncaoTransicao gerarFuncaoTransicao(List<Estado> estados) {
  70.         FuncaoTransicao funcaoTransicao = new FuncaoTransicao();
  71.         String operador = "";
  72.  
  73.         do {
  74.             operador = JOptionPane.showInputDialog("Selecione uma opção:\n"
  75.                     + "\t a. Adicionar transições.\n"
  76.                     + "\t b. Sair.");
  77.  
  78.             if (operador.equals("a")) {
  79.                 Estado origem = estados.get(getInteger("Estado origem da transição:\n ex: 1"));
  80.                 Estado alcancavel = estados.get(getInteger("Estado alcançável da transição:\n ex: 2"));
  81.                
  82.                 List<Character> simbolos = getListaSimbolos("A partir de qual/quais simbolos do alfabeto se dar essa transição?\n ex: a,b");
  83.                 for (Character simbolo : simbolos)
  84.                     funcaoTransicao.addTransicao(origem, alcancavel, simbolo);
  85.                
  86.             } else if (operador.equals("b"))
  87.                 showMessage("Transições adicionadas\n");
  88.             else
  89.                 showMessage(OPCAO_INVALIDA);
  90.  
  91.         } while (!operador.equals("b"));
  92.  
  93.         return funcaoTransicao;
  94.     }
  95.  
  96.     private static int getInteger(String mensagem) {
  97.         return Integer.parseInt(JOptionPane.showInputDialog(mensagem));
  98.     }
  99.  
  100.     private static Alfabeto gerarAlfabeto() {
  101.         return new Alfabeto(getListaSimbolos("digite o alfabeto:\n" + "ex: a,b"));
  102.     }
  103.  
  104.     private static List<Character> getListaSimbolos(String mensagem) {
  105.         List<Character> simbolos = new ArrayList<>();
  106.         String[] alfabeto = JOptionPane.showInputDialog(mensagem).split(",");
  107.        
  108.         for (String string : alfabeto)
  109.             simbolos.add(string.charAt(0));
  110.        
  111.         return simbolos;
  112.     }
  113.  
  114.     private static Collection<Estado> gerarEstadosFinais(List<Estado> estados) {
  115.         Collection<Estado> estadosFinais = new ArrayList<>();
  116.         String[] indexEstadosFinais = JOptionPane.showInputDialog("indique os estados finais:\nex:0,1\n (correspondendo a q0 e q1)").split(",");
  117.        
  118.         for (String index : indexEstadosFinais)
  119.             estadosFinais.add(estados.get(Integer.parseInt(index)));
  120.        
  121.         return estadosFinais;
  122.     }
  123.  
  124.     private static void showMessage(String message) {
  125.         JOptionPane.showMessageDialog(null, message);
  126.     }
  127.  
  128.     private static String processarString(AFN afn) {
  129.         String operacao;
  130.        
  131.         operacao = JOptionPane.showInputDialog("Selecione uma opção:\n"
  132.                 + "\t a.processar String.\n"
  133.                 + "\t b.sair.");
  134.  
  135.         if (operacao.equals("a")) {
  136.             String processar = JOptionPane.showInputDialog("digite a string que será processada!");
  137.            
  138.             if (afn.aceita(processar))
  139.                 JOptionPane.showMessageDialog(null, "String '"+processar+ "' foi aceita pelo automato");
  140.             else
  141.                 JOptionPane.showMessageDialog(null, "String '"+processar+ "' não foi aceita pelo automato");
  142.            
  143.         } else if (operacao.equals("b"))
  144.             JOptionPane.showMessageDialog(null, "Bye Bye!.\n");
  145.         else
  146.             showMessage(OPCAO_INVALIDA);
  147.        
  148.         return operacao;
  149.     }
  150.  
  151.     public static List<Estado> gerarEstados() {
  152.         int numeroEstados = Integer.parseInt(JOptionPane.showInputDialog(null,  "Digite o numero de estados da AFN: \n\n"
  153.                 + "OBS: A CONTAGEM DOS ESTADOS COMEÇA DO ZERO \n ex : 5 -> ESTADOS DE q0 A q4"));
  154.        
  155.         List<Estado> estados = new ArrayList<>();
  156.         for (int i=0; i<numeroEstados; i++)
  157.             estados.add(new Estado(i));
  158.  
  159.         return estados;
  160.     }
  161.    
  162.     private static void buscarTexto() {
  163.         String texto = JOptionPane.showInputDialog("Digite o texto");
  164.         String operacao = "";
  165.        
  166.         do {
  167.             operacao = JOptionPane.showInputDialog("Selecione uma opção:\n"
  168.                     + "\t a. Buscar texto.\n"
  169.                     + "\t b. Voltar");
  170.  
  171.             if (operacao.equals("a")) {
  172.                 String parte = JOptionPane.showInputDialog("Digite o texto que será buscada!");
  173.                
  174.                 if (Buscador.buscar(texto, parte))
  175.                     showMessage("'"+parte+ "' localizada no texto!");
  176.                 else
  177.                     showMessage("'"+parte+ "' não localizada no texto");
  178.  
  179.             } else if(operacao.equals("b"))
  180.                 showMessage("Bye Bye!.\n");
  181.             else
  182.                 showMessage(OPCAO_INVALIDA);
  183.  
  184.        
  185.         } while(!operacao.equals("b"));
  186.     }
  187.     private static void convertErToAfn(){
  188.         String expressaoRegularBruta = JOptionPane.showInputDialog("Digite a expressão regular para ser convertida");
  189.         ConvertToAFN oConvert = new ConvertToAFN(expressaoRegularBruta);
  190.         List<String> resultado = oConvert.toAFDe();
  191.         Collection<Estado> estadoInicial = new ArrayList<>();
  192.         Collection<Estado> estados = new ArrayList<>();
  193.         Collection<Estado> estadosFinais = new ArrayList<>();
  194.        
  195.         geraEstados(resultado,estadoInicial,estados,estadosFinais);
  196.        
  197.     }
  198.    
  199.     private static void geraEstados(List resultado, Collection<Estado> estadoInicial, Collection<Estado> estados, Collection<Estado> estadosFinais){
  200.  
  201.         for (int i = 1; i<resultado.size();i++) {
  202.             boolean estadoFinal = false;
  203.             String numeroEstado="";
  204.             for(int j=0;j<resultado.get(i).toString().length();j++){
  205.                     if(resultado.get(i).toString().charAt(j) == '*'){
  206.                         estadoFinal = true;
  207.                     }else if(verificaCaractereNumero(resultado.get(i).toString().charAt(j))){
  208.                         numeroEstado +=resultado.get(i).toString().charAt(j);
  209.                        
  210.                     }else if(resultado.get(i).toString().charAt(j) == '-'){
  211.                         break;
  212.                     }
  213.                
  214.             }
  215.             //System.out.println(numeroEstado);
  216.             if(numeroEstado.equals("0")){
  217.                 Estado estado = new Estado(Integer.parseInt(numeroEstado));
  218.                 estadoInicial = Estado.estados(estado);
  219.                 //estadoInicial = Estado.estados(new Estado(Integer.parseInt(numeroEstado)));
  220.                 //estadoInicial = (Collection<Estado>) new Estado(Integer.parseInt(numeroEstado));
  221.                 //System.out.println(numeroEstado);
  222.             }else if(estadoFinal){
  223.                 Estado estado = new Estado(Integer.parseInt(numeroEstado));
  224.                 estadosFinais = Estado.estados(estado);
  225.                 //estadosFinais = Estado.estados(new Estado(Integer.parseInt(numeroEstado)));
  226.                 //System.out.println(numeroEstado);
  227.             }
  228.            
  229.             if(!numeroEstado.equals("0")){
  230.                 estados = Estado.estados(new Estado(Integer.parseInt(numeroEstado)));
  231.                 //System.out.println(numeroEstado);
  232.             }
  233.             System.out.println(estadosFinais.size());
  234.             //System.out.println(estadoFinal);
  235.             //System.out.println(Integer.parseInt(numeroEstado));
  236.             //System.out.println(resultado.get(i).toString().charAt(0));
  237.             //System.out.println(resultado.get(i));
  238.         }
  239.        
  240.         //new AFN(estados, new Alfabeto('a', 'b'), new FuncaoTransicao(), estadoInicial, estadosFinais);
  241.  
  242.     }
  243.  
  244.     private static boolean verificaCaractereNumero(char c){
  245.    
  246.         if(c == '0' || c=='1' || c=='2' || c =='3' || c =='4' || c =='5' || c =='6' || c =='7' || c =='8' || c =='9'){
  247.             return  true;
  248.         }else{
  249.             return false;
  250.         }
  251.        
  252.     }
  253.    
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement