Search in sources :

Example 31 with ParserContext

use of org.mule.mvel2.ParserContext in project drools by kiegroup.

the class MVELTest method test1.

@Test
public void test1() {
    final ParserContext pc = new ParserContext();
    pc.addInput("x", String.class);
    pc.setStrongTyping(true);
    final Object o = MVEL.compileExpression("x.startsWith('d')", pc);
    final Map vars = new HashMap();
    vars.put("x", "d");
    MVEL.executeExpression(o, vars);
    System.out.println(o);
}
Also used : HashMap(java.util.HashMap) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 32 with ParserContext

use of org.mule.mvel2.ParserContext in project drools by kiegroup.

the class QueryElementBuilder method getParserContext.

private ParserContext getParserContext(RuleBuildContext context) {
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
    ParserConfiguration conf = data.getParserConfiguration();
    conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
    return new ParserContext(conf);
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 33 with ParserContext

use of org.mule.mvel2.ParserContext in project drools by kiegroup.

the class PatternBuilder method getFieldValue.

private FieldValue getFieldValue(RuleBuildContext context, ValueType vtype, String value) {
    try {
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
        MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
        MVEL.COMPILER_OPT_ALLOW_RESOLVE_INNERCLASSES_WITH_DOTNOTATION = true;
        MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = true;
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        ParserConfiguration pconf = data.getParserConfiguration();
        ParserContext pctx = new ParserContext(pconf);
        Object o = MVELSafeHelper.getEvaluator().executeExpression(MVEL.compileExpression(value, pctx));
        if (o != null && vtype == null) {
            // was a compilation problem else where, so guess valuetype so we can continue
            vtype = ValueType.determineValueType(o.getClass());
        }
        return context.getCompilerFactory().getFieldFactory().getFieldValue(o, vtype);
    } catch (final Exception e) {
    // we will fallback to regular preducates, so don't raise an error
    }
    return null;
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) ParserContext(org.mvel2.ParserContext) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 34 with ParserContext

use of org.mule.mvel2.ParserContext in project drools by kiegroup.

the class ConditionAnalyzer method analyzeNode.

private Expression analyzeNode(ASTNode node) {
    node = analyzeRegEx(analyzeSubstatement(node));
    if (node instanceof LiteralNode) {
        LiteralNode literalNode = (LiteralNode) node;
        return new FixedExpression(literalNode.getEgressType(), literalNode.getLiteralValue());
    }
    if (node instanceof BinaryOperation) {
        BinaryOperation op = (BinaryOperation) node;
        return new AritmeticExpression(analyzeNode(op.getLeft()), AritmeticOperator.fromMvelOpCode(op.getOperation()), analyzeNode(op.getRight()));
    }
    if (node instanceof TypeCast) {
        ExecutableStatement statement = ((TypeCast) node).getStatement();
        if (statement instanceof ExecutableAccessor) {
            ExecutableAccessor accessor = (ExecutableAccessor) statement;
            return new CastExpression(node.getEgressType(), analyzeNode(accessor.getNode()));
        } else {
            ExecutableLiteral literal = (ExecutableLiteral) statement;
            return new CastExpression(node.getEgressType(), new FixedExpression(literal.getLiteral()));
        }
    }
    if (node instanceof Union) {
        ASTNode main = ((Union) node).getMain();
        Accessor accessor = node.getAccessor();
        EvaluatedExpression expression = new EvaluatedExpression();
        expression.firstExpression = analyzeNode(main);
        if (accessor instanceof DynamicGetAccessor) {
            AccessorNode accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
            expression.addInvocation(analyzeAccessorInvocation(accessorNode, node, null, null));
        } else if (accessor instanceof AccessorNode) {
            AccessorNode accessorNode = (AccessorNode) accessor;
            while (accessorNode != null) {
                expression.addInvocation(analyzeAccessorInvocation(accessorNode, node, null, null));
                accessorNode = accessorNode.getNextNode();
            }
        } else {
            throw new RuntimeException("Unexpected accessor: " + accessor);
        }
        return expression;
    }
    if (node instanceof Sign) {
        ExecutableStatement statement = getFieldValue(Sign.class, "stmt", (Sign) node);
        if (statement instanceof ExecutableAccessor) {
            ExecutableAccessor accessor = (ExecutableAccessor) statement;
            return new AritmeticExpression(new FixedExpression(0), AritmeticOperator.SUB, analyzeNode(accessor.getNode()));
        } else {
            ExecutableLiteral literal = (ExecutableLiteral) statement;
            return new AritmeticExpression(new FixedExpression(0), AritmeticOperator.SUB, new FixedExpression(literal.getLiteral()));
        }
    }
    Accessor accessor = node.getAccessor();
    if (accessor instanceof IndexedVariableAccessor) {
        String variableName = node.getName();
        int dot = variableName.indexOf('.');
        if (dot > 0) {
            variableName = variableName.substring(0, dot);
        }
        Class<?> variableType = getVariableType(variableName);
        return new VariableExpression(variableName, analyzeExpressionNode(((AccessorNode) accessor).getNextNode(), node, variableType), variableType != null ? variableType : node.getEgressType());
    }
    if (accessor == null && node instanceof NewObjectNode) {
        accessor = ((NewObjectNode) node).getNewObjectOptimizer();
    }
    if (accessor instanceof VariableAccessor) {
        VariableAccessor variableAccessor = (VariableAccessor) accessor;
        AccessorNode accessorNode = variableAccessor.getNextNode();
        if (accessorNode == null || !isStaticAccessor(accessorNode)) {
            String variableName = (String) (variableAccessor.getProperty());
            Class<?> variableType = getVariableType(variableName);
            if (variableType != null) {
                return new VariableExpression(variableName, analyzeExpressionNode(accessorNode, node, variableType), variableType);
            } else {
                if (node.getLiteralValue() instanceof ParserContext) {
                    ParserContext pCtx = (ParserContext) node.getLiteralValue();
                    // it's not a variable but a method invocation on this
                    Class<?> thisClass = pCtx.getInputs().get("this");
                    try {
                        return new EvaluatedExpression(new MethodInvocation(thisClass.getMethod(variableName)));
                    } catch (NoSuchMethodException e) {
                        if (node.getEgressType() == Class.class) {
                            // there's no method on this with the given name, check if it is a class literal
                            Class<?> classLiteral = pCtx.getParserConfiguration().getImport(variableName);
                            if (classLiteral != null) {
                                return new FixedExpression(Class.class, classLiteral);
                            }
                        }
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
    if (accessor == null) {
        throw new RuntimeException("Null accessor on node: " + node);
    }
    return analyzeNodeAccessor(accessor, node);
}
Also used : BinaryOperation(org.mvel2.ast.BinaryOperation) ExecutableAccessor(org.mvel2.compiler.ExecutableAccessor) FieldAccessor(org.mvel2.optimizers.impl.refl.nodes.FieldAccessor) StaticVarAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor) ListAccessor(org.mvel2.optimizers.impl.refl.nodes.ListAccessor) ExecutableAccessor(org.mvel2.compiler.ExecutableAccessor) GetterAccessor(org.mvel2.optimizers.impl.refl.nodes.GetterAccessor) DynamicGetAccessor(org.mvel2.optimizers.dynamic.DynamicGetAccessor) ExprValueAccessor(org.mvel2.optimizers.impl.refl.collection.ExprValueAccessor) MapAccessor(org.mvel2.optimizers.impl.refl.nodes.MapAccessor) ThisValueAccessor(org.mvel2.optimizers.impl.refl.nodes.ThisValueAccessor) Accessor(org.mvel2.compiler.Accessor) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) MethodAccessor(org.mvel2.optimizers.impl.refl.nodes.MethodAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) ConstructorAccessor(org.mvel2.optimizers.impl.refl.nodes.ConstructorAccessor) StaticReferenceAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor) ArrayAccessor(org.mvel2.optimizers.impl.refl.nodes.ArrayAccessor) Union(org.mvel2.ast.Union) AccessorNode(org.mvel2.compiler.AccessorNode) DynamicGetAccessor(org.mvel2.optimizers.dynamic.DynamicGetAccessor) ExecutableLiteral(org.mvel2.compiler.ExecutableLiteral) ASTNode(org.mvel2.ast.ASTNode) ExecutableStatement(org.mvel2.compiler.ExecutableStatement) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) LiteralNode(org.mvel2.ast.LiteralNode) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) NewObjectNode(org.mvel2.ast.NewObjectNode) Sign(org.mvel2.ast.Sign) TypeCast(org.mvel2.ast.TypeCast) ParserContext(org.mvel2.ParserContext)

Example 35 with ParserContext

use of org.mule.mvel2.ParserContext in project mvel by mvel.

the class ForNode method buildForEach.

private boolean buildForEach(char[] condition, int start, int offset, int blockStart, int blockEnd, int fields, ParserContext pCtx) {
    int end = start + offset;
    int cursor = nextCondPart(condition, start, end, false);
    boolean varsEscape = false;
    try {
        ParserContext spCtx;
        if (pCtx != null) {
            spCtx = pCtx.createSubcontext().createColoringSubcontext();
        } else {
            spCtx = new ParserContext();
        }
        this.initializer = (ExecutableStatement) subCompileExpression(condition, start, cursor - start - 1, spCtx);
        if (pCtx != null) {
            pCtx.pushVariableScope();
        }
        try {
            expectType(pCtx, this.condition = (ExecutableStatement) subCompileExpression(condition, start = cursor, (cursor = nextCondPart(condition, start, end, false)) - start - 1, spCtx), Boolean.class, ((fields & COMPILE_IMMEDIATE) != 0));
        } catch (CompileException e) {
            if (e.getExpr().length == 0) {
                e.setExpr(expr);
                while (start < expr.length && ParseTools.isWhitespace(expr[start])) {
                    start++;
                }
                e.setCursor(start);
            }
            throw e;
        }
        this.after = (ExecutableStatement) subCompileExpression(condition, start = cursor, (nextCondPart(condition, start, end, true)) - start, spCtx);
        if (spCtx != null && (fields & COMPILE_IMMEDIATE) != 0 && spCtx.isVariablesEscape()) {
            if (pCtx != spCtx)
                pCtx.addVariables(spCtx.getVariables());
            varsEscape = true;
        } else if (spCtx != null && pCtx != null) {
            pCtx.addVariables(spCtx.getVariables());
        }
        this.compiledBlock = (ExecutableStatement) subCompileExpression(expr, blockStart, blockEnd, spCtx);
        if (pCtx != null) {
            pCtx.setInputs(spCtx.getInputs());
        }
    } catch (NegativeArraySizeException e) {
        throw new CompileException("wrong syntax; did you mean to use 'foreach'?", expr, start);
    }
    return varsEscape;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) CompileException(org.mvel2.CompileException) ParserContext(org.mvel2.ParserContext)

Aggregations

ParserContext (org.mvel2.ParserContext)340 HashMap (java.util.HashMap)128 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)119 Serializable (java.io.Serializable)82 ParserConfiguration (org.mvel2.ParserConfiguration)70 Map (java.util.Map)64 LinkedHashMap (java.util.LinkedHashMap)62 CompiledExpression (org.mvel2.compiler.CompiledExpression)48 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)42 CompileException (org.mvel2.CompileException)37 Foo (org.mvel2.tests.core.res.Foo)24 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)23 ArrayList (java.util.ArrayList)20 List (java.util.List)19 MapObject (org.mvel2.tests.core.res.MapObject)18 Debugger (org.mvel2.debug.Debugger)15 Frame (org.mvel2.debug.Frame)15 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)14 HashSet (java.util.HashSet)12 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10