Search in sources :

Example 21 with MVELDialectRuntimeData

use of org.drools.core.rule.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) {
        DialectUtil.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.core.base.dataproviders.MVELDataProvider) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) FromDescr(org.drools.compiler.lang.descr.FromDescr) From(org.drools.core.rule.From) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EntryPointId(org.drools.core.rule.EntryPointId) Declaration(org.drools.core.rule.Declaration)

Example 22 with MVELDialectRuntimeData

use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.

the class MVELReturnValueBuilder method build.

public void build(final RuleBuildContext context, final BoundIdentifiers usedIdentifiers, final Declaration[] previousDeclarations, final Declaration[] localDeclarations, final ReturnValueRestriction returnValueRestriction, final ReturnValueRestrictionDescr returnValueRestrictionDescr, final AnalysisResult analysis) {
    boolean typesafe = context.isTypesafe();
    try {
        MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
        context.setTypesafe(((MVELAnalysisResult) analysis).isTypesafe());
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit((String) returnValueRestrictionDescr.getContent(), analysis, previousDeclarations, localDeclarations, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
        MVELReturnValueExpression expr = new MVELReturnValueExpression(unit, context.getDialect().getId());
        returnValueRestriction.setReturnValueExpression(expr);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(returnValueRestriction, expr);
        expr.compile(data, context.getRule());
    } catch (final Exception e) {
        DialectUtil.copyErrorLocation(e, context.getRuleDescr());
        context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression for 'returnValue' : " + e.getMessage() + "'" + context.getRuleDescr().getSalience() + "'"));
    } finally {
        context.setTypesafe(typesafe);
    }
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) MVELReturnValueExpression(org.drools.core.base.mvel.MVELReturnValueExpression) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit)

Example 23 with MVELDialectRuntimeData

use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.

the class MVELConsequence method evaluate.

