Search in sources :

Example 11 with MVELDialectRuntimeData

use of org.drools.mvel.MVELDialectRuntimeData in project drools by kiegroup.

the class MVELObjectExpression method getValue.

@Override
public Object getValue(final Tuple leftTuple, final Declaration[] declrs, final ReteEvaluator reteEvaluator) {
    VariableResolverFactory factory = unit.getFactory(null, declrs, null, null, leftTuple, null, reteEvaluator, reteEvaluator.getGlobalResolver());
    // do we have any functions for this namespace?
    InternalKnowledgePackage pkg = reteEvaluator.getKnowledgeBase().getPackage("MAIN");
    if (pkg != null) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
        factory.setNextFactory(data.getFunctionFactory());
    }
    return evaluator.evaluate(factory);
}
Also used : MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 12 with MVELDialectRuntimeData

use of org.drools.mvel.MVELDialectRuntimeData in project drools by kiegroup.

the class MVELAccumulateBuilder method build.

@SuppressWarnings("unchecked")
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    boolean typesafe = context.isTypesafe();
    try {
        final AccumulateDescr accumDescr = (AccumulateDescr) descr;
        if (!accumDescr.hasValidInput()) {
            return null;
        }
        final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(accumDescr.getInput().getClass());
        // create source CE
        final RuleConditionElement source = builder.build(context, accumDescr.getInput());
        if (source == null) {
            return null;
        }
        MVELDialect dialect = (MVELDialect) context.getDialect();
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        Map<String, Declaration> sourceOuterDeclr = source.getOuterDeclarations();
        Map<String, Class<?>> declarationClasses = DeclarationScopeResolver.getDeclarationClasses(decls);
        declarationClasses.putAll(DeclarationScopeResolver.getDeclarationClasses(sourceOuterDeclr));
        BoundIdentifiers boundIds = new BoundIdentifiers(declarationClasses, context);
        final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
        Accumulator[] accumulators;
        if (accumDescr.isExternalFunction()) {
            // uses accumulate functions
            accumulators = buildExternalFunctions(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple, source, declarationClasses);
        } else {
            // it is a custom accumulate
            accumulators = buildCustomAccumulate(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple);
        }
        List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
        for (Accumulator acc : accumulators) {
            MvelAccumulator mvelAcc = (MvelAccumulator) acc;
            Collections.addAll(requiredDeclarations, mvelAcc.getRequiredDeclarations());
        }
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        Accumulate accumulate;
        if (accumDescr.isMultiFunction()) {
            accumulate = new MultiAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators, accumulators.length);
            int index = 0;
            for (Accumulator accumulator : accumulators) {
                data.addCompileable(((MultiAccumulate) accumulate).new Wirer(index++), (MVELCompileable) accumulator);
                ((MVELCompileable) accumulator).compile(data, context.getRule());
            }
        } else {
            accumulate = new SingleAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators[0]);
            data.addCompileable(((SingleAccumulate) accumulate).new Wirer(), (MVELCompileable) accumulators[0]);
            ((MVELCompileable) accumulators[0]).compile(data, context.getRule());
        }
        return accumulate;
    } catch (Exception e) {
        AsmUtil.copyErrorLocation(e, descr);
        context.addError(new DescrBuildError(context.getParentDescr(), descr, e, "Unable to build expression for 'accumulate' : " + e.getMessage()));
        return null;
    } finally {
        context.setTypesafe(typesafe);
    }
}
Also used : Accumulator(org.drools.core.spi.Accumulator) MVELAccumulator(org.drools.mvel.expr.MVELAccumulator) MvelAccumulator(org.drools.core.spi.MvelAccumulator) MVELCompileable(org.drools.mvel.expr.MVELCompileable) ArrayList(java.util.ArrayList) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) Declaration(org.drools.core.rule.Declaration) MultiAccumulate(org.drools.core.rule.MultiAccumulate) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) RuleConditionElement(org.drools.core.rule.RuleConditionElement) SingleAccumulate(org.drools.core.rule.SingleAccumulate) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MVELConstraint(org.drools.mvel.MVELConstraint) Constraint(org.drools.core.spi.Constraint) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MvelAccumulator(org.drools.core.spi.MvelAccumulator)

