Search in sources :

Example 16 with TypeDeclarationDescr

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

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

the class KnowledgeBuilderTest method testTypeDeclaration.

@Test
public void testTypeDeclaration() throws Exception {
    PackageDescr pkgDescr = new PackageDescr("org.drools.compiler");
    TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("StockTick");
    typeDescr.addAnnotation(Role.class.getCanonicalName(), "Event");
    typeDescr.addAnnotation(TypeSafe.class.getCanonicalName(), "true");
    pkgDescr.addTypeDeclaration(typeDescr);
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    builder.addPackage(pkgDescr);
    if (builder.hasErrors()) {
        fail(builder.getErrors().toString());
    }
    InternalKnowledgePackage pkg = builder.getPackage(pkgDescr.getName());
    assertEquals(1, pkg.getTypeDeclarations().size());
    TypeDeclaration type = pkg.getTypeDeclaration("StockTick");
    assertTrue(type.isTypesafe());
    assertEquals(Role.Type.EVENT, type.getRole());
    assertEquals(StockTick.class, type.getTypeClass());
}
Also used : Role(org.kie.api.definition.type.Role) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) TypeSafe(org.kie.api.definition.type.TypeSafe) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 18 with TypeDeclarationDescr

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

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

the class DrlParserTest method testDeclaredSuperType.

@Test
public void testDeclaredSuperType() throws Exception {
    String drl = "package foo \n" + "declare Bean1 \n" + "age: int \n" + "name : String \n" + "end \n" + "declare Bean2 extends Bean1\n" + "cheese : String \n" + "end";
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr pkgDescr = parser.parse(null, drl);
    TypeDeclarationDescr bean1Type = pkgDescr.getTypeDeclarations().get(0);
    assertNull(bean1Type.getSuperTypeName());
    TypeDeclarationDescr bean2Type = pkgDescr.getTypeDeclarations().get(1);
    assertEquals("Bean1", bean2Type.getSuperTypeName());
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 20 with TypeDeclarationDescr

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

the class TypeDeclarationFactory method processAnnotations.

public static void processAnnotations(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
    Role role = typeDescr.getTypedAnnotation(Role.class);
    if (role != null) {
        type.setRole(role.value());
    }
    TypeSafe typeSafe = typeDescr.getTypedAnnotation(TypeSafe.class);
    if (typeSafe != null) {
        type.setTypesafe(typeSafe.value());
    }
    if (typeDescr instanceof EnumDeclarationDescr) {
        type.setKind(TypeDeclaration.Kind.ENUM);
    } else if (typeDescr instanceof TypeDeclarationDescr && ((TypeDeclarationDescr) typeDescr).isTrait()) {
        type.setKind(TypeDeclaration.Kind.TRAIT);
    }
    type.setDynamic(typeDescr.hasAnnotation(PropertyChangeSupport.class));
}
Also used : Role(org.kie.api.definition.type.Role) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) EnumDeclarationDescr(org.drools.compiler.lang.descr.EnumDeclarationDescr) PropertyChangeSupport(org.kie.api.definition.type.PropertyChangeSupport) TypeSafe(org.kie.api.definition.type.TypeSafe)

Aggregations

TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)20 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)10 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)9 Test (org.junit.Test)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)5 TypeDeclaration (org.drools.core.rule.TypeDeclaration)5 ArrayList (java.util.ArrayList)4 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)4 EnumDeclarationDescr (org.drools.compiler.lang.descr.EnumDeclarationDescr)4 HashMap (java.util.HashMap)3 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)3 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)3 QualifiedName (org.drools.compiler.lang.descr.QualifiedName)3 ClassDefinition (org.drools.core.factmodel.ClassDefinition)3 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)3 Map (java.util.Map)2 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)2 ImportDescr (org.drools.compiler.lang.descr.ImportDescr)2 Traitable (org.drools.core.factmodel.traits.Traitable)2