Evaluate rules against a multimap using MVEL

668 Views Asked by At

Just wondering how can I evaluate rules against a multimap using MVEL.

I.e:

rule-> "foo" == "200"

Possible values of foo in the context [100, 200, 300].

I don't know which are going to be the possible values so I can't include them as part of the rule.

Ideally I would like to be able to do something like this:

List<String> valueList = new ArrayList<String>();
valueList.add("100");
valueList.add("200");

List<String> otherList = new ArrayList<String>();
otherList.add("otherValue");

Map<String, List<String>> context = new HashMap<String, List<String>>();
context.put("foo", valueList);
context.put("other", otherList);

MVEL.eval("foo == 200", context)
1

There are 1 best solutions below

0
On

You can try using the plain java code as well. We have tried this and it executes fine. u can just pass your java code to Mvel and it will give you the result.

Function can be appended to string at run time. Just create a rule/condition with your logic. The bold part is what we write.

public Boolean evaluate(Map contextMap){

   **ClassA A = (ClassA) contextMap.get("contextObjectClassA");
    List<ClassB> classb = A.getclassb();
            if(null!=classb  && classb .size()>0){
                    return true;
                }else{
                return false;
            }**
    }