Search in sources :

Example 1 with Contains

use of org.mvel2.ast.Contains in project mvel by mikebrock.

the class PropertyAccessTests method testBindingCoercion.

public void testBindingCoercion() {
    List list = new LinkedList();
    list.add("Apple");
    list.add("Peach");
    list.add("Icing");
    Cake cake = new Cake();
    MVEL.setProperty(cake, "ingredients", list);
    assertTrue(cake.getIngredients().contains("Apple"));
    assertTrue(cake.getIngredients().contains("Peach"));
    assertTrue(cake.getIngredients().contains("Icing"));
}
Also used : Cake(org.mvel2.tests.core.res.Cake)

Example 2 with Contains

use of org.mvel2.ast.Contains in project mvel by mikebrock.

the class TypesAndInferenceTests method testAnalysisCompile.

public void testAnalysisCompile() {
    ParserContext pCtx = new ParserContext();
    ExpressionCompiler e = new ExpressionCompiler("foo.aValue = 'bar'");
    e.setVerifyOnly(true);
    e.compile(pCtx);
    assertTrue(pCtx.getInputs().keySet().contains("foo"));
    assertEquals(1, pCtx.getInputs().size());
    assertEquals(0, pCtx.getVariables().size());
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 3 with Contains

use of org.mvel2.ast.Contains in project mvel by mikebrock.

the class TypesAndInferenceTests method testVarInputs4.

public void testVarInputs4() {
    ExpressionCompiler compiler = new ExpressionCompiler("System.out.println( message );");
    compiler.compile();
    assertTrue(compiler.getParserContextState().getInputs().keySet().contains("message"));
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 4 with Contains

use of org.mvel2.ast.Contains in project mvel by mikebrock.

the class AbstractParser method reduce.

/**
 * This method is called when we reach the point where we must subEval a trinary operation in the expression.
 * (ie. val1 op val2).  This is not the same as a binary operation, although binary operations would appear
 * to have 3 structures as well.  A binary structure (or also a junction in the expression) compares the
 * current state against 2 downrange structures (usually an op and a val).
 */
protected void reduce() {
    Object v1, v2;
    int operator;
    try {
        switch(operator = (Integer) stk.pop()) {
            case ADD:
            case SUB:
            case DIV:
            case MULT:
            case MOD:
            case EQUAL:
            case NEQUAL:
            case GTHAN:
            case LTHAN:
            case GETHAN:
            case LETHAN:
            case POWER:
                stk.op(operator);
                break;
            case AND:
                v1 = stk.pop();
                stk.push(((Boolean) stk.pop()) && ((Boolean) v1));
                break;
            case OR:
                v1 = stk.pop();
                stk.push(((Boolean) stk.pop()) || ((Boolean) v1));
                break;
            case CHOR:
                v1 = stk.pop();
                if (!isEmpty(v2 = stk.pop()) || !isEmpty(v1)) {
                    stk.clear();
                    stk.push(!isEmpty(v2) ? v2 : v1);
                    return;
                } else
                    stk.push(null);
                break;
            case REGEX:
                stk.push(java.util.regex.Pattern.compile(java.lang.String.valueOf(stk.pop())).matcher(java.lang.String.valueOf(stk.pop())).matches());
                break;
            case INSTANCEOF:
                stk.push(((Class) stk.pop()).isInstance(stk.pop()));
                break;
            case CONVERTABLE_TO:
                stk.push(org.mvel2.DataConversion.canConvert(stk.peek2().getClass(), (Class) stk.pop2()));
                break;
            case CONTAINS:
                stk.push(containsCheck(stk.peek2(), stk.pop2()));
                break;
            case BW_AND:
                stk.push(asInt(stk.peek2()) & asInt(stk.pop2()));
                break;
            case BW_OR:
                stk.push(asInt(stk.peek2()) | asInt(stk.pop2()));
                break;
            case BW_XOR:
                stk.push(asInt(stk.peek2()) ^ asInt(stk.pop2()));
                break;
            case BW_SHIFT_LEFT:
                stk.push(asInt(stk.peek2()) << asInt(stk.pop2()));
                break;
            case BW_USHIFT_LEFT:
                int iv2 = asInt(stk.peek2());
                if (iv2 < 0)
                    iv2 *= -1;
                stk.push(iv2 << asInt(stk.pop2()));
                break;
            case BW_SHIFT_RIGHT:
                stk.push(asInt(stk.peek2()) >> asInt(stk.pop2()));
                break;
            case BW_USHIFT_RIGHT:
                stk.push(asInt(stk.peek2()) >>> asInt(stk.pop2()));
                break;
            case SOUNDEX:
                stk.push(soundex(java.lang.String.valueOf(stk.pop())).equals(soundex(java.lang.String.valueOf(stk.pop()))));
                break;
            case SIMILARITY:
                stk.push(similarity(java.lang.String.valueOf(stk.pop()), java.lang.String.valueOf(stk.pop())));
                break;
        }
    } catch (ClassCastException e) {
        throw new CompileException("syntax error or incompatable types", expr, st, e);
    } catch (ArithmeticException e) {
        throw new CompileException("arithmetic error: " + e.getMessage(), expr, st, e);
    } catch (Exception e) {
        throw new CompileException("failed to subEval expression", expr, st, e);
    }
}
Also used : CompileException(org.mvel2.CompileException) CompileException(org.mvel2.CompileException)

Example 5 with Contains

use of org.mvel2.ast.Contains in project drools by kiegroup.

the class SecurityPolicyTest method testAccumulateFunctionMVEL.

@Test
public void testAccumulateFunctionMVEL() throws Exception {
    String drl = "package org.foo.bar\n" + "import " + MaliciousExitHelper.class.getName().replace('$', '.') + " \n" + "rule testRule dialect \"mvel\" \n" + "    when\n" + "        Number() from accumulate(Object(), " + "               sum(MaliciousExitHelper.exit()))\n" + "    then\n" + "end";
    try {
        KieServices ks = KieServices.Factory.get();
        KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
        ks.newKieBuilder(kfs).buildAll();
        ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
        KieContainer kc = ks.newKieContainer(releaseId);
        KieSession ksession = kc.newKieSession();
        ksession.insert("foo");
        ksession.fireAllRules();
        Assert.fail("The security policy for the rule should have prevented this from executing...");
    } catch (PropertyAccessException e) {
        // weak way of testing but couldn't find a better way
        if (e.toString().contains("The security policy should have prevented")) {
            Assert.fail("The security policy for the rule should have prevented this from executing...");
        } else {
        // test succeeded
        }
    } catch (Exception e) {
        if (e.toString().contains("access denied (\"java.lang.RuntimePermission\" \"exitVM.0\")")) {
        // test succeeded
        } else {
            throw e;
        }
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) PropertyAccessException(org.mvel2.PropertyAccessException) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) ConsequenceException(org.kie.api.runtime.rule.ConsequenceException) PropertyAccessException(org.mvel2.PropertyAccessException) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 KieServices (org.kie.api.KieServices)5 KieFileSystem (org.kie.api.builder.KieFileSystem)5 ReleaseId (org.kie.api.builder.ReleaseId)5 KieContainer (org.kie.api.runtime.KieContainer)5 KieSession (org.kie.api.runtime.KieSession)5 PropertyAccessException (org.mvel2.PropertyAccessException)5 ConsequenceException (org.kie.api.runtime.rule.ConsequenceException)3 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)3 Pattern (java.util.regex.Pattern)1 Triple (org.drools.core.util.Triple)1 TripleStore (org.drools.core.util.TripleStore)1 CompileException (org.mvel2.CompileException)1 Label (org.mvel2.asm.Label)1 BinaryOperation (org.mvel2.ast.BinaryOperation)1 Contains (org.mvel2.ast.Contains)1 Function (org.mvel2.ast.Function)1 Instance (org.mvel2.ast.Instance)1 RegExMatch (org.mvel2.ast.RegExMatch)1 Soundslike (org.mvel2.ast.Soundslike)1