@Override public Object run() { return MVEL.executeExpression(compiler.compile(), new HashMap()); } }, KiePolicyHelper.getAccessContext());
public static Object eval(final String expression) { ExpressionCompiler compiler = new ExpressionCompiler(getRevisedExpression(expression), WID_PARSER_CONTEXT); if(KiePolicyHelper.isPolicyEnabled()) { return AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { return MVEL.executeExpression(compiler.compile(), new HashMap()); } }, KiePolicyHelper.getAccessContext()); } else { return MVEL.executeExpression(compiler.compile(), new HashMap()); } }
public final void testTypeByMethod() { ParserContext context = new ParserContext(); context.setStrongTyping(true); context.addInput("a", A.class); CompiledExpression compiledExpression = new ExpressionCompiler("!a.show", context).compile(); assertEquals(Boolean.class, compiledExpression.getKnownEgressType()); }
/** * Performs an analysis compileShared, which will populate the ParserContext with type, input and variable information, * but will not produce a payload. * * @param expression - the expression to analyze * @param ctx - the parser context */ public static void analysisCompile(char[] expression, ParserContext ctx) { ExpressionCompiler compiler = new ExpressionCompiler(expression, ctx); compiler.setVerifyOnly(true); compiler.compile(); }
ParserContext parserContext = new ParserContext(); ExpressionCompiler compiler = new ExpressionCompiler(actionString, parserContext); compiler.setVerifying(true); compiler.compile(); List<ErrorDetail> mvelErrors = parserContext.getErrorList(); if (mvelErrors != null) {
private static Serializable _optimizeTree(final CompiledExpression compiled) { /** * If there is only one token, and it's an identifier, we can optimize this as an accessor expression. */ if (compiled.isSingleNode()) { ASTNode tk = compiled.getFirstNode(); if (tk.isLiteral() && !tk.isThisVal()) { return new ExecutableLiteral(tk.getLiteralValue()); } return tk.canSerializeAccessor() ? new ExecutableAccessorSafe(tk, compiled.getKnownEgressType()) : new ExecutableAccessor(tk, compiled.getKnownEgressType()); } return compiled; }
public ConditionAnalyzer(ExecutableStatement stmt, Declaration[] declarations, String conditionClass) { this.declarations = declarations; this.conditionClass = conditionClass; if (stmt instanceof ExecutableLiteral) { executableLiteral = (ExecutableLiteral)stmt; } else if (stmt instanceof CompiledExpression) { node = ((CompiledExpression)stmt).getFirstNode(); } else { node = ((ExecutableAccessor)stmt).getNode(); } }
private Object[] executeAll(Object ctx, VariableResolverFactory vars) { if (length == 0) return GetterAccessor.EMPTY; Object[] vals = new Object[length]; for (int i = 0; i < length; i++) { vals[i] = parms[i].getValue(ctx, vars); } return vals; }
public Object getValue(Object ctx, Object elCtx, VariableResolverFactory vars) { if (nextNode != null) { return this.nextNode.getValue(elCtx, elCtx, vars); } else { return elCtx; } }
/** * Increment one cursor position, and move cursor to next non-blank part. * * @return cursor position */ public int incNextNonBlank() { cursor++; return nextNonBlank(); }
@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); }
@Test public void testDebugSymbolCount() { String expr = "System.out.println( \"a1\" );\n" + "System.out.println( \"a2\" );\n" + "System.out.println( \"a3\" );\n" + "System.out.println( \"a4\" );\n"; ParserContext context = new ParserContext(); context.setDebugSymbols( true ); context.addImport( "System", System.class ); context.setStrictTypeEnforcement( true ); //context.setDebugSymbols( true ); context.setSourceFile( "mysource" ); ExpressionCompiler compiler = new ExpressionCompiler( expr, context ); Serializable compiledExpression = compiler.compile(); String s = DebugTools.decompile( compiledExpression ); System.out.println( "s " + s ); int fromIndex = 0; int count = 0; while ( (fromIndex = s.indexOf( "DEBUG_SYMBOL", fromIndex + 1 )) > -1 ) { count++; } assertEquals( 4, count ); }
private ASTNode getRootNode() { if (executableStatement instanceof ExecutableLiteral) { return null; } return executableStatement instanceof CompiledExpression ? ((CompiledExpression) executableStatement).getFirstNode() : ((ExecutableAccessor) executableStatement).getNode(); } }
/** * Executes a compiled expression. * * @param compiledExpression - * @param factory - * @return - * @see #compileExpression(String) */ public static Object executeExpression(final Object compiledExpression, final VariableResolverFactory factory) { return ((ExecutableStatement) compiledExpression).getValue(null, factory); }