Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Evaluating expression with context #20

Open
eklinger opened this issue Dec 11, 2018 · 1 comment
Open

Evaluating expression with context #20

eklinger opened this issue Dec 11, 2018 · 1 comment

Comments

@eklinger
Copy link

Hello,
I have a boolean expression but need additional context to the eval method to determine its boolean value. For example
(4 && 9) || 11

Then I want to pass a collection to eval() and invoke contains() on the collection to see if the literal is there or not and return that true or false value. Is this possible? Thanks!

@sen7961050
Copy link

         <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);

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants