org.mvel2.integration.VariableResolverFactory Java Examples

The following examples show how to use org.mvel2.integration.VariableResolverFactory. 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: IBatisSqlActionShardingRule.java    From cobarclient with Apache License 2.0 6 votes vote down vote up
public boolean isDefinedAt(IBatisRoutingFact routingFact) {
    Validate.notNull(routingFact);
    boolean matches = StringUtils.equals(getTypePattern(), routingFact.getAction());
    if (matches) {
        try {
            Map<String, Object> vrs = new HashMap<String, Object>();
            vrs.putAll(getFunctionMap());
            vrs.put("$ROOT", routingFact.getArgument()); // add top object reference for expression
            VariableResolverFactory vrfactory = new MapVariableResolverFactory(vrs);
            if (MVEL.evalToBoolean(getAttributePattern(), routingFact.getArgument(), vrfactory)) {
                return true;
            }
        } catch (Throwable t) {
            logger
                    .info(
                            "failed to evaluate attribute expression:'{}' with context object:'{}'\n{}",
                            new Object[] { getAttributePattern(), routingFact.getArgument(), t });
        }
    }

    return false;
}
 
Example #2
Source File: IBatisNamespaceShardingRule.java    From cobarclient with Apache License 2.0 6 votes vote down vote up
public boolean isDefinedAt(IBatisRoutingFact routingFact) {
    Validate.notNull(routingFact);
    String namespace = StringUtils.substringBeforeLast(routingFact.getAction(), ".");
    boolean matches = StringUtils.equals(namespace, getTypePattern());
    if (matches) {
        try {
            Map<String, Object> vrs = new HashMap<String, Object>();
            vrs.putAll(getFunctionMap());
            vrs.put("$ROOT", routingFact.getArgument()); // add top object reference for expression
            VariableResolverFactory vrfactory = new MapVariableResolverFactory(vrs);
            if (MVEL.evalToBoolean(getAttributePattern(), routingFact.getArgument(), vrfactory)) {
                return true;
            }
        } catch (Throwable t) {
            logger
                    .info(
                            "failed to evaluate attribute expression:'{}' with context object:'{}'\n{}",
                            new Object[] { getAttributePattern(), routingFact.getArgument(), t });
        }
    }
    return false;
}
 
Example #3
Source File: IMessagePropertyHandler.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
@Override
public Object getProperty(
		String propName,
		Object object,
		VariableResolverFactory arg2) {
	if (object instanceof IMessage) {
		IMessage iMessage = (IMessage)object;
           Object propertyValue = iMessage.getField(propName);
           if(propertyValue instanceof IFilter){
               try {
                   return ((IFilter) propertyValue).getValue();
               } catch(MvelException e) {
                   throw new EPSCommonException("Failed to get value: probably wrong filter type", e);
               }
           }
		return propertyValue;
	}
	throw new IllegalArgumentException("object is not an IMessage");
}
 
Example #4
Source File: WorkflowProcessInstanceImpl.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private String resolveVariable(String s, VariableResolverFactory factory) {
    Map<String, String> replacements = new HashMap<>();
    Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
    while (matcher.find()) {
        String paramName = matcher.group(1);
        if (replacements.get(paramName) == null) {

            Object variableValue = getVariable(paramName);
            if (variableValue != null) {
                replacements.put(paramName, variableValue.toString());
            } else {
                try {
                    variableValue = MVELSafeHelper.getEvaluator().eval(paramName, factory);
                    String variableValueString = variableValue == null ? "" : variableValue.toString();
                    replacements.put(paramName, variableValueString);
                } catch (Throwable t) {
                    logger.error("Could not find variable scope for variable {}", paramName);
                }
            }
        }
    }
    for (Map.Entry<String, String> replacement : replacements.entrySet()) {
        s = s.replace("#{" + replacement.getKey() + "}", replacement.getValue());
    }
    return s;
}
 
Example #5
Source File: MvelConditionEvaluator.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private boolean evaluate(ExecutableStatement statement, InternalFactHandle handle, InternalWorkingMemory workingMemory, Tuple tuple) {
    if (compilationUnit == null) {
        Map<String, Object> vars = valuesAsMap(handle.getObject(), workingMemory, tuple, declarations);
        if (operators.length > 0) {
            if (vars == null) {
                vars = new HashMap<String, Object>();
            }
            InternalFactHandle[] handles = tuple != null ? tuple.toFactHandles() : new InternalFactHandle[0];
            for (EvaluatorWrapper operator : operators) {
                vars.put( operator.getBindingName(), operator );
                operator.loadHandles(handles, handle);
            }
        }
        return evaluate(statement, handle.getObject(), vars);
    }

    VariableResolverFactory factory = compilationUnit.createFactory();
    compilationUnit.updateFactory( handle, tuple, null, workingMemory,
                                   workingMemory.getGlobalResolver(),
                                   factory );

    return (Boolean) MVELSafeHelper.getEvaluator().executeExpression( statement, handle.getObject(), factory );
}
 
