Search in sources :

Example 61 with ParserConfiguration

use of org.mvel2.ParserConfiguration in project drools by kiegroup.

the class MvelConstraintTestUtil method getParserConfiguration.

@Override
protected ParserConfiguration getParserConfiguration(InternalWorkingMemory workingMemory) {
    ParserConfiguration parserConfiguration = new ParserConfiguration();
    parserConfiguration.addImport(Cheese.class);
    return parserConfiguration;
}
Also used : ParserConfiguration(org.mvel2.ParserConfiguration)

Example 62 with ParserConfiguration

use of org.mvel2.ParserConfiguration in project drools by kiegroup.

the class MVELExprAnalyzer method analyzeExpression.

// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
/**
 * Analyze an expression.
 *
 * @param expr
 *            The expression to analyze.
 * @param availableIdentifiers
 *            Total set of declarations available.
 *
 * @return The <code>Set</code> of declarations used by the expression.
 * @throws RecognitionException
 *             If an error occurs in the parser.
 */
@SuppressWarnings("unchecked")
public static MVELAnalysisResult analyzeExpression(final PackageBuildContext context, final String expr, final BoundIdentifiers availableIdentifiers, final Map<String, Class<?>> localTypes, String contextIdentifier, Class kcontextClass) {
    if (expr.trim().length() <= 0) {
        MVELAnalysisResult result = analyze((Set<String>) Collections.EMPTY_SET, availableIdentifiers);
        result.setMvelVariables(new HashMap<String, Class<?>>());
        result.setTypesafe(true);
        return result;
    }
    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;
    MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
    ParserConfiguration conf = context.getMVELDialectRuntimeData().getParserConfiguration();
    conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
    // first compilation is for verification only
    // @todo proper source file name
    final ParserContext parserContext1 = new ParserContext(conf);
    if (localTypes != null) {
        for (Entry entry : localTypes.entrySet()) {
            parserContext1.addInput((String) entry.getKey(), (Class) entry.getValue());
        }
    }
    if (availableIdentifiers.getThisClass() != null) {
        parserContext1.addInput("this", availableIdentifiers.getThisClass());
    }
    if (availableIdentifiers.getOperators() != null) {
        for (Entry<String, EvaluatorWrapper> opEntry : availableIdentifiers.getOperators().entrySet()) {
            parserContext1.addInput(opEntry.getKey(), opEntry.getValue().getClass());
        }
    }
    parserContext1.setStrictTypeEnforcement(false);
    parserContext1.setStrongTyping(false);
    parserContext1.setInterceptors(dialect.getInterceptors());
    Class<?> returnType;
    try {
        returnType = MVEL.analyze(expr, parserContext1);
    } catch (Exception e) {
        BaseDescr base = (context instanceof RuleBuildContext) ? ((RuleBuildContext) context).getRuleDescr() : context.getParentDescr();
        if (e instanceof CompileException && e.getCause() != null && e.getMessage().startsWith("[Error: null]")) {
            // rewrite error message in cause original message is null
            e = new CompileException(e.getCause().toString(), ((CompileException) e).getExpr(), ((CompileException) e).getCursor(), e.getCause());
        }
        DialectUtil.copyErrorLocation(e, context.getParentDescr());
        context.addError(new DescrBuildError(base, context.getParentDescr(), null, "Unable to Analyse Expression " + expr + ":\n" + e.getMessage()));
        return null;
    }
    Set<String> requiredInputs = new HashSet<String>();
    requiredInputs.addAll(parserContext1.getInputs().keySet());
    HashMap<String, Class<?>> variables = (HashMap<String, Class<?>>) ((Map) parserContext1.getVariables());
    if (localTypes != null) {
        for (String str : localTypes.keySet()) {
            // we have to do this due to mvel regressions on detecting true local vars
            variables.remove(str);
        }
    }
    // MVEL includes direct fields of context object in non-strict mode. so we need to strip those
    if (availableIdentifiers.getThisClass() != null) {
        requiredInputs.removeIf(s -> PropertyTools.getFieldOrAccessor(availableIdentifiers.getThisClass(), s) != null);
    }
    // now, set the required input types and compile again
    final ParserContext parserContext2 = new ParserContext(conf);
    parserContext2.setStrictTypeEnforcement(true);
    parserContext2.setStrongTyping(true);
    parserContext2.setInterceptors(dialect.getInterceptors());
    for (String input : requiredInputs) {
        if ("this".equals(input)) {
            continue;
        }
        Class<?> cls = availableIdentifiers.resolveType(input);
        if (cls == null) {
            if (input.equals(contextIdentifier) || input.equals("kcontext")) {
                cls = kcontextClass;
            } else if (input.equals("rule")) {
                cls = Rule.class;
            } else if (localTypes != null) {
                cls = localTypes.get(input);
            }
        }
        if (cls != null) {
            parserContext2.addInput(input, cls);
        }
    }
    if (availableIdentifiers.getThisClass() != null) {
        parserContext2.addInput("this", availableIdentifiers.getThisClass());
    }
    boolean typesafe = context.isTypesafe();
    try {
        returnType = MVEL.analyze(expr, parserContext2);
        typesafe = true;
    } catch (Exception e) {
        // is this an error, or can we fall back to non-typesafe mode?
        if (typesafe) {
            BaseDescr base = (context instanceof RuleBuildContext) ? ((RuleBuildContext) context).getRuleDescr() : context.getParentDescr();
            DialectUtil.copyErrorLocation(e, context.getParentDescr());
            context.addError(new DescrBuildError(base, context.getParentDescr(), null, "Unable to Analyse Expression " + expr + ":\n" + e.getMessage()));
            return null;
        }
    }
    if (typesafe) {
        requiredInputs = new HashSet<String>();
        requiredInputs.addAll(parserContext2.getInputs().keySet());
        requiredInputs.addAll(variables.keySet());
        variables = (HashMap<String, Class<?>>) ((Map) parserContext2.getVariables());
        if (localTypes != null) {
            for (String str : localTypes.keySet()) {
                // we have to do this due to mvel regressions on detecting true local vars
                variables.remove(str);
            }
        }
    }
    MVELAnalysisResult result = analyze(requiredInputs, availableIdentifiers);
    result.setReturnType(returnType);
    result.setMvelVariables(variables);
    result.setTypesafe(typesafe);
    return result;
}
Also used : EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) HashMap(java.util.HashMap) CompileException(org.mvel2.CompileException) RecognitionException(org.antlr.runtime.RecognitionException) ParserConfiguration(org.mvel2.ParserConfiguration) Entry(java.util.Map.Entry) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) CompileException(org.mvel2.CompileException) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Rule(org.kie.api.definition.rule.Rule) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 63 with ParserConfiguration

