Giter Club home page Giter Club logo

Comments (1)

sen7961050 avatar sen7961050 commented on July 18, 2024
         <dependency>
        <groupId>com.scireum</groupId>
        <artifactId>parsii</artifactId>
        <version>4.0</version>
    </dependency> 

public static double evaluate(String expression, Map<String,Double> paramAndVals){
    Scope scope = new Scope();
    Set<String> variableNames = null;
    Expression expr = null;
    try {
        expr = Parser.parse(expression, scope);
        variableNames = scope.getLocalNames();
    } catch (ParseException e) {
        e.printStackTrace();
        logger.error("expression="+expression+" 解析发生异常:"+e.getMessage());
        throw new RuntimeException("expression="+expression+" 解析发生异常:"+e.getMessage());
    }
    if(!checkvariables(variableNames,paramAndVals)) {
        throw new RuntimeException("请确定表达式:"+expression+" 的参数都有传值!");
    }
    if(variableNames != null && !variableNames.isEmpty()){
        Iterator<String> iterator = paramAndVals.keySet().iterator();
        while(iterator.hasNext()){
            String param = iterator.next();
            Double val = paramAndVals.get(param);
            if(val == null) throw new RuntimeException("param="+param+" val is null!");
            Variable variable = scope.getVariable(param);
            variable.setValue(val);
        }
    }

    if(expr != null){
        return  expr.evaluate();
    }
    return 0;
}

private static boolean checkvariables(Set<String> variableNames,Map<String,Double> paramAndVals) 
{
    if(variableNames!=null && !variableNames.isEmpty()){
        if(paramAndVals == null || paramAndVals.size()<variableNames.size()){
            return false;
        }
        Set<String> paramNames = paramAndVals.keySet();
        if(!paramNames.containsAll(variableNames)){
            return false;
        }
    }
    return true;
}

public static void main(String args1[]) throws ParseException {
    Map<String,Double> paramAndVals = new HashMap<>();
    String exp = "R191000_A = R100000_A && R191000_B > R100000_B";
    paramAndVals.put("R191000_A",2.0);
    paramAndVals.put("R100000_A",2.0);
    paramAndVals.put("R191000_B",3.0);
    paramAndVals.put("R100000_B",2.0);
    double result = EvaluateUtils.evaluate(exp,paramAndVals);
    //result ==1.0 true
    //esult ==0.0 false
    System.out.print(result);

}

from parsii.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.