Giter Site home page Giter Site logo

Support of nested expressions? about evalex HOT 9 CLOSED

benze avatar benze commented on September 20, 2024
Support of nested expressions?

from evalex.

Comments (9)

squeek502 avatar squeek502 commented on September 20, 2024

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.

uklimaschewski avatar uklimaschewski commented on September 20, 2024

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.

benze avatar benze commented on September 20, 2024

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.

uklimaschewski avatar uklimaschewski commented on September 20, 2024

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.

benze avatar benze commented on September 20, 2024

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.

uklimaschewski avatar uklimaschewski commented on September 20, 2024

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.

benze avatar benze commented on September 20, 2024

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.

uklimaschewski avatar uklimaschewski commented on September 20, 2024

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.

benze avatar benze commented on September 20, 2024

I respectfully disagree, but that's ok. I will use my forked version for my own needs.

Thanks,

Eric

from evalex.

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.