I want to create dynamic conditional statement in java
following are my expression in file,There are hundreds of expression and they keep on changing
0001|((condition1 == 100) && ((condition2 == 1) || (condition2 == 2) || (condition2 == 3)) && (condition3 > 74))
0002|((condition1 == 100) && ((condition2 == 1) || (condition2 == 2) || (condition2 == 3)) && (condition3 > 59) && ((condition4 == 3) || (condition5 > 30)))
These expression are hardcoded in my class.
if(condition1==100 && ((condition2 == 1) || (condition2 == 2) || (condition2 == 3))){
if(condition3>74){
return "0001"
}
if(condition3>59 && ((condition4 == 3) || (condition5 > 30))){
return "0002"
}
}
i want to create dynamic conditional statement like
first i have check for all expressions which have condition1==100
then for ((condition2 == 1) || (condition2 == 2) || (condition2 == 3))
then return value according to final condition
it is something like first DFS and then BFS
can some body can give me idea how to check first Depth and then Bredth First in java
Your case is : You want define very many conditions and change it continous. You need to have a solution for change dynamically expression and define new condition.
There are two solution for dynamic situation such as your case:
Rule Engine. This has very benefit, you can see more information from http://java.sun.com/developer/technicalArticles/J2SE/JavaRule.htmland you can see its open source implementation from here .
Dynamic LanguageorScript LanguageandScriptapi.in second solution you have several choise. I writing some in following:
Groovy: A complete and wonderful script language. see http://groovy.codehaus.org/Spring Expression Language:Springsolution for calling simple expression. see http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/expressions.htmlBeanShell: A simple but wonderful script language.There are more dynamic language such as
JRubythat you can see it by simple searching in web.You can read more information for
Scriptapi in java from here.Edited:
For sample, you can use
BeanShell Script Languageas follwoing:First create a file with name
test.bshcontaing blow contents:Second set
variable_1from java:and in final call script as following:
and result will be as following:
by this approach you can change
test.bshcontent without recompile or restart.