Search in sources :

Example 1 with GlobalImpl

use of org.drools.core.definitions.rule.impl.GlobalImpl in project drools by kiegroup.

the class KnowledgePackageMetaDataTest method testMetaData.

@Test
public void testMetaData() {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        fail(kBuilder.getErrors().toString());
    }
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addPackages(kBuilder.getKnowledgePackages());
    KiePackage pack = kBase.getPackage("org.drools.compiler.test.definitions");
    assertNotNull(pack);
    assertEquals(2, pack.getFunctionNames().size());
    assertTrue(pack.getFunctionNames().contains("fun1"));
    assertTrue(pack.getFunctionNames().contains("fun2"));
    assertEquals(2, pack.getGlobalVariables().size());
    GlobalImpl g1 = new GlobalImpl("N", "java.lang.Integer");
    GlobalImpl g2 = new GlobalImpl("list", "java.util.List");
    assertTrue(pack.getGlobalVariables().contains(g1));
    assertTrue(pack.getGlobalVariables().contains(g2));
    assertEquals(2, pack.getFactTypes().size());
    FactType type;
    for (int j = 0; j < 2; j++) {
        type = pack.getFactTypes().iterator().next();
        if (type.getName().equals("org.drools.compiler.test.definitions.Person")) {
            assertEquals(2, type.getFields().size());
        } else if (type.getName().equals("org.drools.compiler.test.definitions.Foo")) {
            assertEquals("org.drools.compiler.test.definitions.Person", type.getSuperClass());
            FactField fld = type.getField("bar");
            assertEquals(2, fld.getIndex());
            assertEquals(String.class, fld.getType());
        } else {
            fail("Unexpected fact type " + type);
        }
    }
    assertEquals(2, pack.getQueries().size());
    for (Query q : pack.getQueries()) {
        assertTrue(q.getName().equals("qry1") || q.getName().equals("qry2"));
    }
    assertEquals(4, pack.getRules().size());
    assertTrue(pack.getRules().containsAll(pack.getQueries()));
}
Also used : FactField(org.kie.api.definition.type.FactField) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Query(org.kie.api.definition.rule.Query) KiePackage(org.kie.api.definition.KiePackage) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) GlobalImpl(org.drools.core.definitions.rule.impl.GlobalImpl) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Aggregations

GlobalImpl (org.drools.core.definitions.rule.impl.GlobalImpl)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)1 Test (org.junit.Test)1 KiePackage (org.kie.api.definition.KiePackage)1 Query (org.kie.api.definition.rule.Query)1 FactField (org.kie.api.definition.type.FactField)1 FactType (org.kie.api.definition.type.FactType)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1