use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertions.
@Test(timeout = 10000)
public void testLogicalInsertions() throws Exception {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertions.drl", getClass()), ResourceType.DRL);
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
KieSession session = createKnowledgeSession(kbase);
final List list = new ArrayList();
session.setGlobal("list", list);
final Cheese brie = new Cheese("brie", 12);
FactHandle brieHandle = session.insert(brie);
final Cheese provolone = new Cheese("provolone", 12);
FactHandle provoloneHandle = session.insert(provolone);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
System.out.println(list);
assertEquals(3, list.size());
assertEquals(3, session.getObjects().size());
brieHandle = getFactHandle(brieHandle, session);
session.retract(brieHandle);
session = getSerialisedStatefulKnowledgeSession(session, true);
assertEquals(2, session.getObjects().size());
provoloneHandle = getFactHandle(provoloneHandle, session);
session.retract(provoloneHandle);
session.fireAllRules();
assertEquals(0, session.getObjects().size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsDynamicRule.
// timeout=10000)
@Test()
public void testLogicalInsertionsDynamicRule() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsDynamicRule.drl", getClass()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
kbase.addPackages(kpkgs);
KieSession ksession = createKnowledgeSession(kbase);
final Cheese c1 = new Cheese("a", 1);
final Cheese c2 = new Cheese("b", 2);
final Cheese c3 = new Cheese("c", 3);
List list;
ksession.insert(c1);
FactHandle h = ksession.insert(c2);
ksession.insert(c3);
ksession.fireAllRules();
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
// Check logical Insertions where made for c2 and c3
list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
assertEquals(2, list.size());
assertFalse(list.contains(new Person(c1.getType())));
assertTrue(list.contains(new Person(c2.getType())));
assertTrue(list.contains(new Person(c3.getType())));
// this rule will make a logical assertion for c1 too
kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsDynamicRule2.drl", getClass()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
Collection<KiePackage> kpkgs2 = kbuilder.getKnowledgePackages();
kbase.addPackages(kpkgs2);
kbase = SerializationHelper.serializeObject(kbase);
ksession.fireAllRules();
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
kbase = (InternalKnowledgeBase) ksession.getKieBase();
// check all now have just one logical assertion each
list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
assertEquals(3, list.size());
assertTrue(list.contains(new Person(c1.getType())));
assertTrue(list.contains(new Person(c2.getType())));
assertTrue(list.contains(new Person(c3.getType())));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
// check the packages are correctly populated
assertEquals(3, kbase.getKiePackages().size());
KiePackage test = null, test2 = null;
// different JVMs return the package list in different order
for (KiePackage kpkg : kbase.getKiePackages()) {
if (kpkg.getName().equals("org.drools.compiler.test")) {
test = kpkg;
} else if (kpkg.getName().equals("org.drools.compiler.test2")) {
test2 = kpkg;
}
}
assertNotNull(test);
assertNotNull(test2);
assertEquals("rule1", test.getRules().iterator().next().getName());
assertEquals("rule2", test2.getRules().iterator().next().getName());
// now remove the first rule
kbase.removeRule(test.getName(), test.getRules().iterator().next().getName());
// different JVMs return the package list in different order
for (KiePackage kpkg : kbase.getKiePackages()) {
if (kpkg.getName().equals("org.drools.compiler.test")) {
test = kpkg;
} else if (kpkg.getName().equals("org.drools.compiler.test2")) {
test2 = kpkg;
}
}
assertNotNull(test);
assertNotNull(test2);
// Check the rule was correctly remove
assertEquals(0, test.getRules().size());
assertEquals(1, test2.getRules().size());
assertEquals("rule2", test2.getRules().iterator().next().getName());
list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
assertEquals("removal of the rule should result in retraction of c3's logical assertion", 2, list.size());
assertTrue("c1's logical assertion should not be deleted", list.contains(new Person(c1.getType())));
assertTrue("c2's logical assertion should not be deleted", list.contains(new Person(c2.getType())));
assertFalse("c3's logical assertion should be deleted", list.contains(new Person(c3.getType())));
c2.setPrice(3);
h = getFactHandle(h, ksession);
ksession.update(h, c2);
ksession.fireAllRules();
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
assertEquals("c2 now has a higher price, its logical assertion should be cancelled", 1, list.size());
assertFalse("The logical assertion cor c2 should have been deleted", list.contains(new Person(c2.getType())));
assertTrue("The logical assertion for c1 should exist", list.contains(new Person(c1.getType())));
// different JVMs return the package list in different order
for (KiePackage kpkg : kbase.getKiePackages()) {
if (kpkg.getName().equals("org.drools.compiler.test")) {
test = kpkg;
} else if (kpkg.getName().equals("org.drools.compiler.test2")) {
test2 = kpkg;
}
}
assertNotNull(test);
assertNotNull(test2);
kbase.removeRule(test2.getName(), test2.getRules().iterator().next().getName());
kbase = SerializationHelper.serializeObject(kbase);
// different JVMs return the package list in different order
for (KiePackage kpkg : kbase.getKiePackages()) {
if (kpkg.getName().equals("org.drools.compiler.test")) {
test = kpkg;
} else if (kpkg.getName().equals("org.drools.compiler.test2")) {
test2 = kpkg;
}
}
assertNotNull(test);
assertNotNull(test2);
assertEquals(0, test.getRules().size());
assertEquals(0, test2.getRules().size());
list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
assertEquals(0, list.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsLoop.
@Test(timeout = 10000)
public void testLogicalInsertionsLoop() throws Exception {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsLoop.drl", getClass()), ResourceType.DRL);
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
final KieSession session = createKnowledgeSession(kbase);
final List l = new ArrayList();
final Person a = new Person("a");
session.setGlobal("a", a);
session.setGlobal("l", l);
session.fireAllRules();
Collection<?> list = session.getObjects(new ClassObjectFilter(a.getClass()));
assertEquals("a still asserted.", 0, list.size());
assertEquals("Rule has not fired (looped) expected number of times", 10, l.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion.
@Test(timeout = 10000)
public void testLogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion() throws Exception {
// TODO JBRULES-1804
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion.drl", getClass()), ResourceType.DRL);
Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
kbase.addPackages(pkgs);
kbase = SerializationHelper.serializeObject(kbase);
KieSession session = createKnowledgeSession(kbase);
Sensor sensor1 = new Sensor(100, 0);
FactHandle sensor1Handle = session.insert(sensor1);
Sensor sensor2 = new Sensor(200, 0);
FactHandle sensor2Handle = session.insert(sensor2);
Sensor sensor3 = new Sensor(200, 0);
FactHandle sensor3Handle = session.insert(sensor3);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
List temperatureList = new ArrayList(session.getObjects(new ClassObjectFilter(Integer.class)));
assertTrue(temperatureList.contains(Integer.valueOf(100)));
assertTrue(temperatureList.contains(Integer.valueOf(200)));
assertEquals(2, temperatureList.size());
sensor1.setTemperature(150);
sensor1Handle = getFactHandle(sensor1Handle, session);
session.update(sensor1Handle, sensor1);
session = getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
temperatureList = new ArrayList(session.getObjects(new ClassObjectFilter(Integer.class)));
assertFalse(temperatureList.contains(Integer.valueOf(100)));
assertTrue(temperatureList.contains(Integer.valueOf(150)));
assertTrue(temperatureList.contains(Integer.valueOf(200)));
assertEquals(2, temperatureList.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class IncrementalCompilationTest method testRuleRemoval.
@Test
public void testRuleRemoval() throws Exception {
String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + " $m : Message()\n" + "then\n" + "end\n";
String drl2 = "rule R2 when\n" + " $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
String drl3 = "rule R3 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
// Create an in-memory jar for version 1.0.0
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJar(ks, releaseId1, drl1 + drl2 + drl3);
// Create a session and fire rules
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KiePackage kpkg = kc.getKieBase().getKiePackage("org.drools.compiler");
assertEquals(3, kpkg.getRules().size());
Map<String, Rule> rules = rulestoMap(kpkg.getRules());
assertNotNull(rules.get("R1"));
assertNotNull(rules.get("R2"));
assertNotNull(rules.get("R3"));
RuleTerminalNode rtn1_1 = (RuleTerminalNode) ((KnowledgeBaseImpl) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R1")[0];
RuleTerminalNode rtn2_1 = (RuleTerminalNode) ((KnowledgeBaseImpl) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R2")[0];
RuleTerminalNode rtn3_1 = (RuleTerminalNode) ((KnowledgeBaseImpl) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R3")[0];
// Create a new jar for version 1.1.0
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJar(ks, releaseId2, drl1 + drl3);
// try to update the container to version 1.1.0
kc.updateToVersion(releaseId2);
KnowledgeBaseImpl rb_2 = ((KnowledgeBaseImpl) kc.getKieBase());
RuleTerminalNode rtn1_2 = (RuleTerminalNode) rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R1")[0];
RuleTerminalNode rtn3_2 = (RuleTerminalNode) rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R3")[0];
assertNull(rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R2"));
assertSame(rtn3_1, rtn3_2);
assertSame(rtn1_1, rtn1_2);
kpkg = kc.getKieBase().getKiePackage("org.drools.compiler");
assertEquals(2, kpkg.getRules().size());
rules = rulestoMap(kpkg.getRules());
assertNotNull(rules.get("R1"));
assertNull(rules.get("R2"));
assertNotNull(rules.get("R3"));
}
Aggregations