How do I parse script statements like ${value1==value2} using java?

for example, if I pass in a string ${value1==value2} , how can I use java to parse the meaning of value1 and value2 and get the calculation result, or how to identify the parameters value1 and value2 in this statement?

now I already know that using ScriptEngine , you can add the values of value1 and value2 to the value1==value2 formula according to the js syntax, but you still need to know the parameter name in the expression to replace

.

the requirement now is that after a series of operations, the values of several variables have been saved in a Map, and then you need to read a string s to parse and extract the variable values needed by s from Map to calculate, like this

.
valuesMap.put("value1", value1);
valuesMap.put("value2", value2);
...
String s = "value1==value2";
ScriptEngine engine = ...
//TODO svalueList
engine.put(valueList,get(i), valuesMap.get(valueList,get(i)));

how to solve the step of fetching the parameter list? Or what should I do to achieve this whole function?

May.25,2022

this also requires you to specify your real needs to see if there is a better solution, and sometimes you can't fix how to implement it.
if you have to implement the current solution, it would be nice for the circular map to use engine to define variables, but it feels like you need to understand the requirements more deeply to see if there is a more reasonable solution.

Menu