Java String and Mathematical Expression Evaluators
Asked Answered
U

2

1

We use the Jeks parser at present to evaluate expressions. I cannot see a way to evaluate string expressions with it - for example:

IF( "Test 1" = "Test 2")

Is there anything out there that can evaluate string and mathematical expressions in Java? Preferably free or open source.

Upper answered 30/9, 2010 at 15:47 Comment(4)
That's not remotely syntactically correct Java code. Is that intentional?Stull
I guess it is. He's looking for a Java-Evaluator for a Non-Java-Language (string and mathematical expressions).Fructuous
Take a look at JEKS: eteks.com/jeks/enFructuous
We use Jeks... But I cannot see how you can evaluate IF("Test 1" = "Test 2")Upper
U
0

The answer was a posting on the Jeks forum. I got an answer from Manu. I thought it was not active especially as I could not register on it - but that got sorted.

But if anyone has the same problem then the following lines of code will get it working:

ExpressionParser parser; // we did have = new ExpressionParser(new JeksExpressionSyntax(), null);
JeksInterpreter interpreter; // 

And to create the interpreter:

interpreter = new JeksInterpreter() {

      @Override
      public Object getBinaryOperatorValue (Object binaryOperatorKey, Object param1, Object param2)
      {
        // Only functions may take a cell set as parameter
        if ( param1 instanceof JeksCellSet || param2 instanceof JeksCellSet)
          throw new IllegalArgumentException ();
        // Enabled comparison between any type supported by Jeks
        else if (binaryOperatorKey.equals (JeksExpressionSyntax.OPERATOR_EQUAL))
          return param1 != null && param1.equals (param2)
                   ? Boolean.TRUE : Boolean.FALSE;
        // Enabled comparison between any type supported by Jeks
        else if (binaryOperatorKey.equals (JeksExpressionSyntax.OPERATOR_DIFFERENT))
          return param1 != null && param1.equals (param2) ? Boolean.FALSE : Boolean.TRUE;
        else
          return super.getBinaryOperatorValue (binaryOperatorKey, param1, param2);
      }
    };

    parser = new ExpressionParser(new JeksExpressionSyntax(), null);
Upper answered 14/10, 2010 at 15:11 Comment(0)
H
1

There are a lot of tools out there to evaluate expressions; the correct answer will depend on your exact goals.

Two things that I'd look at:

Halation answered 30/9, 2010 at 16:7 Comment(0)
U
0

The answer was a posting on the Jeks forum. I got an answer from Manu. I thought it was not active especially as I could not register on it - but that got sorted.

But if anyone has the same problem then the following lines of code will get it working:

ExpressionParser parser; // we did have = new ExpressionParser(new JeksExpressionSyntax(), null);
JeksInterpreter interpreter; // 

And to create the interpreter:

interpreter = new JeksInterpreter() {

      @Override
      public Object getBinaryOperatorValue (Object binaryOperatorKey, Object param1, Object param2)
      {
        // Only functions may take a cell set as parameter
        if ( param1 instanceof JeksCellSet || param2 instanceof JeksCellSet)
          throw new IllegalArgumentException ();
        // Enabled comparison between any type supported by Jeks
        else if (binaryOperatorKey.equals (JeksExpressionSyntax.OPERATOR_EQUAL))
          return param1 != null && param1.equals (param2)
                   ? Boolean.TRUE : Boolean.FALSE;
        // Enabled comparison between any type supported by Jeks
        else if (binaryOperatorKey.equals (JeksExpressionSyntax.OPERATOR_DIFFERENT))
          return param1 != null && param1.equals (param2) ? Boolean.FALSE : Boolean.TRUE;
        else
          return super.getBinaryOperatorValue (binaryOperatorKey, param1, param2);
      }
    };

    parser = new ExpressionParser(new JeksExpressionSyntax(), null);
Upper answered 14/10, 2010 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.