Reputation: 1272
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
Upvotes: 0
Views: 3110
Reputation: 6890
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.html
and you can see its open source implementation from here .Dynamic Language
or Script Language
and Script
api. 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
: Spring
solution 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 JRuby
that you can see it by simple searching in web.
You can read more information for Script
api in java from here.
Edited:
For sample, you can use BeanShell Script Language
as follwoing:
First create a file with name test.bsh
containg blow contents:
if(variable_1 == 100 )
{
System.out.println("Sample condition checked and is true.");
}
else
{
System.out.println("Sample condition checked and is false.");
}
Second set variable_1
from java:
import bsh.*;
Interpreter bsh = new Interpreter ();
bsh.set ("variable_1", 100);
and in final call script as following:
bsh.source (script);
and result will be as following:
Sample condition checked and is true.
by this approach you can change test.bsh
content without recompile or restart.
Upvotes: 1