aspose file tools*
The moose likes Java in General and the fly likes Evaluate String expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Evaluate String expression" Watch "Evaluate String expression" New topic
Author

Evaluate String expression

Dinesh Behera
Greenhorn

Joined: Jan 31, 2012
Posts: 2
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

Joined: Jun 30, 2003
Posts: 122
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

Joined: Oct 02, 2003
Posts: 11477
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 31, 2012
Posts: 2
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
 
subject: Evaluate String expression