Search in sources :

Example 1 with MVELSalienceExpression

use of org.drools.core.base.mvel.MVELSalienceExpression in project drools by kiegroup.

the class MVELSalienceBuilderTest method setUp.

@Before
public void setUp() throws Exception {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    ruleDescr.addAttribute(new AttributeDescr("salience", "(p.age + 20)/2"));
    ruleDescr.setConsequence("");
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
    final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
    final ObjectType personObjeectType = new ClassObjectType(Person.class);
    final Pattern pattern = new Pattern(0, personObjeectType);
    final PatternExtractor extractor = new PatternExtractor(personObjeectType);
    final Declaration declaration = new Declaration("p", extractor, pattern);
    final Map<String, Declaration> map = new HashMap<String, Declaration>();
    map.put("p", declaration);
    declarationResolver.setDeclarations(map);
    context.setDeclarationResolver(declarationResolver);
    kBase = KnowledgeBaseFactory.newKnowledgeBase();
    SalienceBuilder salienceBuilder = new MVELSalienceBuilder();
    salienceBuilder.build(context);
    ((MVELSalienceExpression) context.getRule().getSalience()).compile((MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel"));
}
Also used : Pattern(org.drools.core.rule.Pattern) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) HashMap(java.util.HashMap) SalienceBuilder(org.drools.compiler.rule.builder.SalienceBuilder) PatternExtractor(org.drools.core.spi.PatternExtractor) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) MVELSalienceExpression(org.drools.core.base.mvel.MVELSalienceExpression) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Declaration(org.drools.core.rule.Declaration) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Before(org.junit.Before)

Example 2 with MVELSalienceExpression

use of org.drools.core.base.mvel.MVELSalienceExpression 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.core.base.mvel.MVELCompilationUnit) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MVELSalienceExpression(org.drools.core.base.mvel.MVELSalienceExpression) Declaration(org.drools.core.rule.Declaration)

Example 3 with MVELSalienceExpression

use of org.drools.core.base.mvel.MVELSalienceExpression in project drools by kiegroup.

the class RuleTerminalNode method setDeclarations.

public void setDeclarations(Map<String, Declaration> decls) {
    if (rule.getSalience() instanceof MVELSalienceExpression) {
        MVELSalienceExpression expr = (MVELSalienceExpression) rule.getSalience();
        Declaration[] declrs = expr.getMVELCompilationUnit().getPreviousDeclarations();
        this.salienceDeclarations = new Declaration[declrs.length];
        int i = 0;
        for (Declaration declr : declrs) {
            this.salienceDeclarations[i++] = decls.get(declr.getIdentifier());
        }
        Arrays.sort(this.salienceDeclarations, SortDeclarations.instance);
    }
    if (rule.getEnabled() instanceof MVELEnabledExpression) {
        MVELEnabledExpression expr = (MVELEnabledExpression) rule.getEnabled();
        Declaration[] declrs = expr.getMVELCompilationUnit().getPreviousDeclarations();
        this.enabledDeclarations = new Declaration[declrs.length];
        int i = 0;
        for (Declaration declr : declrs) {
            this.enabledDeclarations[i++] = decls.get(declr.getIdentifier());
        }
        Arrays.sort(this.enabledDeclarations, SortDeclarations.instance);
    }
    if (rule.getTimer() instanceof BaseTimer) {
        this.timerDeclarations = ((BaseTimer) rule.getTimer()).getTimerDeclarations(decls);
    }
}
Also used : MVELEnabledExpression(org.drools.core.base.mvel.MVELEnabledExpression) MVELSalienceExpression(org.drools.core.base.mvel.MVELSalienceExpression) BaseTimer(org.drools.core.time.impl.BaseTimer) Declaration(org.drools.core.rule.Declaration)

Example 4 with MVELSalienceExpression

use of org.drools.core.base.mvel.MVELSalienceExpression in project drools by kiegroup.

the class RuleTest method testIsSalienceDynamic.

@Test
public void testIsSalienceDynamic() {
    final RuleImpl rule = new RuleImpl("myrule");
    Salience salience = new MVELSalienceExpression();
    rule.setSalience(salience);
    assertTrue(rule.isSalienceDynamic());
}
Also used : Salience(org.drools.core.spi.Salience) MVELSalienceExpression(org.drools.core.base.mvel.MVELSalienceExpression) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Aggregations

MVELSalienceExpression (org.drools.core.base.mvel.MVELSalienceExpression)4 Declaration (org.drools.core.rule.Declaration)3 HashMap (java.util.HashMap)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)1 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)1 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)1 SalienceBuilder (org.drools.compiler.rule.builder.SalienceBuilder)1 ClassObjectType (org.drools.core.base.ClassObjectType)1 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)1 MVELEnabledExpression (org.drools.core.base.mvel.MVELEnabledExpression)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 SafeSalience (org.drools.core.definitions.rule.impl.RuleImpl.SafeSalience)1 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)1 Pattern (org.drools.core.rule.Pattern)1