use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class TemplatesTest method testManyRules.
private void testManyRules(final String drl, final int expectedRulesCount) {
final Resource drlResource = KieServices.Factory.get().getResources().newReaderResource(new StringReader(drl));
drlResource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, drlResource);
Collection<KiePackage> pkgs = kbase.getKiePackages();
Assertions.assertThat(pkgs.size()).isEqualTo(1);
KiePackage pkg = pkgs.iterator().next();
// check of generated rules size from template
Assertions.assertThat(pkg.getRules().size()).isEqualTo(expectedRulesCount);
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class TemplatesTest method testCorrectnessCheck.
private void testCorrectnessCheck(final String drl) {
final Resource drlResource = KieServices.Factory.get().getResources().newReaderResource(new StringReader(drl));
drlResource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, drlResource);
final KieSession session = kbase.newKieSession();
final List<String> list = new ArrayList<String>();
session.setGlobal("list", list);
session.insert(new Vegetable("tomato", 350, 8, 3, Taste.GOOD));
session.insert(new Vegetable("tomato", 150, 8, 3, Taste.BAD));
session.insert(new Vegetable("tomato", 350, 8, 7, Taste.GOOD));
session.insert(new Vegetable("tomato", 1000, 8, 6, Taste.EXCELENT));
session.insert(new Vegetable("cucumber", 1500, 19, 5, Taste.EXCELENT));
session.insert(new Vegetable("cucumber", 1500, 21, 5, Taste.EXCELENT));
session.insert(new Vegetable("carrot", 1000, 8, 6, Taste.EXCELENT));
session.insert(new Vegetable("carrot", 200, 8, 1, Taste.HORRIBLE));
session.insert(new Vegetable("onion", 500, 7, 4, Taste.EXCELENT));
session.fireAllRules();
// check of size of satisfying items
Assertions.assertThat(list.size()).isEqualTo(4);
final Collection<KiePackage> pkgs = kbase.getKiePackages();
Assertions.assertThat(pkgs.size()).isEqualTo(1);
final KiePackage pkg = pkgs.iterator().next();
// check of generated rules size from template
Assertions.assertThat(pkg.getRules().size()).isEqualTo(3);
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class IncrementalCompilationTest method createAndDeployAndTest.
public static void createAndDeployAndTest(KieContainer kc, String version, String header, String drls, String... ruleNames) {
if (ruleNames == null) {
ruleNames = new String[0];
}
KieServices ks = KieServices.Factory.get();
StringBuilder sbuilder = new StringBuilder();
sbuilder.append(header);
sbuilder.append(drls);
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", version);
KieModule km = createAndDeployJar(ks, releaseId1, sbuilder.toString());
kc.updateToVersion(km.getReleaseId());
KiePackage kpkg = kc.getKieBase().getKiePackage("org.drools.compiler");
assertEquals(ruleNames.length, kpkg.getRules().size());
Map<String, Rule> rules = rulestoMap(kpkg.getRules());
int i = 0;
for (String ruleName : ruleNames) {
assertEquals(ruleName, i++, ((RuleImpl) rules.get(ruleName)).getLoadOrder());
}
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class RemoveRuleTest method testRemoveBigRule.
@Test
public void testRemoveBigRule() throws Exception {
// JBRULES-3496
final String str = "package org.drools.compiler.test\n" + "\n" + "declare SimpleFact\n" + " patientSpaceId : String\n" + " block : int\n" + "end\n" + "\n" + "declare SimpleMembership\n" + " patientSpaceId : String\n" + " listId : String\n" + "end\n" + "\n" + "declare SimplePatient\n" + " spaceId : String\n" + "end\n" + "\n" + "rule \"RTR - 47146 retract\"\n" + "agenda-group \"list membership\"\n" + "when\n" + " $listMembership0 : SimpleMembership( $listMembershipPatientSpaceIdRoot : patientSpaceId, ( listId != null && listId == \"47146\" ) )\n" + " not ( $patient0 : SimplePatient( $patientSpaceIdRoot : spaceId, spaceId != null && spaceId == $listMembershipPatientSpaceIdRoot ) \n" + " and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 1 )\n" + " ) or ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 2 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 3 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 4 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 5 )\n" + " ) ) and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 6 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 7 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 8 )\n" + " ) ) and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 9 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 10 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 11 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 12 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 13 )\n" + " ) or ( (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 14 )\n" + " ) and (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 15 )\n" + " ) ) or ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 16 )\n" + " ) and ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 17 )\n" + " ) ) or ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 18 )\n" + " ) and (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 19 )\n" + " ) ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 20 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 21 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 22 )\n" + " ) or ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 23 )\n" + " ) and (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 24 )\n" + " ) ) ) and ( ( " + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 25 )\n" + " ) or (" + " SimpleFact( patientSpaceId == $patientSpaceIdRoot, block == 26 )\n" + " ) ) )\n" + "then\n" + "end\n";
final Collection<KiePackage> kpgs = loadKnowledgePackagesFromString(str);
assertEquals(1, kpgs.size());
final InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
kbase.addPackages(kpgs);
kbase.removeKiePackage(kpgs.iterator().next().getName());
final EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
for (final ObjectTypeNode otn : epn.getObjectTypeNodes().values()) {
final ObjectSink[] sinks = otn.getObjectSinkPropagator().getSinks();
if (sinks.length > 0) {
fail(otn + " has sinks " + Arrays.toString(sinks));
}
}
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class MarshallingTest method testSerializeAddRemove_NoClassDefFoundError.
/*
* I have tried both the scenarios
* 1. Remove a rule from a pkg.
* 2. Remove a pkg
*
* But both cases after inserting associated data points (i.e data points which are used to fire/activate the removed rule)
* session.fireAllRules() is throwing NoClassDefFoundError
*/
@Test
public void testSerializeAddRemove_NoClassDefFoundError() throws Exception {
// Create a rulebase, a session, and test it
Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1_0.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
kBase = SerializationHelper.serializeObject(kBase);
List results = new ArrayList();
KieSession session = kBase.newKieSession();
session.setGlobal("results", results);
InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie1 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
session.fireAllRules();
assertEquals(1, results.size());
assertEquals(stilton1.getObject(), results.get(0));
// serialize session and rulebase
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dynamic add pkgs
kpkgs = loadKnowledgePackages("../test_Dynamic3_0.drl");
kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
results.clear();
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
InternalFactHandle bob1 = (InternalFactHandle) session.insert(new Person("bob", 20));
InternalFactHandle bob2 = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(3, results.size());
assertEquals(stilton2.getObject(), results.get(0));
assertEquals(bob2.getObject(), results.get(1));
assertEquals(bob1.getObject(), results.get(2));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
results.clear();
// CASE 1: remove rule
kBase.removeRule("org.drools.compiler.test", "like stilton");
InternalFactHandle stilton3 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
InternalFactHandle bob3 = (InternalFactHandle) session.insert(new Person("bob", 20));
InternalFactHandle bob4 = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(2, results.size());
assertEquals(bob4.getObject(), results.get(0));
assertEquals(bob3.getObject(), results.get(1));
// now recreate the rulebase, deserialize the session and test it
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
results.clear();
// CASE 2: remove pkg
kBase.removeKiePackage("org.drools.compiler.test");
InternalFactHandle stilton4 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
InternalFactHandle brie4 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
InternalFactHandle bob5 = (InternalFactHandle) session.insert(new Person("bob", 20));
InternalFactHandle bob6 = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(2, results.size());
assertEquals(bob6.getObject(), results.get(0));
assertEquals(bob5.getObject(), results.get(1));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
results.clear();
session.setGlobal("results", results);
InternalFactHandle stilton5 = (InternalFactHandle) session.insert(new Cheese("stilton", 30));
InternalFactHandle brie5 = (InternalFactHandle) session.insert(new Cheese("brie", 30));
InternalFactHandle bob7 = (InternalFactHandle) session.insert(new Person("bob", 30));
InternalFactHandle bob8 = (InternalFactHandle) session.insert(new Person("bob", 40));
session.fireAllRules();
assertEquals(2, results.size());
assertEquals(bob8.getObject(), results.get(0));
assertEquals(bob7.getObject(), results.get(1));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.dispose();
}
Aggregations