Example #6
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 #7
Source File: MVELObjectExpression.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public Object getValue(final Tuple leftTuple,
                       final Declaration[] declrs,
                       final InternalWorkingMemory wm) {
    VariableResolverFactory factory = unit.getFactory( null, declrs,
                                                       null, null, leftTuple, null, wm, wm.getGlobalResolver() );
    
    // do we have any functions for this namespace?
    InternalKnowledgePackage pkg = wm.getKnowledgeBase().getPackage( "MAIN" );
    if ( pkg != null ) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData( this.id );
        factory.setNextFactory( data.getFunctionFactory() );
    }

    return MVELSafeHelper.getEvaluator().executeExpression(this.expr,
                                  factory);
}
 
Example #8
Source File: MVELEnabledExpression.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public boolean getValue(final Tuple tuple,
                        final Declaration[] declrs,
                        final RuleImpl rule,
                        final WorkingMemory workingMemory) {
    VariableResolverFactory factory = unit.getFactory( null, declrs,
                                                       rule, null, (LeftTuple) tuple, 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() );
    }

    return ((Boolean) MVEL.executeExpression( this.expr,
                                              null,
                                              factory )).booleanValue();
}
 
Example #9
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 #10
Source File: MVELEvalExpression.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public boolean evaluate(final Tuple tuple,
                        final Declaration[] requiredDeclarations,
                        final WorkingMemory workingMemory,
                        final Object context) throws Exception {
    VariableResolverFactory factory = ( VariableResolverFactory ) context;
    
    unit.updateFactory( null,
                        (LeftTuple) tuple,
                        null,
                        (InternalWorkingMemory) workingMemory,
                        workingMemory.getGlobalResolver(),
                        factory );

    // 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() );
    }

    final Boolean result = (Boolean) MVEL.executeExpression( this.expr,
                                                             null,
                                                             factory );
    return result.booleanValue();
}
 
Example #11
Source File: MVELAction.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public void execute(ProcessContext context) throws Exception {
        int length = unit.getOtherIdentifiers().length;
        Object[] vars = new Object[ length ];
        if (unit.getOtherIdentifiers() != null) {
            for (int i = 0; i < length; i++ ) {
                vars[i] = context.getVariable( unit.getOtherIdentifiers()[i] );
            }
        }

        InternalWorkingMemory internalWorkingMemory = (InternalWorkingMemory) context.getKieRuntime();

        VariableResolverFactory factory 
            = unit.getFactory( context, 
                               null, // No previous declarations
                               null, // No rule
                               null, // No "right object" 
                               null, // No (left) tuples
                               vars, 
                               internalWorkingMemory,
                               (GlobalResolver) context.getKieRuntime().getGlobals() );
        
//        KnowledgePackage pkg = context.getKnowledgeRuntime().getKnowledgeBase().getKnowledgePackage( "MAIN" );
//        if ( pkg != null && pkg instanceof KnowledgePackageImp) {
//            MVELDialectRuntimeData data = ( MVELDialectRuntimeData ) ((KnowledgePackageImp) pkg).pkg.getDialectRuntimeRegistry().getDialectData( id );
//            factory.setNextFactory( data.getFunctionFactory() );
//        }
//        
        MVELSafeHelper.getEvaluator().executeExpression( this.expr,
                                null,
                                factory );

    }
 
Example #12
Source File: ELFunction.java    From baymax with Apache License 2.0 5 votes vote down vote up
public Integer execute(String columnValue, Map<String, Object> extension) {
    Map<String, Object> vrs = new HashMap<String, Object>();
    //, Map<String, ElFunction<?,?>> functionMap
    //vrs.putAll(functionMap);// 拓展函数
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", columnValue);
    vrs.put("$ROOT", params);
    VariableResolverFactory vrfactory = new MapVariableResolverFactory(vrs);
    return MVEL.eval(expression, params, vrfactory, Integer.class);
}
 
Example #13
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T eval(final String expression, final Object ctx, final VariableResolverFactory vars, final Class<T> toType) {
    return MVEL.eval(expression, ctx, vars, toType);
}
 
Example #14
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T eval(final String expression, final VariableResolverFactory vars, final Class<T> toType) {
    return MVEL.eval(expression, vars, toType);
}
 
Example #15
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public Object eval(final String expression, final Object ctx, final VariableResolverFactory resolverFactory) {
    return MVEL.eval(expression, ctx, resolverFactory);
}
 
