This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Evaluate String expression

 
Dinesh Behera
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a requirement to evaluate a string like below whose output will be true/false

Evaluation String : ((Priority=High&&Impact=Significant&&Urgency=High)||(Impact=Silent&&(Urgency=High||Urgency=Medium)))&&Privecy!=Secret

where Priority, Impact, Urgency,Privecy are variables and right hand site of '=' are values. The values will be High/Significant/Secret/..... against the variables

If the above condition is satisfied with the values provided, then return true, else return false


Note : Evaluation String is not fix one ....it is a dynamic string stored in datanbase


Can anybody give me a light to process it. Is there any default class/method to process these . If not is there any other way to do it ?


Thanks,
Dinesh
 
Brian Burress
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably need to post the code you have and advise what is or is not working as you expect. If I take what you have posted to be literally the code, then my first comment is that "=" is an assignment operator and that you want to look at using == or the equals method of String in your condition.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Burress wrote:You probably need to post the code you have and advise what is or is not working as you expect. If I take what you have posted to be literally the code, then my first comment is that "=" is an assignment operator and that you want to look at using == or the equals method of String in your condition.

My read on the original post is that they are pulling the above from a database, and need to parse it...and THEN evaluate it. I don't think it is literal java code.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need a parser for whatever language that is. If it's Java, then you can evaluate it using Groovy, Beanshell, Scala or others. If it's something like Javascript or some other language, then you'll need parser for that language. Depending on the language, one may already exist.
 
Dinesh Behera
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a complex condition which is a string

((Priority=High&&Impact=Significant&&Urgency=High)||(Impact=Silent&&(Urgency=High||Urgency=Medium)))&&Privecy!=Secret


i.e

if (

Priority = 'High' and Impact = 'Significant' && Urgency='High'
or
Impact = 'Silent' && (Urgency = 'High' or Urgency = 'Medium'))) && Privecy != 'Secret'

)

then return true;

otherwise return false;


But the difficulty is parsing the string which contains AND, OR, NOT and (...) and evaluting it. is there any way to parse and evaluate it ?


If we can keep the string in some other form then also it is fine... the string is nothing but a rule that we are defining.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh Behera wrote:It is a complex condition which is a string


In what language? It's not Java. So what existing, standard programming or scripting language's rules are you conforming to? Or is this something you're making up on the fly?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh Behera wrote:
But the difficulty is parsing the string which contains AND, OR, NOT and (...) and evaluting it. is there any way to parse and evaluate it ?


If it's an existing language with publicly available Java APIs for parsing it, use those libraries.

If it's something that you're making up on the fly, then you'll have a fairly steep learning curve to learn how to write a parser.

If we can keep the string in some other form then also it is fine... the string is nothing but a rule that we are defining.


So if you have the flexibility to do so, pick some language for which parsing libraries are readily available, such as JavaScript or Java.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic