Search in sources :

Example 51 with KieSession

use of org.kie.api.runtime.KieSession 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());
}
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) Cheese(org.drools.compiler.Cheese) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 52 with KieSession

use of org.kie.api.runtime.KieSession 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 53 with KieSession

use of org.kie.api.runtime.KieSession 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 54 with KieSession

use of org.kie.api.runtime.KieSession 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 55 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertions3.

// (timeout=10000)
@Test
public void testLogicalInsertions3() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_logicalInsertions3.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("events", list);
    // asserting the sensor object
    final Sensor sensor = new Sensor(150, 100);
    FactHandle sensorHandle = ksession.insert(sensor);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    // alarm must sound
    assertEquals(2, list.size());
    assertEquals(2, ksession.getObjects().size());
    // modifying sensor
    sensor.setTemperature(125);
    sensorHandle = getFactHandle(sensorHandle, ksession);
    ksession.update(sensorHandle, sensor);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    // alarm must continue to sound
    assertEquals(3, list.size());
    assertEquals(2, ksession.getObjects().size());
    // modifying sensor
    sensor.setTemperature(80);
    sensorHandle = getFactHandle(sensorHandle, ksession);
    ksession.update(sensorHandle, sensor);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    // no alarms anymore
    assertEquals(3, list.size());
    assertEquals(1, ksession.getObjects().size());
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Aggregations

KieSession (org.kie.api.runtime.KieSession)5328 Test (org.junit.Test)4824 KieBase (org.kie.api.KieBase)2414 ArrayList (java.util.ArrayList)2317 List (java.util.List)1105 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)769 FactHandle (org.kie.api.runtime.rule.FactHandle)598 Person (org.drools.modelcompiler.domain.Person)519 HashMap (java.util.HashMap)416 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)415 KieServices (org.kie.api.KieServices)382 KieHelper (org.kie.internal.utils.KieHelper)355 KieContainer (org.kie.api.runtime.KieContainer)298 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)265 InternalFactHandle (org.drools.core.common.InternalFactHandle)259 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)234 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)234 ReleaseId (org.kie.api.builder.ReleaseId)232 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)229 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)207