Search in sources :

Example 1 with MVELDialectRuntimeData

use of org.drools.mvel.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.mvel.MVELDialectRuntimeData) Person(org.drools.core.test.model.Person) Test(org.junit.Test)

Example 2 with MVELDialectRuntimeData

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

the class MVELClassFieldExtractorTest method setUp.

@Before
public void setUp() throws Exception {
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);
    extractor = (MVELObjectClassFieldReader) store.getMVELReader(Person.class.getPackage().getName(), Person.class.getName(), "addresses['home'].street", true, String.class);
    MVELDialectRuntimeData data = new MVELDialectRuntimeData();
    data.addImport(Person.class.getSimpleName(), Person.class);
    data.onAdd(null, ProjectClassLoader.createProjectClassLoader());
    extractor.compile(data);
    person[0] = new Person("bob", 30);
    business[0] = new Address("Business Street", "999", null);
    home[0] = new Address("Home Street", "555", "55555555");
    person[0].getAddresses().put("business", business[0]);
    person[0].getAddresses().put("home", home[0]);
    person[1] = new Person("mark", 35);
    business[1] = new Address("Another Business Street", "999", null);
    home[1] = new Address("Another Home Street", "555", "55555555");
    person[1].getAddresses().put("business", business[1]);
    person[1].getAddresses().put("home", home[1]);
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) Address(org.drools.core.test.model.Address) Person(org.drools.core.test.model.Person) Before(org.junit.Before)

Example 3 with MVELDialectRuntimeData

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

the class MVELConsequenceBuilder method build.

public void build(final RuleBuildContext context, String consequenceName) {
    // pushing consequence LHS into the stack for variable resolution
    context.getDeclarationResolver().pushOnBuildStack(context.getRule().getLhs());
    try {
        MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
        final RuleDescr ruleDescr = context.getRuleDescr();
        String text = (RuleImpl.DEFAULT_CONSEQUENCE_NAME.equals(consequenceName)) ? (String) ruleDescr.getConsequence() : (String) ruleDescr.getNamedConsequences().get(consequenceName);
        text = processMacros(text);
        text = rewriteModify(text);
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        AnalysisResult analysis = dialect.analyzeBlock(context, text, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context, Collections.EMPTY_MAP, KnowledgeHelper.class), null, "drools", KnowledgeHelper.class);
        if (analysis == null) {
            // something bad happened, issue already logged in errors
            return;
        }
        text = rewriteUpdates(context, analysis, text);
        final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
        final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
        String[] declrStr = new String[declarations.length];
        int j = 0;
        for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
            declrStr[j] = str;
            declarations[j++] = decls.get(str);
        }
        Arrays.sort(declarations, SortDeclarations.instance);
        for (int i = 0; i < declrStr.length; i++) {
            declrStr[i] = declarations[i].getIdentifier();
        }
        context.getRule().setRequiredDeclarationsForConsequence(consequenceName, declrStr);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, declarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.CONSEQUENCE);
        MVELConsequence expr = new MVELConsequence(unit, dialect.getId(), consequenceName);
        if (RuleImpl.DEFAULT_CONSEQUENCE_NAME.equals(consequenceName)) {
            context.getRule().setConsequence(expr);
        } else {
            context.getRule().addNamedConsequence(consequenceName, expr);
        }
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(context.getRule(), expr);
        expr.compile(data, context.getRule());
    } catch (final Exception e) {
        copyErrorLocation(e, context.getRuleDescr());
        context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression for 'consequence': " + e.getMessage() + " '" + context.getRuleDescr().getConsequence() + "'"));
    }
}
Also used : MVELCompilationUnit(org.drools.mvel.expr.MVELCompilationUnit) MVELConsequence(org.drools.mvel.expr.MVELConsequence) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) RuleDescr(org.drools.drl.ast.descr.RuleDescr) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration)

Example 4 with MVELDialectRuntimeData

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

the class MVELEvalBuilder method build.

/**
 * Builds and returns an Eval Conditional Element
 *
 * @param context The current build context
 * @param descr The Eval Descriptor to build the eval conditional element from
 *
 * @return the Eval Conditional Element
 */
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    boolean typesafe = context.isTypesafe();
    // it must be an EvalDescr
    final EvalDescr evalDescr = (EvalDescr) descr;
    try {
        MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        AnalysisResult analysis = context.getDialect().analyzeExpression(context, evalDescr, evalDescr.getContent(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
        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, SortDeclarations.instance);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit((String) evalDescr.getContent(), analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
        final EvalCondition eval = EvalConditionFactory.Factory.get().createEvalCondition(previousDeclarations);
        MVELEvalExpression expr = new MVELEvalExpression(unit, dialect.getId());
        eval.setEvalExpression(KiePolicyHelper.isPolicyEnabled() ? new SafeEvalExpression(expr) : expr);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(eval, expr);
        expr.compile(data, context.getRule());
        return eval;
    } catch (final Exception e) {
        copyErrorLocation(e, evalDescr);
        context.addError(new DescrBuildError(context.getParentDescr(), evalDescr, e, "Unable to build expression for 'eval':" + e.getMessage() + " '" + evalDescr.getContent() + "'"));
        return null;
    } finally {
        context.setTypesafe(typesafe);
    }
}
Also used : MVELCompilationUnit(org.drools.mvel.expr.MVELCompilationUnit) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) EvalCondition(org.drools.core.rule.EvalCondition) MVELEvalExpression(org.drools.mvel.expr.MVELEvalExpression) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EvalDescr(org.drools.drl.ast.descr.EvalDescr) Declaration(org.drools.core.rule.Declaration) SafeEvalExpression(org.drools.core.spi.EvalExpression.SafeEvalExpression)

Example 5 with MVELDialectRuntimeData

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

the class MVELSalienceBuilder method build.

public void build(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(), context.getRuleDescr().getSalience(), 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, SortDeclarations.instance);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(context.getRuleDescr().getSalience(), analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
        MVELSalienceExpression expr = new MVELSalienceExpression(unit, dialect.getId());
        context.getRule().setSalience(KiePolicyHelper.isPolicyEnabled() ? new SafeSalience(expr) : expr);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(context.getRule(), expr);
        expr.compile(data, context.getRule());
    } catch (final Exception e) {
        copyErrorLocation(e, context.getRuleDescr());
        context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression for 'salience' : " + e.getMessage() + "'" + context.getRuleDescr().getSalience() + "'"));
    } finally {
        context.setTypesafe(typesafe);
    }
}
Also used : SafeSalience(org.drools.core.definitions.rule.impl.RuleImpl.SafeSalience) MVELCompilationUnit(org.drools.mvel.expr.MVELCompilationUnit) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MVELSalienceExpression(org.drools.mvel.expr.MVELSalienceExpression) Declaration(org.drools.core.rule.Declaration)

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