Search in sources :

Example 11 with EvalDescr

use of org.drools.compiler.lang.descr.EvalDescr in project drools by kiegroup.

the class AbstractASMEvalBuilder method build.

public RuleConditionElement build(RuleBuildContext context, BaseDescr descr) {
    // it must be an EvalDescr
    final EvalDescr evalDescr = (EvalDescr) descr;
    Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
    AnalysisResult analysis = context.getDialect().analyzeExpression(context, evalDescr, evalDescr.getContent(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
    List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
    for (String usedIdentifier : analysis.getIdentifiers()) {
        Declaration usedDec = decls.get(usedIdentifier);
        if (usedDec != null) {
            requiredDeclarations.add(usedDec);
        }
    }
    final Declaration[] declarations = requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]);
    return buildEval(context, evalDescr, analysis, declarations);
}
Also used : EvalDescr(org.drools.compiler.lang.descr.EvalDescr) ArrayList(java.util.ArrayList) Declaration(org.drools.core.rule.Declaration) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 12 with EvalDescr

use of org.drools.compiler.lang.descr.EvalDescr in project drools by kiegroup.

the class RuleParserTest method testWithEval.

@Test
public void testWithEval() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_eval.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(3, rule.getLhs().getDescrs().size());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Foo", pattern.getObjectType());
    pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("Bar", pattern.getObjectType());
    final EvalDescr eval = (EvalDescr) rule.getLhs().getDescrs().get(2);
    assertEqualsIgnoreWhitespace("abc(\"foo\")", (String) eval.getContent());
    assertEqualsIgnoreWhitespace("Kapow", (String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Aggregations

EvalDescr (org.drools.compiler.lang.descr.EvalDescr)12 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)6 Test (org.junit.Test)5 AndDescr (org.drools.compiler.lang.descr.AndDescr)4 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)4 Declaration (org.drools.core.rule.Declaration)4 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 EvalCondition (org.drools.core.rule.EvalCondition)3 Cheese (org.drools.compiler.Cheese)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)2 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)2 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)2 MVELEvalExpression (org.drools.core.base.mvel.MVELEvalExpression)2 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1