use of org.kie.internal.builder.KnowledgeBuilder 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.internal.builder.KnowledgeBuilder 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.internal.builder.KnowledgeBuilder 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.internal.builder.KnowledgeBuilder 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.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class TruthMaintenanceTest method testTMSwithQueries.
@Test(timeout = 10000)
public void testTMSwithQueries() {
String str = "" + "package org.drools.compiler.test;\n" + "\n" + "global java.util.List list; \n" + "\n" + "declare Bean\n" + " str : String\n" + "end\n" + "\n" + "query bean ( String $s )\n" + " Bean( $s ; )\n" + "end\n" + "\n" + "\n" + "rule \"init\"\n" + "when\n" + "then\n" + " insert( new Bean(\"AAA\") );\n" + " insert( \"x\" );\n" + "end\n" + "\n" + "rule \"LogicIn\"\n" + "when\n" + " String( this == \"x\" )\n" + " ?bean( \"AAA\" ; )\n" + "then\n" + " insertLogical(\"y\");\n" + " retract(\"x\");\n" + "end " + "\n" + "rule \"Never\"\n" + "salience -999\n" + "when\n" + " $s : String( this == \"y\" )\n" + "then\n" + " list.add($s);\n" + "end";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession kSession = createKnowledgeSession(kbase);
List list = new ArrayList();
kSession.setGlobal("list", list);
kSession.fireAllRules();
assertEquals(0, list.size());
// System.err.println(reportWMObjects(kSession));
}
Aggregations