Example #16
Source File: RawMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public Object eval(final String expression, final VariableResolverFactory resolverFactory) {
    return MVEL.eval(expression, resolverFactory);
}
 
Example #17
Source File: MapPropertyHandler.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object getProperty(String name, Object contextObj, VariableResolverFactory variableFactory) {
    if(contextObj instanceof Map<?, ?>) {
        return ((Map<?, ?>)contextObj).get(name);
    }

    throw new IllegalArgumentException("contextObj is not a Map");
}
 
Example #18
Source File: MVELCompilationUnit.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public void updateFactory( InternalFactHandle rightHandle,
                           Tuple tuple,
                           Object[] localVars,
                           InternalWorkingMemory workingMemory,
                           GlobalResolver globalResolver,
                           VariableResolverFactory factory ) {
    updateFactory( null, null, null, rightHandle, rightHandle != null ? rightHandle.getObject() : null, tuple, localVars, workingMemory, globalResolver, factory );
}
 
Example #19
Source File: MVELCompilationUnit.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public VariableResolverFactory getFactory(final Object knowledgeHelper,
                                          final Declaration[] prevDecl,
                                          final Rule rule,
                                          final InternalFactHandle rightHandle,
                                          final Tuple tuple,
                                          final Object[] otherVars,
                                          final InternalWorkingMemory workingMemory,
                                          final GlobalResolver globals) {
    VariableResolverFactory factory = createFactory();
    updateFactory(knowledgeHelper, prevDecl, rule, rightHandle, rightHandle != null ? rightHandle.getObject() : null, tuple, otherVars, workingMemory, globals, factory);
    return factory;
}
 
Example #20
Source File: MVELCompilationUnit.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public VariableResolverFactory getFactory(final Object knowledgeHelper,
                                          final Declaration[] prevDecl,
                                          final Rule rule,
                                          final Tuple tuples,
                                          final Object[] otherVars,
                                          final InternalWorkingMemory workingMemory,
                                          final GlobalResolver globals) {
    VariableResolverFactory factory = createFactory();
    updateFactory(knowledgeHelper, prevDecl, rule, null, knowledgeHelper, tuples, otherVars, workingMemory, globals, factory );
    return factory;
}
 
Example #21
Source File: MVELReturnValueEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Object evaluate(ProcessContext context) throws Exception {
    int length = unit.getOtherIdentifiers().length;
    Object[] vars = new Object[ length ];
    if (unit.getOtherIdentifiers() != null) {
        for (int i = 0; i < length; i++ ) {
            vars[i] = context.getVariable( unit.getOtherIdentifiers()[i] );
        }
    }

    InternalWorkingMemory internalWorkingMemory = (InternalWorkingMemory) context.getKieRuntime();
    
    VariableResolverFactory factory 
        = unit.getFactory( context, 
                           null, // No previous declarations
                           null, // No rule
                           null, // No "right object" 
                           null, // No (left) tuples
                           vars, 
                           internalWorkingMemory,
                           (GlobalResolver) context.getKieRuntime().getGlobals() );

    // do we have any functions for this namespace?
    KiePackage pkg = context.getKieRuntime().getKieBase().getKiePackage("MAIN");
    if ( pkg instanceof KnowledgePackageImpl) {
        MVELDialectRuntimeData data = ( MVELDialectRuntimeData ) ((KnowledgePackageImpl) pkg).getDialectRuntimeRegistry().getDialectData( id );
        factory.setNextFactory( data.getFunctionFactory() );
    }

    Object value = MVELSafeHelper.getEvaluator().executeExpression( this.expr,
                                        null,
                                        factory );

    if ( !(value instanceof Boolean) ) {
        throw new RuntimeException( "Constraints must return boolean values: " + 
    		unit.getExpression() + " returns " + value + 
    		(value == null? "" : " (type=" + value.getClass()));
    }
    return ((Boolean) value).booleanValue();

}
 
