Search in sources :

Example 11 with EvaluatorWrapper

use of org.drools.core.base.EvaluatorWrapper in project drools by kiegroup.

the class MVELExprAnalyzer method analyze.

/**
 * Analyse an expression.
 * @throws RecognitionException
 *             If an error occurs in the parser.
 */
private static MVELAnalysisResult analyze(final Set<String> identifiers, final BoundIdentifiers availableIdentifiers) {
    MVELAnalysisResult result = new MVELAnalysisResult();
    result.setIdentifiers(identifiers);
    final Set<String> notBound = new HashSet<String>(identifiers);
    notBound.remove("this");
    Map<String, Class<?>> usedDecls = new HashMap<String, Class<?>>();
    Map<String, Class<?>> usedGlobals = new HashMap<String, Class<?>>();
    Map<String, EvaluatorWrapper> usedOperators = new HashMap<String, EvaluatorWrapper>();
    for (Entry<String, Class<?>> entry : availableIdentifiers.getDeclrClasses().entrySet()) {
        if (identifiers.contains(entry.getKey())) {
            usedDecls.put(entry.getKey(), entry.getValue());
            notBound.remove(entry.getKey());
        }
    }
    for (String identifier : identifiers) {
        Class<?> type = availableIdentifiers.resolveVarType(identifier);
        if (type != null) {
            usedGlobals.put(identifier, type);
            notBound.remove(identifier);
        }
    }
    for (Map.Entry<String, EvaluatorWrapper> op : availableIdentifiers.getOperators().entrySet()) {
        if (identifiers.contains(op.getKey())) {
            usedOperators.put(op.getKey(), op.getValue());
            notBound.remove(op.getKey());
        }
    }
    BoundIdentifiers boundIdentifiers = new BoundIdentifiers(usedDecls, availableIdentifiers.getContext(), usedOperators, availableIdentifiers.getThisClass());
    boundIdentifiers.setGlobals(usedGlobals);
    result.setBoundIdentifiers(boundIdentifiers);
    result.setNotBoundedIdentifiers(notBound);
    return result;
}
Also used : EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) HashMap(java.util.HashMap) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 12 with EvaluatorWrapper

use of org.drools.core.base.EvaluatorWrapper in project drools by kiegroup.

the class MvelConditionEvaluator method evaluate.

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(workingMemory, 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);
}
Also used : EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 13 with EvaluatorWrapper

use of org.drools.core.base.EvaluatorWrapper in project drools by kiegroup.

the class MVELCompilationUnit method updateFactory.

private void updateFactory(Object knowledgeHelper, Declaration[] prevDecl, Rule rule, InternalFactHandle rightHandle, Object rightObject, Tuple tuple, Object[] otherVars, InternalWorkingMemory workingMemory, GlobalResolver globals, VariableResolverFactory factory) {
    int varLength = inputIdentifiers.length;
    int i = 0;
    if ("this".equals(inputIdentifiers[0])) {
        factory.getIndexedVariableResolver(i++).setValue(rightObject);
    }
    factory.getIndexedVariableResolver(i++).setValue(knowledgeHelper);
    factory.getIndexedVariableResolver(i++).setValue(knowledgeHelper);
    if (inputIdentifiers.length > i && "rule".equals(inputIdentifiers[i])) {
        factory.getIndexedVariableResolver(i++).setValue(rule);
    }
    if (globalIdentifiers != null) {
        for (String globalIdentifier : globalIdentifiers) {
            factory.getIndexedVariableResolver(i++).setValue(globals.resolveGlobal(globalIdentifier));
        }
    }
    InternalFactHandle[] handles = tuple instanceof LeftTuple ? ((LeftTuple) tuple).toFactHandles() : null;
    if (operators.length > 0) {
        for (EvaluatorWrapper operator : operators) {
            // TODO: need to have one operator per working memory
            factory.getIndexedVariableResolver(i++).setValue(operator);
            operator.loadHandles(workingMemory, handles, rightHandle);
        }
    }
    Object[] objs = null;
    if (tuple != null) {
        if (handles == null) {
            objs = tuple.toObjects();
        }
        if (this.previousDeclarations != null && this.previousDeclarations.length > 0) {
            // Consequences with 'or's will have different declaration offsets, so use the one's from the RTN's subrule.
            if (prevDecl == null) {
                // allows the caller to override the member var
                // used for rules, salience and timers so they work with 'or' CEs
                prevDecl = this.previousDeclarations;
            }
            for (Declaration decl : prevDecl) {
                int offset = decl.getPattern().getOffset();
                Object o = decl.getValue(workingMemory, objs != null ? objs[offset] : handles[offset].getObject());
                factory.getIndexedVariableResolver(i++).setValue(o);
            }
        }
    }
    if (this.localDeclarations != null && this.localDeclarations.length > 0) {
        for (Declaration decl : this.localDeclarations) {
            Object value;
            if (readLocalsFromTuple && tuple != null) {
                int offset = decl.getPattern().getOffset();
                value = decl.getValue(workingMemory, objs != null ? objs[offset] : handles[offset].getObject());
            } else {
                value = decl.getValue(workingMemory, rightObject);
            }
            factory.getIndexedVariableResolver(i++).setValue(value);
        }
    }
    int otherVarsPos = 0;
    if (otherVars != null) {
        otherVarsPos = i;
        for (Object o : otherVars) {
            factory.getIndexedVariableResolver(i++).setValue(o);
        }
    }
    int otherVarsLength = i - otherVarsPos;
    for (i = varLength; i < this.allVarsLength; i++) {
        // null all local vars
        factory.getIndexedVariableResolver(i).setValue(null);
    }
    DroolsVarFactory df = (DroolsVarFactory) factory.getNextFactory();
    df.setOtherVarsPos(otherVarsPos);
    df.setOtherVarsLength(otherVarsLength);
    if (knowledgeHelper instanceof KnowledgeHelper) {
        KnowledgeHelper kh = (KnowledgeHelper) knowledgeHelper;
        df.setKnowledgeHelper(kh);
    }
}
Also used : EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Aggregations

EvaluatorWrapper (org.drools.core.base.EvaluatorWrapper)13 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Declaration (org.drools.core.rule.Declaration)6 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)5 HashSet (java.util.HashSet)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 EvaluatorConstraint (org.drools.core.rule.constraint.EvaluatorConstraint)4 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)4 Evaluator (org.drools.core.spi.Evaluator)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Entry (java.util.Map.Entry)2 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 PredicateConstraint (org.drools.core.rule.PredicateConstraint)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 NegConstraint (org.drools.core.rule.constraint.NegConstraint)2