Comments (1)
<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)
- Parsing Block Comments HOT 1
- Add support for parentheses with positive sign HOT 1
- Trailing decimal point and signed number after operand HOT 2
- Feature request: Add support for series variables HOT 1
- Handle unknown variables HOT 4
- Cross compile option not working? HOT 2
- How to calculate use BigDecimal? HOT 1
- Exponent issue HOT 6
- Performance Optimizations and additional Features in invesdwin-util fork
- need jar
- Latest is not on maven central
- Is there a way to get a max value? HOT 4
- logical-not operation supported?
- Scientific notation HOT 3
- Is this thread safe? HOT 1
- can i use it to get derivative formula,or integral calculus,how can i do?
- JDK 1.6 runtime issue: UnsupportedClassVersionError HOT 3
- Parser Error HOT 2
- Floating-point literals starting with decimal point HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parsii.