You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by BENGUIGUI Michael <mi...@irt-saintexupery.com> on 2020/09/23 14:28:33 UTC

[jexl] Make jexl consider power operator

Dear all,

I am trying to evaluate the following expression via jexl :  x^y
What I've tried, is to extend JexlArithmetic:

// Extend Arithmetic
public class ExtendedJexlArithmetic extends JexlArithmetic
{
    public ExtendedJexlArithmetic(boolean strict){
        super(strict);
    }

    public xor(Object l, Object r){
                return Math.pow(l, r);
    }
}

def engine = new JexlBuilder().cache(512).strict(true).silent(false).namespaces(ns).arithmetic(new ExtendedJexlArithmetic(true)).create();
engine.createExpression(model_formula).evaluate(contextMap);


However, it does not consider operator priority. As a result, I am still getting wrong results:

2+4^3 -> 216 (instead of 66).

How can I properly fix this ?

Thhhx!

Re: [jexl] Make jexl consider power operator

Posted by Henri Biestro <he...@apache.org>.
In Java, the following does the trick:
    // Extend Arithmetic
    public static class ExtendedJexlArithmetic extends JexlArithmetic {
        public ExtendedJexlArithmetic(boolean strict){
            super(strict);
        }

        public double xor(Number l, Number r){
            return Math.pow(l.doubleValue(), r.doubleValue());
        }
    }

    @Test
    public void test333() throws Exception {
        JexlEngine jexl = new JexlBuilder().arithmetic(new ExtendedJexlArithmetic(true)).create();
        JexlScript spow = jexl.createScript("x ^ y", "x", "y");
        Object r =  spow.execute(null, 2, 3);
        Assert.assertTrue(r instanceof Number);
        Assert.assertEquals(8.d, ((Number) r).doubleValue(), 1e-7);
    }

On 2020/09/23 14:28:33, BENGUIGUI Michael <mi...@irt-saintexupery.com> wrote: 
> Dear all,
> 
> I am trying to evaluate the following expression via jexl :  x^y
> What I've tried, is to extend JexlArithmetic:
> 
> // Extend Arithmetic
> public class ExtendedJexlArithmetic extends JexlArithmetic
> {
>     public ExtendedJexlArithmetic(boolean strict){
>         super(strict);
>     }
> 
>     public xor(Object l, Object r){
>                 return Math.pow(l, r);
>     }
> }
> 
> def engine = new JexlBuilder().cache(512).strict(true).silent(false).namespaces(ns).arithmetic(new ExtendedJexlArithmetic(true)).create();
> engine.createExpression(model_formula).evaluate(contextMap);
> 
> 
> However, it does not consider operator priority. As a result, I am still getting wrong results:
> 
> 2+4^3 -> 216 (instead of 66).
> 
> How can I properly fix this ?
> 
> Thhhx!
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [jexl] Make jexl consider power operator

Posted by Henri Biestro <he...@apache.org>.
Sorry, I misread your original problem; JEXL does follow Java's operator precedence and there is no way to alter this behaviour (besides modifying the grammar in Parser.jjt file).

On 2020/09/23 14:28:33, BENGUIGUI Michael <mi...@irt-saintexupery.com> wrote: 
> Dear all,
> 
> I am trying to evaluate the following expression via jexl :  x^y
> What I've tried, is to extend JexlArithmetic:
> 
> // Extend Arithmetic
> public class ExtendedJexlArithmetic extends JexlArithmetic
> {
>     public ExtendedJexlArithmetic(boolean strict){
>         super(strict);
>     }
> 
>     public xor(Object l, Object r){
>                 return Math.pow(l, r);
>     }
> }
> 
> def engine = new JexlBuilder().cache(512).strict(true).silent(false).namespaces(ns).arithmetic(new ExtendedJexlArithmetic(true)).create();
> engine.createExpression(model_formula).evaluate(contextMap);
> 
> 
> However, it does not consider operator priority. As a result, I am still getting wrong results:
> 
> 2+4^3 -> 216 (instead of 66).
> 
> How can I properly fix this ?
> 
> Thhhx!
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [jexl] Make jexl consider power operator

Posted by aisen faith marrero <fa...@gmail.com>.
Thanks a million for... !

Sent from my iPhone

> On Sep 23, 2020, at 7:28 AM, BENGUIGUI Michael <mi...@irt-saintexupery.com> wrote:
> 
> Dear all,
> 
> I am trying to evaluate the following expression via jexl :  x^y
> What I've tried, is to extend JexlArithmetic:
> 
> // Extend Arithmetic
> public class ExtendedJexlArithmetic extends JexlArithmetic
> {
>    public ExtendedJexlArithmetic(boolean strict){
>        super(strict);
>    }
> 
>    public xor(Object l, Object r){
>                return Math.pow(l, r);
>    }
> }
> 
> def engine = new JexlBuilder().cache(512).strict(true).silent(false).namespaces(ns).arithmetic(new ExtendedJexlArithmetic(true)).create();
> engine.createExpression(model_formula).evaluate(contextMap);
> 
> 
> However, it does not consider operator priority. As a result, I am still getting wrong results:
> 
> 2+4^3 -> 216 (instead of 66).
> 
> How can I properly fix this ?
> 
> Thhhx!

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org