Java Code Examples for org.mvel2.MVEL#executeExpression()

The following examples show how to use org.mvel2.MVEL#executeExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MVELSalienceExpression.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public int getValue(final KnowledgeHelper khelper,
                    final Rule rule,
                    final WorkingMemory workingMemory) {
    VariableResolverFactory factory = unit.getFactory( khelper, 
                                                       khelper != null ? ((AgendaItem)khelper.getMatch()).getTerminalNode().getSalienceDeclarations() : null, 
                                                       rule, null, 
                                                       khelper != null ? (LeftTuple) khelper.getMatch().getTuple() : null, 
                                                       null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver() );
    
    // do we have any functions for this namespace?
    InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage( "MAIN" );
    if ( pkg != null ) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData( this.id );
        factory.setNextFactory( data.getFunctionFactory() );
    }

    Object value = MVEL.executeExpression( this.expr, factory );
    if (value instanceof String) {
        value = TimeUtils.parseTimeString( (String)value );
    }
    return ((Number)value).intValue();
}
 
Example 2
Source File: WidMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
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());
    }
}
 
Example 3
Source File: MVELAccumulatorFunctionExecutor.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public void accumulate(Object workingMemoryContext,
                       Object context,
                       Tuple tuple,
                       InternalFactHandle handle,
                       Declaration[] declarations,
                       Declaration[] innerDeclarations,
                       WorkingMemory workingMemory) throws Exception {
    
    VariableResolverFactory factory = unit.getFactory( null, null, null, handle, tuple, null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver()  );
    
    final Object value = MVEL.executeExpression( this.expression,
                                                 handle.getObject(),
                                                 factory );
    if ( this.function.supportsReverse() ) {
        ((MVELAccumulatorFunctionContext) context).reverseSupport.put( handle.getId(), value );
    }
    this.function.accumulate( ((MVELAccumulatorFunctionContext) context).context,
                              value );
}
 
Example 4
Source File: ExpressionProvider.java    From test-data-generator with Apache License 2.0 6 votes vote down vote up
@Override
public Object nextValue(Context context, Field field) {
    Class type;
    switch (field.getType()) {
        case TypeConst.STRING:
            type = String.class;
            break;
        case TypeConst.LONG:
            type = Long.class;
            break;
        case TypeConst.INT:
            type = Integer.class;
            break;
        case TypeConst.BOOLEAN:
            type = Boolean.class;
            break;
        default:
            throw new IllegalArgumentException("Field type not known: " + field.getType());
    }
    synchronized (context.getVariables()) {
        return MVEL.executeExpression(compiledExpression, context, context.getVariables(), type);
    }
}
 
Example 5
Source File: MVELTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: MvelRule.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
/**
 * 执行条件匹配后的操作
 */
@Override
public void execute(RuleContext ruleContext) {
    try {
        Serializable exp = MVEL.compileExpression(getAction(), ruleContext);
        MVEL.executeExpression(exp, ruleContext);
    } catch (Exception e) {
        throw new RuntimeException(String.format("后续操作[%s]执行发生异常:", getAction()), e);
    }
}
 
Example 7
Source File: ExpressionLanguageMVELImpl.java    From oval with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
   LOG.debug("Evaluating MVEL expression: {1}", expression);
   try {
      final Object expr = expressionCache.get(expression);
      return MVEL.executeExpression(expr, values);
   } catch (final Exception ex) {
      throw new ExpressionEvaluationException("Evaluating MVEL expression failed: " + expression, ex);
   }
}
 
Example 8
Source File: ClassTests.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Test
public void testCompileExpression() {
    String expression = "foobar > 99";
    Serializable compiled = MVEL.compileExpression(expression);
    Map vars = new HashMap();
    vars.put("foobar", new Integer(100));
    Boolean result = (Boolean) MVEL.executeExpression(compiled, vars);
    Assertions.assertEquals(true, result);
}
 
Example 9
Source File: MVELConsequence.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public void evaluate(final KnowledgeHelper knowledgeHelper,
                     final WorkingMemory workingMemory) throws Exception {
    
    VariableResolverFactory factory = unit.getFactory( knowledgeHelper,  ((AgendaItem)knowledgeHelper.getMatch()).getTerminalNode().getRequiredDeclarations(),
                                                       knowledgeHelper.getRule(), knowledgeHelper.getTuple(), null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver()  );
    
    // do we have any functions for this namespace?
    InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage( "MAIN" );
    if ( pkg != null ) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData( this.id );
        factory.setNextFactory( data.getFunctionFactory() );
    }

    CompiledExpression compexpr = (CompiledExpression) this.expr;

    if ( MVELDebugHandler.isDebugMode() ) {
        if ( MVELDebugHandler.verbose ) {
            logger.info(DebugTools.decompile(compexpr));
        }
        MVEL.executeDebugger( compexpr,
                              knowledgeHelper,
                              factory );
    } else {
        MVEL.executeExpression( compexpr,
                                knowledgeHelper,
                                factory );
    }
}
 
Example 10
Source File: MvelFunction.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T doWith(Class<T> clazz, Object... arguments) {
    MvelHolder holder = threadHolder.get();
    for (int index = 0, size = classes.length; index < size; index++) {
        holder.scope.createAttribute(StringUtility.format("argument{}", index), arguments[index]);
    }
    T object = (T) MVEL.executeExpression(script, threadHolder.get().scope.getAttributes(), clazz);
    holder.scope.deleteAttributes();
    return object;
}
 
Example 11
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public Object executeExpression(final Object compiledExpression, final Object ctx, final VariableResolverFactory resolverFactory) {
    return MVEL.executeExpression(compiledExpression, ctx, resolverFactory);
}
 
Example 12
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public void executeExpression(final Iterable<CompiledExpression> compiledExpression, final Object ctx, final VariableResolverFactory vars) {
    MVEL.executeExpression(compiledExpression, ctx, vars);
}
 
Example 13
Source File: ExpressionHandler.java    From nifi with Apache License 2.0 4 votes vote down vote up
private void executeMVEL(String command, Map<String, Object> facts) {
    MVEL.executeExpression(MVEL.compileExpression(command), facts);
    if(getLogger().isDebugEnabled()) {
        getLogger().debug("Expression was executed successfully: {}: {}", new Object[]{type, command});
    }
}
 
Example 14
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public Object executeExpression(final Object compiledExpression, final Map vars) {
    return MVEL.executeExpression(compiledExpression, vars);
}
 
Example 15
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T executeExpression(final Object compiledExpression, final Object ctx, final Map vars, final Class<T> toType) {
    return MVEL.executeExpression(compiledExpression, ctx, vars, toType);
}
 
Example 16
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T executeExpression(final Object compiledExpression, final Object ctx, final VariableResolverFactory vars, final Class<T> toType) {
    return MVEL.executeExpression(compiledExpression, ctx, vars, toType);
}
 
Example 17
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T executeExpression(final Object compiledExpression, final Map vars, final Class<T> toType) {
    return MVEL.executeExpression(compiledExpression, vars, toType);
}
 
Example 18
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T executeExpression(final Object compiledExpression, final Object ctx, final Class<T> toType) {
    return MVEL.executeExpression(compiledExpression, ctx, toType);
}
 
Example 19
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public void executeExpression(final Iterable<CompiledExpression> compiledExpression) {
    MVEL.executeExpression(compiledExpression);
}
 
Example 20
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public Object executeExpression(final Object compiledExpression) {
    return MVEL.executeExpression(compiledExpression);
}