Comments (9)
Why not evaluate them separately?
int var1 = 10;
Expression innerExpression = new Expression("5 * var1").with("var1", new BigDecimal(var1));
BigDecimal innerResult = innerExpression.eval();
Expression outerExpression = new Expression("25 + var2").with("var2", innerResult);
BigDecimal outerResult = outerExpression.eval();
System.out.println(outerResult.toPlainString());
from evalex.
Adding a function
public Expression with(String variable, Expression value) {
return setVariable(variable, value.eval());
}
might be the quickest way.
It would allow a call like
var1 = 10;
var2 = "5 * var1";
var3 = "25 + var2";
Expression e = new Expression( var3 ).with( "var2", new Expression(var2).with( "var1", var1));
e.eval();
A more sophisticated way might be to check in the with(String, String) method, if the string is an expression and if, then evaluate it first.
from evalex.
The whole catch is that I don't want to have to know what the expression is myself ahead of time, and I don't want to parse it manually.
For instance, if a user is entering the functions as raw data through an online form, his formula may use a var which itself is a formula.
I tried quickly to update the with(String, String) method and if the string wasn't a BigDecimal, then to add it to the variables list as an Expression. Then in the eval() method, when looping over the variables, if it found an expression, to eval the expression, but the RPN generating method threw an exception b/c it is obviously not expecting an Expression as a variable. And I wasn't able to follow the Shunting Yard algorithm well enough to modify that as well.
Then I realized that I might be complicating things a little too much, or perhaps there was more thought that was required, in case the nested expression needed access to the parent's vars (should that even be allowed?), etc.
from evalex.
Benze, please have a look at my latest commit. I think I found an easier way to support nested expressions. I have included your unit test and it evaluates correctly.
I simply do a string replacement (with braces to isolate the nested expression) and invalidate any previous RPN creation.
from evalex.
My first instinct when I saw your solution was wow - simple! Great! But I had a nagging feeling that there might be some conditions in which it would fail - if var is declared before the sub-expression that it is dependent on, since you are doing replacement when the var is added, and not at the end during the execution phase.
Ex:
@Test
public void testNestedVars2(){
String exp = "3 * z";
String b = "c";
String z = "a + b";
String a = "10";
String c = "5";
Expression e = new Expression(exp);
e.with("b", b);
e.with("z", z);
e.with("a", a);
e.with("c", c);
assertEquals("45", e.eval().toString());
}
Since my proposed solution does all the replacement only at execution phase, everything is already defined. Although I much preferred the simplicity in your solution.
I added a recursion limit check to my solution as well and an extra circular argument test to the TestNested class to limit recursion depths and stack overflows
from evalex.
I don't see a reason for having to support an e.with("x","y")
on an expression which does not have an "x" already in it.
from evalex.
Strange. I was sure I had already responded to this, but the response seems to have disappeared.
The problem with your solution is that it requires the variables to be entered in a specific order. In the test example above, the order makes all the difference. Inverting b
& z
definitions makes the test successful.
With my proposed commit, the order becomes irrelevant, since the evaluation is done at execution only. Your solution requires the user to pre-order the variables before setting them in the expression, which can be problematic for the user and the application.
from evalex.
I thought quite a while about it, but I think this feature is so rarely needed, and possibly easier managed on a higher level, so that it does not justify such a change here.
I would like to keep this little class handy and easy to understand and maintain.
While I apreciate any input and improvement, I am sorry, but I have to reject this pull request.
from evalex.
I respectfully disagree, but that's ok. I will use my forked version for my own needs.
Thanks,
Eric
from evalex.
Related Issues (20)
- Support arrays as function parameters HOT 7
- Equality depends on input type in 3.1.0
- problem with placeholder HOT 1
- Add module-info (or at least Automatic-Module-Name) HOT 2
- SQRT function doesn't work HOT 3
- With introduction of null, it's better to allow short circuiting (lazy evaluation) for those boolean operators HOT 4
- Equals/NotEquals operator is now sometimes showing inconsistent behavior HOT 1
- Broken boolean expression evaluation in version 3.1.0
- Allow Java arrays as input parameters
- Restore compatibility with Java 8 HOT 2
- Determine evaluation result type without evaluating HOT 2
- Java exception generated when trying to calculate expressions (it worked before with the original EvalEX library in the previous GitHub) HOT 2
- Can arrayJson filtering be supported? HOT 6
- Add a configurable locale to the configuration
- New Player Guidebook Naming rules HOT 1
- Incorrect behavior of ‘==’ operator in IF() expression for Array typed variable
- Management of the exception IndexOutOfBoundsException for Array variables
- Definition domain of the ACOS function
- does not evaluate if expression operand has a period, hyphen seperater HOT 2
- `SQRT` Javadoc issue.
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 evalex.