Search in sources :

Example 6 with ClassObjectFilter

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());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 7 with ClassObjectFilter

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());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ClassObjectFilter(org.drools.core.ClassObjectFilter) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 8 with ClassObjectFilter

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());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ClassObjectFilter(org.drools.core.ClassObjectFilter) KiePackage(org.kie.api.definition.KiePackage) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Example 9 with ClassObjectFilter

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());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 10 with ClassObjectFilter

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());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

ClassObjectFilter (org.drools.core.ClassObjectFilter)25 Test (org.junit.Test)25 KieSession (org.kie.api.runtime.KieSession)24 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)15 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 FactHandle (org.kie.api.runtime.rule.FactHandle)15 KieBase (org.kie.api.KieBase)11 ArrayList (java.util.ArrayList)9 List (java.util.List)8 Person (org.drools.compiler.Person)8 KieHelper (org.kie.internal.utils.KieHelper)6 Cheese (org.drools.compiler.Cheese)5 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 KiePackage (org.kie.api.definition.KiePackage)5 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)5 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 CheeseEqual (org.drools.compiler.CheeseEqual)3 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)3 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)2