Search in sources :

Example 21 with CompiledExpression

use of org.mule.mvel2.compiler.CompiledExpression in project drools by kiegroup.

the class ResultVerifier method isSuccess.

protected Boolean isSuccess(VerifyField currentField) {
    String s = "__fact__." + currentField.getFieldName() + " " + currentField.getOperator() + " __expected__";
    CompiledExpression expression = new ExpressionCompiler(s, parserContext).compile();
    return (Boolean) MVELSafeHelper.getEvaluator().executeExpression(expression, variables);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 22 with CompiledExpression

use of org.mule.mvel2.compiler.CompiledExpression in project drools by kiegroup.

the class MVELDebugTest method testDebug.

@Test
public void testDebug() throws Exception {
    String rule = "package com.sample; dialect \"mvel\" rule myRule when then\n System.out.println( \"test\" ); end";
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr packageDescr = parser.parse(null, rule);
    RuleDescr ruleDescr = packageDescr.getRules().get(0);
    builder = new KnowledgeBuilderImpl();
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage("com.sample");
    MVELConsequence consequence = (MVELConsequence) pkg.getRule("myRule").getConsequence();
    String sourceName = ((CompiledExpression) consequence.getCompExpr()).getSourceName();
    System.out.println(sourceName);
    String ruleName = ruleDescr.getNamespace() + "." + ruleDescr.getClassName();
    System.out.println(ruleName);
    assertEquals(sourceName, ruleName);
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) MVELConsequence(org.drools.core.base.mvel.MVELConsequence) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompiledExpression(org.mvel2.compiler.CompiledExpression) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 23 with CompiledExpression

use of org.mule.mvel2.compiler.CompiledExpression in project drools by kiegroup.

the class ConditionAnalyzer method analyzeNodeAccessor.

private Expression analyzeNodeAccessor(Accessor accessor, ASTNode node) {
    AccessorNode accessorNode;
    if (accessor instanceof DynamicGetAccessor) {
        accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
    } else if (accessor instanceof AccessorNode) {
        accessorNode = (AccessorNode) accessor;
    } else if (accessor instanceof CompiledExpression) {
        return analyzeNode(((CompiledExpression) accessor).getFirstNode());
    } else if (accessor instanceof ListCreator) {
        return analyzeListCreation(((ListCreator) accessor));
    } else if (accessor instanceof ArrayCreator) {
        return analyzeArrayCreation(((ArrayCreator) accessor));
    } else {
        throw new RuntimeException("Unknown accessor type: " + accessor);
    }
    if (accessorNode instanceof VariableAccessor) {
        if (isStaticAccessor(accessorNode)) {
            while (accessorNode instanceof VariableAccessor) {
                accessorNode = accessorNode.getNextNode();
            }
        } else {
            return analyzeNodeAccessor(accessorNode, node);
        }
    }
    while (accessorNode instanceof StaticReferenceAccessor) {
        StaticReferenceAccessor staticReferenceAccessor = ((StaticReferenceAccessor) accessorNode);
        Object literal = staticReferenceAccessor.getLiteral();
        accessorNode = accessorNode.getNextNode();
        if (accessorNode == null) {
            return new FixedExpression(literal.getClass(), literal);
        }
    }
    return analyzeExpressionNode(accessorNode, node, null);
}
Also used : AccessorNode(org.mvel2.compiler.AccessorNode) DynamicGetAccessor(org.mvel2.optimizers.dynamic.DynamicGetAccessor) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) ArrayCreator(org.mvel2.optimizers.impl.refl.collection.ArrayCreator) CompiledExpression(org.mvel2.compiler.CompiledExpression) ListCreator(org.mvel2.optimizers.impl.refl.collection.ListCreator) StaticReferenceAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)

Example 24 with CompiledExpression

use of org.mule.mvel2.compiler.CompiledExpression in project drools by kiegroup.

the class MvelConstraint method createMvelConditionEvaluator.

protected ConditionEvaluator createMvelConditionEvaluator(InternalWorkingMemory workingMemory) {
    if (compilationUnit != null) {
        MVELDialectRuntimeData data = getMVELDialectRuntimeData(workingMemory);
        ExecutableStatement statement = (ExecutableStatement) compilationUnit.getCompiledExpression(data, evaluationContext);
        ParserConfiguration configuration = statement instanceof CompiledExpression ? ((CompiledExpression) statement).getParserConfiguration() : data.getParserConfiguration();
        return new MvelConditionEvaluator(compilationUnit, configuration, statement, declarations, operators, getAccessedClass());
    } else {
        return new MvelConditionEvaluator(getParserConfiguration(workingMemory), expression, declarations, operators, getAccessedClass());
    }
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) ExecutableStatement(org.mvel2.compiler.ExecutableStatement) CompiledExpression(org.mvel2.compiler.CompiledExpression) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 25 with CompiledExpression

use of org.mule.mvel2.compiler.CompiledExpression in project mvel by mvel.

the class CoreConfidenceTests method testJIRA168.

public void testJIRA168() {
    boolean before = MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL;
    try {
        Map<String, Object> st = new HashMap<String, Object>();
        st.put("__fact__", new ArrayList());
        st.put("__expected__", 0);
        String expressionNaked = "__fact__.size == __expected__";
        String expressionNonNaked = "__fact__.size() == __expected__";
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
        // the following works fine
        ParserContext ctx = new ParserContext();
        for (Map.Entry<String, Object> entry : st.entrySet()) {
            ctx.addInput(entry.getKey(), entry.getValue().getClass());
        }
        CompiledExpression expr = new ExpressionCompiler(expressionNaked, ctx).compile();
        Boolean result = (Boolean) executeExpression(expr, st);
        assertTrue(result);
        // the following works fine
        result = (Boolean) MVEL.eval(expressionNonNaked, st);
        assertTrue(result);
        // the following fails
        result = (Boolean) MVEL.eval(expressionNaked, st);
        assertTrue(result);
    } finally {
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = before;
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) MapObject(org.mvel2.tests.core.res.MapObject) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Aggregations

CompiledExpression (org.mvel2.compiler.CompiledExpression)81 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)65 ParserContext (org.mvel2.ParserContext)48 HashMap (java.util.HashMap)20 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)20 Foo (org.mvel2.tests.core.res.Foo)20 Debugger (org.mvel2.debug.Debugger)16 Frame (org.mvel2.debug.Frame)16 CompiledExpression (org.mule.mvel2.compiler.CompiledExpression)15 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)14 DataType (org.mule.runtime.api.metadata.DataType)11 HashSet (java.util.HashSet)10 Test (org.junit.Test)9 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)9 ParserContext (org.mule.mvel2.ParserContext)8 Map (java.util.Map)6 MVELExpressionLanguage (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage)6 CompileException (org.mvel2.CompileException)6 ASTNode (org.mvel2.ast.ASTNode)6 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)6