You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Greenberg, Gary" <gg...@visa.com.INVALID> on 2020/04/19 05:21:30 UTC

[jexl] How to use math functions with JEXL

I am trying to evaluate an expression that beside simple arithmetic need to calculate square root.
I tried sqrt(), Math.sqrt() and java.lang.Math.sqrt and nothing works
I am always getting something like @1:1 undefined variable java.lang.Math.
I did use before expr4j and it has all these functions built-in.
Switched to JEXL, because need not only numeric, but also Boolean expressions.
Can you also confirm if this type of expressions will work:
                a.equalsIgnoreCase("XYZ") && b.startWith("http")
where a and b are variables in the JexlContext.

Gary Greenberg
Staff Software Engineer
Data Product Development, BI-A
E: ggreenbe@visa.com
M: 650-269-7902

[EmailSig-TaglineVersion]


Re: [jexl] How to use math functions with JEXL

Posted by swapnil vaidya <sw...@gmail.com>.
You will have to register math as name space with JexlContext

https://commons.apache.org/proper/commons-jexl/apidocs/org/apache/commons/jexl3/package-summary.html

Regards,
Swapnil

On Sun, Apr 19, 2020 at 10:51 AM Greenberg, Gary <gg...@visa.com.invalid>
wrote:

> I am trying to evaluate an expression that beside simple arithmetic need
> to calculate square root.
>
> I tried sqrt(), Math.sqrt() and java.lang.Math.sqrt and nothing works
>
> I am always getting something like @1:1 undefined variable java.lang.Math.
>
> I did use before expr4j and it has all these functions built-in.
>
> Switched to JEXL, because need not only numeric, but also Boolean
> expressions.
>
> Can you also confirm if this type of expressions will work:
>
>                 a.equalsIgnoreCase(“XYZ”) && b.startWith(“http”)
>
> where a and b are variables in the JexlContext.
>
>
>
> Gary Greenberg
>
> Staff Software Engineer
>
> Data Product Development, BI-A
>
> E: ggreenbe@visa.com
>
> M: 650-269-7902
>
>
>
> [image: EmailSig-TaglineVersion]
>
>
>

RE: [jexl] How to use math functions with JEXL

Posted by "Greenberg, Gary" <gg...@visa.com.INVALID>.
I already figured it out.
With this snippet it is working:
    private static final JexlEngine ENGINE;
    static {
        Map<String, Object> fnc = new HashMap<>();
        fnc.put("math", Math.class);
        ENGINE = new JexlBuilder().namespaces(fnc).cache(512).strict(true).silent(false).create();
    }

And BTW, You are not importing anything from java.lang. It is already there by default.

Gary Greenberg
Staff Software Engineer
Data Product Development, BI-A
E: ggreenbe@visa.com
M: 650-269-7902




-----Original Message-----
From: Joakim Knudsen <jo...@gmail.com> 
Sent: Sunday, April 19, 2020 9:38 PM
To: Commons Users List <us...@commons.apache.org>
Subject: Re: [jexl] How to use math functions with JEXL

Hi,

Did you import the Math library?

import java.lang.Math;


Joakim

søn. 19. apr. 2020, 07:21 skrev Greenberg, Gary <gg...@visa.com.invalid>:

> I am trying to evaluate an expression that beside simple arithmetic 
> need to calculate square root.
>
> I tried sqrt(), Math.sqrt() and java.lang.Math.sqrt and nothing works
>
> I am always getting something like @1:1 undefined variable java.lang.Math.
>
> I did use before expr4j and it has all these functions built-in.
>
> Switched to JEXL, because need not only numeric, but also Boolean 
> expressions.
>
> Can you also confirm if this type of expressions will work:
>
>                 a.equalsIgnoreCase(“XYZ”) && b.startWith(“http”)
>
> where a and b are variables in the JexlContext.
>
>
>
> Gary Greenberg
>
> Staff Software Engineer
>
> Data Product Development, BI-A
>
> E: ggreenbe@visa.com
>
> M: 650-269-7902
>
>
>
> [image: EmailSig-TaglineVersion]
>
>
>

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


Re: [jexl] How to use math functions with JEXL

Posted by Joakim Knudsen <jo...@gmail.com>.
Hi,

Did you import the Math library?

import java.lang.Math;


Joakim

søn. 19. apr. 2020, 07:21 skrev Greenberg, Gary <gg...@visa.com.invalid>:

> I am trying to evaluate an expression that beside simple arithmetic need
> to calculate square root.
>
> I tried sqrt(), Math.sqrt() and java.lang.Math.sqrt and nothing works
>
> I am always getting something like @1:1 undefined variable java.lang.Math.
>
> I did use before expr4j and it has all these functions built-in.
>
> Switched to JEXL, because need not only numeric, but also Boolean
> expressions.
>
> Can you also confirm if this type of expressions will work:
>
>                 a.equalsIgnoreCase(“XYZ”) && b.startWith(“http”)
>
> where a and b are variables in the JexlContext.
>
>
>
> Gary Greenberg
>
> Staff Software Engineer
>
> Data Product Development, BI-A
>
> E: ggreenbe@visa.com
>
> M: 650-269-7902
>
>
>
> [image: EmailSig-TaglineVersion]
>
>
>