use of org.mvel2.ParserConfiguration in project drools by kiegroup.

the class MVELExprAnalyzer method getExpressionType.

public static Class<?> getExpressionType(PackageBuildContext context, Map<String, Class<?>> declCls, RuleConditionElement source, String expression) {
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
    ParserConfiguration conf = data.getParserConfiguration();
    conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
    ParserContext pctx = new ParserContext(conf);
    pctx.setStrongTyping(true);
    pctx.setStrictTypeEnforcement(true);
    for (Map.Entry<String, Class<?>> entry : declCls.entrySet()) {
        pctx.addInput(entry.getKey(), entry.getValue());
    }
    for (Declaration decl : source.getOuterDeclarations().values()) {
        pctx.addInput(decl.getBindingName(), decl.getDeclarationClass());
    }
    try {
        return MVEL.analyze(expression, pctx);
    } catch (Exception e) {
        log.warn("Unable to parse expression: " + expression, e);
    }
    return null;
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) Declaration(org.drools.core.rule.Declaration) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map) CompileException(org.mvel2.CompileException) RecognitionException(org.antlr.runtime.RecognitionException) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 64 with ParserConfiguration

use of org.mvel2.ParserConfiguration in project drools by kiegroup.

the class MVELCompilationUnit method getCompiledExpression.

