Evaluate String as a condition Java

2019-01-07 21:08发布

I have to retrieve a set of column values from D/B and check it as a condition.

For example, I will have strings like "value > 2", "4 < value < 6" in a D/B column. (value is the one which is compared all the time). I will have a variable value declared in my code and I should evaluate this condition.

int value = getValue();
if (value > 2)  //(the string retrieved from the D/B)
  doSomething();

How can I do this?? Any help is muceh appreciated. Thanks.

8条回答
淡お忘
2楼-- · 2019-01-07 21:24

A very good way of doing this apart from using Java 7 is using enums. Declare enum as shown below

The above enum has a collection of constants whose values are set to the strings that you expect would be returned from the database. As you can use enums in switch cases the remaining code becomes easy

enum MyEnum
{
    val1("value < 4"),val2("4<value<6");
    private String value;

    private MyEnum(String value)
    {
        this.value = value;
    }
}


public static void chooseStrategy(MyEnum enumVal)
{   
    int value = getValue();
    switch(enumVal)
    {
        case val1:
        if(value > 2){}
        break;
        case val2:
        if(4 < value && value < 6) {}
        break;
        default:
    }
}

public static void main(String[] args)
{
    String str = "4<value<6";
    chooseStrategy(MyEnum.valueOf(str));
}

All you have to do is pass your string to the enum.valueof method and it will return the appropiate enum which is put in a switch case block to perform conditional operation . In the above code you can pass any string in place of what is passed in this example

查看更多
疯言疯语
3楼-- · 2019-01-07 21:26

This doesn't answer your question per se; it offers an alternate solution that may effect the same result.

Instead of storing a single database column with pseudocode that defines a condition, make a table in which the schema define types of conditions that must be satisifed and the values of those conditions. This simplifies programmatic evaluation of those conditions, but it may become complicated if you have a variety of types of conditions to evaluate.

For example, you might have a table that looks like the following.

CONDITION_ID | MINIMUM | MAXIMUM | IS_PRIME  |   ETC.
______________________________________________________
      1      |    2    |   NULL  |   NULL    |   ...
      2      |    4    |    6    |   NULL    |   ...

Those row entries, respectively map to the rules value > 2 and 6 > value > 4.

This confers a number of benefits over the approach you provide.

  • Improved performance and cleanliness
  • Your conditions can be evaluated at the database level, and can be used to filter queries
  • You needn't worry about handling scenarios in which your pseudocode syntax is broken
查看更多
SAY GOODBYE
4楼-- · 2019-01-07 21:33

Here is an example using the standard (Java 1.6+) scripting library:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Test {

    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");

        engine.eval("value = 10");
        Boolean greaterThan5 = (Boolean) engine.eval("value > 5");
        Boolean lessThan5 = (Boolean) engine.eval("value < 5");

        System.out.println("10 > 5? " + greaterThan5); // true
        System.out.println("10 < 5? " + lessThan5); // false
    }
}
查看更多
萌系小妹纸
5楼-- · 2019-01-07 21:33

For evaluating the conditions with maximum flexibility use a scripting language designed for embedding, for instance MVEL can parse and evaluate simple conditional expression like the ones in the question.

Using MVEL has one huge advantage over using the Scripting engine in Java 1.6+ (in particular, with JavaScript): with MVEL you can compile the scripts to bytecode, making their evaluation much more efficient at runtime.

查看更多
聊天终结者
6楼-- · 2019-01-07 21:35

It's not going to be trivial: you need a parser for the expression language used in your database. If it's some standard, well-specified language, then you might be able to find one on the Internet, but if it's an in-house thing, then you may need to write your own (perhaps using a parser generator like ANTLR.)

The javax.script package contains some tools for integrating external scripting engines like a Javascript interpreter. An alternative idea would be to bring in a scripting engine and feed the expressions to that.

查看更多
何必那么认真
7楼-- · 2019-01-07 21:36

The latest version of java (Java 7) allows Switch Case statements on Strings, if there are not many possible variations you could just do this or similar :

int value = getValue();
Switch(myString) {
  Case "value > 2" : if (value > 2) {  doSomething();} break;
  Case "4 < value < 6" : if (4 < value < 6) {  doSomethingElse();} break;
  default : doDefault();
}
查看更多
登录 后发表回答