Search in sources :

Example 21 with AndDescr

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

the class RuleParserTest method testNotMemberof.

@Test
public void testNotMemberof() throws Exception {
    final String text = "rule X when Country( $cities : city )\nPerson( city not memberOf $cities ) then end\n";
    AndDescr descrs = ((RuleDescr) parse("rule", text)).getLhs();
    assertEquals(2, descrs.getDescrs().size());
    PatternDescr pat = (PatternDescr) descrs.getDescrs().get(1);
    ExprConstraintDescr fieldConstr = (ExprConstraintDescr) pat.getConstraint().getDescrs().get(0);
    assertEquals("city not memberOf $cities", fieldConstr.getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 22 with AndDescr

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

the class JavaAccumulateBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    final AccumulateDescr accumDescr = (AccumulateDescr) descr;
    if (!accumDescr.hasValidInput()) {
        return null;
    }
    // build source
    BaseDescr input = accumDescr.getInput();
    if (input instanceof AndDescr && ((AndDescr) input).getDescrs().size() == 1) {
        input = ((AndDescr) input).getDescrs().get(0);
    }
    final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(input.getClass());
    final RuleConditionElement source = builder.build(context, input);
    if (source == null) {
        return null;
    }
    final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
    Map<String, Declaration> declsInScope = context.getDeclarationResolver().getDeclarations(context.getRule());
    Map<String, Class<?>> declCls = DeclarationScopeResolver.getDeclarationClasses(declsInScope);
    Accumulate accumulate;
    if (accumDescr.isExternalFunction()) {
        // if it uses 1+ external function, build methods for them
        accumulate = buildExternalFunctionCall(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
    } else {
        // if it uses inline code, build the class for it
        accumulate = buildInlineAccumulate(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
    }
    return accumulate;
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) RuleConditionElement(org.drools.core.rule.RuleConditionElement) Declaration(org.drools.core.rule.Declaration) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate)

Example 23 with AndDescr

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

the class AccumulateDescrBuilderImpl method source.

public CEDescrBuilder<AccumulateDescrBuilder<P>, AndDescr> source() {
    CEDescrBuilder<AccumulateDescrBuilder<P>, AndDescr> and = new CEDescrBuilderImpl<AccumulateDescrBuilder<P>, AndDescr>(this, new AndDescr());
    descr.setInput(and.getDescr());
    return and;
}
Also used : AccumulateDescrBuilder(org.drools.compiler.lang.api.AccumulateDescrBuilder) AndDescr(org.drools.compiler.lang.descr.AndDescr)

Example 24 with AndDescr

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

the class CEDescrBuilderImpl method and.

/**
 * {@inheritDoc}
 */
public CEDescrBuilder<CEDescrBuilder<P, T>, AndDescr> and() {
    AndDescr andDescr = new AndDescr();
    ((ConditionalElementDescr) descr).addDescr(andDescr);
    return new CEDescrBuilderImpl<CEDescrBuilder<P, T>, AndDescr>(this, andDescr);
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr)

Example 25 with AndDescr

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

the class KnowledgeBuilderTest method testPredicate.

@Test
public void testPredicate() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    final BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
    pattern.addConstraint(fieldBindingDescr);
    final BindingDescr fieldBindingDescr2 = new BindingDescr("y", "price");
    pattern.addConstraint(fieldBindingDescr2);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    final ExprConstraintDescr predicate = new ExprConstraintDescr("eval(( ( Integer )map.get( new Integer(x) )).intValue() == y)");
    pattern.addConstraint(predicate);
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Aggregations

AndDescr (org.drools.compiler.lang.descr.AndDescr)79 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)54 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)52 Test (org.junit.Test)52 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)39 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)27 Cheese (org.drools.compiler.Cheese)13 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)12 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)12 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)11 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)8 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)8 OrDescr (org.drools.compiler.lang.descr.OrDescr)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)7 NotDescr (org.drools.compiler.lang.descr.NotDescr)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 List (java.util.List)4 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)4 QueryDescr (org.drools.compiler.lang.descr.QueryDescr)4