public Serializable getCompiledExpression(MVELDialectRuntimeData runtimeData, Object evaluationContext) {
    ParserConfiguration conf = runtimeData.getParserConfiguration();
    final ParserContext parserContext = new ParserContext(conf, evaluationContext);
    if (MVELDebugHandler.isDebugMode()) {
        parserContext.setDebugSymbols(true);
    }
    parserContext.setStrictTypeEnforcement(strictMode);
    parserContext.setStrongTyping(strictMode);
    parserContext.setIndexAllocation(true);
    if (INTERCEPTORS != null) {
        parserContext.setInterceptors(INTERCEPTORS);
    }
    parserContext.addIndexedInput(inputIdentifiers);
    String identifier = null;
    String type = null;
    try {
        for (int i = 0, length = inputIdentifiers.length; i < length; i++) {
            identifier = inputIdentifiers[i];
            type = inputTypes[i];
            Class<?> cls = loadClass(runtimeData.getPackageClassLoader(), inputTypes[i]);
            parserContext.addInput(inputIdentifiers[i], cls);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Unable to resolve class '" + type + "' for identifier '" + identifier);
    }
    parserContext.setSourceFile(name);
    String[] varNames = parserContext.getIndexedVarNames();
    ExecutableStatement stmt = (ExecutableStatement) compile(expression, parserContext);
    Set<String> localNames = parserContext.getVariables().keySet();
    parserContext.addIndexedLocals(localNames);
    String[] locals = localNames.toArray(new String[localNames.size()]);
    String[] allVars = new String[varNames.length + locals.length];
    System.arraycopy(varNames, 0, allVars, 0, varNames.length);
    System.arraycopy(locals, 0, allVars, varNames.length, locals.length);
    this.varModel = new SimpleVariableSpaceModel(allVars);
    this.allVarsLength = allVars.length;
    return stmt;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) SimpleVariableSpaceModel(org.mvel2.util.SimpleVariableSpaceModel) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 65 with ParserConfiguration

use of org.mvel2.ParserConfiguration in project drools by kiegroup.

the class MVELConstraintBuilder method setExprInputs.

@Override
public void setExprInputs(RuleBuildContext context, PatternBuilder.ExprBindings descrBranch, Class<?> thisClass, String expr) {
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
    ParserConfiguration conf = data.getParserConfiguration();
    conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
    final ParserContext pctx = new ParserContext(conf);
    pctx.setStrictTypeEnforcement(false);
    pctx.setStrongTyping(false);
    pctx.addInput("this", thisClass);
    // overrides the mvel empty label
    pctx.addInput("empty", boolean.class);
    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;
    try {
        MVEL.analysisCompile(expr, pctx);
    } catch (Exception e) {
        // reported during expression analysis, so swallow it at the moment
        return;
    }
    if (!pctx.getInputs().isEmpty()) {
        for (String v : pctx.getInputs().keySet()) {
            // to an "empty" property, or the if will evaluate to true even if it doesn't
            if ("this".equals(v) || (PropertyTools.getFieldOrAccessor(thisClass, v) != null && expr.matches("(^|.*\\W)empty($|\\W.*)"))) {
                descrBranch.getFieldAccessors().add(v);
            } else if ("empty".equals(v)) {
            // do nothing
            } else if (!context.getPkg().getGlobals().containsKey(v)) {
                descrBranch.getRuleBindings().add(v);
            } else {
                descrBranch.getGlobalBindings().add(v);
            }
        }
    }
}
Also used : ParserContext(org.mvel2.ParserContext) IOException(java.io.IOException) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)95 ParserContext (org.mvel2.ParserContext)88 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)48 HashMap (java.util.HashMap)43 LinkedHashMap (java.util.LinkedHashMap)30 Map (java.util.Map)27 Serializable (java.io.Serializable)12 CompileException (org.mvel2.CompileException)9 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)9 IOException (java.io.IOException)7 MapObject (org.mvel2.tests.core.res.MapObject)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 Method (java.lang.reflect.Method)5 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)5 RecognitionException (org.antlr.runtime.RecognitionException)4 PropertyAccessException (org.mvel2.PropertyAccessException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3