Search in sources :

Example 26 with MVELDialectRuntimeData

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

the class MVELObjectExpressionBuilder method build.

public static MVELObjectExpression build(String expression, RuleBuildContext context) {
    boolean typesafe = context.isTypesafe();
    // 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, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        MVELAnalysisResult analysis = (MVELAnalysisResult) dialect.analyzeExpression(context, context.getRuleDescr(), expression, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
        context.setTypesafe(analysis.isTypesafe());
        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++] = decls.get(id);
        }
        Arrays.sort(previousDeclarations, RuleTerminalNode.SortDeclarations.instance);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(expression, analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
        MVELObjectExpression expr = new MVELObjectExpression(unit, dialect.getId());
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(context.getRule(), expr);
        expr.compile(data);
        return expr;
    } catch (final Exception e) {
        DialectUtil.copyErrorLocation(e, context.getRuleDescr());
        context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression : " + e.getMessage() + "'" + expression + "'"));
        return null;
    } finally {
        context.setTypesafe(typesafe);
    }
}
Also used : MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) MVELObjectExpression(org.drools.core.base.mvel.MVELObjectExpression) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) Declaration(org.drools.core.rule.Declaration)

Example 27 with MVELDialectRuntimeData

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

the class MVELClassFieldExtractorTest method testIsNullValue.

@Test
public void testIsNullValue() {
    try {
        assertFalse(this.extractor.isNullValue(null, this.person[0]));
        MVELObjectClassFieldReader nullExtractor = (MVELObjectClassFieldReader) store.getMVELReader(Person.class.getPackage().getName(), Person.class.getName(), "addresses['business'].phone", true, String.class);
        MVELDialectRuntimeData data = new MVELDialectRuntimeData();
        data.addImport(Person.class.getSimpleName(), Person.class);
        data.onAdd(null, ProjectClassLoader.createProjectClassLoader());
        nullExtractor.compile(data);
        // 
        // InternalReadAccessor nullExtractor = store.getReader( Person.class,
        // "addresses['business'].phone",
        // getClass().getClassLoader() );
        assertTrue(nullExtractor.isNullValue(null, this.person[0]));
    } catch (final Exception e) {
        fail("Should not throw an exception");
    }
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) Person(org.drools.core.test.model.Person) Test(org.junit.Test)

Example 28 with MVELDialectRuntimeData

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

the class ScenarioTest method createContext.

public BuildContext createContext() {
    RuleBaseConfiguration conf = new RuleBaseConfiguration();
    KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
    BuildContext buildContext = new BuildContext(rbase);
    RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
    pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
    pkg.addRule(rule);
    buildContext.setRule(rule);
    return buildContext;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 29 with MVELDialectRuntimeData

use of org.drools.core.rule.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 void init(Object workingMemoryContext, Object context, Tuple tuple, Declaration[] declarations, WorkingMemory workingMemory) throws Exception {
    Object[] localVars = new Object[initUnit.getOtherIdentifiers().length];
    MVELAccumulatorFactoryContext factoryContext = (MVELAccumulatorFactoryContext) workingMemoryContext;
    VariableResolverFactory factory = factoryContext.getInitFactory();
    initUnit.updateFactory(null, tuple, localVars, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver(), factory);
    InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage("MAIN");
    if (pkg != null) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("mvel");
        factory.setNextFactory(data.getFunctionFactory());
    }
    MVELSafeHelper.getEvaluator().executeExpression(this.init, null, 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);
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) DroolsVarFactory(org.drools.core.base.mvel.MVELCompilationUnit.DroolsVarFactory) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 30 with MVELDialectRuntimeData

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

the class MVELConsequence method evaluate.

public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
    VariableResolverFactory factory = unit.getFactory(knowledgeHelper, ((AgendaItem) knowledgeHelper.getMatch()).getTerminalNode().getRequiredDeclarations(), knowledgeHelper.getRule(), knowledgeHelper.getTuple(), 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());
    }
    CompiledExpression compexpr = (CompiledExpression) this.expr;
    if (MVELDebugHandler.isDebugMode()) {
        if (MVELDebugHandler.verbose) {
            logger.info(DebugTools.decompile(compexpr));
        }
        MVEL.executeDebugger(compexpr, knowledgeHelper, factory);
    } else {
        MVEL.executeExpression(compexpr, knowledgeHelper, factory);
    }
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) AgendaItem(org.drools.core.common.AgendaItem) CompiledExpression(org.mvel2.compiler.CompiledExpression) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

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