@Override
public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
    // same as lambda consequence...
    Tuple tuple = knowledgeHelper.getTuple();
    Declaration[] declarations = ((RuleTerminalNode) knowledgeHelper.getMatch().getTuple().getTupleSink()).getRequiredDeclarations();
    Variable[] vars = consequence.getVariables();
    // ...but the facts are association of Variable and its value, preserving order.
    Map<Variable, Object> facts = new LinkedHashMap<>();
    int declrCounter = 0;
    for (Variable var : vars) {
        if (var.isFact()) {
            Declaration declaration = declarations[declrCounter++];
            InternalFactHandle fh = tuple.get(declaration);
            facts.put(var, declaration.getValue((InternalWorkingMemory) workingMemory, fh.getObject()));
        } else {
            facts.put(var, workingMemory.getGlobal(var.getName()));
        }
    }
    ScriptBlock scriptBlock = null;
    try {
        scriptBlock = (ScriptBlock) consequence.getBlock();
    } catch (ClassCastException e) {
        throw new RuntimeException("I tried to access a ScriptBlock but it was not. So something is thinking is a MVEL consequence but did not set the MVEL script textual representation", e);
    }
    String originalRHS = scriptBlock.getScript();
    String name = context.getRule().getPackageName() + "." + context.getRule().getName();
    String expression = MVELConsequenceBuilder.processMacros(originalRHS);
    String[] globalIdentifiers = new String[] {};
    String[] default_inputIdentifiers = new String[] { "this", "drools", "kcontext", "rule" };
    String[] inputIdentifiers = Stream.concat(Arrays.asList(default_inputIdentifiers).stream(), facts.entrySet().stream().map(kv -> kv.getKey().getName())).collect(Collectors.toList()).toArray(new String[] {});
    String[] default_inputTypes = new String[] { "org.drools.core.spi.KnowledgeHelper", "org.drools.core.spi.KnowledgeHelper", "org.drools.core.spi.KnowledgeHelper", "org.kie.api.definition.rule.Rule" };
    String[] inputTypes = Stream.concat(Arrays.asList(default_inputTypes).stream(), facts.entrySet().stream().map(kv -> kv.getKey().getType().getName())).collect(Collectors.toList()).toArray(new String[] {});
    // ^^ please notice about inputTypes, it is to use the Class.getName(), because is later used by the Classloader internally in MVEL to load the class,
    // do NOT replace with getCanonicalName() otherwise inner classes will not be loaded correctly.
    int languageLevel = 4;
    boolean strictMode = true;
    boolean readLocalsFromTuple = false;
    EvaluatorWrapper[] operators = new EvaluatorWrapper[] {};
    Declaration[] previousDeclarations = new Declaration[] {};
    Declaration[] localDeclarations = new Declaration[] {};
    String[] otherIdentifiers = new String[] {};
    MVELCompilationUnit cu = new MVELCompilationUnit(name, expression, globalIdentifiers, operators, previousDeclarations, localDeclarations, otherIdentifiers, inputIdentifiers, inputTypes, languageLevel, strictMode, readLocalsFromTuple);
    // TODO unfortunately the MVELDialectRuntimeData would be the one of compile time
    // the one from runtime is not helpful, in fact the dialect registry for runtime is empty:
    // MVELDialectRuntimeData runtimeData = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
    MVELDialectRuntimeData runtimeData = new MVELDialectRuntimeData();
    // this classloader will be used by the CompilationUnit to load the imports.
    runtimeData.onAdd(null, Thread.currentThread().getContextClassLoader());
    runtimeData.addPackageImport(context.getPkg().getName());
    runtimeData.addPackageImport("java.lang");
    // therefore we assume for the ScriptBlock all available KBPackages are the default available and imported for the scope of the Script itself.
    for (KiePackage kp : context.getKnowledgePackages()) {
        if (!kp.getName().equals(context.getPkg().getName())) {
            runtimeData.addPackageImport(kp.getName());
        }
    }
    // sometimes here it was passed as a 2nd argument a String?? similar to `rule R in file file.drl`
    Serializable cuResult = cu.getCompiledExpression(runtimeData);
    ExecutableStatement compiledExpression = (ExecutableStatement) cuResult;
    // TODO the part above up to the ExecutableStatement compiledExpression should be cached.
    Map<String, Object> mvelContext = new HashMap<>();
    mvelContext.put("this", knowledgeHelper);
    mvelContext.put("drools", knowledgeHelper);
    mvelContext.put("kcontext", knowledgeHelper);
    mvelContext.put("rule", knowledgeHelper.getRule());
    for (Entry<Variable, Object> kv : facts.entrySet()) {
        mvelContext.put(kv.getKey().getName(), kv.getValue());
    }
    CachingMapVariableResolverFactory cachingFactory = new CachingMapVariableResolverFactory(mvelContext);
    VariableResolverFactory factory = cu.getFactory(knowledgeHelper, ((AgendaItem) knowledgeHelper.getMatch()).getTerminalNode().getRequiredDeclarations(), knowledgeHelper.getRule(), knowledgeHelper.getTuple(), null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver());
    cachingFactory.setNextFactory(factory);
    MVEL.executeExpression(compiledExpression, knowledgeHelper, cachingFactory);
}
Also used : ScriptBlock(org.drools.model.functions.ScriptBlock) AgendaItem(org.drools.core.common.AgendaItem) ExecutableStatement(org.mvel2.compiler.ExecutableStatement) Arrays(java.util.Arrays) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) InternalFactHandle(org.drools.core.common.InternalFactHandle) HashMap(java.util.HashMap) RuleContext(org.drools.modelcompiler.RuleContext) LinkedHashMap(java.util.LinkedHashMap) MVELConsequenceBuilder(org.drools.compiler.rule.builder.dialect.mvel.MVELConsequenceBuilder) Map(java.util.Map) Declaration(org.drools.core.rule.Declaration) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KiePackage(org.kie.api.definition.KiePackage) WorkingMemory(org.drools.core.WorkingMemory) Variable(org.drools.model.Variable) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) Consequence(org.drools.core.spi.Consequence) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) CachingMapVariableResolverFactory(org.mvel2.integration.impl.CachingMapVariableResolverFactory) Stream(java.util.stream.Stream) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Tuple(org.drools.core.spi.Tuple) ScriptBlock(org.drools.model.functions.ScriptBlock) Entry(java.util.Map.Entry) MVEL(org.mvel2.MVEL) EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) Serializable(java.io.Serializable) Variable(org.drools.model.Variable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) AgendaItem(org.drools.core.common.AgendaItem) LinkedHashMap(java.util.LinkedHashMap) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) KiePackage(org.kie.api.definition.KiePackage) CachingMapVariableResolverFactory(org.mvel2.integration.impl.CachingMapVariableResolverFactory) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) ExecutableStatement(org.mvel2.compiler.ExecutableStatement) EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) CachingMapVariableResolverFactory(org.mvel2.integration.impl.CachingMapVariableResolverFactory) Tuple(org.drools.core.spi.Tuple)

