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"));
}
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());
}
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"));
}
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());
}
}
}
}
}
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) }));
}
}
}
Aggregations