Search in sources :

Example 16 with TypeDeclarationDescr

use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.

the class TypeDeclarationFactory method processAnnotations.

public static void processAnnotations(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
    Role role = getTypedAnnotation(typeDescr, Role.class);
    if (role != null) {
        type.setRole(role.value());
    }
    TypeSafe typeSafe = getTypedAnnotation(typeDescr, 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) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) PropertyChangeSupport(org.kie.api.definition.type.PropertyChangeSupport) TypeSafe(org.kie.api.definition.type.TypeSafe)

Example 17 with TypeDeclarationDescr

use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.

the class TypeDeclarationBuilder method mergeTypeDescriptors.

private boolean mergeTypeDescriptors(AbstractClassTypeDeclarationDescr prev, AbstractClassTypeDeclarationDescr descr) {
    boolean isDef1 = isDefinition(prev);
    boolean isDef2 = isDefinition(descr);
    if (isDef1 && isDef2) {
        return false;
    }
    if (!prev.getSuperTypes().isEmpty() && !descr.getSuperTypes().isEmpty() && prev.getSuperTypes().size() != descr.getSuperTypes().size()) {
        return false;
    }
    if (prev.getSuperTypes().isEmpty()) {
        for (QualifiedName qn : descr.getSuperTypes()) {
            ((TypeDeclarationDescr) prev).addSuperType(qn);
        }
    }
    if (prev.getFields().isEmpty()) {
        for (String fieldName : descr.getFields().keySet()) {
            prev.addField(descr.getFields().get(fieldName));
        }
    }
    for (AnnotationDescr ad : descr.getAnnotations()) {
        prev.addQualifiedAnnotation(ad);
    }
    for (AnnotationDescr ad : prev.getAnnotations()) {
        if (!descr.getAnnotations().contains(ad)) {
            descr.addQualifiedAnnotation(ad);
        }
    }
    return true;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) QualifiedName(org.drools.drl.ast.descr.QualifiedName) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 18 with TypeDeclarationDescr

use of org.drools.drl.ast.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.drl.ast.descr.TypeDeclarationDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) BehaviorDescr(org.drools.drl.ast.descr.BehaviorDescr) ClassObjectType(org.drools.core.base.ClassObjectType) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) StockTick(org.drools.mvel.compiler.StockTick) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Behavior(org.drools.core.rule.Behavior) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 19 with TypeDeclarationDescr

use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.

the class RuleParserTest method testTypeDeclarationWithFields.

@Test
public void testTypeDeclarationWithFields() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "declare_type_with_fields.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    List<TypeDeclarationDescr> td = pkg.getTypeDeclarations();
    assertEquals(3, td.size());
    TypeDeclarationDescr d = td.get(0);
    assertEquals("SomeFact", d.getTypeName());
    assertEquals(2, d.getFields().size());
    assertTrue(d.getFields().containsKey("name"));
    assertTrue(d.getFields().containsKey("age"));
    TypeFieldDescr f = d.getFields().get("name");
    assertEquals("String", f.getPattern().getObjectType());
    f = d.getFields().get("age");
    assertEquals("Integer", f.getPattern().getObjectType());
    d = td.get(1);
    assertEquals("AnotherFact", d.getTypeName());
    TypeDeclarationDescr type = td.get(2);
    assertEquals("Person", type.getTypeName());
    assertEquals("fact", type.getAnnotation("role").getValue());
    assertEquals("\"Models a person\"", type.getAnnotation("doc").getValue("descr"));
    assertEquals("\"Bob\"", type.getAnnotation("doc").getValue("author"));
    assertEquals("Calendar.getInstance().getDate()", type.getAnnotation("doc").getValue("date"));
    assertEquals(2, type.getFields().size());
    TypeFieldDescr field = type.getFields().get("name");
    assertEquals("name", field.getFieldName());
    assertEquals("String", field.getPattern().getObjectType());
    assertEquals("\"John Doe\"", field.getInitExpr());
    assertEquals("50", field.getAnnotation("length").getValue("max"));
    assertNotNull(field.getAnnotation("key"));
    field = type.getFields().get("age");
    assertEquals("age", field.getFieldName());
    assertEquals("int", field.getPattern().getObjectType());
    assertEquals("-1", field.getInitExpr());
    assertEquals("0", field.getAnnotation("ranged").getValue("min"));
    assertEquals("150", field.getAnnotation("ranged").getValue("max"));
    assertEquals("-1", field.getAnnotation("ranged").getValue("unknown"));
}
Also used : TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 20 with TypeDeclarationDescr

use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.

the class RuleParserTest method testTypeWithMetaData.

@Test
public void testTypeWithMetaData() throws Exception {
    PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "type_with_meta.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final List<TypeDeclarationDescr> declarations = pkg.getTypeDeclarations();
    assertEquals(3, declarations.size());
}
Also used : TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Aggregations

TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)22 PackageDescr (org.drools.drl.ast.descr.PackageDescr)10 AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)9 Test (org.junit.Test)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 TypeFieldDescr (org.drools.drl.ast.descr.TypeFieldDescr)6 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)5 EnumDeclarationDescr (org.drools.drl.ast.descr.EnumDeclarationDescr)5 ArrayList (java.util.ArrayList)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 HashSet (java.util.HashSet)3 TypeResolver (org.drools.core.addon.TypeResolver)3 ClassDefinition (org.drools.core.factmodel.ClassDefinition)3 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)3 RuleDescr (org.drools.drl.ast.descr.RuleDescr)3 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2