Search in sources :

Example 41 with KiePackage

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

the class MergePackageTest method testMergingDifferentPackages2.

@Test
public void testMergingDifferentPackages2() throws Exception {
    // using different builders
    try {
        final Collection<KiePackage> kpkgs1 = loadKnowledgePackages("test_RuleNameClashes1.drl");
        KiePackage kpkg1 = kpkgs1.stream().filter(pkg -> pkg.getName().equals("org.drools.package1")).findFirst().get();
        assertEquals(1, kpkg1.getRules().size());
        final Collection<KiePackage> kpkgs2 = loadKnowledgePackages("test_RuleNameClashes2.drl");
        KiePackage kpkg2 = kpkgs2.stream().filter(pkg -> pkg.getName().equals("org.drools.package2")).findFirst().get();
        assertEquals(1, kpkg2.getRules().size());
        InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
        kbase.addPackages(kpkgs1);
        kbase.addPackages(kpkgs2);
        kbase = SerializationHelper.serializeObject(kbase);
        final KieSession ksession = createKnowledgeSession(kbase);
        final List results = new ArrayList();
        ksession.setGlobal("results", results);
        ksession.insert(new Cheese("stilton", 10));
        ksession.insert(new Cheese("brie", 5));
        ksession.fireAllRules();
        assertEquals(results.toString(), 2, results.size());
        assertTrue(results.contains("p1.r1"));
        assertTrue(results.contains("p2.r1"));
    } catch (final KnowledgeBuilderImpl.PackageMergeException e) {
        fail("Should not raise exception when merging different packages into the same rulebase: " + e.getMessage());
    } catch (final Exception e) {
        e.printStackTrace();
        fail("unexpected exception: " + e.getMessage());
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 42 with KiePackage

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

the class MergePackageTest method testMergingDifferentPackages.

@Test
public void testMergingDifferentPackages() throws Exception {
    // using the same builder
    try {
        final Collection<KiePackage> kpkgs = loadKnowledgePackages("test_RuleNameClashes1.drl", "test_RuleNameClashes2.drl");
        assertEquals(3, kpkgs.size());
        for (final KiePackage kpkg : kpkgs) {
            if (kpkg.getName().equals("org.drools.package1")) {
                assertEquals("rule 1", kpkg.getRules().iterator().next().getName());
            }
        }
    } catch (final KnowledgeBuilderImpl.PackageMergeException e) {
        fail("unexpected exception: " + e.getMessage());
    } catch (final RuntimeException e) {
        e.printStackTrace();
        fail("unexpected exception: " + e.getMessage());
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Test(org.junit.Test)

Example 43 with KiePackage

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

the class MergePackageTest method testMergePackageWithSameRuleNames.

@Test
public void testMergePackageWithSameRuleNames() throws Exception {
    final InternalKnowledgeBase kbase = (InternalKnowledgeBase) SerializationHelper.serializeObject(loadKnowledgeBase("test_MergePackageWithSameRuleNames1.drl"));
    final Collection<KiePackage> kpkgs = loadKnowledgePackages("test_MergePackageWithSameRuleNames2.drl");
    kbase.addPackages(kpkgs);
    final KieSession ksession = createKnowledgeSession(kbase);
    final List results = new ArrayList();
    ksession.setGlobal("results", results);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals("rule1 for the package2", results.get(0));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 44 with KiePackage

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

the class CommonTestMethodBase method loadKnowledgeBase.

protected KieBase loadKnowledgeBase(KnowledgeBuilderConfiguration kbuilderConf, KieBaseConfiguration kbaseConf, String... classPathResources) {
    Collection<KiePackage> knowledgePackages = loadKnowledgePackages(kbuilderConf, classPathResources);
    if (kbaseConf == null) {
        kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConf);
    kbase.addPackages(knowledgePackages);
    try {
        kbase = SerializationHelper.serializeObject(kbase);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return kbase;
}
Also used : KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 45 with KiePackage

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

the class CommonTestMethodBase method loadKnowledgeBase.

protected KieBase loadKnowledgeBase(KnowledgeBuilderConfiguration kbuilderConf, KieBaseConfiguration kbaseConf, PackageDescr descr) {
    Collection<KiePackage> knowledgePackages = loadKnowledgePackages(kbuilderConf, descr);
    if (kbaseConf == null) {
        kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConf);
    kbase.addPackages(knowledgePackages);
    try {
        kbase = SerializationHelper.serializeObject(kbase);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return kbase;
}
Also used : KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Aggregations

KiePackage (org.kie.api.definition.KiePackage)124 Test (org.junit.Test)93 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)80 KieSession (org.kie.api.runtime.KieSession)70 ArrayList (java.util.ArrayList)44 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)39 List (java.util.List)25 Cheese (org.drools.compiler.Cheese)22 Person (org.drools.compiler.Person)18 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)10 Resource (org.kie.api.io.Resource)9 StringReader (java.io.StringReader)8 HashMap (java.util.HashMap)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 Map (java.util.Map)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 KieBase (org.kie.api.KieBase)7