Example 24 with MVELDialectRuntimeData

use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.

the class MVELEnabledBuilder method build.

public void build(RuleBuildContext context) {
    // pushing consequence LHS into the stack for variable resolution
    context.getDeclarationResolver().pushOnBuildStack(context.getRule().getLhs());
    try {
        // This builder is re-usable in other dialects, so specify by name
        MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
        Map<String, Class<?>> otherVars = new HashMap<String, Class<?>>();
        otherVars.put("rule", RuleImpl.class);
        Map<String, Declaration> declrs = context.getDeclarationResolver().getDeclarations(context.getRule());
        AnalysisResult analysis = dialect.analyzeExpression(context, context.getRuleDescr(), context.getRuleDescr().getEnabled(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(declrs), context), otherVars);
        final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
        int i = usedIdentifiers.getDeclrClasses().keySet().size();
        Declaration[] previousDeclarations = new Declaration[i];
        i = 0;
        for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
            previousDeclarations[i++] = declrs.get(id);
        }
        Arrays.sort(previousDeclarations, SortDeclarations.instance);
        String exprStr = context.getRuleDescr().getEnabled();
        exprStr = exprStr.substring(1, exprStr.length() - 1) + " ";
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(exprStr, analysis, previousDeclarations, null, otherVars, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
        MVELEnabledExpression expr = new MVELEnabledExpression(unit, dialect.getId());
        context.getRule().setEnabled(KiePolicyHelper.isPolicyEnabled() ? new SafeEnabled(expr) : expr);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(context.getRule(), expr);
        expr.compile(data, context.getRule());
    } catch (final Exception e) {
        DialectUtil.copyErrorLocation(e, context.getRuleDescr());
        context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression for 'enabled' : " + e.getMessage() + " '" + context.getRuleDescr().getEnabled() + "'"));
    }
}
Also used : HashMap(java.util.HashMap) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) SafeEnabled(org.drools.core.definitions.rule.impl.RuleImpl.SafeEnabled) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MVELEnabledExpression(org.drools.core.base.mvel.MVELEnabledExpression) Declaration(org.drools.core.rule.Declaration)

Example 25 with MVELDialectRuntimeData

use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.

the class MVELExprAnalyzer method getExpressionType.

public static Class<?> getExpressionType(PackageBuildContext context, Map<String, Class<?>> declCls, RuleConditionElement source, String expression) {
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
    ParserConfiguration conf = data.getParserConfiguration();
    conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
    ParserContext pctx = new ParserContext(conf);
    pctx.setStrongTyping(true);
    pctx.setStrictTypeEnforcement(true);
    for (Map.Entry<String, Class<?>> entry : declCls.entrySet()) {
        pctx.addInput(entry.getKey(), entry.getValue());
    }
    for (Declaration decl : source.getOuterDeclarations().values()) {
        pctx.addInput(decl.getBindingName(), decl.getDeclarationClass());
    }
    try {
        return MVEL.analyze(expression, pctx);
    } catch (Exception e) {
        log.warn("Unable to parse expression: " + expression, e);
    }
    return null;
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) Declaration(org.drools.core.rule.Declaration) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map) CompileException(org.mvel2.CompileException) RecognitionException(org.antlr.runtime.RecognitionException) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)35 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)14 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)10 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)10 Declaration (org.drools.core.rule.Declaration)10 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)9 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)8 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)8 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)5 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)5 ParserConfiguration (org.mvel2.ParserConfiguration)5 MVELDialect (org.drools.compiler.rule.builder.dialect.mvel.MVELDialect)4 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)4 Test (org.junit.Test)4 ParserContext (org.mvel2.ParserContext)4 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)3