How to convert String to Function in Java?

528
January 08, 2017, at 11:45 PM

There is a question with the same title like this on stackoverflow here, but I wanted to ask if it is possible to do something similar to this in Java.

I wanted to make something similar to desmos, just like that guy did in Javascript,but i want to make it in Java using lwjgl 2. I am new to Java and I'd like to know if it is possible to convert a piece of string into a method. Is it possible?

I have looked for possible options and I found out that your can make your own Java eval() method. But I don't want to replace the x in the string for every pixel of the window-width.

Thanks in advance.

Answer 1

What you need is an engine/library that can evaluate expressions, defined as string at execution time. If you wrap the evaluation code into function call (e.g. lambda function), you will get what you need.

Option 1: You can use exp4j. exp4j is a small footprint library, capable of evaluating expressions and functions at execution time. Here is an example:

Expression e = new ExpressionBuilder("3 * sin(y) - 2 / (x - 2)")
        .variables("x", "y")
        .build()
        .setVariable("x", 2.3)
        .setVariable("y", 3.14);
double result = e.evaluate();

Option 2: You can use the Java's script engine. You can use it to evaluate expressions defined, for example, in JavaScript:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval("sin(1.25)");

Option 3: Compile to native Java. With this approach, you use template to generate .java file with a class that contains your expression. Than you call the Java compiler. This approach has the drawback that has some complexity in the implementation and some initial latency (until the class is compiled), but the performance is the best. Here are some links to explore:

  • Create dynamic applications with javax.tools
  • In particular javax.tools.Compiler

Note of Caution Whatever approach you chose, have in mind that you need to think about the security. Allowing the user to enter code which can be evaluated without security restrictions could be very dangerous.

READ ALSO
AsyncTask change ViewGroup properties in BaseAdapter (ListAdapter)

AsyncTask change ViewGroup properties in BaseAdapter (ListAdapter)

What is the best way to update properties of certain elements in a ViewGroup whilst running in an AsyncTaskE

386
Unable to convert date read from a file : result in java.text.ParseException: Unparseable date

Unable to convert date read from a file : result in java.text.ParseException: Unparseable date

I'm trying to convert a string date to a an object dateIt works perfectly if i use a string, but when i read the string date from a file, the result is ko

509
getting multiple errors when using HtmlUnit WebClient.getPage

getting multiple errors when using HtmlUnit WebClient.getPage

I am trying HtmlUnit for the first timeI am developing on Linux Mint 17

483
BoofCV and Android Studio [on hold]

BoofCV and Android Studio [on hold]

I am making app which compare CV libs on Android

503