Package expressao

Source Code of expressao.ParserEquacao

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package expressao;
import java.util.List;
import org.nfunk.jep.JEP;
import org.nfunk.jep.Node;

/**
*
* @author Helder
*/
public class ParserEquacao implements IParser{

    JEP j;

    public ParserEquacao(List<String> var) {

        int i;
       
        j = new JEP();
       
        for (i=0;i<var.size();i++)
            j.addConstant(var.get(i), (Object) 0);

        j.addConstant("ß0", (Object) 0);
        j.addConstant("ß1", (Object) 0);
        j.addConstant("ß2", (Object) 0);
        j.addConstant("ß3", (Object) 0);
        j.addConstant("ß4", (Object) 0);
        j.addConstant("ß5", (Object) 0);
        j.addConstant("ß6", (Object) 0);
        j.addConstant("ß7", (Object) 0);
        j.addConstant("ß8", (Object) 0);
        j.addConstant("ß9", (Object) 0);
        j.addConstant("ß10", (Object) 0);
        j.addFunction("LN", new Func());
        j.addFunction("LOG", new Func());
        j.addFunction("EXP", new Func());
        j.addFunction("SORT", new Func());
        j.addFunction("TRUNC", new Func());

//        j.addStandardConstants();
//        j.addStandardFunctions();
//        j.addComplex();
        j.setImplicitMul(false);
//        j.setAllowAssignment(true);
//        j.setAllowUndeclared(true);
    }

    private int analisaErro(String erro) {

        int colIni, colFin;
        String strRetorno = "";

       
        colIni = erro.indexOf("column")+7;
        colFin = erro.indexOf('.');
        if (colIni > 0 && colFin > 0)
            strRetorno = erro.substring(colIni, colFin);
        if (strRetorno.isEmpty())
            return 0;
        else
            return Integer.parseInt(strRetorno);
           
    }
   
    private String traduzMsgErro(int colIni, String erro, String info){
        String novaMsgErro="";
        int posDoisPontos, col, pos;

       
        if (erro.contains("Could not evaluate")){
            novaMsgErro = "Não foi possível avaliar";
            posDoisPontos = erro.indexOf(':');
            novaMsgErro = novaMsgErro.concat(erro.substring(18,posDoisPontos+1));
            novaMsgErro = novaMsgErro.concat(" a variável/coeficiente não é válida(o)");
        }
        if (info.contains("Function") && info.contains("requires")){
            novaMsgErro = "A função ";
            pos = info.indexOf('"')+1;
            while ((pos < info.length()) && (info.charAt(pos)!='"')) {
                novaMsgErro = novaMsgErro.concat(info.substring(pos, pos + 1));
                pos++;
            }
            novaMsgErro = novaMsgErro.concat(" requer ");
            novaMsgErro = novaMsgErro.concat(info.substring(pos+11,pos+13));
            novaMsgErro = novaMsgErro.concat("parâmetro(s)");
        }

        col = colIni+analisaErro(info);
        if (col>0){
            novaMsgErro = novaMsgErro.concat("\nErro na coluna: ");
            novaMsgErro = novaMsgErro.concat(String.valueOf(col));
        }
        return novaMsgErro;
    }

    @Override
    public boolean expressaoValida(String expressao) throws Exception {

        boolean sucesso=true;
        String msgErro = "", exp1, exp2;
        int i, erro = 0;

        if (expressao.trim().isEmpty()){
            sucesso=false;
            throw new MinhaExcecao("Erro: faltou expressão");
        }else{
            i = 0;
            while ((i < expressao.length() - 1) && (!expressao.substring(i, i + 1).equals("="))) {
                i++;
            }
            if (i > expressao.length() - 1) {
                sucesso=false;
                throw new MinhaExcecao("Erro: faltou igualdade");
            } else {
                exp1 = expressao.substring(0, i);
                exp2 = expressao.substring(i + 1, expressao.length());
                if ((exp1.trim().isEmpty()) || (exp2.trim().isEmpty())) {
                    sucesso=false;
                    throw new MinhaExcecao("Erro: faltou equação");
                } else {
                    try {
                        i=0;
                        Node n = j.parseExpression(exp1);
                        j.evaluate(n);
                        try {
                            Node n1 = j.parseExpression(exp2);
                            j.evaluate(n1);
                        } catch (Exception e) {
                            System.err.println("Erro: " + e.getMessage());
                            System.err.println("Info: " + j.getErrorInfo());
                            erro = 1;
                            msgErro = traduzMsgErro(exp1.length() + 1,e.getMessage(),j.getErrorInfo());
                        }
                    } catch (Exception e) {
                        if (erro == 0){
                            System.err.println("Erro: " + e.getMessage());
                            System.err.println("Info: " + j.getErrorInfo());
                            erro = 1;
                            msgErro = traduzMsgErro(0,e.getMessage(),j.getErrorInfo());
                        }
                    }
               }
                if (erro == 1){
                    sucesso=false;
                    throw new MinhaExcecao(msgErro);
                }
            }
        }
        return sucesso;
    }
}
TOP

Related Classes of expressao.ParserEquacao

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.