use of org.drools.core.rule.TypeDeclaration 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());
}
use of org.drools.core.rule.TypeDeclaration 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.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class TypeDeclarationMergingTest method testOverrideFromParentInterface.
@Test
public void testOverrideFromParentInterface() {
// inherits role but not typesafe
String str = "" + "package org.drools.compiler.test \n" + "global java.util.List list \n" + "declare " + IB.class.getCanonicalName() + "\n" + " @typesafe(true)\n" + " @role(event)\n" + "end\n" + "declare " + DImpl.class.getCanonicalName() + "\n" + " @typesafe(false)\n" + "end\n";
KnowledgeBuilderImpl builder = getPackageBuilder(str);
TypeDeclaration tdecl = builder.getTypeDeclaration(DImpl.class);
assertEquals(false, tdecl.isTypesafe());
assertEquals(Role.Type.EVENT, tdecl.getRole());
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class TypeDeclarationMergingTest method testMask.
@Test
public void testMask() {
TypeDeclaration tdeclr = new TypeDeclaration(CImpl.class.getName());
assertEquals(0, tdeclr.getSetMask());
tdeclr.setRole(Role.Type.EVENT);
assertEquals(TypeDeclaration.ROLE_BIT, tdeclr.getSetMask() & TypeDeclaration.ROLE_BIT);
assertFalse(TypeDeclaration.TYPESAFE_BIT == (tdeclr.getSetMask() & TypeDeclaration.TYPESAFE_BIT));
assertFalse(TypeDeclaration.FORMAT_BIT == (tdeclr.getSetMask() & TypeDeclaration.FORMAT_BIT));
tdeclr.setTypesafe(false);
assertEquals(TypeDeclaration.ROLE_BIT, tdeclr.getSetMask() & TypeDeclaration.ROLE_BIT);
assertEquals(TypeDeclaration.TYPESAFE_BIT, tdeclr.getSetMask() & TypeDeclaration.TYPESAFE_BIT);
assertFalse(TypeDeclaration.FORMAT_BIT == (tdeclr.getSetMask() & TypeDeclaration.FORMAT_BIT));
tdeclr = new TypeDeclaration(CImpl.class.getName());
tdeclr.setTypesafe(true);
assertFalse(TypeDeclaration.ROLE_BIT == (tdeclr.getSetMask() & TypeDeclaration.ROLE_BIT));
assertEquals(TypeDeclaration.TYPESAFE_BIT, tdeclr.getSetMask() & TypeDeclaration.TYPESAFE_BIT);
assertFalse(TypeDeclaration.FORMAT_BIT == (tdeclr.getSetMask() & TypeDeclaration.FORMAT_BIT));
tdeclr.setFormat(Format.POJO);
assertFalse(TypeDeclaration.ROLE_BIT == (tdeclr.getSetMask() & TypeDeclaration.ROLE_BIT));
assertEquals(TypeDeclaration.TYPESAFE_BIT, tdeclr.getSetMask() & TypeDeclaration.TYPESAFE_BIT);
assertEquals(TypeDeclaration.FORMAT_BIT, tdeclr.getSetMask() & TypeDeclaration.FORMAT_BIT);
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class KnowledgeBaseImpl method populateTypeDeclarationMaps.
/**
* type classes must be re-wired after serialization
*
* @throws ClassNotFoundException
*/
private void populateTypeDeclarationMaps() throws ClassNotFoundException {
// FIXME: readLock
this.classTypeDeclaration = new HashMap<String, TypeDeclaration>();
for (InternalKnowledgePackage pkg : this.pkgs.values()) {
for (TypeDeclaration type : pkg.getTypeDeclarations().values()) {
type.setTypeClass(this.rootClassLoader.loadClass(type.getTypeClassName()));
this.classTypeDeclaration.put(type.getTypeClassName(), type);
}
}
}
Aggregations