use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class TruthMaintenanceTest method testStatedDeleteLogicalAssertion.
@Test
public void testStatedDeleteLogicalAssertion() {
// BZ-1317026
String drl = "rule R1 when\n" + "then\n" + " insertLogical( \"test\" ); \n" + "end\n";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
ksession.fireAllRules();
Collection<FactHandle> fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
assertEquals(1, fhs.size());
for (FactHandle fh : fhs) {
ksession.delete(fh, FactHandle.State.STATED);
}
fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
assertEquals(1, fhs.size());
for (FactHandle fh : fhs) {
ksession.delete(fh, FactHandle.State.LOGICAL);
}
fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
assertEquals(0, fhs.size());
}
use of org.drools.core.ClassObjectFilter 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.drools.core.ClassObjectFilter 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.drools.core.ClassObjectFilter in project drools by kiegroup.
the class IncrementalCompilationTest method testRetractLogicalAssertedObjectOnRuleRemoval.
@Test
public void testRetractLogicalAssertedObjectOnRuleRemoval() throws Exception {
// DROOLS-951
String drl1 = "rule R1 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found1\" );" + "end\n";
String drl2 = "package org.drools.compiler\n" + "rule R2 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found2\" );" + "end\n";
String drl3 = "package org.drools.compiler\n" + "rule R3 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found3\");" + "end\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
KieModule km = createAndDeployJar(ks, releaseId1, drl1, drl2, drl3);
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
ksession.insert(4);
ksession.fireAllRules();
assertEquals(3, ksession.getObjects(new ClassObjectFilter(String.class)).size());
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
km = createAndDeployJar(ks, releaseId2, drl1, drl2);
kc.updateToVersion(releaseId2);
ksession.fireAllRules();
assertEquals(2, ksession.getObjects(new ClassObjectFilter(String.class)).size());
ReleaseId releaseId3 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.3");
km = createAndDeployJar(ks, releaseId3, drl1);
kc.updateToVersion(releaseId3);
ksession.fireAllRules();
assertEquals(1, ksession.getObjects(new ClassObjectFilter(String.class)).size());
ReleaseId releaseId4 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.4");
km = createAndDeployJar(ks, releaseId4);
kc.updateToVersion(releaseId4);
ksession.fireAllRules();
assertEquals(0, ksession.getObjects(new ClassObjectFilter(String.class)).size());
}
use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class IncrementalCompilationTest method testRetractLogicalAssertedObjectOnRuleRemovalWithSameObject.
@Test
public void testRetractLogicalAssertedObjectOnRuleRemovalWithSameObject() throws Exception {
// DROOLS-951
String drl1 = "rule R1 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found\" );" + "end\n";
String drl2 = "package org.drools.compiler\n" + "rule R2 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found\" );" + "end\n";
String drl3 = "package org.drools.compiler\n" + "rule R3 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found\");" + "end\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
KieModule km = createAndDeployJar(ks, releaseId1, drl1, drl2, drl3);
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
ksession.insert(4);
ksession.fireAllRules();
assertEquals(1, ksession.getObjects(new ClassObjectFilter(String.class)).size());
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
km = createAndDeployJar(ks, releaseId2, drl1, drl2);
kc.updateToVersion(releaseId2);
ksession.fireAllRules();
assertEquals(1, ksession.getObjects(new ClassObjectFilter(String.class)).size());
ReleaseId releaseId3 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.3");
km = createAndDeployJar(ks, releaseId3, drl1);
kc.updateToVersion(releaseId3);
ksession.fireAllRules();
assertEquals(1, ksession.getObjects(new ClassObjectFilter(String.class)).size());
ReleaseId releaseId4 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.4");
km = createAndDeployJar(ks, releaseId4);
kc.updateToVersion(releaseId4);
ksession.fireAllRules();
assertEquals(0, ksession.getObjects(new ClassObjectFilter(String.class)).size());
}
Aggregations