Search in sources :

Example 6 with FactField

use of org.kie.api.definition.type.FactField 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 7 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class TypeDeclarationTest method testAnnotationReDefinition.

@Test
public void testAnnotationReDefinition() {
    String str1 = "";
    str1 += "package org.kie \n" + "declare org.kie.EventA \n" + "    name : String \n" + "    duration : Long \n" + "end \n";
    String str2 = "";
    str2 += "package org.kie \n" + "declare org.kie.EventA \n" + "    @role (event) \n" + "    @duration (duration) \n" + "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str1.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    // No Warnings
    KnowledgeBuilderResults warnings = kbuilder.getResults(ResultSeverity.WARNING);
    Assert.assertEquals(0, warnings.size());
    // just 1 package was created
    Assert.assertEquals(1, kbuilder.getKnowledgePackages().size());
    // Get the Fact Type for org.kie.EventA
    FactType factType = ((KnowledgePackageImpl) kbuilder.getKnowledgePackages().iterator().next()).getFactType("org.kie.EventA");
    assertNotNull(factType);
    // 'name' field must still be there
    FactField field = factType.getField("name");
    assertNotNull(field);
    // 'duration' field must still be there
    field = factType.getField("duration");
    assertNotNull(field);
    // New Annotations must be there too
    TypeDeclaration typeDeclaration = ((KnowledgePackageImpl) kbuilder.getKnowledgePackages().iterator().next()).getTypeDeclaration("EventA");
    assertEquals(Role.Type.EVENT, typeDeclaration.getRole());
    assertEquals("duration", typeDeclaration.getDurationAttribute());
}
Also used : KnowledgeBuilderResults(org.kie.internal.builder.KnowledgeBuilderResults) FactField(org.kie.api.definition.type.FactField) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 8 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class TypeDeclarationTest method testTypeDeclarationMetadata.

@Test
public void testTypeDeclarationMetadata() {
    String str = "";
    str += "package org.drools.compiler.test; \n" + "import org.drools.compiler.compiler.TypeDeclarationTest.KlassAnnotation; \n" + "import org.drools.compiler.compiler.TypeDeclarationTest.FieldAnnotation; \n" + "import org.drools.compiler.Person\n" + "\n" + "declare Bean \n" + "@role(event) \n" + "@expires( 1s ) \n" + "@KlassAnnotation( \"klass\" )" + "" + "    name : String @key @FieldAnnotation( prop = \"fld\" )\n" + "end \n" + "declare Person @role(event) end";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    System.err.println(kbuilder.getErrors());
    assertFalse(kbuilder.hasErrors());
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addPackages(kbuilder.getKnowledgePackages());
    FactType bean = kBase.getFactType("org.drools.compiler.test", "Bean");
    FactType pers = kBase.getFactType("org.drools", "Person");
    assertEquals("org.drools.compiler.test.Bean", bean.getName());
    assertEquals("Bean", bean.getSimpleName());
    assertEquals("org.drools.compiler.test", bean.getPackageName());
    assertEquals(3, bean.getClassAnnotations().size());
    Annotation ann = bean.getClassAnnotations().get(0);
    if (!ann.getName().equals("org.drools.compiler.compiler.TypeDeclarationTest$KlassAnnotation")) {
        ann = bean.getClassAnnotations().get(1);
    }
    if (!ann.getName().equals("org.drools.compiler.compiler.TypeDeclarationTest$KlassAnnotation")) {
        ann = bean.getClassAnnotations().get(2);
    }
    assertEquals("org.drools.compiler.compiler.TypeDeclarationTest$KlassAnnotation", ann.getName());
    assertEquals("klass", ann.getPropertyValue("value"));
    assertEquals(String.class, ann.getPropertyType("value"));
    assertEquals(2, bean.getMetaData().size());
    assertEquals("event", bean.getMetaData().get("role"));
    FactField field = bean.getField("name");
    assertNotNull(field);
    assertEquals(2, field.getFieldAnnotations().size());
    Annotation fnn = field.getFieldAnnotations().get(0);
    if (!fnn.getName().equals("org.drools.compiler.compiler.TypeDeclarationTest$FieldAnnotation")) {
        fnn = field.getFieldAnnotations().get(1);
    }
    assertEquals("org.drools.compiler.compiler.TypeDeclarationTest$FieldAnnotation", fnn.getName());
    assertEquals("fld", fnn.getPropertyValue("prop"));
    assertEquals(String.class, fnn.getPropertyType("prop"));
    assertEquals(1, field.getMetaData().size());
    assertTrue(field.getMetaData().containsKey("key"));
}
Also used : FactField(org.kie.api.definition.type.FactField) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Annotation(org.kie.api.definition.type.Annotation) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 9 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class TypeDeclarationTest method testDeclareFieldArray.

