3

I have a filtering application written in Java that will allow me to filter companies based on their fundamentals (e.g. pretax profit, dividend yield etc.).

I have created a filtering engine that, at the moment is hard-coded to take filters that have been given at compile time.

What I want to do is open up a web service where I can pass in JSON to run the filtering using filters defined and passed in at runtime.

To do this I need to somehow convert strings into Java code.

So for example the following string in JSON:

current.preTaxProfit > previous.preTaxProfit

into Java code like this:

return current.getPreTaxProfit() > previous.getPreTaxProfit();

The main issue I am having is parsing mathematical strings such as:

current.preTaxProfit > (previous.preTaxProfit * 1.10)

Things could get very complex very quickly, especially when it comes to inner brackets and such, and adhering to BODMAS.

Are there any libraries out there specifically for parsing mathematical strings, or does anyone know any good resources that could help me?

For example, I have found this:

Javaassist: http://davidwinterfeldt.blogspot.co.uk/2009/02/genearting-bytecode.html

Does anyone have any experience of using Javaassist? In my architecture I pass in objects ith 'callback' methods implemented, so the ability to create entire classes and methods could useful.

4
  • What have you attempted, and where did you get bogged-down? Commented May 12, 2014 at 19:30
  • What is your question? What have you tried so far? Commented May 12, 2014 at 19:30
  • Possible duplicate of Convert String to code Commented May 12, 2014 at 19:34
  • javaranch.com/journal/200711/… This looks perfect. Any opinions? Commented May 12, 2014 at 19:43

2 Answers 2

2

Two possibilities:

  • the Java Scripting API:
  • the EL, Expression Language.

The scripting API might be most fast to get results from, but EL is nicer.

Sign up to request clarification or add additional context in comments.

Comments

1

Consider using of an expression language, for instance JEXL.

If you put current and previous into the context, just evaluate:

current.preTaxProfit > (previous.preTaxProfit * 1.10)

Complete example:

// create context
JexlContext jc = new MapContext();
context.set("current", someObject);
context.set("previous", anotherObject);

// create expression
String jexlExp = "current.preTaxProfit > (previous.preTaxProfit * 1.10)";
Expression e = jexl.createExpression(jexlExp);

// evaluate
Object result = e.evaluate(jc);

2 Comments

Can JEXL be used to create classes and instantiate them?
@Frammo You can instantiate objects, but I'm not sure if JEXL allows to create class. I suppose, that even if it is possible, it won't be a pretty solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.