Search in sources :

Example 56 with TypeDeclaration

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());
}
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 57 with TypeDeclaration

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());
}
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 58 with TypeDeclaration

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());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Test(org.junit.Test)

Example 59 with TypeDeclaration

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);
}
Also used : TypeDeclaration(org.drools.core.rule.TypeDeclaration) Test(org.junit.Test)

Example 60 with TypeDeclaration

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);
        }
    }
}
Also used : TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

TypeDeclaration (org.drools.core.rule.TypeDeclaration)72 Test (org.junit.Test)15 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)12 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)10 ClassDefinition (org.drools.core.factmodel.ClassDefinition)9 ClassObjectType (org.drools.core.base.ClassObjectType)7 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)4 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)4 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 ObjectType (org.drools.core.spi.ObjectType)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)3 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)3 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)3