• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Custom Expression Parser

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am working on a online survey project (Database Driven). I have a scenario where I have five questions Q1, Q2, Q3, Q4, and Q5 each question has five options A11, A12, A13, A14, A15 (radio/combo/checkbox).

I have a requirement as below:

To display Q4 i need to check if Q1's answer is A12 and Q2's answer is A21 or Q3's answer is A34 if this is true then i need to display Q4 else i need to display Q5.
I am thinking of having a rule expression in the database as ((Q1:A12 && A2:A21) || (Q3:A34)) ? Q4:Q5 what this means is if Q1's answer is A12 and Q2's answer is A21 or Q3's answer is A34 then the expression should be evaluated and return true or false if it's true then i need to display Q4 else i need to display Q5.

Is there any tool in java which can parse this expression (with operand preference) ?


Thanks,
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sukumar Gaade wrote:Is there any tool in java which can parse this expression (with operand preference) ?


I don't think so; at least not in the standard libraries - but I'm sure I'll be flamed by the experts if I'm wrong. I also suspect that your "expressions" are too specific to your system to make a generalized parser much use.

However, I would suggest that you regulate your expressions: Your example:
((Q1:A12 && A2:A21) || (Q3:A34)) ? Q4:Q5
contains a possible ambiguity because you use the same operator (':') to split a question from it's number and also as a result separator.
I'd suggest Q3-A34 or Q3;A34 instead. If you do that, you're likely to make the parser's business a lot easier.

Also: '&&', '||' and 'A' seem redundant to me. Why not just '&' '|' and a number?
'&&' and '||' have a very specific meaning in Java, and I'm not sure it matters much in your case.

Winston
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An alternative would be to do it with code, something like what you find in mocking frameworks like Mokito. For example,

You don't even have to go that far. Just some well-chosen method names and return types.

For me, this is more readable than your proposed String-based rule notation and would be much more interesting to implement and easier to test. And I don't have to mess around with parsing Strings, a bane to a programmer's life. How to implement that is left as an exercise to you, if you choose to do so. Good luck.
 
Sukumar Gaade
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your responses.

Junilu Lacar wrote:An alternative would be to do it with code, something like what you find in mocking frameworks like Mokito. For example,

You don't even have to go that far. Just some well-chosen method names and return types.



I have manipulated and finally had a string like this: String expression = "((isAnswered(Q1, A12) && isAnswered(Q2, A21)) || isAnswered(Q3, A34))" isAnswered is a private method which returns boolean. but expression is a string and i wanted to evaluate this string. Is it possible to convert the string to expression so that i can pass it to if condition ?

Thanks,
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sukumar Gaade wrote:...but expression is a string


Well there's your basic problem.

and i wanted to evaluate this string.


Why? What Julinu is trying to tell you is that presumably you already have this information somewhere. Why not just use it in its native form, rather than converting it to and from Strings?

Is it possible to convert the string to expression...


Sure. The question is: do you need to?

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic