Search in sources :

Example 91 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr 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 92 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr 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 93 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr 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 94 with PatternDescr

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

the class KnowledgeBuilderTest method testTypeDeclarationWithFieldMetadata.

@Test
public void testTypeDeclarationWithFieldMetadata() throws Exception {
    PackageDescr pkgDescr = new PackageDescr("org.drools.compiler.test");
    TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("TypeWithFieldMeta");
    TypeFieldDescr f1 = new TypeFieldDescr("field", new PatternDescr("String"));
    f1.addAnnotation("custom", null);
    typeDescr.addField(f1);
    pkgDescr.addTypeDeclaration(typeDescr);
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    builder.addPackage(pkgDescr);
    assertFalse(builder.hasErrors());
    InternalKnowledgePackage bp = builder.getPackage(pkgDescr.getName());
    final FactType factType = bp.getFactType("org.drools.compiler.test.TypeWithFieldMeta");
    assertNotNull(factType);
    final FactField field = factType.getField("field");
    assertNotNull(field);
    final Map<String, Object> fieldMetaData = field.getMetaData();
    assertNotNull("No field-level custom metadata got compiled", fieldMetaData);
    assertTrue("Field metadata does not include expected value", fieldMetaData.containsKey("custom"));
}
Also used : FactField(org.kie.api.definition.type.FactField) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) FactType(org.kie.api.definition.type.FactType) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 95 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr 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)

Aggregations

PatternDescr (org.drools.compiler.lang.descr.PatternDescr)150 Test (org.junit.Test)122 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)84 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)56 AndDescr (org.drools.compiler.lang.descr.AndDescr)52 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)19 Cheese (org.drools.compiler.Cheese)14 FromDescr (org.drools.compiler.lang.descr.FromDescr)14 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11 NotDescr (org.drools.compiler.lang.descr.NotDescr)11 OrDescr (org.drools.compiler.lang.descr.OrDescr)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 Element (org.w3c.dom.Element)9 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)8 InputStreamReader (java.io.InputStreamReader)7 List (java.util.List)7 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)7 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7