Example #22
Source File: MVELAccumulator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public void accumulate(Object workingMemoryContext,
                       Object context,
                       Tuple tuple,
                       InternalFactHandle handle,
                       Declaration[] declarations,
                       Declaration[] innerDeclarations,
                       WorkingMemory workingMemory) throws Exception {
    Object[]  localVars = ((MVELAccumulatorContext) context).getVariables();
    MVELAccumulatorFactoryContext factoryContext = (MVELAccumulatorFactoryContext)workingMemoryContext;
    VariableResolverFactory factory = factoryContext.getActionFactory();
    actionUnit.updateFactory( handle, tuple, localVars, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver(), factory );

    DroolsVarFactory df = ( DroolsVarFactory ) factory.getNextFactory();
    
    if ( reverse != null ) {
        Object[] shadow = new Object [df.getOtherVarsPos()];
        for ( int i = 0; i < df.getOtherVarsPos(); i++ ) {
            shadow[i] = factory.getIndexedVariableResolver( i ).getValue();
            
            
        }
        // SNAPSHOT variable values
        ((MVELAccumulatorContext) context).getShadow().put( handle.getId(), shadow);
    }
    MVELSafeHelper.getEvaluator().executeExpression( this.action,
                            null,
                            factory );
    
    
    if ( localVars.length > 0 ) {
        for ( int i = 0; i < df.getOtherVarsLength(); i++ ) {
            localVars[i] = factory.getIndexedVariableResolver( df.getOtherVarsPos() + i ).getValue();
        }
    }
    
    ((MVELAccumulatorContext) context).setVariables( localVars );        
}
 
Example #23
Source File: SafeMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void executeExpression(final Iterable<CompiledExpression> compiledExpression, final Object ctx, final VariableResolverFactory vars) {
    AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            MVEL.executeExpression(compiledExpression, ctx, vars);
            return null;
        }
    }, KiePolicyHelper.getAccessContext());
}
 
Example #24
Source File: SafeMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T executeExpression(final Object compiledExpression, final Object ctx, final VariableResolverFactory vars, final Class<T> toType) {
    return AccessController.doPrivileged(new PrivilegedAction<T>() {

        @Override
        public T run() {
            return MVEL.executeExpression(compiledExpression, ctx, vars, toType);
        }
    }, KiePolicyHelper.getAccessContext());
}
 
Example #25
Source File: SafeMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public Object executeExpression(final Object compiledExpression, final VariableResolverFactory factory) {
    return AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            return MVEL.executeExpression(compiledExpression, factory);
        }
    }, KiePolicyHelper.getAccessContext());
}
 
Example #26
Source File: SafeMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public Object executeExpression(final Object compiledExpression, final Object ctx, final VariableResolverFactory resolverFactory) {
    return AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            return MVEL.executeExpression(compiledExpression, ctx, resolverFactory);
        }
    }, KiePolicyHelper.getAccessContext());
}
 
Example #27
Source File: SafeMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T eval(final String expression, final Object ctx, final VariableResolverFactory vars, final Class<T> toType) {
    return AccessController.doPrivileged(new PrivilegedAction<T>() {

        @Override
        public T run() {
            return MVEL.eval(expression, ctx, vars, toType);
        }
    }, KiePolicyHelper.getAccessContext());
}
 
Example #28
Source File: SafeMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T eval(final String expression, final VariableResolverFactory vars, final Class<T> toType) {
    return AccessController.doPrivileged(new PrivilegedAction<T>() {

        @Override
        public T run() {
            return MVEL.eval(expression, vars, toType);
        }
    }, KiePolicyHelper.getAccessContext());
}
 
Example #29
Source File: SafeMVELEvaluator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public Object eval(final String expression, final Object ctx, final VariableResolverFactory resolverFactory) {
    return AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            return MVEL.eval(expression, ctx, resolverFactory);
        }
    }, KiePolicyHelper.getAccessContext());
}
 
Example #30
Source File: ModifyInterceptor.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public int doAfter(Object value,
                   ASTNode node,
                   VariableResolverFactory factory) {
    while ( factory != null && !(factory instanceof DroolsVarFactory)) {
        factory =  factory.getNextFactory();
    }

    if ( factory == null ) {
        throw new RuntimeException( "Unable to find DroolsMVELIndexedFactory" );
    }

    KnowledgeHelper knowledgeHelper = ((DroolsVarFactory)factory).getKnowledgeHelper();
    if (isEqualityMode == null) {
        isEqualityMode = knowledgeHelper.getWorkingMemory().getKnowledgeBase().getConfiguration().getAssertBehaviour() == RuleBaseConfiguration.AssertBehaviour.EQUALITY;
    }

    if (modificationMask.isSet(PropertySpecificUtil.TRAITABLE_BIT)) {
        calculateModificationMask(knowledgeHelper, (WithNode)node);
    }

    if (isEqualityMode) {
        Tuple tuple = knowledgeHelper.getTuple();
        InternalFactHandle modifiedFh = tuple.getFactHandle();
        while (modifiedFh == null || modifiedFh.getObject() != value) {
            tuple = tuple.getParent();
            modifiedFh = tuple.getFactHandle();
        }
        knowledgeHelper.update(modifiedFh, modificationMask, value.getClass());
    } else {
        knowledgeHelper.update(value, modificationMask, value.getClass());
    }

    return 0;
}