@Test
public void testDeclareFieldArray() {
    String str1 = "" + "package org.drools " + "declare Test end " + "declare Pet " + "    owners : Owner[] " + "    twoDimArray : Foo[][] " + "    friends : Pet[] " + "    ages : int[] " + "end " + "declare Owner " + "     name : String " + "end " + "declare Foo end " + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    for (KiePackage kp : kbuilder.getKnowledgePackages()) {
        if (kp.getName().equals("org.drools")) {
            Collection<FactType> types = kp.getFactTypes();
            for (FactType type : types) {
                if ("org.drools.Pet".equals(type.getName())) {
                    assertEquals(4, type.getFields().size());
                    FactField owners = type.getField("owners");
                    assertTrue(owners != null && owners.getType().getSimpleName().equals("Owner[]") && owners.getType().isArray());
                    FactField twoDim = type.getField("twoDimArray");
                    assertTrue(twoDim != null && twoDim.getType().getSimpleName().equals("Foo[][]") && twoDim.getType().isArray());
                    FactField friends = type.getField("friends");
                    assertTrue(friends != null && friends.getType().getSimpleName().equals("Pet[]") && friends.getType().isArray());
                    FactField ages = type.getField("ages");
                    assertTrue(ages != null && ages.getType().getSimpleName().equals("int[]") && ages.getType().isArray());
                }
            }
        }
    }
}
Also used : FactField(org.kie.api.definition.type.FactField) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 10 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class TraitCoreWrapperClassBuilderImpl method initializeDynamicTypeStructures.

protected void initializeDynamicTypeStructures(MethodVisitor mv, String wrapperName, ClassDefinition coreDef) {
    if (coreDef.isFullTraiting()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, Type.getInternalName(TraitFieldTMSImpl.class));
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TraitFieldTMSImpl.class), "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(wrapperName), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
        for (FactField hardField : coreDef.getFields()) {
            FieldDefinition fld = (FieldDefinition) hardField;
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(wrapperName), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
            mv.visitLdcInsn(Type.getType(Type.getDescriptor(coreDef.getDefinedClass())));
            mv.visitLdcInsn(fld.resolveAlias());
            if (BuildUtils.isPrimitive(fld.getTypeName())) {
                // mv.visitFieldInsn( GETSTATIC, BuildUtils.getInternalType( BuildUtils.box( fld.getTypeName() ) ), "TYPE", Type.getDescriptor( Class.class ) );
                mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(fld.getTypeName()))));
            } else {
                mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(fld.getTypeName())));
            }
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(wrapperName), "core", Type.getDescriptor(coreDef.getDefinedClass()));
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(coreDef.getDefinedClass()), BuildUtils.getterName(fld.getName(), fld.getTypeName()), "()" + BuildUtils.getTypeDescriptor(fld.getTypeName()));
            if (BuildUtils.isPrimitive(fld.getTypeName())) {
                mv.visitMethodInsn(INVOKESTATIC, BuildUtils.getInternalType(BuildUtils.box(fld.getTypeName())), "valueOf", "(" + BuildUtils.getTypeDescriptor(fld.getTypeName()) + ")" + BuildUtils.getTypeDescriptor(BuildUtils.box(fld.getTypeName())));
            }
            if (fld.getInitExpr() != null) {
                mv.visitLdcInsn(fld.getInitExpr());
            } else {
                mv.visitInsn(ACONST_NULL);
            }
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitFieldTMS.class), "registerField", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(Class.class), Type.getType(String.class), Type.getType(Class.class), Type.getType(Object.class), Type.getType(String.class) }));
        }
    }
}
Also used : FactField(org.kie.api.definition.type.FactField) Type(org.mvel2.asm.Type) FieldDefinition(org.drools.core.factmodel.FieldDefinition)

Aggregations

FactField (org.kie.api.definition.type.FactField)14 Test (org.junit.Test)7 FactType (org.kie.api.definition.type.FactType)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)4 FieldDefinition (org.drools.core.factmodel.FieldDefinition)3 BitSet (java.util.BitSet)2 ClassDefinition (org.drools.core.factmodel.ClassDefinition)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 KiePackage (org.kie.api.definition.KiePackage)2 Type (org.mvel2.asm.Type)2 Externalizable (java.io.Externalizable)1 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1