1

I have many expressions like this:

(age=1 AND gender=1) OR (kw>=5rWL6K+V6K+NCua1i+ivleWFs+mUruivjQo= AND
 interest=23100000) OR NOT interest=120101

5rWL6K+V6K+NCua1i+ivleWFs+mUruivjQo is a string encoded by base64. They are words splited by '\n'. >= means fuzzy match.

I have many inputs which in a hashMap like this:

{<age,10>,<gender,1>,<kw,"123">,<interest,1>}

I want to test if the input fit for the expression or not. Is there an efficiently method ?

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
jeffrey
  • 11
  • 1
  • 1
    As your question is currently written, it's unclear what you're asking. Do you need to parse the expression? Or have you already done that, and do you want to match them to the content of the hashmap? What are you expecting as output? What is stopping you from solving the problem? – Manu Nov 05 '15 at 12:28
  • You should definitely parse your conditions first and create some handier objects... – vojta Nov 05 '15 at 12:28
  • Yes, there are efficient methods. Computer languages do these things all the time. What have you tried? – RealSkeptic Nov 05 '15 at 12:32

1 Answers1

0

You could replace each "AND" with "&&", each "OR" with "||", each "=" with "==" and each "NOT" with "!". Then you would replace variables in your expressions with their values from the input hash map (String.replace())

Finally you could use the built-in JavaScript engine to get the result of your expression as described here:

https://stackoverflow.com/a/19384210/3899583

Have a look at ScriptEngine javadoc:

http://docs.oracle.com/javase/6/docs/api/javax/script/ScriptEngine.html

It looks like a lot of work (and a lot of debugging), but I think it is the most efficient way you can follow provided you do not want to implement the whole thing from a scratch.

EDIT:

Your expressions seem to be close to SQL, so you should consider using ZQL, which is an SQL parser written in Java. See:

https://stackoverflow.com/a/16424602/3899583

Community
  • 1
  • 1
vojta
  • 5,591
  • 2
  • 24
  • 64