views:

245

answers:

4

What is the best way to evaluate simple conditional statements like:

"a>b" ?
"x-4<10+y & y>x" ?

Expressions are loaded from external file. Variables are set in application. Syntax used is not essential. It may be "&" or "and" or any other supported with language/library.

I need to take different action depending on result of evaluation - is it true or false.

Could I use any parser already included with Andorid?

Is there some way to use JS "eval" from browser component?

Is it possible to use sqlite expressions to get true/false result without selecting anything?

Those libraries are implemented in native code. Will it be faster and less battery expensive?

+2  A: 

You can use sqlite expressions, eg:

select 10>2 and 1 < 2;

returns 1

You can actually use variables, eg:

select x > y from (select 1 as x, 2 as y)
Hasturkun
That subselect is a neat trick. I was poking at this before posting my answer and couldn't figure out how to make something like that work. With Android, it will require an actual database (albeit perhaps an empty one), and you are limited to whatever math operations are available in SQLite. But, it's a neat trick nonetheless. Thanks!
CommonsWare
Thank you! Nice example :)
Malx
+2  A: 

Could I use any parser already included with Andorid?

Not efficiently.

Is there some way to use JS "eval" from browser component?

You would need to start up a WebView (expensive), load in a specially-crafted Web page, use a javascript: URL to pass the expression to a Javascript method on the Web page, and have that Javascript method call back the result on a Java object injected into the WebView via addJavaScriptInterface().

Is it possible to use sqlite expressions to get true/false result without selecting anything?

If all you had were constants, yes (see Hastrurkun's answer). However, there is no good way I can think of to assign values to variables to use in those expressions.

Those libraries are implemented in native code. Will it be faster and less battery expensive?

"Faster and less battery expensive" than...what?

Have you considered using a Java expression language or JEP?

CommonsWare
Thank you! I'll try JEP. For sql I could think of single variable example like: select value<4 from variables where name="x"; :)"Faster and less battery expensive" than java implementation. See http://www.scribd.com/doc/16917367/Coding-for-Life-Battery-Life-That-Is
Malx
@CommonsWare: You can use variables via a subselect, which I just added to my answer. this isn't better than replacing constants, though.
Hasturkun
+1  A: 

Try my ae, an arithmetic expression package for C programs based on Lua.

lhf
+1  A: 

I had the same need and I decided to use JEXL from Apache

Carlo