Example 13 with MVELDialectRuntimeData

use of org.drools.mvel.MVELDialectRuntimeData in project drools by kiegroup.

the class MVELSalienceExpression method getValue.

public int getValue(final Activation activation, final Rule rule, final ReteEvaluator reteEvaluator) {
    VariableResolverFactory factory = unit.getFactory(reteEvaluator, reteEvaluator != null ? ((AgendaItem) activation).getTerminalNode().getSalienceDeclarations() : null, rule, null, reteEvaluator != null ? activation.getTuple() : null, null, reteEvaluator, reteEvaluator.getGlobalResolver());
    // do we have any functions for this namespace?
    InternalKnowledgePackage pkg = reteEvaluator.getKnowledgeBase().getPackage("MAIN");
    if (pkg != null) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
        factory.setNextFactory(data.getFunctionFactory());
    }
    Object value = evaluator.evaluate(factory);
    if (value instanceof String) {
        value = TimeUtils.parseTimeString((String) value);
    }
    return ((Number) value).intValue();
}
Also used : MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 14 with MVELDialectRuntimeData

use of org.drools.mvel.MVELDialectRuntimeData in project drools by kiegroup.

the class MVELEvalExpression method evaluate.

public boolean evaluate(final Tuple tuple, final Declaration[] requiredDeclarations, final ReteEvaluator reteEvaluator, final Object context) throws Exception {
    VariableResolverFactory factory = (VariableResolverFactory) context;
    unit.updateFactory(null, tuple, null, reteEvaluator, reteEvaluator.getGlobalResolver(), factory);
    // do we have any functions for this namespace?
    InternalKnowledgePackage pkg = reteEvaluator.getKnowledgeBase().getPackage("MAIN");
    if (pkg != null) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
        factory.setNextFactory(data.getFunctionFactory());
    }
    return evaluator.evaluate(factory);
}
Also used : MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 15 with MVELDialectRuntimeData

use of org.drools.mvel.MVELDialectRuntimeData in project drools by kiegroup.

the class MVELReturnValueExpression method evaluate.

public FieldValue evaluate(final InternalFactHandle handle, final Tuple tuple, final Declaration[] previousDeclarations, final Declaration[] requiredDeclarations, final ReteEvaluator reteEvaluator, final Object ctx) throws Exception {
    VariableResolverFactory factory = (VariableResolverFactory) ctx;
    unit.updateFactory(handle, tuple, null, reteEvaluator, reteEvaluator.getGlobalResolver(), factory);
    // do we have any functions for this namespace?
    InternalKnowledgePackage pkg = reteEvaluator.getKnowledgeBase().getPackage("MAIN");
    if (pkg != null) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
        factory.setNextFactory(data.getFunctionFactory());
    }
    Object value = evaluator.evaluate(handle, factory);
    return FieldFactory.getInstance().getFieldValue(value);
}
Also used : MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

MVELDialectRuntimeData (org.drools.mvel.MVELDialectRuntimeData)17 Declaration (org.drools.core.rule.Declaration)8 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)7 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)7 MVELCompilationUnit (org.drools.mvel.expr.MVELCompilationUnit)6 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)4 HashMap (java.util.HashMap)2 Person (org.drools.core.test.model.Person)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 RecognitionException (org.antlr.runtime.RecognitionException)1 RuleConditionBuilder (org.drools.compiler.rule.builder.RuleConditionBuilder)1 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)1 AgendaItem (org.drools.core.common.AgendaItem)1 SafeEnabled (org.drools.core.definitions.rule.impl.RuleImpl.SafeEnabled)1 SafeSalience (org.drools.core.definitions.rule.impl.RuleImpl.SafeSalience)1 Accumulate (org.drools.core.rule.Accumulate)1 EntryPointId (org.drools.core.rule.EntryPointId)1