Search in sources :

Example 16 with MVELDialectRuntimeData

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

the class MVELFromBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    String text = ((FromDescr) descr).getExpression();
    Optional<EntryPointId> entryPointId = context.getEntryPointId(text);
    if (entryPointId.isPresent()) {
        return entryPointId.get();
    }
    // This builder is re-usable in other dialects, so specify by name
    MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
    boolean typeSafe = context.isTypesafe();
    if (!dialect.isStrictMode()) {
        context.setTypesafe(false);
    }
    try {
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        AnalysisResult analysis = dialect.analyzeExpression(context, descr, text, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
        if (analysis == null) {
            // something bad happened
            return null;
        }
        Class<?> returnType = ((MVELAnalysisResult) analysis).getReturnType();
        if (prefixPattern != null && !prefixPattern.isCompatibleWithFromReturnType(returnType)) {
            context.addError(new DescrBuildError(descr, context.getRuleDescr(), null, "Pattern of type: '" + prefixPattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by source"));
            return null;
        }
        final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
        final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
        int j = 0;
        for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
            declarations[j++] = decls.get(str);
        }
        Arrays.sort(declarations, SortDeclarations.instance);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, declarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.CONSEQUENCE);
        MVELDataProvider dataProvider = new MVELDataProvider(unit, context.getDialect().getId());
        From from = new From(dataProvider);
        from.setResultPattern(prefixPattern);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(from, dataProvider);
        dataProvider.compile(data, context.getRule());
        return from;
    } catch (final Exception e) {
        AsmUtil.copyErrorLocation(e, descr);
        context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to build expression for 'from' : " + e.getMessage() + " '" + text + "'"));
        return null;
    } finally {
        context.setTypesafe(typeSafe);
    }
}
Also used : MVELDataProvider(org.drools.mvel.dataproviders.MVELDataProvider) MVELCompilationUnit(org.drools.mvel.expr.MVELCompilationUnit) FromDescr(org.drools.drl.ast.descr.FromDescr) From(org.drools.core.rule.From) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EntryPointId(org.drools.core.rule.EntryPointId) Declaration(org.drools.core.rule.Declaration)

Example 17 with MVELDialectRuntimeData

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

the class MVELAccumulator method init.

/* (non-Javadoc)
     * @see org.kie.spi.Accumulator#init(java.lang.Object, org.kie.spi.Tuple, org.kie.rule.Declaration[], org.kie.WorkingMemory)
     */
public Object init(Object workingMemoryContext, Object context, Tuple tuple, Declaration[] declarations, ReteEvaluator reteEvaluator) {
    Object[] localVars = new Object[initUnit.getOtherIdentifiers().length];
    MVELAccumulatorFactoryContext factoryContext = (MVELAccumulatorFactoryContext) workingMemoryContext;
    VariableResolverFactory factory = factoryContext.getInitFactory();
    initUnit.updateFactory(null, tuple, localVars, reteEvaluator, reteEvaluator.getGlobalResolver(), factory);
    InternalKnowledgePackage pkg = reteEvaluator.getKnowledgeBase().getPackage("MAIN");
    if (pkg != null) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("mvel");
        factory.setNextFactory(data.getFunctionFactory());
    }
    this.init.evaluate(factory);
    DroolsVarFactory df = (DroolsVarFactory) factory.getNextFactory();
    if (localVars.length > 0) {
        for (int i = 0; i < df.getOtherVarsLength(); i++) {
            localVars[i] = factory.getIndexedVariableResolver(df.getOtherVarsPos() + i).getValue();
        }
    }
    ((MVELAccumulatorContext) context).setVariables(localVars);
    return context;
}
Also used : MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) DroolsVarFactory(org.drools.mvel.expr.MVELCompilationUnit.DroolsVarFactory) 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