Search in sources :

Example 41 with AndDescr

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

the class KnowledgeBuilderTest method testTimeWindowBehavior.

@Test
public void testTimeWindowBehavior() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final TypeDeclarationDescr typeDeclDescr = new TypeDeclarationDescr(StockTick.class.getName());
    typeDeclDescr.addAnnotation("role", "event");
    packageDescr.addTypeDeclaration(typeDeclDescr);
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr patternDescr = new PatternDescr(StockTick.class.getName(), "$tick");
    final BehaviorDescr windowDescr = new BehaviorDescr("window");
    windowDescr.setSubType("time");
    windowDescr.setParameters(Collections.singletonList("60000"));
    patternDescr.addBehavior(windowDescr);
    lhs.addDescr(patternDescr);
    ruleDescr.setConsequence("System.out.println( $tick );");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage pkg = builder.getPackageRegistry().get("p1").getPackage();
    final RuleImpl rule = pkg.getRule("rule-1");
    assertNotNull(rule);
    final Pattern pattern = (Pattern) rule.getLhs().getChildren().get(0);
    assertEquals(StockTick.class.getName(), ((ClassObjectType) pattern.getObjectType()).getClassType().getName());
    final Behavior window = pattern.getBehaviors().get(0);
    assertEquals(Behavior.BehaviorType.TIME_WINDOW, window.getType());
    assertEquals(60000, ((SlidingTimeWindow) window).getSize());
}
Also used : Pattern(org.drools.core.rule.Pattern) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) ClassObjectType(org.drools.core.base.ClassObjectType) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) StockTick(org.drools.compiler.StockTick) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Behavior(org.drools.core.rule.Behavior) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 42 with AndDescr

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

the class KnowledgeBuilderTest method testLiteral.

@Test
public void testLiteral() 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);
    pattern.addConstraint(new ExprConstraintDescr("type == 'stilton'"));
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
}
Also used : 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)

Example 43 with AndDescr

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

the class KnowledgeBuilderTest method createReturnValueRule.

private void createReturnValueRule(final PackageDescr packageDescr, final String expression) {
    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);
    BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
    pattern.addConstraint(fieldBindingDescr);
    fieldBindingDescr = new BindingDescr("y", "price");
    pattern.addConstraint(fieldBindingDescr);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    pattern.addConstraint(new ExprConstraintDescr("price == (" + expression + ")"));
    ruleDescr.setConsequence("update(stilton);");
}
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) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 44 with AndDescr

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

the class KnowledgeBuilderTest method createPredicateRule.

private void createPredicateRule(final PackageDescr packageDescr, final String expression) {
    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"));
    pattern.addConstraint(new ExprConstraintDescr(expression));
    ruleDescr.setConsequence("update(stilton);");
}
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) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 45 with AndDescr

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

the class KnowledgeBuilderTest method testNull.

@Test
public void testNull() {
    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 patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    final FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("==", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    assertLength(0, builder.getErrors().getErrors());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FieldConstraintDescr(org.drools.compiler.lang.descr.FieldConstraintDescr) Cheese(org.drools.compiler.Cheese) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) 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