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